Skip to content

Commit 0a7557b

Browse files
authored
[minigraph] add option to specify golden path in load_minigraph (sonic-net#2350)
What I did Add an option for load_minigraph to specify golden path How I did it Add an option for load_minigraph How to verify it Unit test
1 parent 322aefc commit 0a7557b

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

config/main.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -1720,8 +1720,9 @@ def load_mgmt_config(filename):
17201720
expose_value=False, prompt='Reload config from minigraph?')
17211721
@click.option('-n', '--no_service_restart', default=False, is_flag=True, help='Do not restart docker services')
17221722
@click.option('-t', '--traffic_shift_away', default=False, is_flag=True, help='Keep device in maintenance with TSA')
1723+
@click.option('-p', '--golden_config_path', help='The path of golden config file')
17231724
@clicommon.pass_db
1724-
def load_minigraph(db, no_service_restart, traffic_shift_away):
1725+
def load_minigraph(db, no_service_restart, traffic_shift_away, golden_config_path):
17251726
"""Reconfigure based on minigraph."""
17261727
log.log_info("'load_minigraph' executing...")
17271728

@@ -1794,13 +1795,20 @@ def load_minigraph(db, no_service_restart, traffic_shift_away):
17941795
# Keep device isolated with TSA
17951796
if traffic_shift_away:
17961797
clicommon.run_command("TSA", display_cmd=True)
1797-
if os.path.isfile(DEFAULT_GOLDEN_CONFIG_DB_FILE):
1798+
if golden_config_path or not golden_config_path and os.path.isfile(DEFAULT_GOLDEN_CONFIG_DB_FILE):
17981799
log.log_warning("Golden configuration may override System Maintenance state. Please execute TSC to check the current System mode")
17991800
click.secho("[WARNING] Golden configuration may override Traffic-shift-away state. Please execute TSC to check the current System mode")
18001801

18011802
# Load golden_config_db.json
1802-
if os.path.isfile(DEFAULT_GOLDEN_CONFIG_DB_FILE):
1803-
override_config_by(DEFAULT_GOLDEN_CONFIG_DB_FILE)
1803+
if golden_config_path:
1804+
if not os.path.isfile(golden_config_path):
1805+
click.secho("Cannot find '{}'!".format(golden_config_path),
1806+
fg='magenta')
1807+
raise click.Abort()
1808+
override_config_by(golden_config_path)
1809+
else:
1810+
if os.path.isfile(DEFAULT_GOLDEN_CONFIG_DB_FILE):
1811+
override_config_by(DEFAULT_GOLDEN_CONFIG_DB_FILE)
18041812

18051813
# We first run "systemctl reset-failed" to remove the "failed"
18061814
# status from all services before we attempt to restart them

tests/config_test.py

+25
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,31 @@ def is_file_side_effect(filename):
430430
assert result.exit_code == 0
431431
assert expected_output in result.output
432432

433+
def test_load_minigraph_with_non_exist_golden_config_path(self, get_cmd_module):
434+
def is_file_side_effect(filename):
435+
return True if 'golden_config' in filename else False
436+
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command, \
437+
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
438+
(config, show) = get_cmd_module
439+
runner = CliRunner()
440+
result = runner.invoke(config.config.commands["load_minigraph"], ["-p", "non_exist.json", "-y"])
441+
assert result.exit_code != 0
442+
assert "Cannot find 'non_exist.json'" in result.output
443+
444+
def test_load_minigraph_with_golden_config_path(self, get_cmd_module):
445+
def is_file_side_effect(filename):
446+
return True if 'golden_config' in filename else False
447+
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command, \
448+
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
449+
(config, show) = get_cmd_module
450+
runner = CliRunner()
451+
result = runner.invoke(config.config.commands["load_minigraph"], ["-p", "golden_config.json", "-y"])
452+
print(result.exit_code)
453+
print(result.output)
454+
traceback.print_tb(result.exc_info[2])
455+
assert result.exit_code == 0
456+
assert "config override-config-table golden_config.json" in result.output
457+
433458
def test_load_minigraph_with_traffic_shift_away(self, get_cmd_module):
434459
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
435460
(config, show) = get_cmd_module

0 commit comments

Comments
 (0)