LAB 1-Network Automation Testing pyATS_62722(1)
LAB 1-Network Automation Testing pyATS_62722(1)
Update pyATS testbed file to inclure include the R3 router in this lab
Topology
TopologyThe 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 to each other through
the GigabitEthernet2 and GigabitEthernet3 interfaces.
Job Aid
Device Credentials
R1 Username: cisco
Password: cisco
Device Credentials
R2 Username: cisco
Password: cisco
R3 Username: cisco
Password: cisco
Activity Procedure
Complete the following steps:
[Step 1] Connect to the student-vmJumpbox PC.
[Step 2] Onthe Jumpbox student-vm destopdesktop, start Visual Studio Code by double-
clicking its icon.
Step 1[Step 3] On
the VS Code EXPLORER panel, click the pyATS testbed file
pyats_ios_default.yaml file to open and edit the file in the main panel.
Step 2[Step 4] As
you review the content of the YAML testbed file, notice the two main
sections, testbed and topology. Also, notice the two devices in this topology, R1
and R2.
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:
interfaces:
GigabitEthernet1:
ipv4: 172.21.1.21/24
link: n1
type: ethernet
R2:
interfaces:
GigabitEthernet1:
ipv4: 172.21.1.22/24
link: n1
type: ethernet
[Step 5] Onthe VS Code TERMINAL panel, active the pyATSats virtual environment with
the source /pyats/bin/activate command. Then change the working directory to
pyats-ios with the cd pyats-ios/ command.
Step 3[Step 6] Run the pyats_ios.py script with the command python pyats_ios.py.
[Step 7] On In the Detailed Results section, take note of the main sections. Each section
corresponds to pyATS classes that arethe pyATS classes defined in the pyATS
script file. You will review these classes in the following steps.
+------------------------------------------------------------------------------+
| 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
`-- common_cleanup PASSED
`-- disconnect PASSED
|-- Step 1: Disconnecting from ios device: R1 PASSED
`-- Step 2: Disconnecting from ios device: R2 PASSED
+------------------------------------------------------------------------------+
| Summary |
+------------------------------------------------------------------------------+
Number of ABORTED 0
Number of BLOCKED 0
Number of ERRORED 0
Number of FAILED 0
Number of PASSED 6
Number of PASSX 0
Number of SKIPPED 0
Total Number 6
Success Rate 100.0%
--------------------------------------------------------------------------------
(pyats) student@student-vm:~/pyats-ios-sample$
Step 4[Step 8] Onthe VS Code EXPLORER panel, open the pyATS script file by clicking
pyats_ios.py.
[Step 9] Asyou review the content of the script, notice that each defined class corresponds
to each section of the results that pyats_ios.pyscript's file content, notice that
each defined class corresponds to each section of the results you noted in the
previous step. Also, each function defined in these the classes corresponds to
subsections in the previous result.
Step 5[Step 10] Review the content of the common_setup class.
#
# Common Setup Section
#
class common_setup(aetest.CommonSetup):
'''Common Setup Section
'''
@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
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)
[Step 11] Based on your previous revisionanalysis, provide a brief description of the answer
the following questions about member functions of the common_setup class.
[Step 12] Scrolldown through the pyats_ios.pyscript file and analize analyze the contents
of the PingTestcase class.
#
# Ping Testcase: leverage dual-level looping
#
@aetest.loop(ios_name = ('R1', 'R2'))
class PingTestcase(aetest.Testcase):
'''Ping test'''
@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
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[ios_name]['ios'].ping(destination)
# 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,
)
)
)
[Step 13] Based on your previous arevisionalisys, provide answer the folloing questions
abouta brief description of the following member functions of the pingTestcase
class.
[Step 14] Scrolldown on the pyats_ios.pypyATS script file and analyze the contents of the
VerifyInterfaceCountTestcase class.
#
# Verify Interface Count Testcase
#
class VerifyInterfaceCountTestcase(aetest.Testcase):
'''Verify interface count test'''
@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
<....>
'''
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
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`
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'])
else:
# extract ethernet interfaces
ethernet_interfaces = re.finditer(r'\r\nGigabitEthernet\d+\s+', result)
# total number of ethernet interface
len_ethernet_interfaces = len(tuple(ethernet_interfaces))
[Step 15] Based on your previous revision, provide a brief descriptionthe correct answer toof
the following question about member functions of
VerifyInterfaceCountTestcase class.
What does the extract_interface_count function do?
[A)] It gets device interface number from show version
commandextract_interface_count:
___________________________________________________
[B)]
D) It gets device interface number from show ip interface brief command
E) It gets device interface number from show interface description command
________________________________________________________________________
What does the verify_interface_count function do?
A) It gets device interface number from show version command
B) It gets device interface number from show ip interface brief command
C) It gets device interface number from show interface description command
verify_interface_count:____________________________________________________
________________________________________________________________________
@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'])
Activity Verification
You have completed this task when you obtain the following results:
Successfully review reviewed the pyATS testbed file.
Successfully review reviewed the pyATS script file.
Successfully run the pyATS script file.
Activity Procedure
Complete the following steps:
Step 1 On the =-VS Code EXPLORER panel, click the pyats_ios_default.yaml file.
Step 2 In the topology section, add a second link between R1 and R2. When you finish,
compare your testbed file with the following YAML file:
InterfeceInterface GigabitEthernet2 on R1 topology:
GigabitEthernet2:
ipv4: 10.0.12.1/24
link: n2
type: ethernet
Note: Be carefull with spaces in YAML file, spaces in YAML file needs to be formatted property.
[Step 3] Verify that your testbed file is equal to the following YAML file. Then on VS Code
click the File menu and select Save to save your changespress Ctrl + S to 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:
interfaces:
GigabitEthernet1:
ipv4: 172.21.1.21/24
link: n1
type: ethernet
GigabitEthernet2:
ipv4: 10.0.12.1/24
link: n2
type: ethernet
R2:
interfaces:
GigabitEthernet1:
ipv4: 172.21.1.22/24
link: n1
type: ethernet
GigabitEthernet2:
ipv4: 10.0.12.2/24
link: n2
type: ethernet
Step 1 On VS Code, run the pyats_ios.py script with the python pyats_ios.py
command.
Step 1 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.
+------------------------------------------------------------------------------+
| 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
| |-- ping[destination=10.0.12.1] PASSED
| `-- ping[destination=10.0.12.2] PASSED
|-- PingTestcase[ios_name=R2] PASSED
| |-- setup PASSED
| |-- ping[destination=172.21.1.21] PASSED
| |-- ping[destination=172.21.1.22] PASSED
| |-- ping[destination=10.0.12.1] PASSED
| `-- ping[destination=10.0.12.2] 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
`-- common_cleanup PASSED
`-- disconnect PASSED
|-- Step 1: Disconnecting from ios device PASSED
`-- Step 2: Disconnecting from ios device PASSED
+------------------------------------------------------------------------------+
| Summary |
+------------------------------------------------------------------------------+
Number of ABORTED 0
Number of BLOCKED 0
Number of ERRORED 0
Number of FAILED 0
Number of PASSED 6
Number of PASSX 0
Number of SKIPPED 0
Total Number 6
Success Rate 100.0%
--------------------------------------------------------------------------------
(pyats) student@student-vm:~/pyats-ios-sample$
[Step 2] On In the VS Code main 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.
R3 definition in testbed section:
R3:
connections:
defaults:
class: 'unicon.Unicon'
a:
protocol: ssh
ip: 172.21.1.23
port: 22
type: iosxe
os: iosxe
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
R3:
connections:
defaults:
class: 'unicon.Unicon'
a:
protocol: ssh
ip: 172.21.1.23
port: 22
type: iosxe
os: iosxe
topology:
R1:
interfaces:
GigabitEthernet1:
ipv4: 172.21.1.21/24
link: n1
type: ethernet
GigabitEthernet2:
ipv4: 10.0.12.1/24
link: n2
type: ethernet
GigabitEthernet3:
ipv4: 10.0.13.1/24
link: n3
type: ethernet
R2:
interfaces:
GigabitEthernet1:
ipv4: 172.21.1.22/24
link: n1
type: ethernet
GigabitEthernet2:
ipv4: 10.0.12.2/24
link: n2
type: ethernet
GigabitEthernet3:
ipv4: 10.0.23.1/24
link: n4
type: ethernet
R3:
interfaces:
GigabitEthernet1:
ipv4: 172.21.1.23/24
link: n1
type: ethernet
GigabitEthernet2:
ipv4: 10.0.13.2/24
link: n3
type: ethernet
GigabitEthernet3:
ipv4: 10.0.23.2/24
link: n4
type: ethernet
[Step 4] On the VS Code EXPLORER panel, click the pyats_ios.py file to open the mail
main panel.
Step 3[Step 5] Edit line number 135 to include the R3 router in the loop.
#
# Ping Testcase: leverage dual-level looping
#
@aetest.loop(ios_name = ('R1', 'R2', 'R3'))
class PingTestcase(aetest.Testcase):
'''Ping test'''
[Step 6] Edit line number 338 to include the R3 router name in the ios_name variable.
[Step 1] Afteryou updateupdating the pyats_ios.py filescript, press on VS Code click the
File menu and select SaveCtrl + S to save your changes.
[Step 2] On In the VS Code terminal, run the pyats_ios.py script with the python
pyats_ios.py command.
Step 4[Step 3] 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.
+------------------------------------------------------------------------------+
| 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
| |-- ping[destination=172.21.1.21] PASSED
| |-- ping[destination=172.21.1.22] PASSED
| |-- ping[destination=172.21.1.23] PASSED
| |-- ping[destination=10.0.23.1] PASSED
| `-- ping[destination=10.0.23.2] PASSED
|-- PingTestcase[ios_name=R3] PASSED
| |-- setup PASSED
| |-- ping[destination=10.0.13.1] PASSED
| |-- ping[destination=10.0.13.2] PASSED
| |-- ping[destination=172.21.1.21] PASSED
| |-- ping[destination=172.21.1.22] PASSED
| |-- ping[destination=172.21.1.23] PASSED
| |-- ping[destination=10.0.23.1] PASSED
| `-- ping[destination=10.0.23.2] 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
|-- VerifyInterfaceCountTestcase[device=R3] PASSED
| |-- extract_interface_count PASSED
| `-- verify_interface_count PASSED
`-- common_cleanup PASSED
`-- disconnect PASSED
|-- Step 1: Disconnecting from ios device: R1 PASSED
|-- Step 2: Disconnecting from ios device: R2 PASSED
`-- Step 3: Disconnecting from ios device: R3 PASSED
+------------------------------------------------------------------------------+
| Summary |
+------------------------------------------------------------------------------+
Number of ABORTED 0
Number of BLOCKED 0
Number of ERRORED 0
Number of FAILED 0
Number of PASSED 8
Number of PASSX 0
Number of SKIPPED 0
Total Number 8
Success Rate 100.0%
--------------------------------------------------------------------------------
(pyats) student@student-vm:~/pyats-ios-sample$
Activity Verification
You have completed this task when you obtain the following results:
Successfully updated the pyATS testbed file.
Successfully updated the pyATS script file.
Successfully run the pyATS script file.