@@ -415,6 +415,26 @@ def check_bgp_routes_converged(duthost, expected_routes, shutdown_connections=No
415415 pytest .fail (f"BGP routes aren't stable in { timeout } seconds" )
416416
417417
418+ @pytest .fixture (scope = "function" )
419+ def clean_ptf_dataplane (ptfadapter ):
420+ """
421+ Drain queued packets and clear mask counters before and after each test.
422+ The idea is that each test should start with clean dataplane state without
423+ having to restart ptfadapter fixture for each test.
424+ Takes in the function scope so that each parametrized test case also gets a clean dataplane.
425+ """
426+ dp = ptfadapter .dataplane
427+
428+ def _perform_cleanup_on_dp ():
429+ dp .drain ()
430+ dp .clear_masks ()
431+ # Before test run DP cleanup
432+ _perform_cleanup_on_dp ()
433+ yield
434+ # After test run DP cleanup
435+ _perform_cleanup_on_dp ()
436+
437+
418438def compress_expected_routes (expected_routes ):
419439 json_str = json .dumps (expected_routes )
420440 compressed = gzip .compress (json_str .encode ('utf-8' ))
@@ -503,7 +523,7 @@ def _select_targets_to_flap(bgp_peers_info, all_flap, flapping_count):
503523 return flapping_neighbors , injection_neighbor , flapping_ports , injection_port
504524
505525
506- def flapper (duthost , pdp , bgp_peers_info , transient_setup , flapping_count , connection_type , action ):
526+ def flapper (duthost , ptfadapter , bgp_peers_info , transient_setup , flapping_count , connection_type , action ):
507527 """
508528 Orchestrates interface/BGP session flapping and recovery on the DUT, generating test traffic to assess both
509529 control and data plane convergence behavior. This function is designed for use in test scenarios
@@ -525,6 +545,9 @@ def flapper(duthost, pdp, bgp_peers_info, transient_setup, flapping_count, conne
525545 global global_icmp_type , current_test , test_results
526546 current_test = f"flapper_{ action } _{ connection_type } _count_{ flapping_count } "
527547 global_icmp_type += 1
548+ pdp = ptfadapter .dataplane
549+ pdp .clear_masks ()
550+ pdp .set_qlen (PACKET_QUEUE_LENGTH )
528551 exp_mask = setup_packet_mask_counters (pdp , global_icmp_type )
529552 all_flap = (flapping_count == 'all' )
530553
@@ -785,7 +808,9 @@ def test_bgp_admin_flap(
785808 duthost ,
786809 ptfadapter ,
787810 bgp_peers_info ,
788- flapping_neighbor_count
811+ clean_ptf_dataplane ,
812+ flapping_neighbor_count ,
813+ setup_routes_before_test
789814):
790815 """
791816 Validates that both control plane and data plane remain functional with acceptable downtime when BGP sessions are
@@ -798,12 +823,11 @@ def test_bgp_admin_flap(
798823 Expected result:
799824 Dataplane downtime is less than MAX_BGP_SESSION_DOWNTIME or MAX_DOWNTIME_UNISOLATION for all ports.
800825 """
801- pdp = ptfadapter .dataplane
802- pdp .set_qlen (PACKET_QUEUE_LENGTH )
803826 # Measure shutdown convergence
804- transient_setup = flapper (duthost , pdp , bgp_peers_info , None , flapping_neighbor_count , 'bgp_sessions' , 'shutdown' )
827+ transient_setup = flapper (duthost , ptfadapter , bgp_peers_info , None , flapping_neighbor_count ,
828+ 'bgp_sessions' , 'shutdown' )
805829 # Measure startup convergence
806- flapper (duthost , pdp , None , transient_setup , flapping_neighbor_count , 'bgp_sessions' , 'startup' )
830+ flapper (duthost , ptfadapter , None , transient_setup , flapping_neighbor_count , 'bgp_sessions' , 'startup' )
807831
808832
809833@pytest .mark .parametrize ("flapping_port_count" , [1 , 10 , 20 , 'all' ])
@@ -812,6 +836,7 @@ def test_sessions_flapping(
812836 duthost ,
813837 ptfadapter ,
814838 bgp_peers_info ,
839+ clean_ptf_dataplane ,
815840 flapping_port_count ,
816841 setup_routes_before_test
817842):
@@ -826,10 +851,7 @@ def test_sessions_flapping(
826851 Expected result:
827852 Dataplane downtime is less than MAX_DOWNTIME_PORT_FLAPPING or MAX_DOWNTIME_UNISOLATION for all ports.
828853 '''
829- pdp = ptfadapter .dataplane
830- pdp .set_qlen (PACKET_QUEUE_LENGTH )
831-
832854 # Measure shutdown convergence
833- transient_setup = flapper (duthost , pdp , bgp_peers_info , None , flapping_port_count , 'ports' , 'shutdown' )
855+ transient_setup = flapper (duthost , ptfadapter , bgp_peers_info , None , flapping_port_count , 'ports' , 'shutdown' )
834856 # Measure startup convergence
835- flapper (duthost , pdp , None , transient_setup , flapping_port_count , 'ports' , 'startup' )
857+ flapper (duthost , ptfadapter , None , transient_setup , flapping_port_count , 'ports' , 'startup' )
0 commit comments