Skip to content

Commit ae6f302

Browse files
ganglyurajkumar38
authored andcommitted
[db_migrator] Migrate DNS configuratuion (sonic-net#2893)
What I did sonic-net/sonic-buildimage#14549 This PR has introduced DNS configuration to CONFIG_DB, we need to migrate original CONFIG_DB to support DNS configuration during upgrading SONiC. How I did it If there's no DNS_NAMESERVER table in CONFIG_DB, read DNS_NAMESERVER table from minigraph and update CONFIG_DB. How to verify it Run unit test for db migrator.
1 parent c36f9dd commit ae6f302

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

scripts/db_migrator.py

+31-7
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ def __init__(self, namespace, socket=None):
4747
none-zero values.
4848
build: sequentially increase within a minor version domain.
4949
"""
50-
51-
self.CURRENT_VERSION = 'version_4_0_4'
50+
self.CURRENT_VERSION = 'version_4_0_5'
5251

5352
self.TABLE_NAME = 'VERSIONS'
5453
self.TABLE_KEY = 'DATABASE'
@@ -638,6 +637,18 @@ def migrate_route_table(self):
638637
if 'protocol' not in route_attr:
639638
self.appDB.set(self.appDB.APPL_DB, route_key, 'protocol', '')
640639

640+
def migrate_dns_nameserver(self):
641+
"""
642+
Handle DNS_NAMESERVER table migration. Migrations handled:
643+
If there's no DNS_NAMESERVER in config_DB, load DNS_NAMESERVER from minigraph
644+
"""
645+
if not self.minigraph_data or 'DNS_NAMESERVER' not in self.minigraph_data:
646+
return
647+
dns_table = self.configDB.get_table('DNS_NAMESERVER')
648+
if not dns_table:
649+
for addr, config in self.minigraph_data['DNS_NAMESERVER'].items():
650+
self.configDB.set_entry('DNS_NAMESERVER', addr, config)
651+
641652
def migrate_routing_config_mode(self):
642653
# DEVICE_METADATA - synchronous_mode entry
643654
if not self.minigraph_data or 'DEVICE_METADATA' not in self.minigraph_data:
@@ -1042,6 +1053,17 @@ def version_4_0_3(self):
10421053
"""
10431054
log.log_info('Handling version_4_0_3')
10441055

1056+
# Updating DNS nameserver
1057+
self.migrate_dns_nameserver()
1058+
self.set_version('version_4_0_4')
1059+
return 'version_4_0_4'
1060+
1061+
def version_4_0_4(self):
1062+
"""
1063+
Version 4_0_4.
1064+
"""
1065+
log.log_info('Handling version_4_0_4')
1066+
10451067
sflow_tbl = self.configDB.get_table('SFLOW')
10461068
for k, v in sflow_tbl.items():
10471069
if 'sample_direction' not in v:
@@ -1056,14 +1078,16 @@ def version_4_0_3(self):
10561078

10571079
self.migrate_sflow_table()
10581080

1059-
self.set_version('version_4_0_4')
1060-
return 'version_4_0_4'
1081+
self.set_version('version_4_0_5')
1082+
return 'version_4_0_5'
10611083

1062-
def version_4_0_4(self):
1084+
def version_4_0_5(self):
10631085
"""
1064-
Current latest version. Nothing to do here.
1086+
Version 4_0_5.
1087+
This is the latest version for master branch
10651088
"""
1066-
log.log_info('Handling version_4_0_4')
1089+
log.log_info('Handling version_4_0_5')
1090+
10671091
return None
10681092

10691093
def get_version(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"DNS_NAMESERVER|1.1.1.1": {
3+
"state": "enabled"
4+
},
5+
"DNS_NAMESERVER|2001:1001:110:1001::1": {
6+
"state": "enabled"
7+
},
8+
"VERSIONS|DATABASE": {
9+
"VERSION": "version_4_0_4"
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"VERSIONS|DATABASE": {
3+
"VERSION": "version_4_0_3"
4+
}
5+
}

tests/db_migrator_test.py

+32
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,37 @@ def test_lacp_key_migrator(self):
294294
assert dbmgtr.configDB.get_table('PORTCHANNEL') == expected_db.cfgdb.get_table('PORTCHANNEL')
295295
assert dbmgtr.configDB.get_table('VERSIONS') == expected_db.cfgdb.get_table('VERSIONS')
296296

297+
class TestDnsNameserverMigrator(object):
298+
@classmethod
299+
def setup_class(cls):
300+
os.environ['UTILITIES_UNIT_TESTING'] = "2"
301+
302+
@classmethod
303+
def teardown_class(cls):
304+
os.environ['UTILITIES_UNIT_TESTING'] = "0"
305+
dbconnector.dedicated_dbs['CONFIG_DB'] = None
306+
307+
def test_dns_nameserver_migrator(self):
308+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'dns-nameserver-input')
309+
import db_migrator
310+
dbmgtr = db_migrator.DBMigrator(None)
311+
# Set minigraph_data to DNS_NAMESERVERS
312+
dbmgtr.minigraph_data = {
313+
'DNS_NAMESERVER': {
314+
'1.1.1.1': {},
315+
'2001:1001:110:1001::1': {}
316+
}
317+
}
318+
dbmgtr.migrate()
319+
dbconnector.dedicated_dbs['CONFIG_DB'] = os.path.join(mock_db_path, 'config_db', 'dns-nameserver-expected')
320+
expected_db = Db()
321+
advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, 'version_4_0_4')
322+
resulting_keys = dbmgtr.configDB.keys(dbmgtr.configDB.CONFIG_DB, 'DNS_NAMESERVER*')
323+
expected_keys = expected_db.cfgdb.keys(expected_db.cfgdb.CONFIG_DB, 'DNS_NAMESERVER*')
324+
325+
diff = DeepDiff(resulting_keys, expected_keys, ignore_order=True)
326+
assert not diff
327+
297328
class TestQosDBFieldValueReferenceRemoveMigrator(object):
298329
@classmethod
299330
def setup_class(cls):
@@ -680,6 +711,7 @@ def test_fast_reboot_upgrade_to_4_0_3(self):
680711
dbmgtr = db_migrator.DBMigrator(None)
681712
dbmgtr.migrate()
682713
expected_db = self.mock_dedicated_config_db(db_after_migrate)
714+
advance_version_for_expected_database(dbmgtr.configDB, expected_db.cfgdb, 'version_4_0_3')
683715
assert not self.check_config_db(dbmgtr.configDB, expected_db.cfgdb)
684716
if dbmgtr.CURRENT_VERSION == 'version_4_0_3':
685717
assert dbmgtr.CURRENT_VERSION == expected_db.cfgdb.get_entry('VERSIONS', 'DATABASE')['VERSION']

0 commit comments

Comments
 (0)