Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[action] [PR:3576] Add YANG validation for config reload if file is given (#3576) #3581

Merged
merged 1 commit into from
Oct 21, 2024
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
30 changes: 21 additions & 9 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,19 @@ def multiasic_write_to_db(filename, load_sysinfo):
migrate_db_to_lastest(ns)


def config_file_yang_validation(filename):
config_to_check = read_json_file(filename)
sy = sonic_yang.SonicYang(YANG_DIR)
sy.loadYangModel()
try:
sy.loadData(configdbJson=config_to_check)
sy.validate_data_tree()
except sonic_yang.SonicYangException as e:
click.secho("{} fails YANG validation! Error: {}".format(filename, str(e)),
fg='magenta')
raise click.Abort()


# This is our main entrypoint - the main 'config' command
@click.group(cls=clicommon.AbbreviationGroup, context_settings=CONTEXT_SETTINGS)
@click.pass_context
Expand Down Expand Up @@ -1780,6 +1793,13 @@ 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:
if multi_asic.is_multi_asic():
# Multiasic has not 100% fully validated. Thus pass here.
pass
else:
config_file_yang_validation(filename)

#Stop services before config push
if not no_service_restart:
log.log_notice("'reload' stopping services...")
Expand Down Expand Up @@ -1970,15 +1990,7 @@ def load_minigraph(db, no_service_restart, traffic_shift_away, override_config,
# Multiasic has not 100% fully validated. Thus pass here.
pass
else:
sy = sonic_yang.SonicYang(YANG_DIR)
sy.loadYangModel()
try:
sy.loadData(configdbJson=config_to_check)
sy.validate_data_tree()
except sonic_yang.SonicYangException as e:
click.secho("{} fails YANG validation! Error: {}".format(golden_config_path, str(e)),
fg='magenta')
raise click.Abort()
config_file_yang_validation(golden_config_path)

# Dependency check golden config json
if multi_asic.is_multi_asic():
Expand Down
28 changes: 28 additions & 0 deletions tests/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,34 @@ def test_reload_yang_config(self, get_cmd_module,
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) \
== RELOAD_YANG_CFG_OUTPUT.format(config.SYSTEM_RELOAD_LOCK)

def test_reload_config_fails_yang_validation(self, get_cmd_module, setup_single_broadcom_asic):
with open(self.dummy_cfg_file, 'w') as f:
device_metadata = {
"DEVICE_METADATA": {
"localhost": {
"invalid_hwsku": "some_hwsku"
}
}
}
f.write(json.dumps(device_metadata))

with mock.patch(
"utilities_common.cli.run_command",
mock.MagicMock(side_effect=mock_run_command_side_effect)
):
(config, _) = get_cmd_module
runner = CliRunner()

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

print(result.exit_code)
print(result.output)
traceback.print_tb(result.exc_info[2])
assert result.exit_code != 0
assert "fails YANG validation! Error" in result.output

@classmethod
def teardown_class(cls):
os.environ['UTILITIES_UNIT_TESTING'] = "0"
Expand Down
Loading