Skip to content

Commit 6b5bf99

Browse files
wen587yxieca
authored andcommitted
[config]config reload should generate sysinfo if missing (#2778)
What I did Missing platform and mac in CONFIG_DB will result in container failure. We should make the config reload generate those info if missing. How I did it Add missing sys info if config_db.json doesn't contain it. How to verify it Unit test
1 parent a8455fd commit 6b5bf99

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

config/main.py

+13
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,19 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart, force, file_form
14651465
click.echo("The config file {} doesn't exist".format(file))
14661466
continue
14671467

1468+
if file_format == 'config_db':
1469+
file_input = read_json_file(file)
1470+
1471+
platform = file_input.get("DEVICE_METADATA", {}).\
1472+
get("localhost", {}).get("platform")
1473+
mac = file_input.get("DEVICE_METADATA", {}).\
1474+
get("localhost", {}).get("mac")
1475+
1476+
if not platform or not mac:
1477+
log.log_warning("Input file does't have platform or mac. platform: {}, mac: {}"
1478+
.format(None if platform is None else platform, None if mac is None else mac))
1479+
load_sysinfo = True
1480+
14681481
if load_sysinfo:
14691482
try:
14701483
command = "{} -j {} -v DEVICE_METADATA.localhost.hwsku".format(SONIC_CFGGEN_PATH, file)

tests/config_test.py

+60-1
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,66 @@ def setup_class(cls):
408408
print("SETUP")
409409
import config.main
410410
importlib.reload(config.main)
411-
open(cls.dummy_cfg_file, 'w').close()
411+
412+
def add_sysinfo_to_cfg_file(self):
413+
with open(self.dummy_cfg_file, 'w') as f:
414+
device_metadata = {
415+
"DEVICE_METADATA": {
416+
"localhost": {
417+
"platform": "some_platform",
418+
"mac": "02:42:f0:7f:01:05"
419+
}
420+
}
421+
}
422+
f.write(json.dumps(device_metadata))
423+
424+
def test_reload_config_invalid_input(self, get_cmd_module, setup_single_broadcom_asic):
425+
open(self.dummy_cfg_file, 'w').close()
426+
with mock.patch(
427+
"utilities_common.cli.run_command",
428+
mock.MagicMock(side_effect=mock_run_command_side_effect)
429+
) as mock_run_command:
430+
(config, show) = get_cmd_module
431+
runner = CliRunner()
432+
433+
result = runner.invoke(
434+
config.config.commands["reload"],
435+
[self.dummy_cfg_file, '-y', '-f'])
436+
437+
print(result.exit_code)
438+
print(result.output)
439+
traceback.print_tb(result.exc_info[2])
440+
assert result.exit_code != 0
441+
442+
def test_reload_config_no_sysinfo(self, get_cmd_module, setup_single_broadcom_asic):
443+
with open(self.dummy_cfg_file, 'w') as f:
444+
device_metadata = {
445+
"DEVICE_METADATA": {
446+
"localhost": {
447+
"hwsku": "some_hwsku"
448+
}
449+
}
450+
}
451+
f.write(json.dumps(device_metadata))
452+
453+
with mock.patch(
454+
"utilities_common.cli.run_command",
455+
mock.MagicMock(side_effect=mock_run_command_side_effect)
456+
) as mock_run_command:
457+
(config, show) = get_cmd_module
458+
runner = CliRunner()
459+
460+
result = runner.invoke(
461+
config.config.commands["reload"],
462+
[self.dummy_cfg_file, '-y', '-f'])
463+
464+
print(result.exit_code)
465+
print(result.output)
466+
traceback.print_tb(result.exc_info[2])
467+
assert result.exit_code == 0
412468

413469
def test_reload_config(self, get_cmd_module, setup_single_broadcom_asic):
470+
self.add_sysinfo_to_cfg_file()
414471
with mock.patch(
415472
"utilities_common.cli.run_command",
416473
mock.MagicMock(side_effect=mock_run_command_side_effect)
@@ -430,6 +487,7 @@ def test_reload_config(self, get_cmd_module, setup_single_broadcom_asic):
430487
== RELOAD_CONFIG_DB_OUTPUT
431488

432489
def test_config_reload_disabled_service(self, get_cmd_module, setup_single_broadcom_asic):
490+
self.add_sysinfo_to_cfg_file()
433491
with mock.patch(
434492
"utilities_common.cli.run_command",
435493
mock.MagicMock(side_effect=mock_run_command_side_effect_disabled_timer)
@@ -449,6 +507,7 @@ def test_config_reload_disabled_service(self, get_cmd_module, setup_single_broad
449507
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) == reload_config_with_disabled_service_output
450508

451509
def test_reload_config_masic(self, get_cmd_module, setup_multi_broadcom_masic):
510+
self.add_sysinfo_to_cfg_file()
452511
with mock.patch(
453512
"utilities_common.cli.run_command",
454513
mock.MagicMock(side_effect=mock_run_command_side_effect)

0 commit comments

Comments
 (0)