Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wen587 committed May 14, 2024
1 parent a30ce28 commit b810d94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
42 changes: 22 additions & 20 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,22 @@ def validate_gre_type(ctx, _, value):
except ValueError:
raise click.UsageError("{} is not a valid GRE type".format(value))

def save_to_single_file_multi(db, filename):
"""A function to save all asic's config to single file
"""
all_files_config = {}
cfgdb_clients = db.cfgdb_clients

for ns, config_db in cfgdb_clients.items():
current_config = config_db.get_config()
sonic_cfggen.FormatConverter.to_serialized(current_config)
asic_name = "localhost" if ns == DEFAULT_NAMESPACE else ns
all_files_config[asic_name] = sort_dict(current_config)
click.echo("Integrate each ASIC's config into a single JSON file {}.".format(filename))
with open(filename, 'w') as all_files_file:
json.dump(all_files_config, all_files_file, indent=4)


# This is our main entrypoint - the main 'config' command
@click.group(cls=clicommon.AbbreviationGroup, context_settings=CONTEXT_SETTINGS)
@click.pass_context
Expand Down Expand Up @@ -1222,7 +1238,8 @@ def config(ctx):
@click.option('-y', '--yes', is_flag=True, callback=_abort_if_false,
expose_value=False, prompt='Existing files will be overwritten, continue?')
@click.argument('filename', required=False)
def save(filename):
@clicommon.pass_db
def save(db, filename):
"""Export current config DB to a file on disk.\n
<filename> : Names of configuration file(s) to save, separated by comma with no spaces in between
"""
Expand All @@ -1240,26 +1257,21 @@ def save(filename):
# If only one filename is provided in multi-ASIC mode,
# save all ASIC configurations to that single file.
if len(cfg_files) == 1 and multi_asic.is_multi_asic():
single_file_mode = True
filename = cfg_files[0]
cfg_files = [filename] * num_cfg_file
save_to_single_file_multi(db, filename)
return
elif len(cfg_files) != num_cfg_file:
click.echo("Input {} config file(s) separated by comma for multiple files ".format(num_cfg_file))
return
else:
single_file_mode = False

# List to hold config DB for all ASICs
all_files_config = {}

# In case of multi-asic mode we have additional config_db{NS}.json files for
# various namespaces created per ASIC. {NS} is the namespace index.
for inst in range(-1, num_cfg_file-1):
#inst = -1, refers to the linux host where there is no namespace.
if inst == -1:
namespace = None
asic_name = "localhost"
else:
namespace = "{}{}".format(NAMESPACE_PREFIX, inst)
asic_name = namespace

# Get the file from user input, else take the default file /etc/sonic/config_db{NS_id}.json
if cfg_files:
Expand All @@ -1282,16 +1294,6 @@ def save(filename):
with open(file, 'w') as config_db_file:
json.dump(config_db, config_db_file, indent=4)

# Append config DB to all_files_config
all_files_config[asic_name] = config_db

# If filename is provided and multi-ASIC mode, save all ASICs' configurations to a single file
if filename is not None and multi_asic.is_multi_asic():
if single_file_mode:
click.echo("Integrate each ASIC's config into a single JSON file {}.".format(filename))
with open(filename, 'w') as all_files_file:
json.dump(all_files_config, all_files_file, indent=4)

@config.command()
@click.option('-y', '--yes', is_flag=True)
@click.argument('filename', required=False)
Expand Down
22 changes: 9 additions & 13 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,6 @@
"""

save_config_onefile_masic_output="""\
Running command: /usr/local/bin/sonic-cfggen -d --print-data > /tmp/all_config_db.json
Running command: /usr/local/bin/sonic-cfggen -n asic0 -d --print-data > /tmp/all_config_db.json
Running command: /usr/local/bin/sonic-cfggen -n asic1 -d --print-data > /tmp/all_config_db.json
Integrate each ASIC's config into a single JSON file /tmp/all_config_db.json.
"""

Expand Down Expand Up @@ -343,7 +340,7 @@ def read_json_file_side_effect(filename):

with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command,\
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)),\
mock.patch('builtins.open', mock.MagicMock()) as mocked_open:
mock.patch('config.main.open', mock.MagicMock()) as mocked_open:
(config, show) = get_cmd_module

runner = CliRunner()
Expand All @@ -363,7 +360,7 @@ def read_json_file_side_effect(filename):

with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command,\
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)),\
mock.patch('builtins.open', mock.MagicMock()) as mocked_open:
mock.patch('config.main.open', mock.MagicMock()) as mocked_open:

(config, show) = get_cmd_module

Expand Down Expand Up @@ -405,7 +402,7 @@ def read_json_file_side_effect(filename):

with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command,\
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)),\
mock.patch('builtins.open', mock.MagicMock()) as mocked_open:
mock.patch('config.main.open', mock.MagicMock()) as mocked_open:
(config, show) = get_cmd_module

runner = CliRunner()
Expand All @@ -425,7 +422,7 @@ def read_json_file_side_effect(filename):

with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command,\
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)),\
mock.patch('builtins.open', mock.MagicMock()) as mocked_open:
mock.patch('config.main.open', mock.MagicMock()) as mocked_open:
(config, show) = get_cmd_module

runner = CliRunner()
Expand All @@ -448,7 +445,7 @@ def read_json_file_side_effect(filename):

with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command,\
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)),\
mock.patch('builtins.open', mock.MagicMock()) as mocked_open:
mock.patch('config.main.open', mock.MagicMock()) as mocked_open:
(config, show) = get_cmd_module

runner = CliRunner()
Expand All @@ -464,14 +461,13 @@ def read_json_file_side_effect(filename):

assert "Input 3 config file(s) separated by comma for multiple files" in result.output

def test_config_save_onefile_masic(self, get_cmd_module, setup_multi_broadcom_masic):
def read_json_file_side_effect(filename):
def test_config_save_onefile_masic(self):
def get_config_side_effect():
return {}

with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command,\
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
#mock.patch('builtins.open', mock.MagicMock()) as mocked_open:
(config, show) = get_cmd_module
mock.patch('swsscommon.swsscommon.ConfigDBConnector.get_config',
mock.MagicMock(side_effect=get_config_side_effect)):

runner = CliRunner()

Expand Down

0 comments on commit b810d94

Please sign in to comment.