Skip to content

Commit

Permalink
add sample code and make regex more restrict
Browse files Browse the repository at this point in the history
  • Loading branch information
wen587 committed Jun 1, 2022
1 parent 90e1c86 commit ba0977d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
11 changes: 7 additions & 4 deletions tests/generic_config_updater/test_bgp_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand All @@ -43,15 +46,15 @@ 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

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"
)
Expand Down
9 changes: 6 additions & 3 deletions tests/generic_config_updater/test_bgp_speaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -81,15 +84,15 @@ 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

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"
)
Expand Down
11 changes: 8 additions & 3 deletions tests/generic_config_updater/test_bgpl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import pytest
import re

from netaddr import IPNetwork
from tests.common.helpers.assertions import pytest_assert
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -36,15 +41,15 @@ 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

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"
)
Expand Down

0 comments on commit ba0977d

Please sign in to comment.