From 65fa874ec561da349f4c69c9f359b598f78367cd Mon Sep 17 00:00:00 2001 From: Sangita Maity Date: Thu, 4 Jun 2020 16:11:15 -0700 Subject: [PATCH] [test] Adding platform.json configuration file unit test (#3911) - What I did In order to allow the SONiC community to check in platform capability file i.e. platform.json file directly under device folder. We need to add this test to make sure the contents of the this file is compliant with platform capability design specified in DPB HLD doc - How I did it Added platformJson_checker.py file in Test folder. Signed-off-by: Sangita Maity --- src/sonic-device-data/src/Makefile | 3 + src/sonic-device-data/tests/media_checker | 2 +- .../tests/platform_json_checker | 99 +++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100755 src/sonic-device-data/tests/platform_json_checker diff --git a/src/sonic-device-data/src/Makefile b/src/sonic-device-data/src/Makefile index aca0cbea2a..10525fdeb2 100644 --- a/src/sonic-device-data/src/Makefile +++ b/src/sonic-device-data/src/Makefile @@ -11,4 +11,7 @@ test: for f in $$(find ../../../device -name media_settings.json); do ./media_checker $$f done + for f in $$(find ../../../device -name platform.json); do + ./platform_json_checker $$f + done popd diff --git a/src/sonic-device-data/tests/media_checker b/src/sonic-device-data/tests/media_checker index 6b4139585a..132756cb4b 100755 --- a/src/sonic-device-data/tests/media_checker +++ b/src/sonic-device-data/tests/media_checker @@ -138,7 +138,7 @@ def main(argv): # Load target file if len(argv) == 0: - files = glob.glob('*.json') + files = glob.glob('*media_settings.json') else: files = argv diff --git a/src/sonic-device-data/tests/platform_json_checker b/src/sonic-device-data/tests/platform_json_checker new file mode 100755 index 0000000000..c0257a27e8 --- /dev/null +++ b/src/sonic-device-data/tests/platform_json_checker @@ -0,0 +1,99 @@ +#!/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") + +# TODO: need to remove basestring once migrate to Python 3 and just change to str +try: + basestring +except NameError: + basestring = str + +# 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] + " " + 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 + # TODO: need to remove basestring once migrate to Python 3 and just change to str + 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:])