forked from sonic-net/sonic-mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathport_toggle.py
More file actions
70 lines (52 loc) · 2.31 KB
/
Copy pathport_toggle.py
File metadata and controls
70 lines (52 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Tool used for shutdown/startup port on the DUT.
"""
import time
import logging
import pprint
from tests.common.helpers.assertions import pytest_assert
from tests.platform_tests.link_flap.link_flap_utils import watch_system_status
from tests.common.utilities import wait_until
logger = logging.getLogger(__name__)
def port_toggle(duthost, ports=None, wait=60, wait_after_ports_up=60, watch=False):
"""
Toggle ports on DUT.
Args:
duthost: DUT host object
ports: Specify list of ports, None if toggle all ports
wait: Time to wait for interface to become up
wait_after_ports_up: Time to wait after interfaces become up
watch: Logging system state
"""
def __check_interface_state(state='up'):
"""
Check interfaces status
Args:
state: state of DUT's interface
"""
ports_down = duthost.interface_facts(up_ports=ports)['ansible_facts']['ansible_interface_link_down_ports']
if 'down' in state:
return len(ports_down) == len(ports)
else:
return len(ports_down) == 0
if ports is None:
logger.debug('ports is None, toggling all minigraph ports')
mg_facts = duthost.get_extended_minigraph_facts()
ports = mg_facts['minigraph_ports'].keys()
logger.info('toggling ports:\n%s', pprint.pformat(ports))
for port in ports:
duthost.command('config interface shutdown {}'.format(port))
if watch:
time.sleep(1)
watch_system_status(duthost)
# verify all interfaces are down
pytest_assert(wait_until(3, 1, __check_interface_state, 'down'),
"dut ports {} didn't go down as expected"
.format(list(set(ports).difference(set(duthost.interface_facts(up_ports=ports)['ansible_facts']['ansible_interface_link_down_ports'])))))
for port in ports:
duthost.command('config interface startup {}'.format(port))
logger.info('waiting for ports to become up')
pytest_assert(wait_until(wait, 1, __check_interface_state),
"dut ports {} didn't go up as expected".format(duthost.interface_facts(up_ports=ports)['ansible_facts']['ansible_interface_link_down_ports']))
logger.info('wait %d seconds for system to startup', wait_after_ports_up)
time.sleep(wait_after_ports_up)