|
| 1 | +import contextlib |
1 | 2 | import logging |
2 | 3 | import pytest |
3 | 4 | import time |
4 | 5 |
|
5 | 6 | from tests.common.helpers.assertions import pytest_assert |
| 7 | +from tests.common.utilities import wait_until |
6 | 8 |
|
7 | 9 | logger = logging.getLogger(__name__) |
8 | 10 |
|
@@ -35,15 +37,43 @@ def interfaceConfig(self, duthosts, rand_one_dut_hostname): |
35 | 37 | None |
36 | 38 | """ |
37 | 39 | duthost = duthosts[rand_one_dut_hostname] |
38 | | - logger.info("Configure the DUT interface, start interface, add IP address") |
39 | | - self.__startInterface(duthost) |
40 | | - self.__configureInterfaceIp(duthost, action="add") |
41 | 40 |
|
42 | | - yield |
43 | | - |
44 | | - logger.info("Restore the DUT interface config, remove IP address") |
45 | | - self.__configureInterfaceIp(duthost, action="remove") |
46 | | - self.__shutdownInterface(duthost) |
| 41 | + intfStatus = duthost.show_interface(command="status")["ansible_facts"]["int_status"] |
| 42 | + if self.DUT_ETH_IF not in intfStatus: |
| 43 | + pytest.skip('{} not found'.format(self.DUT_ETH_IF)) |
| 44 | + |
| 45 | + status = intfStatus[self.DUT_ETH_IF] |
| 46 | + if "up" not in status["oper_state"]: |
| 47 | + pytest.skip('{} is down'.format(self.DUT_ETH_IF)) |
| 48 | + |
| 49 | + portchannel = status["vlan"] if "PortChannel" in status["vlan"] else None |
| 50 | + |
| 51 | + @contextlib.contextmanager |
| 52 | + def removeFromPortChannel(duthost, portchannel, intf): |
| 53 | + try: |
| 54 | + if portchannel: |
| 55 | + duthost.command("sudo config portchannel member del {} {}".format(portchannel, intf)) |
| 56 | + pytest_assert(wait_until( |
| 57 | + 10, 1, 0, |
| 58 | + lambda: 'routed' in duthost.show_interface(command="status") |
| 59 | + ["ansible_facts"]["int_status"][intf]["vlan"]), |
| 60 | + '{} is not in routed status'.format(intf) |
| 61 | + ) |
| 62 | + yield |
| 63 | + finally: |
| 64 | + if portchannel: |
| 65 | + duthost.command("sudo config portchannel member add {} {}".format(portchannel, intf)) |
| 66 | + |
| 67 | + with removeFromPortChannel(duthost, portchannel, self.DUT_ETH_IF): |
| 68 | + logger.info("Configure the DUT interface, start interface, add IP address") |
| 69 | + self.__startInterface(duthost) |
| 70 | + self.__configureInterfaceIp(duthost, action="add") |
| 71 | + |
| 72 | + yield |
| 73 | + |
| 74 | + logger.info("Restore the DUT interface config, remove IP address") |
| 75 | + self.__configureInterfaceIp(duthost, action="remove") |
| 76 | + self.__shutdownInterface(duthost) |
47 | 77 |
|
48 | 78 | @pytest.fixture(params=[0, 1]) |
49 | 79 | def macIndex(self, request): |
|
0 commit comments