@@ -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
@@ -788,7 +811,9 @@ def test_bgp_admin_flap(
788811 duthost ,
789812 ptfadapter ,
790813 bgp_peers_info ,
791- flapping_neighbor_count
814+ clean_ptf_dataplane ,
815+ flapping_neighbor_count ,
816+ setup_routes_before_test
792817):
793818 """
794819 Validates that both control plane and data plane remain functional with acceptable downtime when BGP sessions are
@@ -801,12 +826,11 @@ def test_bgp_admin_flap(
801826 Expected result:
802827 Dataplane downtime is less than MAX_BGP_SESSION_DOWNTIME or MAX_DOWNTIME_UNISOLATION for all ports.
803828 """
804- pdp = ptfadapter .dataplane
805- pdp .set_qlen (PACKET_QUEUE_LENGTH )
806829 # Measure shutdown convergence
807- transient_setup = flapper (duthost , pdp , bgp_peers_info , None , flapping_neighbor_count , 'bgp_sessions' , 'shutdown' )
830+ transient_setup = flapper (duthost , ptfadapter , bgp_peers_info , None , flapping_neighbor_count ,
831+ 'bgp_sessions' , 'shutdown' )
808832 # Measure startup convergence
809- flapper (duthost , pdp , None , transient_setup , flapping_neighbor_count , 'bgp_sessions' , 'startup' )
833+ flapper (duthost , ptfadapter , None , transient_setup , flapping_neighbor_count , 'bgp_sessions' , 'startup' )
810834
811835
812836@pytest .mark .parametrize ("flapping_port_count" , [1 , 10 , 20 , 'all' ])
@@ -815,6 +839,7 @@ def test_sessions_flapping(
815839 duthost ,
816840 ptfadapter ,
817841 bgp_peers_info ,
842+ clean_ptf_dataplane ,
818843 flapping_port_count ,
819844 setup_routes_before_test
820845):
@@ -829,10 +854,7 @@ def test_sessions_flapping(
829854 Expected result:
830855 Dataplane downtime is less than MAX_DOWNTIME_PORT_FLAPPING or MAX_DOWNTIME_UNISOLATION for all ports.
831856 '''
832- pdp = ptfadapter .dataplane
833- pdp .set_qlen (PACKET_QUEUE_LENGTH )
834-
835857 # Measure shutdown convergence
836- transient_setup = flapper (duthost , pdp , bgp_peers_info , None , flapping_port_count , 'ports' , 'shutdown' )
858+ transient_setup = flapper (duthost , ptfadapter , bgp_peers_info , None , flapping_port_count , 'ports' , 'shutdown' )
837859 # Measure startup convergence
838- flapper (duthost , pdp , None , transient_setup , flapping_port_count , 'ports' , 'startup' )
860+ flapper (duthost , ptfadapter , None , transient_setup , flapping_port_count , 'ports' , 'startup' )
0 commit comments