wiki.techinc.nl/tests/phpunit/includes/db/DatabaseTestHelper.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

258 lines
6.7 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock;
use MediaWiki\Tests\Unit\Libs\Rdbms\SQLPlatformTestHelper;
use Psr\Log\NullLogger;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\Database\DatabaseFlags;
use Wikimedia\Rdbms\DatabaseDomain;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\QueryStatus;
use Wikimedia\Rdbms\Replication\ReplicationReporter;
use Wikimedia\Rdbms\TransactionProfiler;
rdbms: detect corrupt Database instances due to critical section failure This checks that the Database state has not diverged from the driver DB handle nor server-side connection state due to an exception being thrown in an unexpected place within internal Database methods. DB handles with possible state corruption will not accept queries. For example, a PHP extension like Excimer might be used to throw request timeout exceptions. Such exceptions can trigger after any PHP or Zend function returns, e.g. within DatabaseMysqlBase::doSelectDomain() after the "USE" query completes but before $this->currentDomain gets updated. Also: * Make getApproximateLagStatus() catch getLag() errors since begin() expects it to simply use "false" for the lag value on failure. This helps assure that $this->trxAutomatic gets properly set. * Unsuppress exceptions in runOnTransactionPreCommitCallbacks() as the transaction needs to get aborted anyway (as already happens). * Unsuppress exceptions in runOnAtomicSectionCancelCallbacks() since the safest thing to do is just roll back the transaction. * Only suppress DBError exceptions in runOnTransactionIdleCallbacks(). and runTransactionListenerCallbacks(). Return the array of errors rather than throw the first one. Most of the callers had to catch the errors, so it's easier to avoid throwing them to begin with. * Avoid blanket try/catch in sourceStream(), doReplace(), upsert(), doInsertSelectGenericand(). * Clarify various code comments and add missing @internal tags. Bug: T193565 Change-Id: I6b7b02c02b24c2ff01094af3df54c989fe504af7
2020-12-01 05:08:32 +00:00
use Wikimedia\RequestTimeout\RequestTimeout;
/**
* Helper for testing the methods from the Database class
* @since 1.22
*/
class DatabaseTestHelper extends Database {
/**
* @var string __CLASS__ of the test suite,
* used to determine, if the function name is passed every time to query()
*/
protected string $testName;
/**
* @var string[] Array of lastSqls passed to query(),
* This is an array since some methods in Database can do more than one
* query. Cleared when calling getLastSqls().
*/
protected $lastSqls = [];
/** @var array Stack of result maps */
protected $nextResMapQueue = [];
/** @var array|null */
protected $lastResMap = null;
/**
* @var string[] Array of tables to be considered as existing by tableExist()
* Use setExistingTables() to alter.
*/
protected $tablesExists;
/** @var int[] */
protected $forcedAffectedCountQueue = [];
public function __construct( string $testName, array $opts = [] ) {
$params = $opts + [
'host' => null,
'user' => null,
'password' => null,
'dbname' => null,
'schema' => null,
'tablePrefix' => '',
'flags' => 0,
'cliMode' => true,
'agent' => '',
'serverName' => null,
'topologyRole' => null,
'srvCache' => new HashBagOStuff(),
'profiler' => null,
'trxProfiler' => new TransactionProfiler(),
rdbms: Consolidate logger channels into one Notable changes: * In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were injected. The rest implicitly got a NullLogger due to being absent. These are now effectively unsilenced. * Database::__construct() required almost all parameters, even the loggers. I've wanted to move some of DatabaseFactory into the ctor here for a while. In order to make this change not a breaking change, the new 'logger' parameter is optional with NullLogger as default. This allowed some of the test cases, which were simply passing NullLogger, to be fixed by passing nothing instead of passing the new option name. The Database class is behind a dozen layers of indirection for real use, so this will still be injected just fine (DBF, LB, LBF, MWLBF, etc.). * In LegacyLogger, the handling for $wgDBerrorLog was previously limited to DBConnection and DBQuery. This now includes errors from other (generally, newer) parts of Rdbms as well, which were previously missing. This only affects sites (typically CI and dev setup) where $wgDBerrorLog is used, as opposed to the more common $wgDebugLogGroups by-channel configuration. * TransactionProfiler gets its logger injected in a rather odd way, via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner) as opposed to service wiring. This is kept as-is for now. * In LBFactoryTest, in particular testInvalidSelectDBIndependent2, there are cases that intentionally produce failures of which the result is then observed. In CI we assert that dberror.log is empty so instead of adding the missing logger fields to that LBFactory instance, the only one set (replLogger) is removed. The alternative is to set 'logger' now, which would naturally cause CI failures due to unexpected entries coming through to non-mocked error log. Bug: T320873 Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
'logger' => new NullLogger(),
'errorLogger' => static function ( Exception $e ) {
wfWarn( get_class( $e ) . ': ' . $e->getMessage() );
},
'deprecationLogger' => static function ( $msg ) {
wfWarn( $msg );
rdbms: detect corrupt Database instances due to critical section failure This checks that the Database state has not diverged from the driver DB handle nor server-side connection state due to an exception being thrown in an unexpected place within internal Database methods. DB handles with possible state corruption will not accept queries. For example, a PHP extension like Excimer might be used to throw request timeout exceptions. Such exceptions can trigger after any PHP or Zend function returns, e.g. within DatabaseMysqlBase::doSelectDomain() after the "USE" query completes but before $this->currentDomain gets updated. Also: * Make getApproximateLagStatus() catch getLag() errors since begin() expects it to simply use "false" for the lag value on failure. This helps assure that $this->trxAutomatic gets properly set. * Unsuppress exceptions in runOnTransactionPreCommitCallbacks() as the transaction needs to get aborted anyway (as already happens). * Unsuppress exceptions in runOnAtomicSectionCancelCallbacks() since the safest thing to do is just roll back the transaction. * Only suppress DBError exceptions in runOnTransactionIdleCallbacks(). and runTransactionListenerCallbacks(). Return the array of errors rather than throw the first one. Most of the callers had to catch the errors, so it's easier to avoid throwing them to begin with. * Avoid blanket try/catch in sourceStream(), doReplace(), upsert(), doInsertSelectGenericand(). * Clarify various code comments and add missing @internal tags. Bug: T193565 Change-Id: I6b7b02c02b24c2ff01094af3df54c989fe504af7
2020-12-01 05:08:32 +00:00
},
'criticalSectionProvider' =>
RequestTimeout::singleton()->createCriticalSectionProvider( 120 )
];
parent::__construct( $params );
$this->testName = $testName;
$this->platform = new SQLPlatformTestHelper( new AddQuoterMock() );
$this->flagsHolder = new DatabaseFlags( 0 );
$this->replicationReporter = new ReplicationReporter(
$params['topologyRole'],
rdbms: Consolidate logger channels into one Notable changes: * In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were injected. The rest implicitly got a NullLogger due to being absent. These are now effectively unsilenced. * Database::__construct() required almost all parameters, even the loggers. I've wanted to move some of DatabaseFactory into the ctor here for a while. In order to make this change not a breaking change, the new 'logger' parameter is optional with NullLogger as default. This allowed some of the test cases, which were simply passing NullLogger, to be fixed by passing nothing instead of passing the new option name. The Database class is behind a dozen layers of indirection for real use, so this will still be injected just fine (DBF, LB, LBF, MWLBF, etc.). * In LegacyLogger, the handling for $wgDBerrorLog was previously limited to DBConnection and DBQuery. This now includes errors from other (generally, newer) parts of Rdbms as well, which were previously missing. This only affects sites (typically CI and dev setup) where $wgDBerrorLog is used, as opposed to the more common $wgDebugLogGroups by-channel configuration. * TransactionProfiler gets its logger injected in a rather odd way, via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner) as opposed to service wiring. This is kept as-is for now. * In LBFactoryTest, in particular testInvalidSelectDBIndependent2, there are cases that intentionally produce failures of which the result is then observed. In CI we assert that dberror.log is empty so instead of adding the missing logger fields to that LBFactory instance, the only one set (replLogger) is removed. The alternative is to set 'logger' now, which would naturally cause CI failures due to unexpected entries coming through to non-mocked error log. Bug: T320873 Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2022-10-15 20:16:07 +00:00
$params['logger'],
$params['srvCache']
);
$this->currentDomain = DatabaseDomain::newUnspecified();
$this->open( 'localhost', 'testuser', 'password', 'testdb', null, '' );
}
/**
* Returns SQL queries grouped by '; '
* Clear the list of queries that have been done so far.
* @return string
*/
public function getLastSqls() {
$lastSqls = implode( '; ', $this->lastSqls );
$this->lastSqls = [];
return $lastSqls;
}
public function setExistingTables( $tablesExists ) {
$this->tablesExists = (array)$tablesExists;
}
/**
* @param mixed $res Use an array of row arrays to set row result
* @param int $errno Error number
* @param string $error Error text
* @param array $options
* - isKnownStatementRollbackError: Return value for isKnownStatementRollbackError()
*/
public function forceNextResult( $res, $errno = 0, $error = '', $options = [] ) {
$this->nextResMapQueue[] = [
'res' => $res,
'errno' => $errno,
'error' => $error
] + $options;
}
protected function addSql( $sql ) {
// clean up spaces before and after some words and the whole string
$this->lastSqls[] = trim( preg_replace(
'/\s{2,}(?=FROM|WHERE|GROUP BY|ORDER BY|LIMIT)|(?<=SELECT|INSERT|UPDATE)\s{2,}/',
' ', $sql
) );
}
protected function checkFunctionName( $fname ) {
if ( $fname === 'Wikimedia\\Rdbms\\Database::close' ) {
return; // no $fname parameter
}
// Handle some internal calls from the Database class
$check = $fname;
if ( preg_match(
'/^Wikimedia\\\\Rdbms\\\\Database::(?:query|beginIfImplied) \((.+)\)$/',
$fname,
$m
) ) {
$check = $m[1];
}
if ( !str_starts_with( $check, $this->testName ) ) {
throw new LogicException( 'function name does not start with test class. ' .
$fname . ' vs. ' . $this->testName . '. ' .
'Please provide __METHOD__ to database methods.' );
}
}
public function strencode( $s ) {
// Choose apos to avoid handling of escaping double quotes in quoted text
return str_replace( "'", "\'", $s );
}
public function query( $sql, $fname = '', $flags = 0 ) {
$this->checkFunctionName( $fname );
return parent::query( $sql, $fname, $flags );
}
public function tableExists( $table, $fname = __METHOD__ ) {
[ $db, $pt ] = $this->platform->getDatabaseAndTableIdentifier( $table );
if ( isset( $this->sessionTempTables[$db][$pt] ) ) {
return true; // already known to exist
}
$this->checkFunctionName( $fname );
return in_array( $table, (array)$this->tablesExists );
}
public function getType() {
return 'test';
}
public function open( $server, $user, $password, $db, $schema, $tablePrefix ) {
$this->conn = (object)[ 'test' ];
return true;
}
protected function lastInsertId() {
return -1;
}
public function lastErrno() {
return $this->lastResMap ? $this->lastResMap['errno'] : -1;
}
public function lastError() {
return $this->lastResMap ? $this->lastResMap['error'] : 'test';
}
protected function isKnownStatementRollbackError( $errno ) {
return ( $this->lastResMap['errno'] ?? 0 ) === $errno
? ( $this->lastResMap['isKnownStatementRollbackError'] ?? false )
: false;
}
public function fieldInfo( $table, $field ) {
return false;
}
public function indexInfo( $table, $index, $fname = 'Database::indexInfo' ) {
return false;
}
public function getSoftwareLink() {
return 'test';
}
public function getServerVersion() {
return 'test';
}
public function getServerInfo() {
return 'test';
}
public function ping( &$rtt = null ) {
$rtt = 0.0;
return true;
}
protected function closeConnection() {
return true;
}
public function setNextQueryAffectedRowCounts( array $counts ) {
$this->forcedAffectedCountQueue = $counts;
}
protected function doSingleStatementQuery( string $sql ): QueryStatus {
$sql = preg_replace( '< /\* .+? \*/>', '', $sql );
$this->addSql( $sql );
if ( $this->nextResMapQueue ) {
$this->lastResMap = array_shift( $this->nextResMapQueue );
if ( !$this->lastResMap['errno'] && $this->forcedAffectedCountQueue ) {
$count = array_shift( $this->forcedAffectedCountQueue );
$this->lastQueryAffectedRows = $count;
}
} else {
$this->lastResMap = [ 'res' => [], 'errno' => 0, 'error' => '' ];
}
$res = $this->lastResMap['res'];
return new QueryStatus(
is_bool( $res ) ? $res : new FakeResultWrapper( $res ),
$this->affectedRows(),
$this->lastError(),
$this->lastErrno()
);
}
}