]> git.puffer.fish Git - matthieu/frr.git/commitdiff
tests: factorize code in bgp_bmp_vrf
authorLouis Scalbert <louis.scalbert@6wind.com>
Thu, 24 Oct 2024 14:33:43 +0000 (16:33 +0200)
committerLouis Scalbert <louis.scalbert@6wind.com>
Fri, 25 Oct 2024 12:17:20 +0000 (14:17 +0200)
Factorize code in bgp_bmp_vrf

Signed-off-by: Louis Scalbert <louis.scalbert@6wind.com>
tests/topotests/bgp_bmp_vrf/test_bgp_bmp_vrf.py

index 0c255167e71f5d5da2eeb1210c167fe4218e6a98..a7a1e87d9771ba0eceb23592b57fab6cfd7d880c 100644 (file)
@@ -324,7 +324,7 @@ def configure_prefixes(tgen, node, asn, safi, prefixes, vrf=None, update=True):
         tgen.gears[node].vtysh_cmd("".join(cmd))
 
 
-def unicast_prefixes(policy):
+def _test_prefixes(policy, step=1):
     """
     Setup the BMP  monitor policy, Add and withdraw ipv4/v6 prefixes.
     Check if the previous actions are logged in the BMP server with the right
@@ -333,62 +333,40 @@ def unicast_prefixes(policy):
     tgen = get_topogen()
     set_bmp_policy(tgen, "r1", 65501, "bmp1", "unicast", policy, vrf="vrf1")
 
-    update_seq()
-
     prefixes = ["172.31.0.15/32", "2111::1111/128"]
-    # add prefixes
-    configure_prefixes(tgen, "r2", 65502, "unicast", prefixes)
 
-    logger.info("checking for updated prefixes")
+    for type in ("update", "withdraw"):
+        update_seq()
 
-    for ipver in [4, 6]:
-        if UPDATE_EXPECTED_JSON:
-            continue
-        ref_file = "{}/r1/show-bgp-ipv{}-update-step1.json".format(CWD, ipver)
-        expected = json.loads(open(ref_file).read())
-
-        test_func = partial(
-            topotest.router_json_cmp,
-            tgen.gears["r1"],
-            f"show bgp vrf vrf1 ipv{ipver} json",
-            expected,
+        # add prefixes
+        configure_prefixes(
+            tgen, "r2", 65502, "unicast", prefixes, update=(type == "update")
         )
-        _, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
-        assertmsg = f"r1: BGP IPv{ipver} convergence failed"
-        assert res is None, assertmsg
-
-    # check
-    test_func = partial(check_for_prefixes, prefixes, "update", policy, 1)
-    success, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
-    assert success, "Checking the updated prefixes has been failed ! %s" % res
-
-    update_seq()
-
-    # withdraw prefixes
-    configure_prefixes(tgen, "r2", 65502, "unicast", prefixes, update=False)
 
-    logger.info("checking for withdrawn prefixes")
+        logger.info(f"checking for prefixes {type}")
 
-    for ipver in [4, 6]:
-        if UPDATE_EXPECTED_JSON:
-            continue
-        ref_file = "{}/r1/show-bgp-ipv{}-withdraw-step1.json".format(CWD, ipver)
-        expected = json.loads(open(ref_file).read())
-
-        test_func = partial(
-            topotest.router_json_cmp,
-            tgen.gears["r1"],
-            f"show bgp vrf vrf1 ipv{ipver} json",
-            expected,
-        )
-        _, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
-        assertmsg = f"r1: BGP IPv{ipver} convergence failed"
-        assert res is None, assertmsg
-
-    # check
-    test_func = partial(check_for_prefixes, prefixes, "withdraw", policy, 1)
-    success, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
-    assert success, "Checking the withdrawn prefixes has been failed ! %s" % res
+        for ipver in [4, 6]:
+            if UPDATE_EXPECTED_JSON:
+                continue
+            ref_file = "{}/r1/show-bgp-ipv{}-{}-step{}.json".format(
+                CWD, ipver, type, step
+            )
+            expected = json.loads(open(ref_file).read())
+
+            test_func = partial(
+                topotest.router_json_cmp,
+                tgen.gears["r1"],
+                f"show bgp vrf vrf1 ipv{ipver} json",
+                expected,
+            )
+            _, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
+            assertmsg = f"r1: BGP IPv{ipver} convergence failed"
+            assert res is None, assertmsg
+
+        # check
+        test_func = partial(check_for_prefixes, prefixes, type, policy, step)
+        success, res = topotest.run_and_expect(test_func, None, count=30, wait=1)
+        assert success, "Checking the updated prefixes has been failed ! %s" % res
 
 
 def test_bmp_server_logging():
@@ -428,11 +406,11 @@ def test_bmp_bgp_unicast():
     Add/withdraw bgp unicast prefixes and check the bmp logs.
     """
     logger.info("*** Unicast prefixes pre-policy logging ***")
-    unicast_prefixes(PRE_POLICY)
+    _test_prefixes(PRE_POLICY)
     logger.info("*** Unicast prefixes post-policy logging ***")
-    unicast_prefixes(POST_POLICY)
+    _test_prefixes(POST_POLICY)
     logger.info("*** Unicast prefixes loc-rib logging ***")
-    unicast_prefixes(LOC_RIB)
+    _test_prefixes(LOC_RIB)
 
 
 def test_peer_down():