From 4e6e3e66c65811eeeae4595e17732f3d993fd768 Mon Sep 17 00:00:00 2001 From: Christian Hopps Date: Thu, 8 Jun 2023 04:12:26 -0400 Subject: [PATCH] tests: convert old pim test to more cleanly use pytest fixture This is a good way to run a per-test background helper process. Here the helper object is created before the test function requesting it (through param name match), and then cleaned up after the test function exits (pass or failed). A context manager is used to further guarantee the cleanup is done. Signed-off-by: Christian Hopps (cherry picked from commit b28bc2561e7231e410e304f7ec23f9d795e3e479) --- .../test_multicast_pim6_sm2.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/tests/topotests/multicast_pim6_sm_topo1/test_multicast_pim6_sm2.py b/tests/topotests/multicast_pim6_sm_topo1/test_multicast_pim6_sm2.py index b049a1a57d..767264a7c0 100644 --- a/tests/topotests/multicast_pim6_sm_topo1/test_multicast_pim6_sm2.py +++ b/tests/topotests/multicast_pim6_sm_topo1/test_multicast_pim6_sm2.py @@ -84,6 +84,16 @@ ASSERT_MSG = "Testcase {} : Failed Error: {}" pytestmark = [pytest.mark.pim6d] +@pytest.fixture(scope="function") +def app_helper(): + # helper = McastTesterHelper(get_topogen()) + # yield helepr + # helper.cleanup() + # Even better use contextmanager functionality: + with McastTesterHelper(get_topogen()) as ah: + yield ah + + def setup_module(mod): """ Sets up the pytest environment @@ -119,9 +129,6 @@ def setup_module(mod): # Creating configuration from JSON build_config_from_json(tgen, tgen.json_topo) - global app_helper - app_helper = McastTesterHelper(tgen) - logger.info("Running setup_module() done") @@ -132,8 +139,6 @@ def teardown_module(): tgen = get_topogen() - app_helper.cleanup() - # Stop toplogy and Remove tmp files tgen.stop_topology() @@ -196,7 +201,7 @@ def verify_state_incremented(state_before, state_after): ##################################################### -def test_clear_mroute_and_verify_multicast_data_p0(request): +def test_clear_mroute_and_verify_multicast_data_p0(request, app_helper): """ Verify (*,G) and (S,G) entry populated again after clear the PIM nbr and mroute from FRR node @@ -424,7 +429,9 @@ def test_clear_mroute_and_verify_multicast_data_p0(request): write_test_footer(tc_name) -def test_verify_SPT_switchover_when_RPT_and_SPT_path_is_different_p0(request): +def test_verify_SPT_switchover_when_RPT_and_SPT_path_is_different_p0( + request, app_helper +): """ Verify SPT switchover working when RPT and SPT path is different @@ -437,8 +444,6 @@ def test_verify_SPT_switchover_when_RPT_and_SPT_path_is_different_p0(request): # Creating configuration from JSON reset_config_on_routers(tgen) - app_helper.stop_all_hosts() - # Don"t run this test if we have any failure. if tgen.routers_have_failure(): pytest.skip(tgen.errors) -- 2.39.5