Skip to content

Commit 3df762f

Browse files
authored
[config] no op if Golden Config is invalid (sonic-net#3367)
ADO: 27941719 What I did Improve Golden Config workflow and make sure no op if invalid config detected How I did it Add table dependency check right after Golden Config path is enabled How to verify it Unit test
1 parent 8f715ac commit 3df762f

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

config/main.py

+8
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,14 @@ def load_minigraph(db, no_service_restart, traffic_shift_away, override_config,
19541954
fg='magenta')
19551955
raise click.Abort()
19561956

1957+
# Dependency check golden config json
1958+
config_to_check = read_json_file(golden_config_path)
1959+
if multi_asic.is_multi_asic():
1960+
host_config = config_to_check.get('localhost', {})
1961+
else:
1962+
host_config = config_to_check
1963+
table_hard_dependency_check(host_config)
1964+
19571965
#Stop services before config push
19581966
if not no_service_restart:
19591967
log.log_notice("'load_minigraph' stopping services...")

tests/config_test.py

+47-3
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,13 @@ def is_file_side_effect(filename):
987987
def test_load_minigraph_with_specified_golden_config_path(self, get_cmd_module):
988988
def is_file_side_effect(filename):
989989
return True if 'golden_config' in filename else False
990+
991+
def read_json_file_side_effect(filename):
992+
return {}
993+
990994
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command, \
991-
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
995+
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)), \
996+
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
992997
(config, show) = get_cmd_module
993998
runner = CliRunner()
994999
result = runner.invoke(config.config.commands["load_minigraph"], ["--override_config", "--golden_config_path", "golden_config.json", "-y"])
@@ -999,14 +1004,48 @@ def is_file_side_effect(filename):
9991004
def test_load_minigraph_with_default_golden_config_path(self, get_cmd_module):
10001005
def is_file_side_effect(filename):
10011006
return True if 'golden_config' in filename else False
1007+
1008+
def read_json_file_side_effect(filename):
1009+
return {}
1010+
10021011
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command, \
1003-
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
1012+
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)), \
1013+
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
10041014
(config, show) = get_cmd_module
10051015
runner = CliRunner()
10061016
result = runner.invoke(config.config.commands["load_minigraph"], ["--override_config", "-y"])
10071017
assert result.exit_code == 0
10081018
assert "config override-config-table /etc/sonic/golden_config_db.json" in result.output
10091019

1020+
@mock.patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs',
1021+
mock.MagicMock(return_value=("dummy_path", None)))
1022+
def test_load_minigraph_hard_dependency_check(self, get_cmd_module):
1023+
def is_file_side_effect(filename):
1024+
return True if 'golden_config' in filename else False
1025+
1026+
def read_json_file_side_effect(filename):
1027+
return {
1028+
"AAA": {
1029+
"authentication": {
1030+
"login": "tacacs+"
1031+
}
1032+
},
1033+
"TACPLUS": {
1034+
"global": {
1035+
"passkey": ""
1036+
}
1037+
}
1038+
}
1039+
1040+
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)), \
1041+
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)), \
1042+
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
1043+
(config, _) = get_cmd_module
1044+
runner = CliRunner()
1045+
result = runner.invoke(config.config.commands["load_minigraph"], ["--override_config", "-y"])
1046+
assert result.exit_code != 0
1047+
assert "Authentication with 'tacacs+' is not allowed when passkey not exits." in result.output
1048+
10101049
@mock.patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', mock.MagicMock(return_value=("dummy_path", None)))
10111050
def test_load_minigraph_with_traffic_shift_away(self, get_cmd_module):
10121051
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
@@ -1024,7 +1063,12 @@ def test_load_minigraph_with_traffic_shift_away_with_golden_config(self, get_cmd
10241063
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
10251064
def is_file_side_effect(filename):
10261065
return True if 'golden_config' in filename else False
1027-
with mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
1066+
1067+
def read_json_file_side_effect(filename):
1068+
return {}
1069+
1070+
with mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)), \
1071+
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
10281072
(config, show) = get_cmd_module
10291073
db = Db()
10301074
golden_config = {}

0 commit comments

Comments
 (0)