Skip to content

Commit

Permalink
Fix database initialization for db_migrator (#3100)
Browse files Browse the repository at this point in the history
What I did
db_migrator failed to initialize SonicDBConfig, and I fix this issue.

How I did it
If SonicDBConfig is already initialized, do not invoke initialize() again.

How to verify it
Run unit test, and verified on DUT.
  • Loading branch information
ganglyu authored and mssonicbld committed Jan 30, 2024
1 parent e9ae14d commit 9c1d489
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 3 additions & 5 deletions scripts/db_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import re

from sonic_py_common import device_info, logger
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector, SonicDBConfig
from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector
from minigraph import parse_xml
from utilities_common.helper import update_config
from utilities_common.general import load_db_config

INIT_CFG_FILE = '/etc/sonic/init_cfg.json'
MINIGRAPH_FILE = '/etc/sonic/minigraph.xml'
Expand Down Expand Up @@ -1256,10 +1257,7 @@ def main():
socket_path = args.socket
namespace = args.namespace

if args.namespace is not None:
SonicDBConfig.load_sonic_global_db_config(namespace=args.namespace)
else:
SonicDBConfig.initialize()
load_db_config()

if socket_path:
dbmgtr = DBMigrator(namespace, socket=socket_path)
Expand Down
18 changes: 17 additions & 1 deletion tests/db_migrator_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import pytest
import sys

import argparse
from unittest import mock
from deepdiff import DeepDiff

from swsscommon.swsscommon import SonicV2Connector
Expand Down Expand Up @@ -904,3 +905,18 @@ def test_golden_config_hostname(self):
hostname = host.get('hostname', '')
# hostname is from minigraph.xml
assert hostname == 'SONiC-Dummy'

class TestMain(object):
@classmethod
def setup_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "2"

@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"

@mock.patch('argparse.ArgumentParser.parse_args')
def test_init(self, mock_args):
mock_args.return_value=argparse.Namespace(namespace=None, operation='get_version', socket=None)
import db_migrator
db_migrator.main()

0 comments on commit 9c1d489

Please sign in to comment.