-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpyats_ios_example.py
375 lines (288 loc) · 12.7 KB
/
pyats_ios_example.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/env python
'''pyats_ios_example.py
This is a testscript example intended to walk users through basic Cisco IOS
device connection, command execution and result verification using pyATS.
Arguments:
This script requires one script argument (testbed) to be passed in when run
under standalone execution for demonstration purposes.
testbed: the path to testbed yaml file
Topology:
+-------------+ Eth0/0 <-> Eth0/0 +-------------+
| | ------------------------------------ | |
| ios1 | | ios2 |
| | ------------------------------------ | |
+-------------+ Eth0/1 <-> Eth0/1 +-------------+
Testing:
This script performs the following tests for demonstration purposes.
- router connection: basic device connection test
- `ping` command: basic device ping test; logs ping result.
- interface count verification
- execute `show version` command: basic command execution and data
parsing; extract ethernet and serial
interface counts; logs interface
counts.
- execute `show ip interface brief` command: basic command execution and
data parsing; extract all
ethernet and serial
interfaces; logs number of
interface counts.
- verify ethernet and serial interface counts from above commands.
- router disconnect: basic device disconnect test
Examples:
# to run under standalone execution
bash$ python pyats_ios_example.py --testbed pyats_ios_example.yaml
References:
For the complete and up-to-date user guide on pyATS, visit:
https://developer.cisco.com/site/pyats/docs/
'''
#
# optional author information
#
__author__ = 'Wei Chen <[email protected]>'
__copyright__ = 'Copyright 2017, Cisco Systems'
__email__ = '[email protected]'
__date__= 'Nov 15, 2017'
#
# import statements
#
import re
import logging
from ats import aetest
from ats.log.utils import banner
#
# create a logger for this testscript
#
logger = logging.getLogger(__name__)
#
# Common Setup Section
#
class common_setup(aetest.CommonSetup):
'''Common Setup Section
Defines subsections that performs configuration common to the entire script.
'''
@aetest.subsection
def check_topology(self,
testbed,
ios1_name = 'ios1',
ios2_name = 'ios2'):
'''
check that we have at least two devices and a link between the devices
If so, mark the next subsection for looping.
'''
# abort/fail the testscript if no testbed was provided
if not testbed or not testbed.devices:
self.failed('No testbed was provided to script launch',
goto = ['exit'])
# abort/fail the testscript if no matching device was provided
for ios_name in (ios1_name, ios2_name):
if ios_name not in testbed:
self.failed('testbed needs to contain device {ios_name}'.format(
ios_name=ios_name,
),
goto = ['exit'])
ios1 = testbed.devices[ios1_name]
ios2 = testbed.devices[ios2_name]
# add them to testscript parameters
self.parent.parameters.update(ios1 = ios1, ios2 = ios2)
# get corresponding links
links = ios1.find_links(ios2)
assert len(links) >= 1, 'require one link between ios1 and ios2'
# save link as uut link parameter
self.parent.parameters['uut_link'] = links.pop()
@aetest.subsection
def establish_connections(self, steps, ios1, ios2):
'''
establish connection to both devices
'''
with steps.start('Connecting to Router-1'):
ios1.connect()
with steps.start('Connecting to Router-2'):
ios2.connect()
# abort/fail the testscript if any device isn't connected
if not ios1.connected or not ios2.connected:
self.failed('One of the two devices could not be connected to',
goto = ['exit'])
@aetest.subsection
def marking_interface_count_testcases(self, testbed):
'''
mark the VerifyInterfaceCountTestcase for looping.
'''
# ignore CML terminal_server
devices = [d for d in testbed.devices.keys() if 'terminal_server' not in d]
logger.info(banner('Looping VerifyInterfaceCountTestcase'
' for {}'.format(devices)))
# dynamic loop marking on testcase
aetest.loop.mark(VerifyInterfaceCountTestcase, device = devices)
#
# Ping Testcase: leverage dual-level looping
#
@aetest.loop(device = ('ios1', 'ios2'))
class PingTestcase(aetest.Testcase):
'''Ping test'''
groups = ('basic', 'looping')
@aetest.setup
def setup(self, uut_link):
destination = []
# To get the link interfaces ip
for intf in uut_link.interfaces:
parsed_dict = self.parent.parameters[intf.device.name].\
parse('show ip interface brief')
intf_ip = parsed_dict['interface'][intf.name]['ip_address']
destination.append(intf_ip)
# apply loop to next section
aetest.loop.mark(self.ping, destination = destination)
@aetest.test
def ping(self, device, destination):
'''
ping destination ip address from device
Sample of ping command result:
ping
Protocol [ip]:
Target IP address: 10.10.10.2
Repeat count [5]:
Datagram size [100]:
Timeout in seconds [2]:
Extended commands [n]: n
Sweep range of sizes [n]: n
Type escape sequence to abort.
Sending 5, 100-byte ICMP Echos to 10.10.10.2, timeout is 2 seconds:
!!!!!
Success rate is 100 percent (5/5), round-trip min/avg/max = 1/1/1 ms
'''
try:
# store command result for later usage
result = self.parameters[device].ping(destination)
except Exception as e:
# abort/fail the testscript if ping command returns any exception
# such as connection timeout or command failure
self.failed('Ping {} from device {} failed with error: {}'.format(
destination,
device,
str(e),
),
goto = ['exit'])
else:
# extract success rate from ping result with regular expression
match = re.search(r'Success rate is (?P<rate>\d+) percent', result)
success_rate = match.group('rate')
# log the success rate
logger.info(banner('Ping {} with success rate of {}%'.format(
destination,
success_rate,
)
)
)
#
# Verify Interface Count Testcase
#
class VerifyInterfaceCountTestcase(aetest.Testcase):
'''Verify interface count test'''
groups = ('basic', 'looping')
@aetest.test
def extract_interface_count(self, device):
'''
extract interface counts from `show version`
Sample of show version command result:
show version
Cisco IOS Software, IOSv Software (VIOS-ADVENTERPRISEK9-M), Version 15.6(2)T, RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2016 by Cisco Systems, Inc.
Compiled Tue 22-Mar-16 16:19 by prod_rel_team
ROM: Bootstrap program is IOSv
ios2 uptime is 1 hour, 17 minutes
System returned to ROM by reload
System image file is "flash0:/vios-adventerprisek9-m"
Last reload reason: Unknown reason
<....>
Cisco IOSv (revision 1.0) with with 484609K/37888K bytes of memory.
Processor board ID 9QTSICFAZS7Q2I61N8WNZ
2 Gigabit Ethernet interfaces
DRAM configuration is 72 bits wide with parity disabled.
256K bytes of non-volatile configuration memory.
2097152K bytes of ATA System CompactFlash 0 (Read/Write)
0K bytes of ATA CompactFlash 1 (Read/Write)
0K bytes of ATA CompactFlash 2 (Read/Write)
10080K bytes of ATA CompactFlash 3 (Read/Write)
Configuration register is 0x0
'''
try:
# store execution result for later usage
result = self.parameters[device].execute('show version')
except Exception as e:
# abort/fail the testscript if show version command returns any
# exception such as connection timeout or command failure
self.failed('Device {} \'show version\' failed: {}'.format(device,
str(e)),
goto = ['exit'])
else:
# extract interfaces counts from `show version`
match = re.search(r'(?P<ethernet>\d+) Gigabit Ethernet interfaces\r\n', result)
ethernet_intf_count = int(match.group('ethernet'))
# log the interface counts
logger.info(banner('\'show version\' returns {} ethernet interfaces'
.format(
ethernet_intf_count
)
)
)
# add them to testcase parameters
self.parameters.update(ethernet_intf_count = ethernet_intf_count,
serial_intf_count = 0)
@aetest.test
def verify_interface_count(self,
device,
ethernet_intf_count = 0,
serial_intf_count = 0):
'''
verify interface counts with `show ip interface brief`
Sample of show ip interface brief command result:
show ip interface brief
Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0 unassigned YES unset administratively down down
GigabitEthernet0/1 10.10.10.2 YES manual up up
'''
try:
# store execution result for later usage
result = self.parameters[device].execute('show ip interface brief')
except Exception as e:
# abort/fail the testscript if show ip interface brief command
# returns any exception such as connection timeout or command
# failure
self.failed('Device {} \'show ip interface brief\' failed: '
'{}'.format(device, str(e)),
goto = ['exit'])
else:
# extract ethernet interfaces
ethernet_interfaces = re.finditer(r'\r\nGigabitEthernet\d+/\d+\s+', result)
# total number of ethernet interface
len_ethernet_interfaces = len(tuple(ethernet_interfaces))
# log the ethernet interface counts
logger.info(banner('\'show ip interface brief\' returns {} ethernet'
' interfaces'.format(len_ethernet_interfaces)))
# compare the ethernet interface count between
# `show ip interface brief` and `show version`
assert len_ethernet_interfaces == ethernet_intf_count
class common_cleanup(aetest.CommonCleanup):
'''disconnect from ios routers'''
@aetest.subsection
def disconnect(self, steps, ios1, ios2):
'''disconnect from both devices'''
with steps.start('Disconnecting from Router-1'):
ios1.disconnect()
with steps.start('Disconnecting from Router-2'):
ios2.disconnect()
if ios1.connected or ios2.connected:
# abort/fail the testscript if device connection still exists
self.failed('One of the two devices could not be disconnected from',
goto = ['exit'])
if __name__ == '__main__':
# local imports
import argparse
from ats.topology import loader
parser = argparse.ArgumentParser(description = "standalone parser")
parser.add_argument('--testbed', dest = 'testbed',
type = loader.load)
# parse args
args, unknown = parser.parse_known_args()
# and pass all arguments to aetest.main() as kwargs
aetest.main(**vars(args))