Skip to content

Commit 6f0b059

Browse files

12 files changed

+379
-387
lines changed

src/sonic-config-engine/sonic-cfggen

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def main():
351351
if yaml.__version__ >= "5.1":
352352
additional_data = yaml.full_load(stream)
353353
else:
354-
additional_data = yaml.load(stream)
354+
additional_data = yaml.safe_load(stream)
355355
deep_update(data, FormatConverter.to_deserialized(additional_data))
356356

357357
if args.additional_data is not None:

src/sonic-config-engine/tests/common_utils.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import sys
66
import subprocess
77
import argparse
8-
import shlex
98

109
PY3x = sys.version_info >= (3, 0)
1110
PYvX_DIR = "py3" if PY3x else "py2"
@@ -47,7 +46,7 @@ def __init__(self, path=YANG_MODELS_DIR):
4746
self.yang_parser = sonic_yang.SonicYang(path)
4847
self.yang_parser.loadYangModel()
4948
self.test_dir = os.path.dirname(os.path.realpath(__file__))
50-
self.script_file = PYTHON_INTERPRETTER + ' ' + os.path.join(self.test_dir, '..', 'sonic-cfggen')
49+
self.script_file = [PYTHON_INTERPRETTER, os.path.join(self.test_dir, '..', 'sonic-cfggen')]
5150

