-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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-config-engine] Replace os.system, replace yaml.load, remove subprocess with shell=True #12533
[sonic-config-engine] Replace os.system, replace yaml.load, remove subprocess with shell=True #12533
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,7 +5,6 @@ | |||||
import sys | ||||||
import subprocess | ||||||
import argparse | ||||||
import shlex | ||||||
|
||||||
PY3x = sys.version_info >= (3, 0) | ||||||
PYvX_DIR = "py3" if PY3x else "py2" | ||||||
|
@@ -47,7 +46,7 @@ def __init__(self, path=YANG_MODELS_DIR): | |||||
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') | ||||||
self.script_file = [PYTHON_INTERPRETTER] + [os.path.join(self.test_dir, '..', 'sonic-cfggen')] | ||||||
|
||||||
def validate(self, argument): | ||||||
""" | ||||||
|
@@ -62,22 +61,29 @@ def validate(self, argument): | |||||
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) | ||||||
parser.add_argument("-j", "--json", help="additional json file input, used with -p, -S and -m or -k", nargs='?', const=None) | ||||||
args, unknown = parser.parse_known_args(shlex.split(argument)) | ||||||
parser.add_argument("-o", "--output-file", help="Output file", nargs='?', const=None) | ||||||
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. 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. Because there're some commands that have output redirection.
Changed command
Implementation of new behavior: https://github.com/sonic-net/sonic-buildimage/blob/ce3e0759543cd00a5a515b31ffb3e87de48c2e76/src/sonic-config-engine/tests/test_j2files.py#L46-#L59 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. I prefer following subprocess functions, adding |
||||||
args, unknown = parser.parse_known_args(argument) | ||||||
print(args) | ||||||
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. |
||||||
|
||||||
print('\n Validating yang schema') | ||||||
cmd = self.script_file + ' -m ' + args.minigraph | ||||||
cmd = self.script_file + ['-m', args.minigraph] | ||||||
if args.hwsku is not None: | ||||||
cmd += ' -k ' + args.hwsku | ||||||
cmd += ['-k', args.hwsku] | ||||||
if args.hwsku_config is not None: | ||||||
cmd += ' -S ' + args.hwsku_config | ||||||
cmd += ['-S', args.hwsku_config] | ||||||
if args.port_config is not None: | ||||||
cmd += ' -p ' + args.port_config | ||||||
cmd += ['-p', args.port_config] | ||||||
if args.namespace is not None: | ||||||
cmd += ' -n ' + args.namespace | ||||||
cmd += ['-n', args.namespace] | ||||||
if args.json is not None: | ||||||
cmd += ' -j ' + args.json | ||||||
cmd += ' --print-data' | ||||||
output = subprocess.check_output(cmd, shell=True).decode() | ||||||
cmd += ['-j', args.json] | ||||||
cmd += ['--print-data'] | ||||||
if not args.output_file: | ||||||
output = subprocess.check_output(cmd).decode() | ||||||
else: | ||||||
output = subprocess.check_output(cmd).decode() | ||||||
with open(args.output_file, 'w') as f: | ||||||
f.write(output) | ||||||
try: | ||||||
self.yang_parser.loadData(configdbJson=json.loads(output)) | ||||||
self.yang_parser.validate_data_tree() | ||||||
|
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import os | ||
import subprocess | ||
import sys | ||
|
||
import ast | ||
import tests.common_utils as utils | ||
|
||
from unittest import TestCase | ||
|
@@ -21,17 +21,17 @@ class TestCfgGenPlatformJson(TestCase): | |
|
||
def setUp(self): | ||
self.test_dir = os.path.dirname(os.path.realpath(__file__)) | ||
self.script_file = utils.PYTHON_INTERPRETTER + ' ' + os.path.join(self.test_dir, '..', 'sonic-cfggen') | ||
self.script_file = [utils.PYTHON_INTERPRETTER] + [os.path.join(self.test_dir, '..', 'sonic-cfggen')] | ||
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. |
||
self.platform_sample_graph = os.path.join(self.test_dir, 'platform-sample-graph.xml') | ||
self.platform_json = os.path.join(self.test_dir, 'sample_platform.json') | ||
self.hwsku_json = os.path.join(self.test_dir, 'sample_hwsku.json') | ||
|
||
def run_script(self, argument, check_stderr=False): | ||
print('\n Running sonic-cfggen ' + argument) | ||
print('\n Running sonic-cfggen ', argument) | ||
if check_stderr: | ||
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True) | ||
output = subprocess.check_output(self.script_file + argument, stderr=subprocess.STDOUT) | ||
else: | ||
output = subprocess.check_output(self.script_file + ' ' + argument, shell=True) | ||
output = subprocess.check_output(self.script_file + argument) | ||
|
||
if utils.PY3x: | ||
output = output.decode() | ||
|
@@ -44,18 +44,18 @@ def run_script(self, argument, check_stderr=False): | |
return output | ||
|
||
def test_dummy_run(self): | ||
argument = '' | ||
argument = [] | ||
output = self.run_script(argument) | ||
self.assertEqual(output, '') | ||
|
||
def test_print_data(self): | ||
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" --print-data' | ||
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '--print-data'] | ||
output = self.run_script(argument) | ||
self.assertTrue(len(output.strip()) > 0) | ||
|
||
# Check whether all interfaces present or not as per platform.json | ||
def test_platform_json_interfaces_keys(self): | ||
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT.keys()|list"' | ||
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT.keys()|list"] | ||
output = self.run_script(argument) | ||
self.maxDiff = None | ||
|
||
|
@@ -71,24 +71,24 @@ def test_platform_json_interfaces_keys(self): | |
'Ethernet139', 'Ethernet140', 'Ethernet141', 'Ethernet142', 'Ethernet144' | ||
] | ||
|
||
self.assertEqual(sorted(eval(output.strip())), sorted(expected)) | ||
self.assertEqual(sorted(ast.literal_eval(output.strip())), sorted(expected)) | ||
|
||
# Check specific Interface with it's proper configuration as per platform.json | ||
def test_platform_json_specific_ethernet_interfaces(self): | ||
|
||
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT[\'Ethernet8\']"' | ||
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT[\'Ethernet8\']"] | ||
output = self.run_script(argument) | ||
self.maxDiff = None | ||
expected = "{'index': '3', 'lanes': '8', 'description': 'Eth3/1', 'mtu': '9100', 'alias': 'Eth3/1', 'pfc_asym': 'off', 'speed': '25000', 'tpid': '0x8100'}" | ||
self.assertEqual(utils.to_dict(output.strip()), utils.to_dict(expected)) | ||
|
||
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT[\'Ethernet112\']"' | ||
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT[\'Ethernet112\']"] | ||
output = self.run_script(argument) | ||
self.maxDiff = None | ||
expected = "{'index': '29', 'lanes': '112', 'description': 'Eth29/1', 'mtu': '9100', 'alias': 'Eth29/1', 'pfc_asym': 'off', 'speed': '25000', 'tpid': '0x8100'}" | ||
self.assertEqual(utils.to_dict(output.strip()), utils.to_dict(expected)) | ||
|
||
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT[\'Ethernet4\']"' | ||
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT[\'Ethernet4\']"] | ||
output = self.run_script(argument) | ||
self.maxDiff = None | ||
expected = "{'index': '2', 'lanes': '4,5', 'description': 'Eth2/1', 'admin_status': 'up', 'mtu': '9100', 'alias': 'Eth2/1', 'pfc_asym': 'off', 'speed': '50000', 'tpid': '0x8100'}" | ||
|
@@ -97,7 +97,7 @@ def test_platform_json_specific_ethernet_interfaces(self): | |
|
||
# Check all Interface with it's proper configuration as per platform.json | ||
def test_platform_json_all_ethernet_interfaces(self): | ||
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT"' | ||
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT"] | ||
output = self.run_script(argument) | ||
self.maxDiff = None | ||
|
||
|
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.
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.
All are fixed.