Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCU test for bgp speaker #5234

Merged
merged 4 commits into from
Mar 14, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 197 additions & 0 deletions tests/generic_config_updater/test_bgp_speaker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import logging
import pytest
import re
import ipaddress

from tests.common.helpers.assertions import pytest_assert
from tests.generic_config_updater.gu_utils import apply_patch, expect_op_success
from tests.generic_config_updater.gu_utils import generate_tmpfile, delete_tmpfile
from tests.generic_config_updater.gu_utils import create_checkpoint, delete_checkpoint, rollback_or_reload

pytestmark = [
pytest.mark.topology('t0'), # BGP Speaker is limited to t0 only
]

logger = logging.getLogger(__name__)

BGPSPEAKER_V4 = "BGPSLBPassive"
BGPSPEAKER_V6 = "BGPSLBPassiveV6"
BGPSPEAKER_SRC_ADDR_RE = "neighbor {} update-source {}"
BGPSPEAKER_IP_RANGE_RE = "bgp listen range {} peer-group {}"
DUMMY_IP_RANGE_V4 = "10.255.0.0/25"
DUMMY_IP_RANGE_V6 = "cc98:2008:2012:2022::/64"

@pytest.fixture(scope="module")
def vlan_intf_ip_ranges(duthost, tbinfo):
""" Get vlan subnet. This will be used as bgp speaker ip range
"""
mg_facts = duthost.get_extended_minigraph_facts(tbinfo)
ip_range, ip_rangev6 = "", ""
for vlan_interface in mg_facts['minigraph_vlan_interfaces']:
if ipaddress.ip_address(vlan_interface['addr']).version == 4:
ip_range = vlan_interface['subnet']
elif ipaddress.ip_address(vlan_interface['addr']).version == 6:
ip_rangev6 = vlan_interface['subnet']
if ip_range and ip_rangev6:
return ip_range, ip_rangev6
pytest_assert(True, "Required ip_range and ip_rangev6 to start the test")

@pytest.fixture(scope="module")
def lo_intf_ips(duthost, tbinfo):
""" Get loopback interface ip. This will be used as src_address in speaker
"""
mg_facts = duthost.get_extended_minigraph_facts(tbinfo)
ip, ipv6 = "", ""
for lo_interface in mg_facts['minigraph_lo_interfaces']:
if ipaddress.ip_address(lo_interface['addr']).version == 4:
ip = lo_interface['addr']
elif ipaddress.ip_address(lo_interface['addr']).version == 6:
ipv6 = lo_interface['addr']
if ip and ipv6:
return ip, ipv6
pytest_assert(True, "Required ipv4 and ipv6 to start the test")


@pytest.fixture(autouse=True)
def setup_env(duthosts, rand_one_dut_hostname):
"""
Setup/teardown fixture for bgp speaker config
Args:
duthosts: list of DUTs.
rand_selected_dut: The fixture returns a randomly selected DuT.
"""
duthost = duthosts[rand_one_dut_hostname]
create_checkpoint(duthost)

yield

try:
logger.info("Rolled back to original checkpoint")
rollback_or_reload(duthost)
finally:
delete_checkpoint(duthost)

def bgp_speaker_config_cleanup(duthost):
""" Clean up bgp speaker config to avoid ip range conflict
"""
cmds = 'sonic-db-cli CONFIG_DB keys "BGP_PEER_RANGE|*" | xargs -r sonic-db-cli CONFIG_DB del'
output = duthost.shell(cmds)
pytest_assert(not output['rc'],
"bgp speaker config cleanup failed."
)

def show_bgp_running_config(duthost):
return duthost.shell("show runningconfiguration bgp")['stdout']

def bgp_speaker_tc1_add_config(duthost, lo_intf_ip, vlan_intf_ip_range, bgp_speaker):
""" Test to add desired v4/v6 bgp speaker config
"""
json_patch = [
{
"op": "add",
"path": "/BGP_PEER_RANGE",
"value": {
"{}".format(bgp_speaker): {
"ip_range": [
"{}".format(vlan_intf_ip_range)
],
"name": "{}".format(bgp_speaker),
"src_address": "{}".format(lo_intf_ip)
}
}
}
]

tmpfile = generate_tmpfile(duthost)
logger.info("tmpfile {}".format(tmpfile))

