Skip to content

Commit 661c467

Browse files
authored
Revert "[sonic-config-engine] Replace os.system, replace yaml.load, remove subprocess with shell=True (#12533)" (#12616)
This reverts commit 934871c. Unblocking sync from github to internal
1 parent 61a085e commit 661c467

12 files changed

+384
-377
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.safe_load(stream)
354+
additional_data = yaml.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

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

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

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

6667
print('\n Validating yang schema')
67-
cmd = self.script_file + ['-m', args.minigraph]
68+
cmd = self.script_file + ' -m ' + args.minigraph
6869
if args.hwsku is not None:
69-
cmd += ['-k', args.hwsku]
70+
cmd += ' -k ' + args.hwsku
7071
if args.hwsku_config is not None:
71-
cmd += ['-S', args.hwsku_config]
72+
cmd += ' -S ' + args.hwsku_config
7273
if args.port_config is not None:
73-
cmd += ['-p', args.port_config]
74+
cmd += ' -p ' + args.port_config
7475
if args.namespace is not None:
75-
cmd += ['-n', args.namespace]
76+
cmd += ' -n ' + args.namespace
7677
if args.json is not None:
77-
cmd += ['-j', args.json]
78-
cmd += ['--print-data']
79-
output = subprocess.check_output(cmd).decode()
78+
cmd += ' -j ' + args.json
79+
cmd += ' --print-data'
80+
output = subprocess.check_output(cmd, shell=True).decode()
8081
try:
8182
self.yang_parser.loadData(configdbJson=json.loads(output))
8283
self.yang_parser.validate_data_tree()

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

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

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

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

66
import tests.common_utils as utils
7-
from sonic_py_common.general import getstatusoutput_noshell
87

98

109
#TODO: Remove this fixuture once SONiC moves to python3.x
@@ -22,18 +21,20 @@ class TestCfgGen(object):
2221
@pytest.fixture(autouse=True)
2322
def setup_teardown(self):
2423
self.test_dir = os.path.dirname(os.path.realpath(__file__))
25-
self.script_file = [utils.PYTHON_INTERPRETTER, os.path.join(
26-
self.test_dir, '..', 'sonic-cfggen')]
24+
self.script_file = utils.PYTHON_INTERPRETTER + ' ' + os.path.join(
25+
self.test_dir, '..', 'sonic-cfggen')
2726
self.sample_yang_file = os.path.join(self.test_dir,
2827
'test_yang_data.json')
2928

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

3839
if utils.PY3x:
3940
output = output.decode()
@@ -47,31 +48,32 @@ def run_script(self, arg, check_stderr=False):
4748
return output
4849

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

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

5759
def test_print_data(self):
58-
arg = ["--print-data"]
60+
arg = "--print-data"
5961
output = self.run_script_with_yang_arg(arg)
6062
assert len(output.strip()) > 0
6163

6264

6365
def test_jinja_expression(self, expected_router_type='LeafRouter'):
64-
arg = ["-v", "DEVICE_METADATA[\'localhost\'][\'type\']"]
66+
arg = " -v \"DEVICE_METADATA[\'localhost\'][\'type\']\" "
6567
output = self.run_script_with_yang_arg(arg)
6668
assert output.strip() == expected_router_type
6769

6870
def test_hwsku(self):
69-
arg = ["-v", "DEVICE_METADATA[\'localhost\'][\'hwsku\']"]
71+
arg = "-v \"DEVICE_METADATA[\'localhost\'][\'hwsku\']\" "
7072
output = self.run_script_with_yang_arg(arg)
7173
assert output.strip() == "Force10-S6000"
7274

7375
def test_device_metadata(self):
74-
arg = ["--var-json", "DEVICE_METADATA"]
76+
arg = "--var-json \"DEVICE_METADATA\" "
7577
output = json.loads(self.run_script_with_yang_arg(arg))
7678
assert (output['localhost'] == {\
7779
'bgp_asn': '65100',
@@ -85,7 +87,7 @@ def test_device_metadata(self):
8587

8688

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

101103
def test_portchannel_table(self):
102-
arg = ["--var-json", "PORTCHANNEL"]
104+
arg = "--var-json \"PORTCHANNEL\""
103105
output = json.loads(self.run_script_with_yang_arg(arg))
104106
assert(output == \
105107
{'PortChannel1001': {'admin_status': 'up',
@@ -114,7 +116,7 @@ def test_portchannel_table(self):
114116
'mtu': '9100'}})
115117

116118
def test_portchannel_member_table(self):
117-
arg = ["--var-json", "PORTCHANNEL_MEMBER"]
119+
arg = "--var-json \"PORTCHANNEL_MEMBER\""
118120
output = json.loads(self.run_script_with_yang_arg(arg))
119121
assert(output ==\
120122
{ "PortChannel1001|Ethernet0": {},
@@ -124,7 +126,7 @@ def test_portchannel_member_table(self):
124126
})
125127

126128
def test_interface_table(self):
127-
arg = ["--var-json", "INTERFACE"]
129+
arg = "--var-json \"INTERFACE\""
128130
output = json.loads(self.run_script_with_yang_arg(arg))
129131
assert(output =={\
130132
"Ethernet8": {},
@@ -148,15 +150,15 @@ def test_interface_table(self):
148150
})
149151

150152
def test_portchannel_interface_table(self):
151-
arg = ["--var-json", "PORTCHANNEL_INTERFACE"]
153+
arg = "--var-json \"PORTCHANNEL_INTERFACE\""
152154
output = json.loads(self.run_script_with_yang_arg(arg))
153155
assert(output =={\
154156
"PortChannel1001|10.0.0.1/31": {},
155157
"PortChannel1002|10.0.0.60/31": {}
156158
})
157159

158160
def test_loopback_table(self):
159-
arg = ["--var-json", "LOOPBACK_INTERFACE"]
161+
arg = "--var-json \"LOOPBACK_INTERFACE\""
160162
output = json.loads(self.run_script_with_yang_arg(arg))
161163
assert(output == {\
162164
"Loopback0": {},
@@ -171,7 +173,7 @@ def test_loopback_table(self):
171173
})
172174

173175
def test_acl_table(self):
174-
arg = ["--var-json", "ACL_TABLE"]
176+
arg = "--var-json \"ACL_TABLE\""
175177
output = json.loads(self.run_script_with_yang_arg(arg))
176178
assert(output == {\
177179
'DATAACL': {'policy_desc': 'DATAACL', 'ports': ['PortChannel1001','PortChannel1002'], 'stage': 'ingress', 'type': 'L3'},
@@ -181,7 +183,7 @@ def test_acl_table(self):
181183
'SSH_ONLY': {'policy_desc': 'SSH_ONLY', 'services': ['SSH'], 'stage': 'ingress', 'type': 'CTRLPLANE'}})
182184

183185
def test_acl_rule(self):
184-
arg = ["--var-json", "ACL_RULE"]
186+
arg = "--var-json \"ACL_RULE\""
185187
output = json.loads(self.run_script_with_yang_arg(arg))
186188
assert(output == {\
187189
"DATAACL|Rule1": {
@@ -199,7 +201,7 @@ def test_acl_rule(self):
199201
})
200202

201203
def test_vlan_table(self):
202-
arg = ["--var-json", "VLAN"]
204+
arg = "--var-json \"VLAN\""
203205
output = json.loads(self.run_script_with_yang_arg(arg))
204206
assert(output == {\
205207
"Vlan100": {
@@ -216,7 +218,7 @@ def test_vlan_table(self):
216218
})
217219

218220
def test_vlan_interface(self):
219-
arg = ["--var-json", "VLAN_INTERFACE"]
221+
arg = "--var-json \"VLAN_INTERFACE\""
220222
output = json.loads(self.run_script_with_yang_arg(arg))
221223
assert(output == {\
222224
"Vlan100": {},
@@ -231,7 +233,7 @@ def test_vlan_interface(self):
231233
})
232234

233235
def test_vlan_member(self):
234-
arg = ["--var-json", "VLAN_MEMBER"]
236+
arg = "--var-json \"VLAN_MEMBER\""
235237
output = json.loads(self.run_script_with_yang_arg(arg))
236238
assert(output == {\
237239
"Vlan100|Ethernet24": {
@@ -243,7 +245,7 @@ def test_vlan_member(self):
243245
})
244246

245247
def test_vlan_crm(self):
246-
arg = ["--var-json", "CRM"]
248+
arg = "--var-json \"CRM\""
247249
output = json.loads(self.run_script_with_yang_arg(arg))
248250
assert(output == {\
249251
"Config": {

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ class TestPfxFilter(TestCase):
99
def test_comprehensive(self):
1010
# Generate output
1111
data_dir = "tests/data/pfx_filter"
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)
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)
1716
# Compare outputs
18-
cmd = ["diff", "-u", "tests/data/pfx_filter/result_1.txt", "/tmp/result_1.txt"]
17+
cmd = "diff -u tests/data/pfx_filter/result_1.txt /tmp/result_1.txt"
1918
try:
20-
res = subprocess.check_output(cmd)
19+
res = subprocess.check_output(cmd, shell=True)
2120
except subprocess.CalledProcessError as e:
2221
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-
import ast
6+
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)
32+
output = subprocess.check_output(self.script_file + ' ' + argument, stderr=subprocess.STDOUT, shell=True)
3333
else:
34-
output = subprocess.check_output(self.script_file + argument)
34+
output = subprocess.check_output(self.script_file + ' ' + argument, shell=True)
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(ast.literal_eval(output.strip())), sorted(expected))
74+
self.assertEqual(sorted(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)