4
4
import os
5
5
6
6
import tests .common_utils as utils
7
+ from sonic_py_common .general import getstatusoutput_noshell
7
8
8
9
9
10
#TODO: Remove this fixuture once SONiC moves to python3.x
@@ -21,20 +22,18 @@ class TestCfgGen(object):
21
22
@pytest .fixture (autouse = True )
22
23
def setup_teardown (self ):
23
24
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' )]
26
27
self .sample_yang_file = os .path .join (self .test_dir ,
27
28
'test_yang_data.json' )
28
29
29
30
def run_script (self , arg , check_stderr = False ):
30
- print ('\n Running sonic-cfggen ' + arg )
31
+ print ('\n Running sonic-cfggen ' , arg )
31
32
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 )
35
35
else :
36
- output = subprocess .check_output (self .script_file + ' ' + arg ,
37
- shell = True )
36
+ output = subprocess .check_output (self .script_file + arg )
38
37
39
38
if utils .PY3x :
40
39
output = output .decode ()
@@ -48,32 +47,31 @@ def run_script(self, arg, check_stderr=False):
48
47
return output
49
48
50
49
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
54
52
55
53
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
57
55
return self .run_script (arg = args , check_stderr = check_stderr )
58
56
59
57
def test_print_data (self ):
60
- arg = "--print-data"
58
+ arg = [ "--print-data" ]
61
59
output = self .run_script_with_yang_arg (arg )
62
60
assert len (output .strip ()) > 0
63
61
64
62
65
63
def test_jinja_expression (self , expected_router_type = 'LeafRouter' ):
66
- arg = " -v \ " DEVICE_METADATA[\' localhost\' ][\' type\' ]\" "
64
+ arg = [ "-v" , "DEVICE_METADATA[\' localhost\' ][\' type\' ]" ]
67
65
output = self .run_script_with_yang_arg (arg )
68
66
assert output .strip () == expected_router_type
69
67
70
68
def test_hwsku (self ):
71
- arg = "-v \ " DEVICE_METADATA[\' localhost\' ][\' hwsku\' ]\" "
69
+ arg = [ "-v" , "DEVICE_METADATA[\' localhost\' ][\' hwsku\' ]" ]
72
70
output = self .run_script_with_yang_arg (arg )
73
71
assert output .strip () == "Force10-S6000"
74
72
75
73
def test_device_metadata (self ):
76
- arg = "--var-json \ " DEVICE_METADATA\" "
74
+ arg = [ "--var-json" , "DEVICE_METADATA" ]
77
75
output = json .loads (self .run_script_with_yang_arg (arg ))
78
76
assert (output ['localhost' ] == {\
79
77
'bgp_asn' : '65100' ,
@@ -87,7 +85,7 @@ def test_device_metadata(self):
87
85
88
86
89
87
def test_port_table (self ):
90
- arg = "--var-json \ " PORT\" "
88
+ arg = [ "--var-json" , "PORT" ]
91
89
output = json .loads (self .run_script_with_yang_arg (arg ))
92
90
assert (output == \
93
91
{'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):
101
99
})
102
100
103
101
def test_portchannel_table (self ):
104
- arg = "--var-json \ " PORTCHANNEL\" "
102
+ arg = [ "--var-json" , "PORTCHANNEL" ]
105
103
output = json .loads (self .run_script_with_yang_arg (arg ))
106
104
assert (output == \
107
105
{'PortChannel1001' : {'admin_status' : 'up' ,
@@ -116,7 +114,7 @@ def test_portchannel_table(self):
116
114
'mtu' : '9100' }})
117
115
118
116
def test_portchannel_member_table (self ):
119
- arg = "--var-json \ " PORTCHANNEL_MEMBER\" "
117
+ arg = [ "--var-json" , "PORTCHANNEL_MEMBER" ]
120
118
output = json .loads (self .run_script_with_yang_arg (arg ))
121
119
assert (output == \
122
120
{ "PortChannel1001|Ethernet0" : {},
@@ -126,7 +124,7 @@ def test_portchannel_member_table(self):
126
124
})
127
125
128
126
def test_interface_table (self ):
129
- arg = "--var-json \ " INTERFACE\" "
127
+ arg = [ "--var-json" , "INTERFACE" ]
130
128
output = json .loads (self .run_script_with_yang_arg (arg ))
131
129
assert (output == {\
132
130
"Ethernet8" : {},
@@ -150,15 +148,15 @@ def test_interface_table(self):
150
148
})
151
149
152
150
def test_portchannel_interface_table (self ):
153
- arg = "--var-json \ " PORTCHANNEL_INTERFACE\" "
151
+ arg = [ "--var-json" , "PORTCHANNEL_INTERFACE" ]
154
152
output = json .loads (self .run_script_with_yang_arg (arg ))
155
153
assert (output == {\
156
154
"PortChannel1001|10.0.0.1/31" : {},
157
155
"PortChannel1002|10.0.0.60/31" : {}
158
156
})
159
157
160
158
def test_loopback_table (self ):
161
- arg = "--var-json \ " LOOPBACK_INTERFACE\" "
159
+ arg = [ "--var-json" , "LOOPBACK_INTERFACE" ]
162
160
output = json .loads (self .run_script_with_yang_arg (arg ))
163
161
assert (output == {\
164
162
"Loopback0" : {},
@@ -173,7 +171,7 @@ def test_loopback_table(self):
173
171
})
174
172
175
173
def test_acl_table (self ):
176
- arg = "--var-json \ " ACL_TABLE\" "
174
+ arg = [ "--var-json" , "ACL_TABLE" ]
177
175
output = json .loads (self .run_script_with_yang_arg (arg ))
178
176
assert (output == {\
179
177
'DATAACL' : {'policy_desc' : 'DATAACL' , 'ports' : ['PortChannel1001' ,'PortChannel1002' ], 'stage' : 'ingress' , 'type' : 'L3' },
@@ -183,7 +181,7 @@ def test_acl_table(self):
183
181
'SSH_ONLY' : {'policy_desc' : 'SSH_ONLY' , 'services' : ['SSH' ], 'stage' : 'ingress' , 'type' : 'CTRLPLANE' }})
184
182
185
183
def test_acl_rule (self ):
186
- arg = "--var-json \ " ACL_RULE\" "
184
+ arg = [ "--var-json" , "ACL_RULE" ]
187
185
output = json .loads (self .run_script_with_yang_arg (arg ))
188
186
assert (output == {\
189
187
"DATAACL|Rule1" : {
@@ -201,7 +199,7 @@ def test_acl_rule(self):
201
199
})
202
200
203
201
def test_vlan_table (self ):
204
- arg = "--var-json \ " VLAN\" "
202
+ arg = [ "--var-json" , "VLAN" ]
205
203
output = json .loads (self .run_script_with_yang_arg (arg ))
206
204
assert (output == {\
207
205
"Vlan100" : {
@@ -218,7 +216,7 @@ def test_vlan_table(self):
218
216
})
219
217
220
218
def test_vlan_interface (self ):
221
- arg = "--var-json \ " VLAN_INTERFACE\" "
219
+ arg = [ "--var-json" , "VLAN_INTERFACE" ]
222
220
output = json .loads (self .run_script_with_yang_arg (arg ))
223
221
assert (output == {\
224
222
"Vlan100" : {},
@@ -233,7 +231,7 @@ def test_vlan_interface(self):
233
231
})
234
232
235
233
def test_vlan_member (self ):
236
- arg = "--var-json \ " VLAN_MEMBER\" "
234
+ arg = [ "--var-json" , "VLAN_MEMBER" ]
237
235
output = json .loads (self .run_script_with_yang_arg (arg ))
238
236
assert (output == {\
239
237
"Vlan100|Ethernet24" : {
@@ -245,7 +243,7 @@ def test_vlan_member(self):
245
243
})
246
244
247
245
def test_vlan_crm (self ):
248
- arg = "--var-json \ " CRM\" "
246
+ arg = [ "--var-json" , "CRM" ]
249
247
output = json .loads (self .run_script_with_yang_arg (arg ))
250
248
assert (output == {\
251
249
"Config" : {
0 commit comments