Skip to content

Commit

Permalink
Add CLI to configure YANG config validation (sonic-net#2147)
Browse files Browse the repository at this point in the history
**- 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
  • Loading branch information
isabelmsft authored and preetham-singh committed Nov 18, 2022
1 parent 30f1e13 commit 87cee04
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
15 changes: 15 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,21 @@ def synchronous_mode(sync_mode):
else:
raise click.BadParameter("Error: Invalid argument %s, expect either enable or disable" % sync_mode)

#
# 'yang_config_validation' command ('config yang_config_validation ...')
#
@config.command('yang_config_validation')
@click.argument('yang_config_validation', metavar='<enable|disable>', required=True)
def yang_config_validation(yang_config_validation):
# Enable or disable YANG validation on updates to ConfigDB
if yang_config_validation == 'enable' or yang_config_validation == 'disable':
config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry('DEVICE_METADATA', 'localhost', {"yang_config_validation": yang_config_validation})
click.echo("""Wrote %s yang config validation into CONFIG_DB""" % yang_config_validation)
else:
raise click.BadParameter("Error: Invalid argument %s, expect either enable or disable" % yang_config_validation)

#
# 'portchannel' group ('config portchannel ...')
#
Expand Down
33 changes: 33 additions & 0 deletions tests/yang_config_validation_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from click.testing import CliRunner
import config.main as config

class TestYangConfigValidation(object):
@classmethod
def setup_class(cls):
print("SETUP")

def __check_result(self, result_msg, mode):
if mode == "enable" or mode == "disable":
expected_msg = """Wrote %s yang config validation into CONFIG_DB""" % mode
else:
expected_msg = "Error: Invalid argument %s, expect either enable or disable" % mode

return expected_msg in result_msg

def test_yang_config_validation(self):
runner = CliRunner()

result = runner.invoke(config.config.commands["yang_config_validation"], ["enable"])
print(result.output)
assert result.exit_code == 0
assert self.__check_result(result.output, "enable")

result = runner.invoke(config.config.commands["yang_config_validation"], ["disable"])
print(result.output)
assert result.exit_code == 0
assert self.__check_result(result.output, "disable")

result = runner.invoke(config.config.commands["yang_config_validation"], ["invalid-input"])
print(result.output)
assert result.exit_code != 0
assert self.__check_result(result.output, "invalid-input")

0 comments on commit 87cee04

Please sign in to comment.