From 2a0b57807b6cd7be5546c1b2b5f5113b5528c1bf Mon Sep 17 00:00:00 2001 From: Stephen Sun <5379172+stephenxs@users.noreply.github.com> Date: Sun, 23 May 2021 16:45:34 +0800 Subject: [PATCH] Make advance_version_for_expected_database available for other db migrator test cases as well (#1614) - What I did Originally, the method advance_version_for_expected_database was introduced (in #1566) to handle the case the latest version in CONFIG_DB is greater than the latest version in mellanox_buffer_migrator. Now there are other database migrators whose test cases can also encounter this situation, like port auto-negotiation (#1568) and port-channel for LACP key (#1473). So I would like to make the method public, available for all database migrators. Related database migrator test cases have been updated accordingly. - How to verify it Run the unit test. Signed-off-by: Stephen Sun --- .../non-default-config-expected.json | 2 +- tests/db_migrator_test.py | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/db_migrator_input/config_db/non-default-config-expected.json b/tests/db_migrator_input/config_db/non-default-config-expected.json index 46e75f26f7..a31a50b45e 100644 --- a/tests/db_migrator_input/config_db/non-default-config-expected.json +++ b/tests/db_migrator_input/config_db/non-default-config-expected.json @@ -1115,6 +1115,6 @@ "speed": "50000" }, "VERSIONS|DATABASE": { - "VERSION": "version_2_0_1" + "VERSION": "version_2_0_0" } } diff --git a/tests/db_migrator_test.py b/tests/db_migrator_test.py index 293ee1d09f..56cfb2900f 100644 --- a/tests/db_migrator_test.py +++ b/tests/db_migrator_test.py @@ -24,6 +24,17 @@ def get_sonic_version_info_mlnx(): return {'asic_type': 'mellanox'} +def advance_version_for_expected_database(migrated_db, expected_db, last_interested_version): + # In case there are new db versions greater than the latest one that mellanox buffer migrator is interested, + # we just advance the database version in the expected database to make the test pass + expected_dbversion = expected_db.get_entry('VERSIONS', 'DATABASE') + dbmgtr_dbversion = migrated_db.get_entry('VERSIONS', 'DATABASE') + if expected_dbversion and dbmgtr_dbversion: + if expected_dbversion['VERSION'] == last_interested_version and dbmgtr_dbversion['VERSION'] > expected_dbversion['VERSION']: + expected_dbversion['VERSION'] = dbmgtr_dbversion['VERSION'] + expected_db.set_entry('VERSIONS', 'DATABASE', expected_dbversion) + + class TestMellanoxBufferMigrator(object): @classmethod def setup_class(cls): @@ -76,16 +87,6 @@ def check_appl_db(self, result, expected): for key in keys: assert expected.get_all(expected.APPL_DB, key) == result.get_all(result.APPL_DB, key) - def advance_version_for_expected_database(self, migrated_db, expected_db): - # In case there are new db versions greater than the latest one that mellanox buffer migrator is interested, - # we just advance the database version in the expected database to make the test pass - expected_dbversion = expected_db.get_entry('VERSIONS', 'DATABASE') - dbmgtr_dbversion = migrated_db.get_entry('VERSIONS', 'DATABASE') - if expected_dbversion and dbmgtr_dbversion: - if expected_dbversion['VERSION'] == self.version_list[-1] and dbmgtr_dbversion['VERSION'] > expected_dbversion['VERSION']: - expected_dbversion['VERSION'] = dbmgtr_dbversion['VERSION'] - expected_db.set_entry('VERSIONS', 'DATABASE', expected_dbversion) - @pytest.mark.parametrize('scenario', ['empty-config', 'non-default-config', @@ -103,7 +104,7 @@ def test_mellanox_buffer_migrator_negative_cold_reboot(self, scenario): dbmgtr = db_migrator.DBMigrator(None) dbmgtr.migrate() expected_db = self.mock_dedicated_config_db(db_after_migrate) - self.advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb) + advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, self.version_list[-1]) self.check_config_db(dbmgtr.configDB, expected_db.cfgdb) assert not dbmgtr.mellanox_buffer_migrator.is_buffer_config_default @@ -142,7 +143,7 @@ def test_mellanox_buffer_migrator_for_cold_reboot(self, sku_version, topo): # Eventually, the config db should be migrated to the latest version expected_db = self.mock_dedicated_config_db(self.make_db_name_by_sku_topo_version(sku, topo, self.version_list[-1])) - self.advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb) + advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, self.version_list[-1]) self.check_config_db(dbmgtr.configDB, expected_db.cfgdb) assert dbmgtr.mellanox_buffer_migrator.is_buffer_config_default @@ -158,7 +159,7 @@ def mellanox_buffer_migrator_warm_reboot_runner(self, input_config_db, input_app import db_migrator dbmgtr = db_migrator.DBMigrator(None) dbmgtr.migrate() - self.advance_version_for_expected_database(dbmgtr.configDB, expected_config_db.cfgdb) + advance_version_for_expected_database(dbmgtr.configDB, expected_config_db.cfgdb, self.version_list[-1]) assert dbmgtr.mellanox_buffer_migrator.is_buffer_config_default == is_buffer_config_default_expected self.check_config_db(dbmgtr.configDB, expected_config_db.cfgdb) self.check_appl_db(dbmgtr.appDB, expected_appl_db) @@ -210,8 +211,10 @@ def test_port_autoneg_migrator(self): import db_migrator dbmgtr = db_migrator.DBMigrator(None) dbmgtr.migrate() + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'port-an-expected') expected_db = Db() + advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, 'version_2_0_1') assert dbmgtr.configDB.get_table('PORT') == expected_db.cfgdb.get_table('PORT') assert dbmgtr.configDB.get_table('VERSIONS') == expected_db.cfgdb.get_table('VERSIONS')