Skip to content

Commit

Permalink
[db_migrator] Migrate DNS configuratuion (sonic-net#2893)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ganglyu authored and rajkumar38 committed Jul 25, 2023
1 parent c36f9dd commit ae6f302
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 7 deletions.
38 changes: 31 additions & 7 deletions scripts/db_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions tests/db_migrator_input/config_db/dns-nameserver-expected.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
5 changes: 5 additions & 0 deletions tests/db_migrator_input/config_db/dns-nameserver-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"VERSIONS|DATABASE": {
"VERSION": "version_4_0_3"
}
}
32 changes: 32 additions & 0 deletions tests/db_migrator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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']
Expand Down

0 comments on commit ae6f302

Please sign in to comment.