From ba0977df315eaa4894ed7871bc9b192010252677 Mon Sep 17 00:00:00 2001 From: Jingwen Xie Date: Wed, 6 Apr 2022 08:59:14 +0000 Subject: [PATCH] add sample code and make regex more restrict --- tests/generic_config_updater/test_bgp_prefix.py | 11 +++++++---- tests/generic_config_updater/test_bgp_speaker.py | 9 ++++++--- tests/generic_config_updater/test_bgpl.py | 11 ++++++++--- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/generic_config_updater/test_bgp_prefix.py b/tests/generic_config_updater/test_bgp_prefix.py index 29f8524449..84cbf2e031 100644 --- a/tests/generic_config_updater/test_bgp_prefix.py +++ b/tests/generic_config_updater/test_bgp_prefix.py @@ -21,7 +21,7 @@ PREFIXES_V4_RE = "ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_{}_V4 seq \d+ permit {}" PREFIXES_V6_RE = "ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_{}_V6 seq \d+ permit {}" -def get_bgp_prefix_config(duthost): +def get_bgp_prefix_runningconfig(duthost): """ Get bgp prefix config """ cmds = "show runningconfiguration bgp" @@ -30,7 +30,10 @@ def get_bgp_prefix_config(duthost): "'{}' failed with rc={}".format(cmds, output['rc']) ) - bgp_prefix_pattern = r".*prefix-list.*" + # Sample: + # ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_empty_V4 seq 30 permit 10.20.0.0/16 le 32 + # ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_empty_V6 seq 20 deny ::/0 ge 65 + bgp_prefix_pattern = r"(?:ip|ipv6) prefix-list.*(?:deny|permit).*" bgp_prefix_config = re.findall(bgp_prefix_pattern, output['stdout']) return bgp_prefix_config @@ -43,7 +46,7 @@ def setup_env(duthosts, rand_one_dut_hostname): rand_selected_dut: The fixture returns a randomly selected DuT. """ duthost = duthosts[rand_one_dut_hostname] - original_bgp_prefix_config = get_bgp_prefix_config(duthost) + original_bgp_prefix_config = get_bgp_prefix_runningconfig(duthost) create_checkpoint(duthost) yield @@ -51,7 +54,7 @@ def setup_env(duthosts, rand_one_dut_hostname): try: logger.info("Rolled back to original checkpoint") rollback_or_reload(duthost) - current_bgp_prefix_config = get_bgp_prefix_config(duthost) + current_bgp_prefix_config = get_bgp_prefix_runningconfig(duthost) pytest_assert(set(original_bgp_prefix_config) == set(current_bgp_prefix_config), "bgp prefix config are not suppose to change after test" ) diff --git a/tests/generic_config_updater/test_bgp_speaker.py b/tests/generic_config_updater/test_bgp_speaker.py index 427f778b42..b100bc124a 100644 --- a/tests/generic_config_updater/test_bgp_speaker.py +++ b/tests/generic_config_updater/test_bgp_speaker.py @@ -53,7 +53,7 @@ def lo_intf_ips(duthost, tbinfo): return ip, ipv6 pytest_assert(True, "Required ipv4 and ipv6 to start the test") -def get_bgp_speaker_config(duthost): +def get_bgp_speaker_runningconfig(duthost): """ Get bgp speaker config that contains src_address and ip_range Sample output in t0: @@ -68,6 +68,9 @@ def get_bgp_speaker_config(duthost): "'{}' failed with rc={}".format(cmds, output['rc']) ) + # Sample: + # neighbor BGPSLBPassive update-source 10.1.0.32 + # bgp listen range 192.168.0.0/21 peer-group BGPVac bgp_speaker_pattern = r"\s+neighbor.*update-source.*|\s+bgp listen range.*" bgp_speaker_config = re.findall(bgp_speaker_pattern, output['stdout']) return bgp_speaker_config @@ -81,7 +84,7 @@ def setup_env(duthosts, rand_one_dut_hostname): rand_selected_dut: The fixture returns a randomly selected DuT. """ duthost = duthosts[rand_one_dut_hostname] - original_bgp_speaker_config = get_bgp_speaker_config(duthost) + original_bgp_speaker_config = get_bgp_speaker_runningconfig(duthost) create_checkpoint(duthost) yield @@ -89,7 +92,7 @@ def setup_env(duthosts, rand_one_dut_hostname): try: logger.info("Rolled back to original checkpoint") rollback_or_reload(duthost) - current_bgp_speaker_config = get_bgp_speaker_config(duthost) + current_bgp_speaker_config = get_bgp_speaker_runningconfig(duthost) pytest_assert(set(original_bgp_speaker_config) == set(current_bgp_speaker_config), "bgp speaker config are not suppose to change after test" ) diff --git a/tests/generic_config_updater/test_bgpl.py b/tests/generic_config_updater/test_bgpl.py index cf93d52142..78dbcb279e 100644 --- a/tests/generic_config_updater/test_bgpl.py +++ b/tests/generic_config_updater/test_bgpl.py @@ -1,5 +1,6 @@ import logging import pytest +import re from netaddr import IPNetwork from tests.common.helpers.assertions import pytest_assert @@ -14,7 +15,7 @@ logger = logging.getLogger(__name__) -def get_bgp_listener_config(duthost): +def show_bgp_monitor(duthost): """ Get bgp listener config """ cmds = "show ip bgp summary" @@ -23,6 +24,10 @@ def get_bgp_listener_config(duthost): "'{}' failed with rc={}".format(cmds, output['rc']) ) + # Sample: + # Neighbhor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd NeighborName + # ----------- --- ----- --------- --------- -------- ----- ------ --------- -------------- -------------- + # 11.0.0.1 4 65100 0 0 0 0 0 never Connect BGPMonitor bgp_listener_pattern = r".*BGPMonitor.*" bgp_listener_config = re.findall(bgp_listener_pattern, output['stdout']) return bgp_listener_config @@ -36,7 +41,7 @@ def setup_env(duthosts, rand_one_dut_hostname): rand_selected_dut: The fixture returns a randomly selected DuT. """ duthost = duthosts[rand_one_dut_hostname] - original_bgp_listener_config = get_bgp_listener_config(duthost) + original_bgp_listener_config = show_bgp_monitor(duthost) create_checkpoint(duthost) yield @@ -44,7 +49,7 @@ def setup_env(duthosts, rand_one_dut_hostname): try: logger.info("Rolled back to original checkpoint") rollback_or_reload(duthost) - current_bgp_listener_config = get_bgp_listener_config(duthost) + current_bgp_listener_config = show_bgp_monitor(duthost) pytest_assert(set(original_bgp_listener_config) == set(current_bgp_listener_config), "bgp listener config are not suppose to change after test" )