diff --git a/scripts/db_migrator.py b/scripts/db_migrator.py index 33060b99a1..30582f53bb 100755 --- a/scripts/db_migrator.py +++ b/scripts/db_migrator.py @@ -576,6 +576,23 @@ def migrate_port_qos_map_global(self): self.configDB.set_entry('PORT_QOS_MAP', 'global', {"dscp_to_tc_map": dscp_to_tc_map_table_names[0]}) log.log_info("Created entry for global DSCP_TO_TC_MAP {}".format(dscp_to_tc_map_table_names[0])) + def migrate_route_table(self): + """ + Handle route table migration. Migrations handled: + 1. 'weight' attr in ROUTE object was introduced 202205 onwards. + Upgrade from older branch to 202205 will require this 'weight' attr to be added explicitly + """ + route_table = self.appDB.get_table("ROUTE_TABLE") + for route_prefix, route_attr in route_table.items(): + if 'weight' not in route_attr: + if type(route_prefix) == tuple: + # IPv6 route_prefix is returned from db as tuple + route_key = "ROUTE_TABLE:" + ":".join(route_prefix) + else: + # IPv4 route_prefix is returned from db as str + route_key = "ROUTE_TABLE:{}".format(route_prefix) + self.appDB.set(self.appDB.APPL_DB, route_key, 'weight','') + def version_unknown(self): """ version_unknown tracks all SONiC versions that doesn't have a version @@ -851,6 +868,8 @@ def common_migration_ops(self): else: log.log_notice("Asic Type: {}, Hwsku: {}".format(self.asic_type, self.hwsku)) + self.migrate_route_table() + def migrate(self): version = self.get_version() log.log_info('Upgrading from version ' + version) diff --git a/tests/db_migrator_input/appl_db/routes_migrate_expected.json b/tests/db_migrator_input/appl_db/routes_migrate_expected.json new file mode 100644 index 0000000000..5cad371c31 --- /dev/null +++ b/tests/db_migrator_input/appl_db/routes_migrate_expected.json @@ -0,0 +1,12 @@ +{ + "ROUTE_TABLE:192.168.104.0/25": { + "nexthop": "10.0.0.57,10.0.0.59,10.0.0.61,10.0.0.63", + "ifname" : "PortChannel101,PortChannel102,PortChannel103,PortChannel104", + "weight": "" + }, + "ROUTE_TABLE:20c0:fe28:0:80::/64": { + "nexthop": "fc00::72,fc00::76,fc00::7a,fc00::7e", + "ifname" : "PortChannel101,PortChannel102,PortChannel103,PortChannel104", + "weight": "" + } +} diff --git a/tests/db_migrator_input/appl_db/routes_migrate_input.json b/tests/db_migrator_input/appl_db/routes_migrate_input.json new file mode 100644 index 0000000000..7249488cd6 --- /dev/null +++ b/tests/db_migrator_input/appl_db/routes_migrate_input.json @@ -0,0 +1,10 @@ +{ + "ROUTE_TABLE:192.168.104.0/25": { + "nexthop": "10.0.0.57,10.0.0.59,10.0.0.61,10.0.0.63", + "ifname" : "PortChannel101,PortChannel102,PortChannel103,PortChannel104" + }, + "ROUTE_TABLE:20c0:fe28:0:80::/64": { + "nexthop": "fc00::72,fc00::76,fc00::7a,fc00::7e", + "ifname" : "PortChannel101,PortChannel102,PortChannel103,PortChannel104" + } +} diff --git a/tests/db_migrator_input/config_db/routes_migrate_input.json b/tests/db_migrator_input/config_db/routes_migrate_input.json new file mode 100644 index 0000000000..672268b286 --- /dev/null +++ b/tests/db_migrator_input/config_db/routes_migrate_input.json @@ -0,0 +1,3 @@ +{ + "VERSIONS|DATABASE": {"VERSION": "version_1_0_1"} +} diff --git a/tests/db_migrator_test.py b/tests/db_migrator_test.py index efb7aef298..c780ed710d 100644 --- a/tests/db_migrator_test.py +++ b/tests/db_migrator_test.py @@ -478,3 +478,38 @@ def test_migrate_loopback_int(self): expected_keys = expected_appl_db.get_all(expected_appl_db.APPL_DB, key) diff = DeepDiff(resulting_keys, expected_keys, ignore_order=True) assert not diff + +class TestWarmUpgrade_without_route_weights(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 + dbconnector.dedicated_dbs['APPL_DB'] = None + + def test_migrate_weights_for_nexthops(self): + dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'routes_migrate_input') + dbconnector.dedicated_dbs['APPL_DB'] = os.path.join(mock_db_path, 'appl_db', 'routes_migrate_input') + + import db_migrator + dbmgtr = db_migrator.DBMigrator(None) + dbmgtr.migrate() + dbconnector.dedicated_dbs['APPL_DB'] = os.path.join(mock_db_path, 'appl_db', 'routes_migrate_expected') + expected_db = Db() + + # verify migrated appDB + expected_appl_db = SonicV2Connector(host='127.0.0.1') + expected_appl_db.connect(expected_appl_db.APPL_DB) + expected_keys = expected_appl_db.keys(expected_appl_db.APPL_DB, "ROUTE_TABLE:*") + expected_keys.sort() + resulting_keys = dbmgtr.appDB.keys(dbmgtr.appDB.APPL_DB, "ROUTE_TABLE:*") + resulting_keys.sort() + assert expected_keys == resulting_keys + for key in expected_keys: + resulting_keys = dbmgtr.appDB.get_all(dbmgtr.appDB.APPL_DB, key) + expected_keys = expected_appl_db.get_all(expected_appl_db.APPL_DB, key) + diff = DeepDiff(resulting_keys, expected_keys, ignore_order=True) + assert not diff