1
1
import logging
2
2
import os
3
+ import sys
3
4
import ipaddress
4
5
import time
5
6
import random
45
46
46
47
logger = logging .getLogger (__name__ )
47
48
49
+ if sys .version_info [0 ] >= 3 :
50
+ unicode = str
51
+
52
+
48
53
def pytest_addoption (parser ):
49
54
"""
50
55
Adds options to pytest that are used by the sub-ports tests.
@@ -57,31 +62,39 @@ def pytest_addoption(parser):
57
62
help = "Max numbers of sub-ports for test_max_numbers_of_sub_ports test case" ,
58
63
)
59
64
60
- @pytest .fixture (params = ['port' , 'port_in_lag' ])
65
+
66
+ @pytest .fixture (params = ['port' , 'port_in_lag' ], scope = 'module' )
61
67
def port_type (request ):
62
68
"""Port type to test, could be either port or port-channel."""
63
69
return request .param
64
70
65
- @pytest .fixture
71
+
72
+ @pytest .fixture (scope = 'module' )
66
73
def acl_rule_cleanup (duthost , tbinfo ):
67
- """Cleanup all the existing DATAACL rules"""
74
+ """
75
+ Cleanup all the existing DATAACL rules
76
+ """
68
77
if "t0-backend" in tbinfo ["topo" ]["name" ]:
69
78
duthost .shell ('acl-loader delete' )
70
79
71
80
yield
72
81
73
- @pytest .fixture
82
+
83
+ @pytest .fixture (scope = 'module' )
74
84
def modify_acl_table (duthost , tbinfo , port_type , acl_rule_cleanup ):
75
- """ Remove the DATAACL table prior to the test and recreate it at the end"""
76
- if "t0-backend" in tbinfo ["topo" ]["name" ] and 'lag' in port_type :
77
- duthost .command ('config acl remove table DATAACL' )
85
+ """
86
+ Remove the DATAACL table prior to the test and recreate it at the end
87
+ """
88
+ if "t0-backend" in tbinfo ["topo" ]["name" ] and 'lag' in port_type :
89
+ duthost .command ('config acl remove table DATAACL' )
78
90
79
- yield
91
+ yield
92
+
93
+ if "t0-backend" in tbinfo ["topo" ]["name" ] and 'lag' in port_type :
94
+ bind_acl_table (duthost , tbinfo )
80
95
81
- if "t0-backend" in tbinfo ["topo" ]["name" ] and 'lag' in port_type :
82
- bind_acl_table (duthost , tbinfo )
83
96
84
- @pytest .fixture
97
+ @pytest .fixture ( scope = 'class' )
85
98
def define_sub_ports_configuration (request , duthost , ptfhost , ptfadapter , port_type , tbinfo ):
86
99
"""
87
100
Define configuration of sub-ports for TC run
@@ -118,10 +131,10 @@ def define_sub_ports_configuration(request, duthost, ptfhost, ptfadapter, port_t
118
131
vlan_ranges_dut = range (20 , 60 , 10 )
119
132
vlan_ranges_ptf = range (20 , 60 , 10 )
120
133
121
- if 'invalid' in request .node .name :
134
+ if 'invalid' in request ._pyfuncitem .name :
122
135
vlan_ranges_ptf = range (21 , 41 , 10 )
123
136
124
- if 'max_numbers' in request .node .name :
137
+ if 'max_numbers' in request ._pyfuncitem .name :
125
138
vlan_ranges_dut = range (11 , max_numbers_of_sub_ports + 11 )
126
139
vlan_ranges_ptf = range (11 , max_numbers_of_sub_ports + 11 )
127
140
@@ -142,7 +155,8 @@ def define_sub_ports_configuration(request, duthost, ptfhost, ptfadapter, port_t
142
155
# for normal t0, get_port tries to retrieve test ports from vlan members
143
156
# let's enforce same behavior for t0-backend
144
157
if "t0-backend" in tbinfo ["topo" ]["name" ]:
145
- config_port_indices , ptf_ports = get_port (duthost , ptfhost , interface_num , port_type , exclude_sub_interface_ports = True )
158
+ config_port_indices , ptf_ports = get_port (duthost , ptfhost , interface_num , port_type ,
159
+ exclude_sub_interface_ports = True )
146
160
else :
147
161
config_port_indices , ptf_ports = get_port (duthost , ptfhost , interface_num , port_type )
148
162
@@ -167,7 +181,7 @@ def define_sub_ports_configuration(request, duthost, ptfhost, ptfadapter, port_t
167
181
}
168
182
169
183
170
- @pytest .fixture
184
+ @pytest .fixture ( scope = 'class' )
171
185
def apply_config_on_the_dut (define_sub_ports_configuration , duthost , reload_dut_config , modify_acl_table ):
172
186
"""
173
187
Apply Sub-ports configuration on the DUT and remove after tests
@@ -202,7 +216,7 @@ def apply_config_on_the_dut(define_sub_ports_configuration, duthost, reload_dut_
202
216
yield sub_ports_vars
203
217
204
218
205
- @pytest .fixture
219
+ @pytest .fixture ( scope = 'class' )
206
220
def apply_config_on_the_ptf (define_sub_ports_configuration , ptfhost , reload_ptf_config ):
207
221
"""
208
222
Apply Sub-ports configuration on the PTF and remove after tests
@@ -219,7 +233,8 @@ def apply_config_on_the_ptf(define_sub_ports_configuration, ptfhost, reload_ptf_
219
233
220
234
221
235
@pytest .fixture (params = ['same' , 'different' ])
222
- def apply_route_config (request , tbinfo , duthost , ptfhost , port_type , define_sub_ports_configuration , apply_config_on_the_dut , apply_config_on_the_ptf ):
236
+ def apply_route_config (request , tbinfo , duthost , ptfhost , port_type , define_sub_ports_configuration ,
237
+ apply_config_on_the_dut , apply_config_on_the_ptf ):
223
238
"""
224
239
Apply route configuration on the PTF and remove after tests
225
240
@@ -237,6 +252,7 @@ def apply_route_config(request, tbinfo, duthost, ptfhost, port_type, define_sub_
237
252
sub_ports = define_sub_ports_configuration ['sub_ports' ]
238
253
dut_ports = define_sub_ports_configuration ['dut_ports' ]
239
254
sub_ports_keys = sub_ports .copy ()
255
+ namespaces = {}
240
256
241
257
for port in dut_ports .values ():
242
258
if 'same' in request .param :
@@ -262,8 +278,12 @@ def apply_route_config(request, tbinfo, duthost, ptfhost, port_type, define_sub_
262
278
sub_ports [next_hop_sub_port ]['neighbor_port' ],
263
279
sub_ports [next_hop_sub_port ]['neighbor_ip' ])
264
280
265
- if 'tunneling' not in request .node .name :
266
- add_static_route_to_ptf (ptfhost , src_port_network , sub_ports [next_hop_sub_port ]['ip' ], name_of_namespace )
281
+ namespaces [name_of_namespace ] = (sub_ports [next_hop_sub_port ]['neighbor_port' ],
282
+ sub_ports [next_hop_sub_port ]['neighbor_ip' ])
283
+
284
+ if 'tunneling' not in request ._pyfuncitem .name :
285
+ add_static_route_to_ptf (ptfhost , src_port_network , sub_ports [next_hop_sub_port ]['ip' ],
286
+ name_of_namespace )
267
287
add_static_route_to_ptf (ptfhost , dst_port_network , sub_ports [src_port ]['ip' ])
268
288
269
289
new_sub_ports [src_port ].append ((next_hop_sub_port , name_of_namespace ))
@@ -284,15 +304,17 @@ def apply_route_config(request, tbinfo, duthost, ptfhost, port_type, define_sub_
284
304
sub_port , name_of_namespace = next_hop_sub_port
285
305
dst_port_network = ipaddress .ip_network (unicode (sub_ports [sub_port ]['ip' ]), strict = False )
286
306
287
- if 'tunneling' not in request .node .name :
307
+ if 'tunneling' not in request ._pyfuncitem .name :
288
308
remove_static_route_from_ptf (ptfhost , src_port_network , sub_ports [sub_port ]['ip' ], name_of_namespace )
289
309
remove_static_route_from_ptf (ptfhost , dst_port_network , sub_ports [src_port ]['ip' ])
290
310
291
- remove_namespace (ptfhost , name_of_namespace )
311
+ namespace_member , namespace_member_ip = namespaces [name_of_namespace ]
312
+ remove_namespace (ptfhost , name_of_namespace , namespace_member , namespace_member_ip )
292
313
293
314
294
315
@pytest .fixture (params = ['svi' , 'l3' ])
295
- def apply_route_config_for_port (request , tbinfo , duthost , ptfhost , port_type , define_sub_ports_configuration , apply_config_on_the_dut , apply_config_on_the_ptf ):
316
+ def apply_route_config_for_port (request , tbinfo , duthost , ptfhost , port_type , define_sub_ports_configuration ,
317
+ apply_config_on_the_dut , apply_config_on_the_ptf ):
296
318
"""
297
319
Apply route configuration on the PTF and remove after tests
298
320
@@ -314,13 +336,15 @@ def apply_route_config_for_port(request, tbinfo, duthost, ptfhost, port_type, de
314
336
dut_ports = define_sub_ports_configuration ['dut_ports' ]
315
337
port_type = define_sub_ports_configuration ['port_type' ]
316
338
subnet = define_sub_ports_configuration ['subnet' ]
339
+ namespaces = {}
317
340
318
341
# Get additional port for configuration of SVI port or L3 RIF
319
342
if 'svi' in request .param :
320
343
interface_num = 1
321
344
else :
322
345
interface_num = 2
323
- dut_ports , ptf_ports = get_port (duthost , ptfhost , interface_num , port_type , dut_ports .values (), exclude_sub_interface_ports = True )
346
+ dut_ports , ptf_ports = get_port (duthost , ptfhost , interface_num , port_type , dut_ports .values (),
347
+ exclude_sub_interface_ports = True )
324
348
325
349
# Get additional IP addresses for configuration of RIF on the DUT and PTF
326
350
subnet = ipaddress .ip_network (str (subnet .broadcast_address + 1 ) + u'/24' )
@@ -350,16 +374,16 @@ def apply_route_config_for_port(request, tbinfo, duthost, ptfhost, port_type, de
350
374
add_ip_to_ptf_port (ptfhost , ptf_port , ptf_port_ip )
351
375
352
376
# Get two random sub-ports which are not part of the selected DUT interface
353
- sub_ports_on_port = random .sample ([sub_port for sub_port in sub_ports_keys if dut_port + '.' not in sub_port ], 2 )
377
+ sub_ports_on_port = random .sample ([sub_port for sub_port in sub_ports_keys
378
+ if dut_port + '.' not in sub_port ], 2 )
354
379
355
380
for sub_port in sub_ports_on_port :
356
381
sub_ports_keys .pop (sub_port )
357
382
358
383
port_map [ptf_port ] = {'dut_port' : dut_port ,
359
384
'ip' : ptf_port_ip ,
360
385
'neighbor_ip' : dut_port_ip ,
361
- 'dst_ports' : []
362
- }
386
+ 'dst_ports' : []}
363
387
364
388
# Configure static route between selected sub-ports and selected interfaces on the PTF
365
389
for next_hop_sub_port in sub_ports_on_port :
@@ -372,6 +396,9 @@ def apply_route_config_for_port(request, tbinfo, duthost, ptfhost, port_type, de
372
396
sub_ports [next_hop_sub_port ]['neighbor_port' ],
373
397
sub_ports [next_hop_sub_port ]['neighbor_ip' ])
374
398
399
+ namespaces [name_of_namespace ] = (sub_ports [next_hop_sub_port ]['neighbor_port' ],
400
+ sub_ports [next_hop_sub_port ]['neighbor_ip' ])
401
+
375
402
# Add static route from sub-port to selected interface on the PTF
376
403
add_static_route_to_ptf (ptfhost , subnet , sub_ports [next_hop_sub_port ]['ip' ], name_of_namespace )
377
404
# Add static route from selected interface to sub-port on the PTF
@@ -398,7 +425,9 @@ def apply_route_config_for_port(request, tbinfo, duthost, ptfhost, port_type, de
398
425
dst_port_network = ipaddress .ip_network (unicode (sub_ports [sub_port ]['ip' ]), strict = False )
399
426
remove_static_route_from_ptf (ptfhost , src_port_network , sub_ports [sub_port ]['ip' ], name_of_namespace )
400
427
remove_static_route_from_ptf (ptfhost , dst_port_network , next_hop_sub_ports ['neighbor_ip' ])
401
- remove_namespace (ptfhost , name_of_namespace )
428
+
429
+ namespace_member , namespace_member_ip = namespaces [name_of_namespace ]
430
+ remove_namespace (ptfhost , name_of_namespace , namespace_member , namespace_member_ip )
402
431
403
432
if 'svi' in request .param :
404
433
# Remove SVI port from the DUT
@@ -465,7 +494,8 @@ def apply_tunnel_table_to_dut(duthost, apply_route_config):
465
494
466
495
467
496
@pytest .fixture ()
468
- def apply_balancing_config (duthost , ptfhost , ptfadapter , define_sub_ports_configuration , apply_config_on_the_dut , apply_config_on_the_ptf , tbinfo ):
497
+ def apply_balancing_config (duthost , ptfhost , ptfadapter , define_sub_ports_configuration , apply_config_on_the_dut ,
498
+ apply_config_on_the_ptf , tbinfo ):
469
499
"""
470
500
Apply balancing configuration on the DUT and remove after tests
471
501
Args:
@@ -494,7 +524,8 @@ def apply_balancing_config(duthost, ptfhost, ptfadapter, define_sub_ports_config
494
524
sub_intf_name = vlan_sub_interface ['attachto' ]
495
525
port = sub_intf_name .split (constants .VLAN_SUB_INTERFACE_SEPARATOR )[0 ]
496
526
vlan_id = vlan_sub_interface ['vlan' ]
497
- src_ports .add ("eth" + str (mg_facts ['minigraph_ptf_indices' ][port ]) + constants .VLAN_SUB_INTERFACE_SEPARATOR + str (vlan_id ))
527
+ src_ports .add ("eth" + str (mg_facts ['minigraph_ptf_indices' ][port ])
528
+ + constants .VLAN_SUB_INTERFACE_SEPARATOR + str (vlan_id ))
498
529
src_ports = tuple (src_ports )
499
530
else :
500
531
mg_facts = duthost .get_extended_minigraph_facts (tbinfo )
@@ -532,8 +563,8 @@ def apply_balancing_config(duthost, ptfhost, ptfadapter, define_sub_ports_config
532
563
remove_static_route_from_dut (duthost , str (subnet ), sub_ports [next_hop_sub_port ]['neighbor_ip' ])
533
564
534
565
535
- @pytest .fixture
536
- def reload_dut_config (request , duthost , define_sub_ports_configuration , loganalyzer ):
566
+ @pytest .fixture ( scope = 'class' )
567
+ def reload_dut_config (request , duthost , define_sub_ports_configuration , port_type ):
537
568
"""
538
569
DUT's configuration reload on teardown
539
570
@@ -543,8 +574,6 @@ def reload_dut_config(request, duthost, define_sub_ports_configuration, loganaly
543
574
define_sub_ports_configuration: Dictonary of parameters for configuration DUT
544
575
"""
545
576
yield
546
- if loganalyzer and loganalyzer [duthost .hostname ]:
547
- loganalyzer [duthost .hostname ].add_start_ignore_mark ()
548
577
549
578
sub_ports = define_sub_ports_configuration ['sub_ports' ]
550
579
dut_ports = define_sub_ports_configuration ['dut_ports' ]
@@ -556,24 +585,24 @@ def reload_dut_config(request, duthost, define_sub_ports_configuration, loganaly
556
585
557
586
py_assert (check_sub_port (duthost , sub_ports .keys (), True ), "Some sub-port were not deleted" )
558
587
559
- if 'port_in_lag' in request . node . name :
588
+ if 'port_in_lag' in port_type :
560
589
for lag_port in dut_ports .values ():
561
590
remove_lag_port (duthost , cfg_facts , lag_port )
562
591
563
592
duthost .shell ('sudo config load -y /etc/sonic/config_db.json' )
564
593
wait_critical_processes (duthost )
565
- if loganalyzer and loganalyzer [duthost .hostname ]:
566
- loganalyzer [duthost .hostname ].add_end_ignore_mark ()
567
594
568
- @pytest .fixture
569
- def reload_ptf_config (request , ptfhost , define_sub_ports_configuration ):
595
+
596
+ @pytest .fixture (scope = 'class' )
597
+ def reload_ptf_config (request , ptfhost , define_sub_ports_configuration , port_type ):
570
598
"""
571
599
PTF's configuration reload on teardown
572
600
573
601
Args:
574
602
request: pytest request object
575
603
ptfhost: PTF host object
576
604
define_sub_ports_configuration: Dictonary of parameters for configuration DUT
605
+ port_type: Type of port
577
606
"""
578
607
yield
579
608
sub_ports = define_sub_ports_configuration ['sub_ports' ]
@@ -583,7 +612,7 @@ def reload_ptf_config(request, ptfhost, define_sub_ports_configuration):
583
612
if sub_port_info ['neighbor_port' ] in ptf_port_list :
584
613
remove_sub_port_from_ptf (ptfhost , sub_port_info ['neighbor_port' ], sub_port_info ['neighbor_ip' ])
585
614
586
- if 'port_in_lag' in request . node . name :
615
+ if 'port_in_lag' in port_type :
587
616
ptf_ports = define_sub_ports_configuration ['ptf_ports' ]
588
617
for bond_port , port_name in ptf_ports .items ():
589
618
if bond_port in ptf_port_list :
@@ -593,7 +622,7 @@ def reload_ptf_config(request, ptfhost, define_sub_ports_configuration):
593
622
time .sleep (5 )
594
623
595
624
596
- @pytest .fixture (scope = "module " , autouse = True )
625
+ @pytest .fixture (scope = "package " , autouse = True )
597
626
def teardown_test_class (duthost ):
598
627
"""
599
628
Reload DUT configuration after running of test suite
@@ -604,11 +633,12 @@ def teardown_test_class(duthost):
604
633
yield
605
634
config_reload (duthost )
606
635
636
+
607
637
@pytest .fixture (autouse = True )
608
638
def ignore_expected_loganalyzer_exception (duthost , loganalyzer ):
609
639
if loganalyzer and loganalyzer [duthost .hostname ]:
610
640
ignore_regex_list = [
611
- ".*ERR teamd[0-9]*#tlm_teamd.*process_add_queue: Can't connect to teamd after.*attempts. LAG 'PortChannel.*'" ,
612
- ".*ERR swss[0-9]*#orchagent.*update: Failed to get port by bridge port ID.*"
641
+ ".*ERR teamd[0-9]*#tlm_teamd.*process_add_queue: Can't connect to teamd after.*attempts. LAG 'PortChannel.*'" ,
642
+ ".*ERR swss[0-9]*#orchagent.*update: Failed to get port by bridge port ID.*"
613
643
]
614
644
loganalyzer [duthost .hostname ].ignore_regex .extend (ignore_regex_list )
0 commit comments