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

Add CLI to configure YANG config validation #2147

Merged
merged 5 commits into from
Aug 3, 2022
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
15 changes: 15 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1714,6 +1714,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):
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yang_config_validation

Let's focus on config yang_config_validation in this PR, and move the sample implementation of yang validation to another PR. #Closed

# 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")