Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,10 @@ def config_file_yang_validation(filename):
if multi_asic.is_multi_asic():
asic_list.extend(multi_asic.get_namespace_list())
for scope in asic_list:
config_to_check = config.get(scope) if multi_asic.is_multi_asic() else config
if multi_asic.is_multi_asic() and HOST_NAMESPACE in config:
config_to_check = config.get(scope)
else:
config_to_check = config if scope == HOST_NAMESPACE else None
if config_to_check:
try:
sy.loadData(configdbJson=config_to_check)
Expand Down Expand Up @@ -2161,7 +2164,50 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart, force, file_form
click.echo("Input {} config file(s) separated by comma for multiple files ".format(num_cfg_file))
return

if filename is not None and filename != "/dev/stdin":
if filename is None and file_format == 'config_db':
if not force:
cfg_files_to_validate = [DEFAULT_CONFIG_DB_FILE]
if multi_asic.is_multi_asic():
default_cfg_file_root, default_cfg_file_ext = os.path.splitext(
DEFAULT_CONFIG_DB_FILE
)
cfg_files_to_validate.extend([
"{}{}{}".format(
default_cfg_file_root,
inst,
default_cfg_file_ext,
)
for inst in range(num_asic)
])

for cfg_file in cfg_files_to_validate:
if not os.path.exists(cfg_file):
click.echo(
"The config file {} doesn't exist".format(cfg_file)
)
raise click.Abort()
if not os.access(cfg_file, os.R_OK):
click.echo(
"The config file {} is not readable".format(cfg_file)
)
raise click.Abort()
try:
if not config_file_yang_validation(cfg_file):
click.secho(
"Invalid config file:'{}'!".format(cfg_file),
fg='magenta'
)
raise click.Abort()
except click.Abort:
raise
except Exception as e:
click.echo(
"Failed to read config file {}: {}".format(
cfg_file, str(e)
)
)
raise click.Abort()
elif filename is not None and filename != "/dev/stdin":
if multi_asic.is_multi_asic():
for cfg_file in cfg_files:
if cfg_file is not None:
Expand Down
181 changes: 179 additions & 2 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,12 @@ def setup_class(cls):
open(cls.dummy_cfg_file, 'w').close()

def test_config_reload(self, get_cmd_module, setup_single_broadcom_asic):
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
(config, show) = get_cmd_module
(config, show) = get_cmd_module

with mock.patch(
"utilities_common.cli.run_command",
mock.MagicMock(side_effect=mock_run_command_side_effect)), \
mock.patch.object(config, "config_file_yang_validation") as mock_validate_config:

jsonfile_config = os.path.join(mock_db_path, "config_db.json")
jsonfile_init_cfg = os.path.join(mock_db_path, "init_cfg.json")
Expand All @@ -753,6 +757,132 @@ def test_config_reload(self, get_cmd_module, setup_single_broadcom_asic):

assert "\n".join([line.rstrip() for line in result.output.split('\n')][:2]) == \
reload_config_with_sys_info_command_output.format(config.SYSTEM_RELOAD_LOCK)
mock_validate_config.assert_called_once_with(jsonfile_config)

def test_config_reload_default_config_validation_failure(self, get_cmd_module, setup_single_broadcom_asic):
(config, show) = get_cmd_module

jsonfile_config = os.path.join(mock_db_path, "config_db.json")
config.DEFAULT_CONFIG_DB_FILE = jsonfile_config

with mock.patch("utilities_common.cli.run_command") as mock_run_command, \
mock.patch.object(
config, "config_file_yang_validation", side_effect=click.Abort) as mock_validate_config:
runner = CliRunner()
result = runner.invoke(config.config.commands["reload"], ["-y", "-n"])

assert result.exit_code != 0
mock_validate_config.assert_called_once_with(jsonfile_config)
mock_run_command.assert_not_called()

def test_config_reload_force_skips_validation(self, get_cmd_module, setup_single_broadcom_asic):
(config, show) = get_cmd_module

config.DEFAULT_CONFIG_DB_FILE = os.path.join(
mock_db_path, 'missing_config_db.json'
)

with mock.patch(
"utilities_common.cli.run_command",
mock.MagicMock(side_effect=mock_run_command_side_effect)), \
mock.patch.object(config, "config_file_yang_validation") as mock_validate_config:
runner = CliRunner()
result = runner.invoke(
config.config.commands["reload"], ["-y", "-f", "-n"]
)

assert result.exit_code == 0
mock_validate_config.assert_not_called()

