diff --git a/scripts/db_migrator.py b/scripts/db_migrator.py index a942746f77..7302e5428b 100755 --- a/scripts/db_migrator.py +++ b/scripts/db_migrator.py @@ -47,8 +47,7 @@ def __init__(self, namespace, socket=None): none-zero values. build: sequentially increase within a minor version domain. """ - - self.CURRENT_VERSION = 'version_4_0_4' + self.CURRENT_VERSION = 'version_4_0_5' self.TABLE_NAME = 'VERSIONS' self.TABLE_KEY = 'DATABASE' @@ -638,6 +637,18 @@ def migrate_route_table(self): if 'protocol' not in route_attr: self.appDB.set(self.appDB.APPL_DB, route_key, 'protocol', '') + def migrate_dns_nameserver(self): + """ + Handle DNS_NAMESERVER table migration. Migrations handled: + If there's no DNS_NAMESERVER in config_DB, load DNS_NAMESERVER from minigraph + """ + if not self.minigraph_data or 'DNS_NAMESERVER' not in self.minigraph_data: + return + dns_table = self.configDB.get_table('DNS_NAMESERVER') + if not dns_table: + for addr, config in self.minigraph_data['DNS_NAMESERVER'].items(): + self.configDB.set_entry('DNS_NAMESERVER', addr, config) + def migrate_routing_config_mode(self): # DEVICE_METADATA - synchronous_mode entry if not self.minigraph_data or 'DEVICE_METADATA' not in self.minigraph_data: @@ -1042,6 +1053,17 @@ def version_4_0_3(self): """ log.log_info('Handling version_4_0_3') + # Updating DNS nameserver + self.migrate_dns_nameserver() + self.set_version('version_4_0_4') + return 'version_4_0_4' + + def version_4_0_4(self): + """ + Version 4_0_4. + """ + log.log_info('Handling version_4_0_4') + sflow_tbl = self.configDB.get_table('SFLOW') for k, v in sflow_tbl.items(): if 'sample_direction' not in v: @@ -1056,14 +1078,16 @@ def version_4_0_3(self): self.migrate_sflow_table() - self.set_version('version_4_0_4') - return 'version_4_0_4' + self.set_version('version_4_0_5') + return 'version_4_0_5' - def version_4_0_4(self): + def version_4_0_5(self): """ - Current latest version. Nothing to do here. + Version 4_0_5. + This is the latest version for master branch """ - log.log_info('Handling version_4_0_4') + log.log_info('Handling version_4_0_5') + return None def get_version(self): diff --git a/tests/db_migrator_input/config_db/dns-nameserver-expected.json b/tests/db_migrator_input/config_db/dns-nameserver-expected.json new file mode 100644 index 0000000000..0d9924e2cc --- /dev/null +++ b/tests/db_migrator_input/config_db/dns-nameserver-expected.json @@ -0,0 +1,11 @@ +{ + "DNS_NAMESERVER|1.1.1.1": { + "state": "enabled" + }, + "DNS_NAMESERVER|2001:1001:110:1001::1": { + "state": "enabled" + }, + "VERSIONS|DATABASE": { + "VERSION": "version_4_0_4" + } +} diff --git a/tests/db_migrator_input/config_db/dns-nameserver-input.json b/tests/db_migrator_input/config_db/dns-nameserver-input.json new file mode 100644 index 0000000000..eca39b0628 --- /dev/null +++ b/tests/db_migrator_input/config_db/dns-nameserver-input.json @@ -0,0 +1,5 @@ +{ + "VERSIONS|DATABASE": { + "VERSION": "version_4_0_3" + } +} diff --git a/tests/db_migrator_test.py b/tests/db_migrator_test.py index c648a84ad7..0109c8932c 100644 --- a/tests/db_migrator_test.py +++ b/tests/db_migrator_test.py @@ -294,6 +294,37 @@ def test_lacp_key_migrator(self): assert dbmgtr.configDB.get_table('PORTCHANNEL') == expected_db.cfgdb.get_table('PORTCHANNEL') assert dbmgtr.configDB.get_table('VERSIONS') == expected_db.cfgdb.get_table('VERSIONS') +class TestDnsNameserverMigrator(object): + @classmethod + def setup_class(cls): + os.environ['UTILITIES_UNIT_TESTING'] = "2" + + @classmethod + def teardown_class(cls): + os.environ['UTILITIES_UNIT_TESTING'] = "0" + dbconnector.dedicated_dbs['CONFIG_DB'] = None + + def test_dns_nameserver_migrator(self): + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'dns-nameserver-input') + import db_migrator + dbmgtr = db_migrator.DBMigrator(None) + # Set minigraph_data to DNS_NAMESERVERS + dbmgtr.minigraph_data = { + 'DNS_NAMESERVER': { + '1.1.1.1': {}, + '2001:1001:110:1001::1': {} + } + } + dbmgtr.migrate() + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'dns-nameserver-expected') + expected_db = Db() + advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, 'version_4_0_4') + resulting_keys = dbmgtr.configDB.keys(dbmgtr.configDB.CONFIG_DB, 'DNS_NAMESERVER*') + expected_keys = expected_db.cfgdb.keys(expected_db.cfgdb.CONFIG_DB, 'DNS_NAMESERVER*') + + diff = DeepDiff(resulting_keys, expected_keys, ignore_order=True) + assert not diff + class TestQosDBFieldValueReferenceRemoveMigrator(object): @classmethod def setup_class(cls): @@ -680,6 +711,7 @@ def test_fast_reboot_upgrade_to_4_0_3(self): dbmgtr = db_migrator.DBMigrator(None) dbmgtr.migrate() expected_db = self.mock_dedicated_config_db(db_after_migrate) + advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, 'version_4_0_3') assert not self.check_config_db(dbmgtr.configDB, expected_db.cfgdb) if dbmgtr.CURRENT_VERSION == 'version_4_0_3': assert dbmgtr.CURRENT_VERSION == expected_db.cfgdb.get_entry('VERSIONS', 'DATABASE')['VERSION']