LAB 1 – Network Automation Testing pyATS_0714c
LAB 1 – Network Automation Testing pyATS_0714c
Update the pyATS testbed file to include the R3 router in this lab
Update the pyATS script file to include the R3 router in this lab
Topology
The topology for this lab includes three routers, R1, R2, and R3. These routers are connected to a
management network by the GigabitEthernet1 interface. Also, they connect through the
GigabitEthernet2 and GigabitEthernet3 interfaces.
Device Credentials
R1 Username: cisco
Password: cisco
R2 Username: cisco
Password: cisco
R3 Username: cisco
Password: cisco
Activity Procedure
Complete the following steps:
Step 1 Connect to the student-vm.
Navigate to topology page and click student-vm icon, login with credential
provided in Job Aid
Step 3 Open the pyATS testbed file pyats_ios_default.yaml and edit the file in the
main panel.
Here is an example:
testbed:
name: pyATS_IOS_Example_Testbed
credentials:
default:
username: cisco
password: cisco
devices:
R1:
connections:
defaults:
class: 'unicon.Unicon'
a:
protocol: ssh
ip: 172.21.1.21
port: 22
type: iosxe
os: iosxe
R2:
connections:
defaults:
Step 5 On the VS Code TERMINAL panel, activate the pyATS virtual environment
/pyats/. Then, change the working directory to pyats-ios directory.
Step 6 On the VS Code TERMINAL panel, use source /pyats/bin/activate command to
activate the pyATS virtual environment. Then, use cd pyats-ios/ command to
change the working directory to pyats-ios.
Step 8 In the Detailed Results section, take note of the main sections. Each section
corresponds to the pyATS classes that are defined in the pyATS script file. You
will analyze these classes in the following steps.
Here is an example:
+------------------------------------------------------------------------------+
| Detailed Results |
+------------------------------------------------------------------------------+
SECTIONS/TESTCASES RESULT
--------------------------------------------------------------------------------
.
|-- common_setup PASSED
| |-- check_topology PASSED
| |-- establish_connections PASSED
| | |-- Step 1: Connecting to ios device: R1 PASSED
| | `-- Step 2: Connecting to ios device: R2 PASSED
| `-- marking_interface_count_testcases PASSED
|-- PingTestcase[ios_name=R1] PASSED
| |-- setup PASSED
| |-- ping[destination=172.21.1.21] PASSED
| `-- ping[destination=172.21.1.22] PASSED
|-- PingTestcase[ios_name=R2] PASSED
| |-- setup PASSED
| |-- ping[destination=172.21.1.21] PASSED
| `-- ping[destination=172.21.1.22] PASSED
|-- VerifyInterfaceCountTestcase[device=R1] PASSED
| |-- extract_interface_count PASSED
| `-- verify_interface_count PASSED
|-- VerifyInterfaceCountTestcase[device=R2] PASSED
| |-- extract_interface_count PASSED
| `-- verify_interface_count PASSED
'''
@aetest.subsection
def check_topology(self, testbed, ios_names):
'''
check that we have at least two devices and a link between the devices
If so, mark the next subsection for looping.
'''
@aetest.subsection
def establish_connections(self, steps, ios_names):
'''
establish connection to both devices
'''
for ios_name in ios_names:
with steps.start('Connecting to ios device: %s'%(ios_name)):
self.parent.parameters[ios_name]['ios'].connect()
# abort/fail the testscript if any device isn't connected
if not self.parent.parameters[ios_name]['ios'].connected:
self.failed('One of the 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
10
logger.info(banner('Looping VerifyInterfaceCountTestcase'
' for {}'.format(devices)))
Step 12 Based on your previous analysis, answer the following questions about member
functions of the common_setup class.
Step 13 Scroll through the pyats_ios.py file, analyze the contents of the PingTestcase
class and save the name of class member functions.
11
@aetest.setup
def setup(self, ios_name):
destination = []
for link in self.parent.parameters[ios_name]['links']:
@aetest.test
def ping(self, ios_name, destination):
'''
ping destination ip address from device
ping
Protocol [ip]:
Target IP address: 10.10.10.2
Repeat count [5]:
Datagram size [100]:
Timeout in seconds [2]:
Extended commands [n]: n
12
'''
try:
# store command result for later usage
result = self.parameters[ios_name]['ios'].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,
)
)
)
Step 14 Based on your previous analysis, answer the following questions about member
functions of the pingTestcase class.
13
@aetest.test
def extract_interface_count(self, device):
'''
extract interface counts from `show version`
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
14
<....>
'''
try:
# store execution result for later usage
result = self.parameters[device]['ios'].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
15
)
)
)
# 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`
try:
# store execution result for later usage
result = self.parameters[device]['ios'].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'])
16
Step 16 Based on your previous analysis, provide the correct answer to the following
question about member functions of the VerifyInterfaceCountTestcase class.
What action does the extract_interface_count function take?
A) It gets the device interface number from the show version command.
B) It gets the device interface number from the show ip interface brief command.
C) It gets the device interface number from the show interface description command.
D) It gets the device interface number from the show inventory command.
Answer (A)
17
@aetest.subsection
def disconnect(self, steps, ios_names):
'''disconnect from both devices'''
for ios_name in ios_names:
with steps.start('Disconnecting from ios device: %s'%(ios_name)):
self.parameters[ios_name]['ios'].disconnect()
if self.parameters[ios_name]['ios'].connected:
# abort/fail the testscript if device connection still exists
self.failed('One of the devices could not be disconnected from',
goto = ['exit'])
Step 18 Based on your previous analysis, provide the correct answer to the following
question about member functions of the common_cleanup class.
What action does the disconnect function take?
A) It disconnects from all devices.
B) It disconnects from all devices and verify disconnection was successful.
C) It disconnects from all devices and connect to again.
D) It disconnects from all devices and waits until connections reestablish.
Answer (B)
Activity Verification
You have completed this task when you obtain the following results:
You successfully reviewed the pyATS testbed file.
You successfully reviewed the pyATS script file.
You successfully ran the pyATS script file.
18
Activity Procedure
Complete the following steps:
Step 1 On the VS Code, Edit the testbed file.
On the VS Code EXPLORER panel, click the pyats_ios_default.yaml file.
Step 2 In testbed file, in the topology section, add a second link between R1 and R2.
Use information provided in Job Aid.
Note: Be careful with spaces in the YAML file, as spaces in YAML files must be formatted properly.
19
Step 3 Verify that your testbed file is equal to the following YAML file. Then, save your
changes.
testbed:
name: pyATS_IOS_Example_Testbed
credentials:
default:
username: cisco
password: cisco
devices:
R1:
connections:
defaults:
class: 'unicon.Unicon'
a:
protocol: ssh
ip: 172.21.1.21
port: 22
type: iosxe
os: iosxe
R2:
connections:
defaults:
class: 'unicon.Unicon'
a:
protocol: ssh
ip: 172.21.1.22
port: 22
type: iosxe
os: iosxe
topology:
R1:
20
On VS Code, click the File menu and click Save to save your changes.
Step 4 On VS Code, run the pyats_ios.py script.
On VS Code, use python pyats_ios.py command.
Step 5 From the Detailed Results section, notice that pyATS automatically discovered
the new link between R1 and R2 and includes the IP addresses for the interfaces
in the ping test.
Here is an example:
+------------------------------------------------------------------------------+
| Detailed Results |
+------------------------------------------------------------------------------+
SECTIONS/TESTCASES RESULT
--------------------------------------------------------------------------------
.
|-- common_setup PASSED
| |-- check_topology PASSED
21
22
Step 6 In the VS Code editor panel, edit the testbed file to add a new router, R3. Also,
edit the topology section to include a link between R3 and the R1 and R2 routers.
Use information provided in Job Aid.
Note Be careful with spaces in the YAML file, as spaces in YAML files must be formatted
properly.
23
Step 7 Verify that your testbed file is equal to the following YAML file. Then save your
changes.
testbed:
name: pyATS_IOS_Example_Testbed
credentials:
default:
username: cisco
password: cisco
devices:
R1:
connections:
defaults:
class: 'unicon.Unicon'
a:
protocol: ssh
ip: 172.21.1.21
24
25
On VS Code, click the File menu and click Save to save your changes.
Step 8 On the VS Code, open the pyats_ios.py file.
On the VS Code EXPLORER panel, click the pyats_ios.py file to open the main
panel.
26
Step 10 Edit line number 338 to include the R3 router name in the ios_name variable.
Here is an example:
from ats.topology import loader
parser = argparse.ArgumentParser(description = "standalone parser")
parser.add_argument('--ios', dest = 'ios_names', type = list, default = ['R1',
'R2', 'R3'])
parser.add_argument('--testbed', dest = 'testbed', type = loader.load, default =
'pyats_ios_default.yaml')
# parse args
Step 11 After updating the pyats_ios.py file, on VS Code, save your changes.
On VS Code, click the File menu and select Save to save your changes.
Step 12 In the VS Code terminal, run the pyats_ios.py script.
In the VS Code terminal, use the python pyats_ios.py command to run the
pyats_ios.py script.
27
+------------------------------------------------------------------------------+
| Detailed Results |
+------------------------------------------------------------------------------+
SECTIONS/TESTCASES RESULT
--------------------------------------------------------------------------------
.
|-- common_setup PASSED
| |-- check_topology PASSED
| |-- establish_connections PASSED
| | |-- Step 1: Connecting to ios device: R1 PASSED
| | |-- Step 2: Connecting to ios device: R2 PASSED
| | `-- Step 3: Connecting to ios device: R3 PASSED
| `-- marking_interface_count_testcases PASSED
|-- PingTestcase[ios_name=R1] PASSED
| |-- setup PASSED
| |-- ping[destination=10.0.13.1] PASSED
| |-- ping[destination=10.0.13.2] PASSED
| |-- ping[destination=10.0.12.1] PASSED
| |-- ping[destination=10.0.12.2] PASSED
| |-- ping[destination=172.21.1.21] PASSED
| |-- ping[destination=172.21.1.22] PASSED
| `-- ping[destination=172.21.1.23] PASSED
|-- PingTestcase[ios_name=R2] PASSED
| |-- setup PASSED
| |-- ping[destination=10.0.12.1] PASSED
| |-- ping[destination=10.0.12.2] PASSED
28
29
Activity Verification
You have completed this task when you obtain the following results:
You successfully updated the pyATS testbed file.
You successfully updated the pyATS script file.
You successfully ran the pyATS script file.
30