diff --git a/config/main.py b/config/main.py index b304b9a7eac..3d18498552c 100644 --- a/config/main.py +++ b/config/main.py @@ -2161,7 +2161,10 @@ 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': + # Validate the default config before stopping services and flushing CONFIG_DB. + config_file_yang_validation(DEFAULT_CONFIG_DB_FILE) + 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: diff --git a/tests/config_test.py b/tests/config_test.py index c0e29c70a70..e8d8b07ee91 100644 --- a/tests/config_test.py +++ b/tests/config_test.py @@ -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") @@ -753,6 +757,23 @@ 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", "-f"]) + + assert result.exit_code != 0 + 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):