From a663e9fdedd3eaf1595bdb53e8b6baa5573215a1 Mon Sep 17 00:00:00 2001 From: Pierre Goiffon Date: Wed, 12 Jan 2022 08:40:04 +0100 Subject: [PATCH] :white_check_mark: Fix DBBackupTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DB connection dependency was added in a222ead4 (N°2336) in \DBBackup::GetMysqlCliTlsOptions but test wasn't updated accordingly :/^ The test wasn't ran on Jenkins until b11bf308, so we saw the regression only yesterday :( This is now fixed ! 🥳 --- core/cmdbsource.class.inc.php | 10 +++++++++- setup/backup.class.inc.php | 3 +++ test/setup/DBBackupTest.php | 35 +++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/core/cmdbsource.class.inc.php b/core/cmdbsource.class.inc.php index 668ed60b8..66fd4bb70 100644 --- a/core/cmdbsource.class.inc.php +++ b/core/cmdbsource.class.inc.php @@ -447,6 +447,12 @@ class CMDBSource } + /** + * @return string + * @throws \MySQLException + * + * @uses \CMDBSource::QueryToCol() so needs a connection opened ! + */ public static function GetDBVersion() { $aVersions = self::QueryToCol('SELECT Version() as version', 'version'); @@ -464,8 +470,10 @@ class CMDBSource /** * Get the DB vendor between MySQL and its main forks * @return string + * + * @uses \CMDBSource::GetServerVariable() so needs a connection opened ! */ - static public function GetDBVendor() + public static function GetDBVendor() { $sDBVendor = static::ENUM_DB_VENDOR_MYSQL; diff --git a/setup/backup.class.inc.php b/setup/backup.class.inc.php index 8287997a4..64d4ff1c0 100644 --- a/setup/backup.class.inc.php +++ b/setup/backup.class.inc.php @@ -466,6 +466,9 @@ EOF; * * @return string TLS arguments for CLI programs such as mysqldump. Empty string if the config does not use TLS. * + * @uses \CMDBSource::GetDBVendor() so needs a connection opened ! + * @uses \CMDBSource::GetDBVersion() so needs a connection opened ! + * * @since 2.5.0 N°1260 * @link https://dev.mysql.com/doc/refman/5.6/en/connection-options.html#encrypted-connection-options "Command Options for Encrypted Connections" */ diff --git a/test/setup/DBBackupTest.php b/test/setup/DBBackupTest.php index 798d453a5..176f02e5d 100644 --- a/test/setup/DBBackupTest.php +++ b/test/setup/DBBackupTest.php @@ -2,9 +2,10 @@ namespace Combodo\iTop\Test\UnitTest\Core; +use CMDBSource; use Combodo\iTop\Test\UnitTest\ItopTestCase; -use Config; use DBBackup; +use utils; /** * @runTestsInSeparateProcesses @@ -21,21 +22,35 @@ class DBBackupTest extends ItopTestCase require_once(APPROOT.'core/cmdbsource.class.inc.php'); // DBBackup dependency } + /** + * @throws \CoreException + * @throws \ConfigException + * @throws \MySQLException + */ public function testGetMysqlCliTlsOptions() { - $oConfig = new Config(); - $oConfig->Set('db_tls.enabled', false); + $oConfigToTest = utils::GetConfig(); - $sCliArgsNoTls = DBBackup::GetMysqlCliTlsOptions($oConfig); + // No TLS connection = no additional CLI args ! + $oConfigToTest->Set('db_tls.enabled', false); + $sCliArgsNoTls = DBBackup::GetMysqlCliTlsOptions($oConfigToTest); $this->assertEmpty($sCliArgsNoTls); - $oConfig->Set('db_tls.enabled', true); - $sCliArgsMinCfg = DBBackup::GetMysqlCliTlsOptions($oConfig); - $this->assertEquals(' --ssl', $sCliArgsMinCfg); + // We need a connection to the DB, so let's open it ! + $oConfigOnDisk = utils::GetConfig(); + CMDBSource::InitFromConfig($oConfigOnDisk); + // TLS connection configured = we need one CLI arg + $oConfigToTest->Set('db_tls.enabled', true); + $sCliArgsMinCfg = DBBackup::GetMysqlCliTlsOptions($oConfigToTest); + // depending on the MySQL version, we would have `--ssl` or `--ssl-mode=VERIFY_CA` + $this->assertStringStartsWith(' --ssl', $sCliArgsMinCfg); + + // TLS connection configured + CA option = we need multiple CLI args $sTestCa = 'my_test_ca'; - $oConfig->Set('db_tls.ca', $sTestCa); - $sCliArgsCapathCfg = DBBackup::GetMysqlCliTlsOptions($oConfig); - $this->assertEquals(' --ssl --ssl-ca="'.$sTestCa.'"', $sCliArgsCapathCfg); + $oConfigToTest->Set('db_tls.ca', $sTestCa); + $sCliArgsCapathCfg = DBBackup::GetMysqlCliTlsOptions($oConfigToTest); + $this->assertStringStartsWith(' --ssl', $sCliArgsMinCfg); + $this->assertStringEndsWith('--ssl-ca="'.$sTestCa.'"', $sCliArgsCapathCfg); } }