|
1 | | -from typing import Dict, List |
2 | | - |
3 | 1 | import pytest |
4 | 2 | import logging |
5 | 3 | import itertools |
6 | 4 | import collections |
7 | 5 | import ipaddress |
8 | 6 | import time |
9 | 7 | import json |
10 | | - |
11 | | -from tests.common import config_reload |
12 | 8 | from tests.common.helpers.assertions import pytest_assert |
13 | 9 | from tests.common.utilities import wait_until |
14 | 10 | from jinja2 import Template |
15 | | -from netaddr import valid_ipv4, valid_ipv6 |
| 11 | +from netaddr import valid_ipv4 |
16 | 12 |
|
17 | 13 |
|
18 | 14 | logger = logging.getLogger(__name__) |
@@ -602,131 +598,3 @@ def check_bgp_router_id(duthost, mgFacts): |
602 | 598 | return False |
603 | 599 | except Exception as e: |
604 | 600 | logger.error("Error loading BGP routerID - {}".format(e)) |
605 | | - |
606 | | - |
607 | | -@pytest.fixture(scope="module") |
608 | | -def convert_and_restore_config_db_to_ipv6_only(duthosts): |
609 | | - """Back up the existing config_db.json file and restore it once the test ends. |
610 | | -
|
611 | | - Some cases will update the running config during the test and save the config |
612 | | - to be recovered after reboot. In such a case we need to backup config_db.json before |
613 | | - the test starts and then restore it after the test ends. |
614 | | - """ |
615 | | - config_db_file = "/etc/sonic/config_db.json" |
616 | | - config_db_bak_file = "/etc/sonic/config_db.json.before_ipv6_only" |
617 | | - |
618 | | - # Sample MGMT_INTERFACE: |
619 | | - # "MGMT_INTERFACE": { |
620 | | - # "eth0|192.168.0.2/24": { |
621 | | - # "forced_mgmt_routes": [ |
622 | | - # "192.168.1.1/24" |
623 | | - # ], |
624 | | - # "gwaddr": "192.168.0.1" |
625 | | - # }, |
626 | | - # "eth0|fc00:1234:5678:abcd::2/64": { |
627 | | - # "gwaddr": "fc00:1234:5678:abcd::1", |
628 | | - # "forced_mgmt_routes": [ |
629 | | - # "fc00:1234:5678:abc1::1/64" |
630 | | - # ] |
631 | | - # } |
632 | | - # } |
633 | | - |
634 | | - # duthost_name: config_db_modified |
635 | | - config_db_modified: Dict[str, bool] = {duthost.hostname: False |
636 | | - for duthost in duthosts.nodes} |
637 | | - # duthost_name: [ip_addr] |
638 | | - ipv4_address: Dict[str, List] = {duthost.hostname: [] |
639 | | - for duthost in duthosts.nodes} |
640 | | - ipv6_address: Dict[str, List] = {duthost.hostname: [] |
641 | | - for duthost in duthosts.nodes} |
642 | | - # Check IPv6 mgmt-ip is set, otherwise the DUT will lose control after v4 mgmt-ip is removed |
643 | | - for duthost in duthosts.nodes: |
644 | | - mgmt_interface = json.loads(duthost.shell(f"jq '.MGMT_INTERFACE' {config_db_file}", |
645 | | - module_ignore_errors=True)["stdout"]) |
646 | | - # Use list() to make a copy of mgmt_interface.keys() to avoid |
647 | | - # "RuntimeError: dictionary changed size during iteration" error |
648 | | - for key in list(mgmt_interface): |
649 | | - ip_addr = key.split("|")[1] |
650 | | - ip_addr_without_mask = ip_addr.split('/')[0] |
651 | | - if ip_addr: |
652 | | - is_ipv6 = valid_ipv6(ip_addr_without_mask) |
653 | | - if is_ipv6: |
654 | | - logger.info(f"Host[{duthost.hostname}] IPv6[{ip_addr}]") |
655 | | - ipv6_address[duthost.hostname].append(ip_addr_without_mask) |
656 | | - pytest_assert(len(ipv6_address[duthost.hostname]) > 0, |
657 | | - f"{duthost.hostname} doesn't have IPv6 Management IP address") |
658 | | - |
659 | | - # Remove IPv4 mgmt-ip |
660 | | - for duthost in duthosts.nodes: |
661 | | - logger.info(f"Backup {config_db_file} to {config_db_bak_file} on {duthost.hostname}") |
662 | | - duthost.shell(f"cp {config_db_file} {config_db_bak_file}") |
663 | | - mgmt_interface = json.loads(duthost.shell(f"jq '.MGMT_INTERFACE' {config_db_file}", |
664 | | - module_ignore_errors=True)["stdout"]) |
665 | | - |
666 | | - # Use list() to make a copy of mgmt_interface.keys() to avoid |
667 | | - # "RuntimeError: dictionary changed size during iteration" error |
668 | | - for key in list(mgmt_interface): |
669 | | - ip_addr = key.split("|")[1] |
670 | | - ip_addr_without_mask = ip_addr.split('/')[0] |
671 | | - if ip_addr: |
672 | | - is_ipv4 = valid_ipv4(ip_addr_without_mask) |
673 | | - if is_ipv4: |
674 | | - ipv4_address[duthost.hostname].append(ip_addr_without_mask) |
675 | | - logger.info(f"Removing host[{duthost.hostname}] IPv4[{ip_addr}]") |
676 | | - duthost.shell(f"""jq 'del(."MGMT_INTERFACE"."{key}")' {config_db_file} > temp.json""" |
677 | | - f"""&& mv temp.json {config_db_file}""", module_ignore_errors=True) |
678 | | - config_db_modified[duthost.hostname] = True |
679 | | - config_reload(duthost, wait=120) |
680 | | - duthosts.reset() |
681 | | - |
682 | | - # Verify mgmt-interface status |
683 | | - mgmt_intf_name = "eth0" |
684 | | - for duthost in duthosts.nodes: |
685 | | - logger.info(f"Checking host[{duthost.hostname}] mgmt interface[{mgmt_intf_name}]") |
686 | | - mgmt_intf_ifconfig = duthost.shell(f"ifconfig {mgmt_intf_name}", module_ignore_errors=True)["stdout"] |
687 | | - assert_addr_in_ifconfig(addr_set=ipv4_address, hostname=duthost.hostname, |
688 | | - expect_exists=False, ifconfig_output=mgmt_intf_ifconfig) |
689 | | - assert_addr_in_ifconfig(addr_set=ipv6_address, hostname=duthost.hostname, |
690 | | - expect_exists=True, ifconfig_output=mgmt_intf_ifconfig) |
691 | | - |
692 | | - yield |
693 | | - |
694 | | - # Recover IPv4 mgmt-ip |
695 | | - for duthost in duthosts.nodes: |
696 | | - if config_db_modified[duthost.hostname]: |
697 | | - logger.info(f"Restore {config_db_file} with {config_db_bak_file} on {duthost.hostname}") |
698 | | - duthost.shell(f"mv {config_db_bak_file} {config_db_file}") |
699 | | - config_reload(duthost, safe_reload=True) |
700 | | - duthosts.reset() |
701 | | - |
702 | | - # Verify mgmt-interface status |
703 | | - for duthost in duthosts.nodes: |
704 | | - logger.info(f"Checking host[{duthost.hostname}] mgmt interface[{mgmt_intf_name}]") |
705 | | - mgmt_intf_ifconfig = duthost.shell(f"ifconfig {mgmt_intf_name}", module_ignore_errors=True)["stdout"] |
706 | | - assert_addr_in_ifconfig(addr_set=ipv4_address, hostname=duthost.hostname, |
707 | | - expect_exists=True, ifconfig_output=mgmt_intf_ifconfig) |
708 | | - assert_addr_in_ifconfig(addr_set=ipv6_address, hostname=duthost.hostname, |
709 | | - expect_exists=True, ifconfig_output=mgmt_intf_ifconfig) |
710 | | - |
711 | | - |
712 | | -def assert_addr_in_ifconfig(addr_set: Dict[str, List], hostname: str, expect_exists: bool, ifconfig_output: str): |
713 | | - """ |
714 | | - Assert the address status in the ifconfig output, |
715 | | - if status not as expected, assert as failure |
716 | | -
|
717 | | - @param addr_set: addr_set, key is dut hostname, value is the list of ip addresses |
718 | | - @param hostname: hostname |
719 | | - @param expect_exists: Expectation of the ip, |
720 | | - True means expect all ip addresses in addr_set appears in the output of ifconfig |
721 | | - False means expect no ip addresses in addr_set appears in the output of ifconfig |
722 | | - @param ifconfig_output: output of 'ifconfig' |
723 | | - """ |
724 | | - for addr in addr_set[hostname]: |
725 | | - if expect_exists: |
726 | | - pytest_assert(addr in ifconfig_output, |
727 | | - f"{addr} not appeared in {hostname} mgmt interface") |
728 | | - logger.info(f"{addr} exists in the output of ifconfig") |
729 | | - else: |
730 | | - pytest_assert(addr not in ifconfig_output, |
731 | | - f"{hostname} mgmt interface still with addr {addr}") |
732 | | - logger.info(f"{addr} not exists in the output of ifconfig which is expected") |
0 commit comments