Skip to content

Commit ecb9136

Browse files
authored
Add CLI to configure YANG config validation (sonic-net#2147)
**- What I did** Add CLI to configure YANG config validation mode `config yang_config_validation <enable|disable>` **- How I did it** Add a CLI script that writes the configuration of YANG config validation enable/disable into CONFIG_DB
1 parent e9ab523 commit ecb9136

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

config/main.py

+15
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,21 @@ def synchronous_mode(sync_mode):
19231923
else:
19241924
raise click.BadParameter("Error: Invalid argument %s, expect either enable or disable" % sync_mode)
19251925

1926+
#
1927+
# 'yang_config_validation' command ('config yang_config_validation ...')
1928+
#
1929+
@config.command('yang_config_validation')
1930+
@click.argument('yang_config_validation', metavar='<enable|disable>', required=True)
1931+
def yang_config_validation(yang_config_validation):
1932+
# Enable or disable YANG validation on updates to ConfigDB
1933+
if yang_config_validation == 'enable' or yang_config_validation == 'disable':
1934+
config_db = ConfigDBConnector()
1935+
config_db.connect()
1936+
config_db.mod_entry('DEVICE_METADATA', 'localhost', {"yang_config_validation": yang_config_validation})
1937+
click.echo("""Wrote %s yang config validation into CONFIG_DB""" % yang_config_validation)
1938+
else:
1939+
raise click.BadParameter("Error: Invalid argument %s, expect either enable or disable" % yang_config_validation)
1940+
19261941
#
19271942
# 'portchannel' group ('config portchannel ...')
19281943
#

tests/yang_config_validation_test.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from click.testing import CliRunner
2+
import config.main as config
3+
4+
class TestYangConfigValidation(object):
5+
@classmethod
6+
def setup_class(cls):
7+
print("SETUP")
8+
9+
def __check_result(self, result_msg, mode):
10+
if mode == "enable" or mode == "disable":
11+
expected_msg = """Wrote %s yang config validation into CONFIG_DB""" % mode
12+
else:
13+
expected_msg = "Error: Invalid argument %s, expect either enable or disable" % mode
14+
15+
return expected_msg in result_msg
16+
17+
def test_yang_config_validation(self):
18+
runner = CliRunner()
19+
20+
result = runner.invoke(config.config.commands["yang_config_validation"], ["enable"])
21+
print(result.output)
22+
assert result.exit_code == 0
23+
assert self.__check_result(result.output, "enable")
24+
25+
result = runner.invoke(config.config.commands["yang_config_validation"], ["disable"])
26+
print(result.output)
27+
assert result.exit_code == 0
28+
assert self.__check_result(result.output, "disable")
29+
30+
result = runner.invoke(config.config.commands["yang_config_validation"], ["invalid-input"])
31+
print(result.output)
32+
assert result.exit_code != 0
33+
assert self.__check_result(result.output, "invalid-input")

0 commit comments

Comments
 (0)