-
Notifications
You must be signed in to change notification settings - Fork 3
/
task7_labpyats.py
executable file
·114 lines (86 loc) · 3.09 KB
/
task7_labpyats.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# To get a logger for the script
import logging
from pyats import aetest
from pyats.log.utils import banner
# To handle errors with connections to devices
from unicon.core import errors
import argparse
from pyats.topology import loader
# Get your logger for your script
global log
log = logging.getLogger(__name__)
log.level = logging.INFO
# SNs that has to be changed to the actual:
contract_sn = ['9AQHSSAS8AU', '9Q3YV06WJ71', '9IFUH4GPSGL']
class MyCommonSetup(aetest.CommonSetup):
"""
CommonSetup class to prepare for testcases
Establishes connections to all devices in testbed
"""
@aetest.subsection
def establish_connections(self, pyats_testbed):
"""
Establishes connections to all devices in testbed
:param testbed:
:return:
"""
device_list = []
for device in pyats_testbed.devices.values():
log.info(banner(f"Connect to device '{device.name}'"))
try:
device.connect(log_stdout=False)
except errors.ConnectionError:
self.failed(f"Failed to establish "
f"connection to '{device.name}'")
device_list.append(device)
# Pass list of devices to testcases
self.parent.parameters.update(dev=device_list)
class Inventory(aetest.Testcase):
"""
Inventory Testcase - extract Serial numbers information from devices
Verify that all SNs are covered by service contract (exist in contract_sn)
"""
@aetest.setup
def setup(self):
"""
Get list of all devices in testbed and
run inventory testcase for each device
:return:
"""
devices = self.parent.parameters['dev']
aetest.loop.mark(self.inventory, device=devices)
@aetest.test
def inventory(self, device):
"""
Verify that all SNs are covered by
service contract (exist in contract_sn)
:return:
"""
if device.os == 'iosxe':
out1 = device.parse('show inventory')
chassis_sn = out1['main']['chassis']['CSR1000V']['sn']
if chassis_sn not in contract_sn:
self.failed(f'{chassis_sn} is not covered by contract')
else:
pass
elif device.os == 'nxos':
out2 = device.parse('show inventory')
chassis_sn = out2['name']['Chassis']['serial_number']
if chassis_sn not in contract_sn:
self.failed(f'{chassis_sn} is not covered by contract')
else:
pass
elif device.os == 'asa':
out3 = device.parse('show inventory')
chassis_sn = out3['Chassis']['sn']
if chassis_sn not in contract_sn:
self.failed(f'{chassis_sn} is not covered by contract')
else:
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--testbed', dest='pyats_testbed',
type=loader.load)
args, unknown = parser.parse_known_args()
aetest.main(**vars(args))