Skip to content

Commit

Permalink
Add negative test
Browse files Browse the repository at this point in the history
  • Loading branch information
xincunli-sonic committed Feb 22, 2025
1 parent 5a91a6b commit 6cada06
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 1 addition & 5 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,11 +1394,7 @@ def config_file_yang_validation(filename):
config = read_json_file(filename)

# Check if the config is empty
if not config:
return False

# Check if the config is not a dictionary
if not isinstance(config, dict):
if not config or not isinstance(config, dict):
return False

# If the device is multi-ASIC, check if all required namespaces exist
Expand Down
14 changes: 14 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,20 @@ def is_file_side_effect(filename):
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_with_invalid_golden_config(self, get_cmd_module):
def is_file_side_effect(filename):
return True if 'golden_config' in filename else False

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('config.main.read_json_file', mock.MagicMock(return_value={})):
(config, show) = get_cmd_module
runner = CliRunner()
result = runner.invoke(config.config.commands["load_minigraph"], ["--override_config", "-y"])
assert result.exit_code != 0
assert "Invalid golden config file:" 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):
Expand Down

0 comments on commit 6cada06

Please sign in to comment.