]> git.puffer.fish Git - matthieu/frr.git/commitdiff
tests: convert old pim test to more cleanly use pytest fixture
authorChristian Hopps <chopps@labn.net>
Thu, 8 Jun 2023 08:12:26 +0000 (04:12 -0400)
committerChristian Hopps <chopps@labn.net>
Thu, 8 Jun 2023 08:55:32 +0000 (04:55 -0400)
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 <chopps@labn.net>
tests/topotests/multicast_pim6_sm_topo1/test_multicast_pim6_sm2.py

index b049a1a57dec4bd561bc3cc85ac9487f9516c0b9..767264a7c04b449624827e162d7391a0d3a1c53f 100644 (file)
@@ -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)