def test_config_reload_default_config_missing_file(
self, get_cmd_module, setup_single_broadcom_asic
):
(config, show) = get_cmd_module

config.DEFAULT_CONFIG_DB_FILE = os.path.join(
mock_db_path, 'missing_config_db.json'
)

with mock.patch(
"utilities_common.cli.run_command"
) as mock_run_command:
runner = CliRunner()
result = runner.invoke(
config.config.commands["reload"], ["-y", "-n"]
)

assert result.exit_code != 0
assert (
"The config file {} doesn't exist".format(
config.DEFAULT_CONFIG_DB_FILE
) in result.output
)
assert "Traceback" not in result.output
mock_run_command.assert_not_called()

def test_config_reload_default_config_invalid_json(
self, get_cmd_module, setup_single_broadcom_asic
):
(config, show) = get_cmd_module

jsonfile_config = os.path.join(mock_db_path, "config_db.json")
config.DEFAULT_CONFIG_DB_FILE = jsonfile_config

with mock.patch(
"utilities_common.cli.run_command"
) as mock_run_command:
with mock.patch('config.main.os.access', return_value=True):
with mock.patch.object(
config,
"config_file_yang_validation",
side_effect=Exception("bad json"),
):
runner = CliRunner()
result = runner.invoke(
config.config.commands["reload"], ["-y", "-n"]
)

assert result.exit_code != 0
assert (
"Failed to read config file {}: bad json".format(
jsonfile_config
) in result.output
)
assert "Traceback" not in result.output
mock_run_command.assert_not_called()

def test_config_reload_default_config_invalid_root(
self, get_cmd_module, setup_single_broadcom_asic
):
(config, show) = get_cmd_module

jsonfile_config = os.path.join(mock_db_path, "config_db.json")
config.DEFAULT_CONFIG_DB_FILE = jsonfile_config

with mock.patch(
"utilities_common.cli.run_command"
) as mock_run_command:
with mock.patch('config.main.os.access', return_value=True):
with mock.patch.object(
config,
"config_file_yang_validation",
return_value=False,
) as mock_validate_config:
runner = CliRunner()
result = runner.invoke(
config.config.commands["reload"], ["-y", "-n"]
)

assert result.exit_code != 0
assert (
"Invalid config file:'{}'!".format(
jsonfile_config
) in result.output
)
mock_validate_config.assert_called_once_with(
jsonfile_config
)
mock_run_command.assert_not_called()

def test_config_reload_stdin(self, get_cmd_module, setup_single_broadcom_asic):
def mock_json_load(f):
Expand Down Expand Up @@ -1111,6 +1241,53 @@ def test_config_reload_multiple_files_with_spaces(self):
assert "\n".join([li.rstrip() for li in result.output.split('\n')]) == \
RELOAD_MASIC_CONFIG_DB_OUTPUT.format(config.SYSTEM_RELOAD_LOCK)

def test_config_reload_default_files_validate_all_namespaces(self):
dummy_cfg_file = os.path.join(
os.sep, "tmp", "reload_validate_config_db.json"
)
dummy_cfg_file_asic0 = os.path.join(
os.sep, "tmp", "reload_validate_config_db0.json"
)
dummy_cfg_file_asic1 = os.path.join(
os.sep, "tmp", "reload_validate_config_db1.json"
)
device_metadata = {
"DEVICE_METADATA": {
"localhost": {
"platform": "some_platform",
"mac": "02:42:f0:7f:01:05"
}
}
}
self._create_dummy_config(dummy_cfg_file, device_metadata)
self._create_dummy_config(dummy_cfg_file_asic0, device_metadata)
self._create_dummy_config(dummy_cfg_file_asic1, device_metadata)

with mock.patch("utilities_common.cli.run_command",
mock.MagicMock(
side_effect=mock_run_command_side_effect
)), \
mock.patch.object(
config, 'DEFAULT_CONFIG_DB_FILE', dummy_cfg_file
), \
mock.patch(
'config.main.config_file_yang_validation',
return_value=True
) as mock_validate_config:
runner = CliRunner()

result = runner.invoke(
config.config.commands["reload"], ['-y', '-n']
)

assert result.exit_code == 0
assert mock_validate_config.call_count == 3
assert mock_validate_config.call_args_list == [
mock.call(dummy_cfg_file),
mock.call(dummy_cfg_file_asic0),
mock.call(dummy_cfg_file_asic1),
]

@classmethod
def teardown_class(cls):
print("TEARDOWN")
Expand Down
Loading