Skip to content

Commit

Permalink
[config] no op if Golden Config is invalid (#3367)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wen587 authored and mssonicbld committed Aug 24, 2024
1 parent 1f4315e commit 2e9009e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
8 changes: 8 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,14 @@ def load_minigraph(db, no_service_restart, traffic_shift_away, override_config,
fg='magenta')
raise click.Abort()

# Dependency check golden config json
config_to_check = read_json_file(golden_config_path)
if multi_asic.is_multi_asic():
host_config = config_to_check.get('localhost', {})
else:
host_config = config_to_check
table_hard_dependency_check(host_config)

#Stop services before config push
if not no_service_restart:
log.log_notice("'load_minigraph' stopping services...")
Expand Down
50 changes: 47 additions & 3 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,13 @@ def is_file_side_effect(filename):
def test_load_minigraph_with_specified_golden_config_path(self, get_cmd_module):
def is_file_side_effect(filename):
return True if 'golden_config' in filename else False

def read_json_file_side_effect(filename):
return {}

with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command, \
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)), \
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
(config, show) = get_cmd_module
runner = CliRunner()
result = runner.invoke(config.config.commands["load_minigraph"], ["--override_config", "--golden_config_path", "golden_config.json", "-y"])
Expand All @@ -999,14 +1004,48 @@ def is_file_side_effect(filename):
def test_load_minigraph_with_default_golden_config_path(self, get_cmd_module):
def is_file_side_effect(filename):
return True if 'golden_config' in filename else False

def read_json_file_side_effect(filename):
return {}

with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command, \
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)), \
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
(config, show) = get_cmd_module
runner = CliRunner()
result = runner.invoke(config.config.commands["load_minigraph"], ["--override_config", "-y"])
assert result.exit_code == 0
assert "config override-config-table /etc/sonic/golden_config_db.json" in result.output

@mock.patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs',
mock.MagicMock(return_value=("dummy_path", None)))
def test_load_minigraph_hard_dependency_check(self, get_cmd_module):
def is_file_side_effect(filename):
return True if 'golden_config' in filename else False

def read_json_file_side_effect(filename):
return {
"AAA": {
"authentication": {
"login": "tacacs+"
}
},
"TACPLUS": {
"global": {
"passkey": ""
}
}
}

with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)), \
mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)), \
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
(config, _) = get_cmd_module
runner = CliRunner()
result = runner.invoke(config.config.commands["load_minigraph"], ["--override_config", "-y"])
assert result.exit_code != 0
assert "Authentication with 'tacacs+' is not allowed when passkey not exits." in result.output

@mock.patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', mock.MagicMock(return_value=("dummy_path", None)))
def test_load_minigraph_with_traffic_shift_away(self, get_cmd_module):
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
Expand All @@ -1024,7 +1063,12 @@ def test_load_minigraph_with_traffic_shift_away_with_golden_config(self, get_cmd
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
def is_file_side_effect(filename):
return True if 'golden_config' in filename else False
with mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)):

def read_json_file_side_effect(filename):
return {}

with mock.patch('os.path.isfile', mock.MagicMock(side_effect=is_file_side_effect)), \
mock.patch('config.main.read_json_file', mock.MagicMock(side_effect=read_json_file_side_effect)):
(config, show) = get_cmd_module
db = Db()
golden_config = {}
Expand Down

0 comments on commit 2e9009e

Please sign in to comment.