-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[test] Adding platform.json configuration file unit test #3911
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
Merged
jleveque
merged 3 commits into
sonic-net:master
from
samaity:config_checker_platform_msft
Jun 4, 2020
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #!/usr/bin/env python | ||
| try: | ||
| import re | ||
| import sys | ||
| import glob | ||
| import json | ||
| except ImportError as e: | ||
| raise ImportError (str(e) + "- required module not found") | ||
| try: | ||
| basestring | ||
| except NameError: | ||
| basestring = str | ||
samaity marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Global variable | ||
| PORT_ATTRIBUTES = ["index", "lanes", "alias_at_lanes", "breakout_modes"] | ||
| ATTR_LEN = len(PORT_ATTRIBUTES) | ||
| PORT_REG = "Ethernet(\d+)" | ||
| PLATFORM_JSON = '*platform.json' | ||
| INTF_KEY = "interfaces" | ||
|
|
||
| def usage(): | ||
| print "Usage: " + sys.argv[0] + " <platform_json_file>" | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def check_port_attr(port_attr): | ||
| for each_key in port_attr: | ||
| if each_key not in PORT_ATTRIBUTES: | ||
| print "Error: "+ each_key + " is not the correct Port attribute." | ||
| return False | ||
| if not port_attr[each_key]: | ||
| print "Error: "+ each_key + " has no value." | ||
| return False | ||
| if not isinstance(port_attr[each_key], basestring): | ||
| print "Error:value type of "+ each_key + " must be string." | ||
| return False | ||
| return True | ||
|
|
||
| def check_file(platform_json_file): | ||
| try: | ||
| platform_cap_file = open(platform_json_file,"r") | ||
| platform_file_data = platform_cap_file.read() | ||
| port_dict = json.loads(platform_file_data) | ||
|
|
||
| for each_port in port_dict[INTF_KEY]: | ||
|
|
||
| # Validate port at top level | ||
| port_id = re.search(PORT_REG, each_port) | ||
| if port_id is None: | ||
| print "Error: Unknown Interface " + str(each_port) + " at top level" | ||
| return False | ||
|
|
||
| total_attr = len(port_dict[INTF_KEY][each_port].keys()) | ||
| port_attr = port_dict[INTF_KEY][each_port] | ||
|
|
||
| if total_attr != ATTR_LEN: | ||
| missing_attr = ', '.join(set(PORT_ATTRIBUTES).difference(list(port_attr))) | ||
| print "Error: " + missing_attr + " of " + each_port + " is/are missing" | ||
| return False | ||
|
|
||
| #Validate port attributes for each port | ||
| if not check_port_attr(port_attr): | ||
| return False | ||
|
|
||
| except IOError: | ||
| print "Error: Cannot open file " + platform_json_file | ||
| return False | ||
| except ValueError,e: | ||
| print "Error in parsing json file " + platform_json_file + " " | ||
| print str(e) | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def main(argv): | ||
|
|
||
| if len(argv) > 0 and argv[0] == "-h": | ||
| usage() | ||
|
|
||
| # Load target file | ||
| if len(argv) == 0: | ||
| files = glob.glob(PLATFORM_JSON) | ||
| else: | ||
| files = argv | ||
|
|
||
| all_good = True | ||
|
|
||
| for f in files: | ||
| good = check_file(f) | ||
| if good: | ||
| print "File " + f + " passed validity check" | ||
| else: | ||
| print "File " + f + " failed validity check" | ||
|
|
||
| all_good = all_good and good | ||
|
|
||
| if not all_good: | ||
| sys.exit(-1) | ||
|
|
||
| if __name__ == "__main__": | ||
| main(sys.argv[1:]) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.