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