5251
def validate(self, argument):
5352
"""
@@ -62,22 +61,22 @@ def validate(self, argument):
6261
parser.add_argument("-p", "--port-config", help="port config file, used with -m or -k", nargs='?', const=None)
6362
parser.add_argument("-S", "--hwsku-config", help="hwsku config file, used with -p and -m or -k", nargs='?', const=None)
6463
parser.add_argument("-j", "--json", help="additional json file input, used with -p, -S and -m or -k", nargs='?', const=None)
65-
args, unknown = parser.parse_known_args(shlex.split(argument))
64+
args, unknown = parser.parse_known_args(argument)
6665

6766
print('\n Validating yang schema')
68-
cmd = self.script_file + ' -m ' + args.minigraph
67+
cmd = self.script_file + ['-m', args.minigraph]
6968
if args.hwsku is not None:
70-
cmd += ' -k ' + args.hwsku
69+
cmd += ['-k', args.hwsku]
7170
if args.hwsku_config is not None:
72-
cmd += ' -S ' + args.hwsku_config
71+
cmd += ['-S', args.hwsku_config]
7372
if args.port_config is not None:
74-
cmd += ' -p ' + args.port_config
73+
cmd += ['-p', args.port_config]
7574
if args.namespace is not None:
76-
cmd += ' -n ' + args.namespace
75+
cmd += ['-n', args.namespace]
7776
if args.json is not None:
78-
cmd += ' -j ' + args.json
79-
cmd += ' --print-data'
80-
output = subprocess.check_output(cmd, shell=True).decode()
77+
cmd += ['-j', args.json]
78+
cmd += ['--print-data']
79+
output = subprocess.check_output(cmd).decode()
8180
try:
8281
self.yang_parser.loadData(configdbJson=json.loads(output))
8382
self.yang_parser.validate_data_tree()

src/sonic-config-engine/tests/test_cfggen.py

+100-102
Large diffs are not rendered by default.

src/sonic-config-engine/tests/test_cfggen_from_yang.py

+26-28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55

66
import tests.common_utils as utils
7+
from sonic_py_common.general import getstatusoutput_noshell
78

89

910
#TODO: Remove this fixuture once SONiC moves to python3.x
@@ -21,20 +22,18 @@ class TestCfgGen(object):
2122
@pytest.fixture(autouse=True)
2223
def setup_teardown(self):
2324
self.test_dir = os.path.dirname(os.path.realpath(__file__))
24-
self.script_file = utils.PYTHON_INTERPRETTER + ' ' + os.path.join(
25-
self.test_dir, '..', 'sonic-cfggen')
25+
self.script_file = [utils.PYTHON_INTERPRETTER, os.path.join(
26+
self.test_dir, '..', 'sonic-cfggen')]
2627
self.sample_yang_file = os.path.join(self.test_dir,
2728
'test_yang_data.json')
2829

2930
def run_script(self, arg, check_stderr=False):
30-
print('\n Running sonic-cfggen ' + arg)
31+
print('\n Running sonic-cfggen ', arg)
3132
if check_stderr:
32-
output = subprocess.check_output(self.script_file + ' ' + arg,
33-
stderr=subprocess.STDOUT,
34-
shell=True)
33+
output = subprocess.check_output(self.script_file + arg,
34+
stderr=subprocess.STDOUT)
3535
else:
36-
output = subprocess.check_output(self.script_file + ' ' + arg,
37-
shell=True)
36+
output = subprocess.check_output(self.script_file + arg)
3837

3938
if utils.PY3x:
4039
output = output.decode()
@@ -48,32 +47,31 @@ def run_script(self, arg, check_stderr=False):
4847
return output
4948

5049
def run_diff(self, file1, file2):
51-
return subprocess.check_output('diff -u {} {} || true'.format(
52-
file1, file2),
53-
shell=True)
50+
_, output = getstatusoutput_noshell(['diff', '-u', file1, file2])
51+
return output
5452

5553
def run_script_with_yang_arg(self, arg, check_stderr=False):
56-
args = "-Y {} {}".format(self.sample_yang_file, arg)
54+
args = ["-Y", self.sample_yang_file] + arg
5755
return self.run_script(arg=args, check_stderr=check_stderr)
5856

5957
def test_print_data(self):
60-
arg = "--print-data"
58+
arg = ["--print-data"]
6159
output = self.run_script_with_yang_arg(arg)
6260
assert len(output.strip()) > 0
6361

6462

6563
def test_jinja_expression(self, expected_router_type='LeafRouter'):
66-
arg = " -v \"DEVICE_METADATA[\'localhost\'][\'type\']\" "
64+
arg = ["-v", "DEVICE_METADATA[\'localhost\'][\'type\']"]
6765
output = self.run_script_with_yang_arg(arg)
6866
assert output.strip() == expected_router_type
6967

7068
def test_hwsku(self):
71-
arg = "-v \"DEVICE_METADATA[\'localhost\'][\'hwsku\']\" "
69+
arg = ["-v", "DEVICE_METADATA[\'localhost\'][\'hwsku\']"]
7270
output = self.run_script_with_yang_arg(arg)
7371
assert output.strip() == "Force10-S6000"
7472

7573
def test_device_metadata(self):
76-
arg = "--var-json \"DEVICE_METADATA\" "
74+
arg = ["--var-json", "DEVICE_METADATA"]
7775
output = json.loads(self.run_script_with_yang_arg(arg))
7876
assert (output['localhost'] == {\
7977
'bgp_asn': '65100',
@@ -87,7 +85,7 @@ def test_device_metadata(self):
8785

8886

8987
def test_port_table(self):
90-
arg = "--var-json \"PORT\""
88+
arg = ["--var-json", "PORT"]
9189
output = json.loads(self.run_script_with_yang_arg(arg))
9290
assert(output == \
9391
{'Ethernet0': {'admin_status': 'up', 'alias': 'eth0', 'description': 'Ethernet0', 'fec': 'rs', 'lanes': '65, 66', 'mtu': '9100', 'pfc_asym': 'on', 'speed': '40000'},
@@ -101,7 +99,7 @@ def test_port_table(self):
10199
})
102100

103101
def test_portchannel_table(self):
104-
arg = "--var-json \"PORTCHANNEL\""
102+
arg = ["--var-json", "PORTCHANNEL"]
105103
output = json.loads(self.run_script_with_yang_arg(arg))
106104
assert(output == \
107105
{'PortChannel1001': {'admin_status': 'up',
@@ -116,7 +114,7 @@ def test_portchannel_table(self):
116114
'mtu': '9100'}})
117115

118116
def test_portchannel_member_table(self):
119-
arg = "--var-json \"PORTCHANNEL_MEMBER\""
117+
arg = ["--var-json", "PORTCHANNEL_MEMBER"]
120118
output = json.loads(self.run_script_with_yang_arg(arg))
121119
assert(output ==\
122120
{ "PortChannel1001|Ethernet0": {},
@@ -126,7 +124,7 @@ def test_portchannel_member_table(self):
126124
})
127125

128126
def test_interface_table(self):
129-
arg = "--var-json \"INTERFACE\""
127+
arg = ["--var-json", "INTERFACE"]
130128
output = json.loads(self.run_script_with_yang_arg(arg))
131129
assert(output =={\
132130
"Ethernet8": {},
@@ -150,15 +148,15 @@ def test_interface_table(self):
150148
})
151149

152150
def test_portchannel_interface_table(self):
153-
arg = "--var-json \"PORTCHANNEL_INTERFACE\""
151+
arg = ["--var-json", "PORTCHANNEL_INTERFACE"]
154152
output = json.loads(self.run_script_with_yang_arg(arg))
155153
assert(output =={\
156154
"PortChannel1001|10.0.0.1/31": {},
157155
"PortChannel1002|10.0.0.60/31": {}
158156
})
159157

160158
def test_loopback_table(self):
161-
arg = "--var-json \"LOOPBACK_INTERFACE\""
159+
arg = ["--var-json", "LOOPBACK_INTERFACE"]
162160
output = json.loads(self.run_script_with_yang_arg(arg))
163161
assert(output == {\
164162
"Loopback0": {},
@@ -173,7 +171,7 @@ def test_loopback_table(self):
173171
})
174172

175173
def test_acl_table(self):
176-
arg = "--var-json \"ACL_TABLE\""
174+
arg = ["--var-json", "ACL_TABLE"]
177175
output = json.loads(self.run_script_with_yang_arg(arg))
178176
assert(output == {\
179177
'DATAACL': {'policy_desc': 'DATAACL', 'ports': ['PortChannel1001','PortChannel1002'], 'stage': 'ingress', 'type': 'L3'},
@@ -183,7 +181,7 @@ def test_acl_table(self):
183181
'SSH_ONLY': {'policy_desc': 'SSH_ONLY', 'services': ['SSH'], 'stage': 'ingress', 'type': 'CTRLPLANE'}})
184182

185183
def test_acl_rule(self):
186-
arg = "--var-json \"ACL_RULE\""
184+
arg = ["--var-json", "ACL_RULE"]
187185
output = json.loads(self.run_script_with_yang_arg(arg))
188186
assert(output == {\
189187
"DATAACL|Rule1": {
@@ -201,7 +199,7 @@ def test_acl_rule(self):
201199
})
202200

203201
def test_vlan_table(self):
204-
arg = "--var-json \"VLAN\""
202+
arg = ["--var-json", "VLAN"]
205203
output = json.loads(self.run_script_with_yang_arg(arg))
206204
assert(output == {\
207205
"Vlan100": {
@@ -218,7 +216,7 @@ def test_vlan_table(self):
218216
})
219217

220218
def test_vlan_interface(self):
221-
arg = "--var-json \"VLAN_INTERFACE\""
219+
arg = ["--var-json", "VLAN_INTERFACE"]
222220
output = json.loads(self.run_script_with_yang_arg(arg))
223221
assert(output == {\
224222
"Vlan100": {},
@@ -233,7 +231,7 @@ def test_vlan_interface(self):
233231
})
234232

235233
def test_vlan_member(self):
236-
arg = "--var-json \"VLAN_MEMBER\""
234+
arg = ["--var-json", "VLAN_MEMBER"]
237235
output = json.loads(self.run_script_with_yang_arg(arg))
238236
assert(output == {\
239237
"Vlan100|Ethernet24": {
@@ -245,7 +243,7 @@ def test_vlan_member(self):
245243
})
246244

247245
def test_vlan_crm(self):
248-
arg = "--var-json \"CRM\""
246+
arg = ["--var-json", "CRM"]
249247
output = json.loads(self.run_script_with_yang_arg(arg))
250248
assert(output == {\
251249
"Config": {

src/sonic-config-engine/tests/test_cfggen_pfx_filter.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ class TestPfxFilter(TestCase):
99
def test_comprehensive(self):
1010
# Generate output
1111
data_dir = "tests/data/pfx_filter"
12-
cmd = "{} ./sonic-cfggen -j {}/param_1.json -t {}/tmpl_1.txt.j2 > /tmp/result_1.txt".format(
13-
utils.PYTHON_INTERPRETTER, data_dir, data_dir
14-
)
15-
subprocess.check_output(cmd, shell=True)
12+
output_file = "/tmp/result_1.txt"
13+
cmd = [utils.PYTHON_INTERPRETTER, "./sonic-cfggen", "-j", "{}/param_1.json".format(data_dir), "-t", "{}/tmpl_1.txt.j2".format(data_dir)]
14+
output = subprocess.check_output(cmd, universal_newlines=True)
15+
with open(output_file, 'w') as f:
16+
f.write(output)
1617
# Compare outputs
17-
cmd = "diff -u tests/data/pfx_filter/result_1.txt /tmp/result_1.txt"
18+
cmd = ["diff", "-u", "tests/data/pfx_filter/result_1.txt", "/tmp/result_1.txt"]
1819
try:
19-
res = subprocess.check_output(cmd, shell=True)
20+
res = subprocess.check_output(cmd)
2021
except subprocess.CalledProcessError as e:
2122
assert False, "Wrong output. return code: %d, Diff: %s" % (e.returncode, e.output)

src/sonic-config-engine/tests/test_cfggen_platformJson.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44
import subprocess
55
import sys
6-
6+
import ast
77
import tests.common_utils as utils
88

99
from unittest import TestCase
@@ -21,17 +21,17 @@ class TestCfgGenPlatformJson(TestCase):
2121

2222
def setUp(self):
2323
self.test_dir = os.path.dirname(os.path.realpath(__file__))
24-
self.script_file = utils.PYTHON_INTERPRETTER + ' ' + os.path.join(self.test_dir, '..', 'sonic-cfggen')
24+
self.script_file = [utils.PYTHON_INTERPRETTER, os.path.join(self.test_dir, '..', 'sonic-cfggen')]
2525
self.platform_sample_graph = os.path.join(self.test_dir, 'platform-sample-graph.xml')
2626
self.platform_json = os.path.join(self.test_dir, 'sample_platform.json')
2727
self.hwsku_json = os.path.join(self.test_dir, 'sample_hwsku.json')
2828

2929
def run_script(self, argument, check_stderr=False):
30-
print('\n Running sonic-cfggen ' + argument)
30+
print('\n Running sonic-cfggen ', argument)
3131
if check_stderr:
32-
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
32+
output = subprocess.check_output(self.script_file + argument, stderr=subprocess.STDOUT)
3333
else:
34-
output = subprocess.check_output(self.script_file + ' ' + argument, shell=True)
34+
output = subprocess.check_output(self.script_file + argument)
3535

3636
if utils.PY3x:
3737
output = output.decode()
@@ -44,18 +44,18 @@ def run_script(self, argument, check_stderr=False):
4444
return output
4545

4646
def test_dummy_run(self):
47-
argument = ''
47+
argument = []
4848
output = self.run_script(argument)
4949
self.assertEqual(output, '')
5050

5151
def test_print_data(self):
52-
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" --print-data'
52+
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '--print-data']
5353
output = self.run_script(argument)
5454
self.assertTrue(len(output.strip()) > 0)
5555

5656
# Check whether all interfaces present or not as per platform.json
5757
def test_platform_json_interfaces_keys(self):
58-
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT.keys()|list"'
58+
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT.keys()|list"]
5959
output = self.run_script(argument)
6060
self.maxDiff = None
6161

@@ -71,24 +71,24 @@ def test_platform_json_interfaces_keys(self):
7171
'Ethernet139', 'Ethernet140', 'Ethernet141', 'Ethernet142', 'Ethernet144'
7272
]
7373

74-
self.assertEqual(sorted(eval(output.strip())), sorted(expected))
74+
self.assertEqual(sorted(ast.literal_eval(output.strip())), sorted(expected))
7575

7676
# Check specific Interface with it's proper configuration as per platform.json
7777
def test_platform_json_specific_ethernet_interfaces(self):
7878

79-
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT[\'Ethernet8\']"'
79+
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT[\'Ethernet8\']"]
8080
output = self.run_script(argument)
8181
self.maxDiff = None
8282
expected = "{'index': '3', 'lanes': '8', 'description': 'Eth3/1', 'mtu': '9100', 'alias': 'Eth3/1', 'pfc_asym': 'off', 'speed': '25000', 'tpid': '0x8100'}"
8383
self.assertEqual(utils.to_dict(output.strip()), utils.to_dict(expected))
8484

85-
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT[\'Ethernet112\']"'
85+
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT[\'Ethernet112\']"]
8686
output = self.run_script(argument)
8787
self.maxDiff = None
8888
expected = "{'index': '29', 'lanes': '112', 'description': 'Eth29/1', 'mtu': '9100', 'alias': 'Eth29/1', 'pfc_asym': 'off', 'speed': '25000', 'tpid': '0x8100'}"
8989
self.assertEqual(utils.to_dict(output.strip()), utils.to_dict(expected))
9090

91-
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT[\'Ethernet4\']"'
91+
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT[\'Ethernet4\']"]
9292
output = self.run_script(argument)
9393
self.maxDiff = None
9494
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):
9797

9898
# Check all Interface with it's proper configuration as per platform.json
9999
def test_platform_json_all_ethernet_interfaces(self):
100-
argument = '-m "' + self.platform_sample_graph + '" -p "' + self.platform_json + '" -S "' + self.hwsku_json + '" -v "PORT"'
100+
argument = ['-m', self.platform_sample_graph, '-p', self.platform_json, '-S', self.hwsku_json, '-v', "PORT"]
101101
output = self.run_script(argument)
102102
self.maxDiff = None
103103

0 commit comments

Comments
 (0)