try:
output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile)
expect_op_success(duthost, output)

bgp_config = show_bgp_running_config(duthost)
pytest_assert(re.search(BGPSPEAKER_SRC_ADDR_RE.format(bgp_speaker, lo_intf_ip), bgp_config),
"Failed to update bgp speaker src address."
)
pytest_assert(re.search(BGPSPEAKER_IP_RANGE_RE.format(bgp_speaker, vlan_intf_ip_range), bgp_config),
"Failed to add bgp speaker ip range."
)

finally:
delete_tmpfile(duthost, tmpfile)

def bgp_speaker_tc1_add_dummy_ip_range(duthost, bgp_speaker, dummy_ip_range):
""" Test to add dummy ip range to existed config
"""
json_patch = [
{
"op": "add",
"path": "/BGP_PEER_RANGE/{}/ip_range/1".format(bgp_speaker),
"value": "{}".format(dummy_ip_range)
}
]

tmpfile = generate_tmpfile(duthost)
logger.info("tmpfile {}".format(tmpfile))

try:
output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile)
expect_op_success(duthost, output)

bgp_config = show_bgp_running_config(duthost)
pytest_assert(re.search(BGPSPEAKER_IP_RANGE_RE.format(bgp_speaker, dummy_ip_range), bgp_config),
"Failed to add bgp speaker dummy ip range."
)

finally:
delete_tmpfile(duthost, tmpfile)

def bgp_speaker_tc1_rm_dummy_ip_range(duthost, bgp_speaker, dummy_ip_range):
""" Test to remove dummy ip range to existed config
"""
json_patch = [
{
"op": "remove",
"path": "/BGP_PEER_RANGE/{}/ip_range/1".format(bgp_speaker)
}
]

tmpfile = generate_tmpfile(duthost)
logger.info("tmpfile {}".format(tmpfile))

try:
output = apply_patch(duthost, json_data=json_patch, dest_file=tmpfile)
expect_op_success(duthost, output)

bgp_config = show_bgp_running_config(duthost)
pytest_assert(not re.search(BGPSPEAKER_IP_RANGE_RE.format(bgp_speaker, dummy_ip_range), bgp_config),
"Failed to remove bgp speaker dummy ip range."
)

finally:
delete_tmpfile(duthost, tmpfile)

@pytest.mark.parametrize("ip_version", ["v4", "v6"])
def test_bgp_speaker_tc1_test_config(duthost, lo_intf_ips, vlan_intf_ip_ranges, ip_version):
""" Test suite for bgp speaker config for v4 and v6
"""
lo_ip, lo_ipv6 = lo_intf_ips
ip_range, ip_rangev6 = vlan_intf_ip_ranges

if ip_version == "v4":
lo_intf_ip = lo_ip
vlan_intf_ip_range = ip_range
bgp_speaker = BGPSPEAKER_V4
dummy_ip_range = DUMMY_IP_RANGE_V4
elif ip_version == "v6":
lo_intf_ip = lo_ipv6
vlan_intf_ip_range = ip_rangev6
bgp_speaker = BGPSPEAKER_V6
dummy_ip_range = DUMMY_IP_RANGE_V6
else:
pytest.fail("Invalid ip version!")

bgp_speaker_config_cleanup(duthost)
bgp_speaker_tc1_add_config(duthost, lo_intf_ip, vlan_intf_ip_range, bgp_speaker)
Copy link
Contributor

@qiluo-msft qiluo-msft Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bgp_speaker_tc1_add_config

So you test with ipv4 only, and then ipv6 only. Should you test add ipv6 and ipv6 config in the same JsonPatch? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess they are the same. Do you think testing them together is better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combined.

bgp_speaker_tc1_add_dummy_ip_range(duthost, bgp_speaker, dummy_ip_range)
bgp_speaker_tc1_rm_dummy_ip_range(duthost, bgp_speaker, dummy_ip_range)
Copy link

@ghooo ghooo Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for changing the src_address .. for example switching from loopback0 to loobpack1?

Also, we would like to test adding a new VLAN and also a new Loopback is that covered under VLAN/Loopback testing? (i think so, just double checking). #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added src_address change to a dummy ip.

Also, we would like to test adding a new VLAN and also a new Loopback is that covered under VLAN/Loopback testing? (i think so, just double checking).

Yes. That is covered.