-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[sonic-cfggen]: Update UT to run yang validation #9700
Merged
Merged
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5bd2e90
[sonic-cfggen]: Update UT to run yang validation
ganglyu e2056e8
sonic_yang does not work for python2
ganglyu aaa5493
Merge branch 'Azure:master' into config_validate
ganglyu c27192c
Reuse yang validation and use argparser to replace regular expression
ganglyu 5704bc9
Merge branch 'Azure:master' into config_validate
ganglyu 593d244
Fix comment
ganglyu 3f40271
Update import
ganglyu 39442f1
Merge branch 'Azure:master' into config_validate
ganglyu eb97f30
Should not raise exception and return False.
ganglyu 183c814
Add empty line.
ganglyu a8ee320
Merge branch 'Azure:master' into config_validate
ganglyu 242fe35
Merge branch 'Azure:master' into config_validate
ganglyu edd011f
Merge branch 'master' into config_validate
ganglyu 808ed62
Merge branch 'Azure:master' into config_validate
ganglyu b1e615e
Merge branch 'Azure:master' into config_validate
ganglyu 5e2e1af
Remove duplicated import
ganglyu 882ebd4
Merge branch 'Azure:master' into config_validate
ganglyu ae3d4c3
Merge branch 'Azure:master' into config_validate
ganglyu 6d4af4d
check
ganglyu 0daac2e
Merge branch 'Azure:master' into config_validate
ganglyu 068362f
Merge branch 'Azure:master' into config_validate
ganglyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import json | ||
import re | ||
import sys | ||
import os | ||
import subprocess | ||
import argparse | ||
import shlex | ||
|
||
|
||
PY3x = sys.version_info >= (3, 0) | ||
PYvX_DIR = "py3" if PY3x else "py2" | ||
PYTHON_INTERPRETTER = "python3" if PY3x else "python2" | ||
YANG_MODELS_DIR = "/usr/local/yang-models" | ||
|
||
def tuple_to_str(tuplestr): | ||
""" Convert Python tuple '('elem1', 'elem2')' representation into string on the for "elem1|elem2" """ | ||
|
@@ -31,3 +37,46 @@ def liststr_to_dict(liststr): | |
|
||
return list_obj | ||
|
||
class YangWrapper(object): | ||
def __init__(self, path=YANG_MODELS_DIR): | ||
""" | ||
sonic_yang only supports python3 | ||
""" | ||
if PY3x: | ||
import sonic_yang | ||
self.yang_parser = sonic_yang.SonicYang(path) | ||
self.yang_parser.loadYangModel() | ||
self.test_dir = os.path.dirname(os.path.realpath(__file__)) | ||
self.script_file = PYTHON_INTERPRETTER + ' ' + os.path.join(self.test_dir, '..', 'sonic-cfggen') | ||
|
||
def validate(self, argument): | ||
""" | ||
Raise exception when yang validation failed | ||
""" | ||
if PY3x and "-m" in argument: | ||
parser=argparse.ArgumentParser(description="Render configuration file from minigraph data and jinja2 template.") | ||
parser.add_argument("-m", "--minigraph", help="minigraph xml file", nargs='?', const='/etc/sonic/minigraph.xml') | ||
parser.add_argument("-k", "--hwsku", help="HwSKU") | ||
parser.add_argument("-n", "--namespace", help="namespace name", nargs='?', const=None, default=None) | ||
parser.add_argument("-p", "--port-config", help="port config file, used with -m or -k", nargs='?', const=None) | ||
parser.add_argument("-S", "--hwsku-config", help="hwsku config file, used with -p and -m or -k", nargs='?', const=None) | ||
args, unknown = parser.parse_known_args(shlex.split(argument)) | ||
|
||
print('\n Validating yang schema') | ||
cmd = self.script_file + ' -m ' + args.minigraph | ||
if args.hwsku is not None: | ||
cmd += ' -k ' + args.hwsku | ||
if args.hwsku_config is not None: | ||
cmd += ' -S ' + args.hwsku_config | ||
if args.port_config is not None: | ||
cmd += ' -p ' + args.port_config | ||
if args.namespace is not None: | ||
cmd += ' -n ' + args.namespace | ||
cmd += ' --print-data' | ||
output = subprocess.check_output(cmd, shell=True).decode() | ||
self.yang_parser.loadData(configdbJson=json.loads(output)) | ||
try: | ||
self.yang_parser.validate_data_tree() | ||
except sonic_yang.SonicYangException as e: | ||
print("yang data generated from %s is not valid"%(args.minigraph)) | ||
raise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line can throw, place it in the try catch.