Skip to content

Commit f63ef9a

Browse files
authored
Revert "sonic-utilities: Update config reload() to verify formatting of an input file (#2529)" (#2586)
This reverts commit 42f51c2. Reverts sonic-net/sonic-utilities#2529 Reason: There are use cases like `config reload /dev/stdin`, for example [L2 Switch mode · sonic-net/SONiC Wiki (github.com)](https://github.com/sonic-net/SONiC/wiki/L2-Switch-mode). The original PR would read input file twice, so /dev/stdin does not work.
1 parent 3a09ecb commit f63ef9a

File tree

3 files changed

+8
-208
lines changed

3 files changed

+8
-208
lines changed

config/main.py

+6-33
Original file line numberDiff line numberDiff line change
@@ -1189,17 +1189,6 @@ def load_backend_acl(cfg_db, device_type):
11891189
if os.path.isfile(BACKEND_ACL_FILE):
11901190
clicommon.run_command("acl-loader update incremental {}".format(BACKEND_ACL_FILE), display_cmd=True)
11911191

1192-
def validate_config_file(file):
1193-
"""
1194-
A validator to check config files for syntax errors
1195-
"""
1196-
try:
1197-
# Load golden config json
1198-
read_json_file(file)
1199-
except Exception as e:
1200-
click.secho("Bad format: json file '{}' broken.\n{}".format(file, str(e)),
1201-
fg='magenta')
1202-
sys.exit(1)
12031192

12041193
# This is our main entrypoint - the main 'config' command
12051194
@click.group(cls=clicommon.AbbreviationGroup, context_settings=CONTEXT_SETTINGS)
@@ -1578,8 +1567,10 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart, force, file_form
15781567
click.echo("Input {} config file(s) separated by comma for multiple files ".format(num_cfg_file))
15791568
return
15801569

1581-
# Create a dictionary to store each cfg_file, namespace, and a bool representing if a the file exists
1582-
cfg_file_dict = {}
1570+
#Stop services before config push
1571+
if not no_service_restart:
1572+
log.log_info("'reload' stopping services...")
1573+
_stop_services()
15831574

15841575
# In Single ASIC platforms we have single DB service. In multi-ASIC platforms we have a global DB
15851576
# service running in the host + DB services running in each ASIC namespace created per ASIC.
@@ -1604,27 +1595,9 @@ def reload(db, filename, yes, load_sysinfo, no_service_restart, force, file_form
16041595
else:
16051596
file = DEFAULT_CONFIG_YANG_FILE
16061597

1607-
# Check if the file exists before proceeding
1608-
# Instead of exiting, skip the current namespace and check the next one
1609-
if not os.path.exists(file):
1610-
cfg_file_dict[inst] = [file, namespace, False]
1611-
continue
1612-
cfg_file_dict[inst] = [file, namespace, True]
1613-
1614-
# Check the file is properly formatted before proceeding.
1615-
validate_config_file(file)
1616-
1617-
#Validate INIT_CFG_FILE if it exits
1618-
if os.path.isfile(INIT_CFG_FILE):
1619-
validate_config_file(INIT_CFG_FILE)
1620-
1621-
#Stop services before config push
1622-
if not no_service_restart:
1623-
log.log_info("'reload' stopping services...")
1624-
_stop_services()
16251598

1626-
for file, namespace, file_exists in cfg_file_dict.values():
1627-
if not file_exists:
1599+
# Check the file exists before proceeding.
1600+
if not os.path.exists(file):
16281601
click.echo("The config file {} doesn't exist".format(file))
16291602
continue
16301603

tests/config_reload_input/config_db_invalid.json

-62
This file was deleted.

tests/config_test.py

+2-113
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import traceback
55
import json
66
import jsonpatch
7-
import shutil
87
import sys
98
import unittest
109
import ipaddress
@@ -89,12 +88,6 @@
8988
Reloading Monit configuration ...
9089
"""
9190

92-
RELOAD_CONFIG_DB_OUTPUT_INVALID_MSG = """\
93-
Bad format: json file"""
94-
95-
RELOAD_CONFIG_DB_OUTPUT_INVALID_ERROR = """\
96-
Expecting ',' delimiter: line 12 column 5 (char 321)"""
97-
9891
RELOAD_YANG_CFG_OUTPUT = """\
9992
Stopping SONiC target ...
10093
Running command: /usr/local/bin/sonic-cfggen -Y /tmp/config.json --write-to-db
@@ -111,15 +104,6 @@
111104
Reloading Monit configuration ...
112105
"""
113106

114-
RELOAD_MASIC_CONFIG_DB_OUTPUT_FILE_NOT_EXIST = """\
115-
Stopping SONiC target ...
116-
Running command: /usr/local/bin/sonic-cfggen -j /tmp/config.json --write-to-db
117-
The config file non_exist.json doesn't exist
118-
Running command: /usr/local/bin/sonic-cfggen -j /tmp/config.json -n asic1 --write-to-db
119-
Restarting SONiC target ...
120-
Reloading Monit configuration ...
121-
"""
122-
123107
reload_config_with_sys_info_command_output="""\
124108
Running command: /usr/local/bin/sonic-cfggen -H -k Seastone-DX010-25-50 --write-to-db"""
125109

@@ -211,7 +195,6 @@ def mock_run_command_side_effect_gnmi(*args, **kwargs):
211195

212196
class TestConfigReload(object):
213197
dummy_cfg_file = os.path.join(os.sep, "tmp", "config.json")
214-
dummy_cfg_file_contents = os.path.join(mock_db_path, "config_db.json")
215198

216199
@classmethod
217200
def setup_class(cls):
@@ -223,8 +206,7 @@ def setup_class(cls):
223206

224207
import config.main
225208
importlib.reload(config.main)
226-
shutil.copyfile(cls.dummy_cfg_file_contents, cls.dummy_cfg_file)
227-
open(cls.dummy_cfg_file, 'r').close()
209+
open(cls.dummy_cfg_file, 'w').close()
228210

229211
def test_config_reload(self, get_cmd_module, setup_single_broadcom_asic):
230212
with mock.patch("utilities_common.cli.run_command", mock.MagicMock(side_effect=mock_run_command_side_effect)) as mock_run_command:
@@ -497,17 +479,14 @@ def teardown_class(cls):
497479

498480
class TestReloadConfig(object):
499481
dummy_cfg_file = os.path.join(os.sep, "tmp", "config.json")
500-
dummy_cfg_file_contents = os.path.join(mock_db_path, "config_db.json")
501-
dummy_cfg_file_invalid = os.path.join(mock_db_path, "config_db_invalid.json")
502482

503483
@classmethod
504484
def setup_class(cls):
505485
os.environ['UTILITIES_UNIT_TESTING'] = "1"
506486
print("SETUP")
507487
import config.main
508488
importlib.reload(config.main)
509-
shutil.copyfile(cls.dummy_cfg_file_contents, cls.dummy_cfg_file)
510-
open(cls.dummy_cfg_file, 'r').close()
489+
open(cls.dummy_cfg_file, 'w').close()
511490

512491
def test_reload_config(self, get_cmd_module, setup_single_broadcom_asic):
513492
with mock.patch(
@@ -528,27 +507,6 @@ def test_reload_config(self, get_cmd_module, setup_single_broadcom_asic):
528507
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) \
529508
== RELOAD_CONFIG_DB_OUTPUT
530509

531-
def test_reload_config_invalid_config_file(self, get_cmd_module, setup_single_broadcom_asic):
532-
with mock.patch(
533-
"utilities_common.cli.run_command",
534-
mock.MagicMock(side_effect=mock_run_command_side_effect)
535-
) as mock_run_command:
536-
(config, show) = get_cmd_module
537-
runner = CliRunner()
538-
539-
result = runner.invoke(
540-
config.config.commands["reload"],
541-
[self.dummy_cfg_file_invalid, '-y', '-f'])
542-
543-
print(result.exit_code)
544-
print(result.output)
545-
traceback.print_tb(result.exc_info[2])
546-
assert result.exit_code == 1
547-
548-
output = "\n".join([l.rstrip() for l in result.output.split('\n')])
549-
assert RELOAD_CONFIG_DB_OUTPUT_INVALID_MSG in output
550-
assert RELOAD_CONFIG_DB_OUTPUT_INVALID_ERROR in output
551-
552510
def test_config_reload_disabled_service(self, get_cmd_module, setup_single_broadcom_asic):
553511
with mock.patch(
554512
"utilities_common.cli.run_command",
@@ -591,54 +549,6 @@ def test_reload_config_masic(self, get_cmd_module, setup_multi_broadcom_masic):
591549
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) \
592550
== RELOAD_MASIC_CONFIG_DB_OUTPUT
593551

594-
def test_reload_config_masic_invalid(self, get_cmd_module, setup_multi_broadcom_masic):
595-
with mock.patch(
596-
"utilities_common.cli.run_command",
597-
mock.MagicMock(side_effect=mock_run_command_side_effect)
598-
) as mock_run_command:
599-
(config, show) = get_cmd_module
600-
runner = CliRunner()
601-
# 3 config files: 1 for host and 2 for asic
602-
cfg_files = "{},{},{}".format(
603-
self.dummy_cfg_file,
604-
self.dummy_cfg_file_invalid,
605-
self.dummy_cfg_file)
606-
result = runner.invoke(
607-
config.config.commands["reload"],
608-
[cfg_files, '-y', '-f'])
609-
610-
print(result.exit_code)
611-
print(result.output)
612-
traceback.print_tb(result.exc_info[2])
613-
assert result.exit_code == 1
614-
615-
output = "\n".join([l.rstrip() for l in result.output.split('\n')])
616-
assert RELOAD_CONFIG_DB_OUTPUT_INVALID_MSG in output
617-
assert RELOAD_CONFIG_DB_OUTPUT_INVALID_ERROR in output
618-
619-
def test_reload_config_masic_non_exist_file(self, get_cmd_module, setup_multi_broadcom_masic):
620-
with mock.patch(
621-
"utilities_common.cli.run_command",
622-
mock.MagicMock(side_effect=mock_run_command_side_effect)
623-
) as mock_run_command:
624-
(config, show) = get_cmd_module
625-
runner = CliRunner()
626-
# 3 config files: 1 for host and 2 for asic
627-
cfg_files = "{},{},{}".format(
628-
self.dummy_cfg_file,
629-
"non_exist.json",
630-
self.dummy_cfg_file)
631-
result = runner.invoke(
632-
config.config.commands["reload"],
633-
[cfg_files, '-y', '-f'])
634-
635-
print(result.exit_code)
636-
print(result.output)
637-
traceback.print_tb(result.exc_info[2])
638-
assert result.exit_code == 0
639-
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) \
640-
== RELOAD_MASIC_CONFIG_DB_OUTPUT_FILE_NOT_EXIST
641-
642552
def test_reload_yang_config(self, get_cmd_module,
643553
setup_single_broadcom_asic):
644554
with mock.patch(
@@ -658,27 +568,6 @@ def test_reload_yang_config(self, get_cmd_module,
658568
assert "\n".join([l.rstrip() for l in result.output.split('\n')]) \
659569
== RELOAD_YANG_CFG_OUTPUT
660570

661-
def test_reload_yang_config_invalid(self, get_cmd_module,
662-
setup_single_broadcom_asic):
663-
with mock.patch(
664-
"utilities_common.cli.run_command",
665-
mock.MagicMock(side_effect=mock_run_command_side_effect)
666-
) as mock_run_command:
667-
(config, show) = get_cmd_module
668-
runner = CliRunner()
669-
670-
result = runner.invoke(config.config.commands["reload"],
671-
[self.dummy_cfg_file_invalid, '-y', '-f', '-t', 'config_yang'])
672-
673-
print(result.exit_code)
674-
print(result.output)
675-
traceback.print_tb(result.exc_info[2])
676-
assert result.exit_code == 1
677-
678-
output = "\n".join([l.rstrip() for l in result.output.split('\n')])
679-
assert RELOAD_CONFIG_DB_OUTPUT_INVALID_MSG in output
680-
assert RELOAD_CONFIG_DB_OUTPUT_INVALID_ERROR in output
681-
682571
@classmethod
683572
def teardown_class(cls):
684573
os.environ['UTILITIES_UNIT_TESTING'] = "0"

0 commit comments

Comments
 (0)