From 377a9e50c270b6739d0e72c2a39e3f5e09b7df16 Mon Sep 17 00:00:00 2001 From: jingwenxie Date: Thu, 10 Feb 2022 11:07:34 -0800 Subject: [PATCH] [tests/generic_config_updater] Replace IP testcase for BGP Listener (#5092) ### Description of PR Summary: Replace IP testcase for BGP Listener Fixes # (issue) ### Type of change - [ ] Bug fix - [ ] Testbed and Framework(new/improvement) - [x] Test case(new/improvement) ### Approach #### What is the motivation for this PR? 1. Add IP replacement case for BGPL; 2. Modify generators function to adapt to our case #### How did you do it? Add the testcase. #### How did you verify/test it? Run test of sonic-mgmt/tests/generic_config_updater/test_bgpl.py on KVM --- tests/common/helpers/generators.py | 8 +++-- tests/generic_config_updater/test_bgpl.py | 42 ++++++++++++++++++++++- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/tests/common/helpers/generators.py b/tests/common/helpers/generators.py index fa820eaf427..a8033dee983 100755 --- a/tests/common/helpers/generators.py +++ b/tests/common/helpers/generators.py @@ -10,7 +10,7 @@ def generate_ips(num, prefix, exclude_ips): exclude_ips.append(prefix.network) available_ips = list(prefix) - if len(available_ips) - len(exclude_ips)< num: + if len(available_ips) - len(exclude_ips) < num: raise Exception("Not enough available IPs") generated_ips = [] @@ -41,14 +41,16 @@ def route_through_default_routes(host, ip_addr): return ret -def generate_ip_through_default_route(host): +def generate_ip_through_default_route(host, exclude_ips=None): """ @summary: Generate a random IP address routed through default routes @param host: The duthost + @param exclude_ips: IPs to exclude, default None @return: A str, on None if non ip is found in given range """ + exclude_ips = exclude_ips if exclude_ips is not None else [] for leading in range(11, 255): - ip_addr = generate_ips(1, "{}.0.0.1/24".format(leading), [])[0] + ip_addr = generate_ips(1, "{}.0.0.1/24".format(leading), exclude_ips)[0] if route_through_default_routes(host, ip_addr): return ip_addr return None diff --git a/tests/generic_config_updater/test_bgpl.py b/tests/generic_config_updater/test_bgpl.py index 5b3f65c8210..c83d851f3a5 100644 --- a/tests/generic_config_updater/test_bgpl.py +++ b/tests/generic_config_updater/test_bgpl.py @@ -135,7 +135,7 @@ def bgpmon_tc1_add_duplicate(duthost, bgpmon_setup_info): finally: delete_tmpfile(duthost, tmpfile) -def bgpmon_tc1_admin_change(duthost): +def bgpmon_tc1_admin_change(duthost, bgpmon_setup_info): """ Test to admin down bgpmon config """ peer_addr, _, _ = bgpmon_setup_info @@ -162,6 +162,44 @@ def bgpmon_tc1_admin_change(duthost): finally: delete_tmpfile(duthost, tmpfile) +def bgpmon_tc1_ip_change(duthost, bgpmon_setup_info): + """ Test to replace bgpmon ip address + """ + peer_addr, local_addr, bgp_asn = bgpmon_setup_info + peer_addr_replaced = generate_ip_through_default_route(duthost, [IPNetwork(peer_addr).ip]) + peer_addr_replaced = str(IPNetwork(peer_addr_replaced).ip) + json_patch = [ + { + "op": "remove", + "path": "/BGP_MONITORS/{}".format(peer_addr) + }, + { + "op": "add", + "path": "/BGP_MONITORS/{}".format(peer_addr_replaced), + "value": { + "admin_status": "up", + "asn": bgp_asn, + "holdtime": "180", + "keepalive": "60", + "local_addr": local_addr, + "name": "BGPMonitor", + "nhopself": "0", + "rrclient": "0" + } + } + ] + + 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) + + check_bgpmon_with_addr(duthost, peer_addr_replaced) + finally: + delete_tmpfile(duthost, tmpfile) + def bgpmon_tc1_remove(duthost): """ Test to remove bgpmon config """ @@ -194,4 +232,6 @@ def test_bgpmon_tc1_add_and_remove(duthost, bgpmon_setup_info): """ bgpmon_tc1_add_init(duthost, bgpmon_setup_info) bgpmon_tc1_add_duplicate(duthost, bgpmon_setup_info) + bgpmon_tc1_admin_change(duthost, bgpmon_setup_info) + bgpmon_tc1_ip_change(duthost, bgpmon_setup_info) bgpmon_tc1_remove(duthost)