7.6.3 Lab - Automated Testing Using PyATS and Genie - ILM
7.6.3 Lab - Automated Testing Using PyATS and Genie - ILM
Version)
Instructor Note: Red font color or gray highlights indicate text that appears in the instructor copy only.
Background / Scenario
In this lab, you will explore the fundamentals pyATS (pronounced "py" followed by each letter individually, "A",
"T". "S") and Genie. The pyATS tool is an end-to-end testing ecosystem, specializing in data-driven and
reusable testing, and engineered to be suitable for Agile, rapid development iterations. Extensible by design,
pyATS enables developers start with small, simple, and linear test cases, and scale towards large, complex,
and asynchronous test suites.
Genie extends and builds on pyATS to be used in a networking environment. Examples of features Genie
provides include:
device connectivity, parsers, and APIs
platform-agnostic Python object models for features such as OSPF and BGP
pool of reusable test cases
YAML-driven test-runner engine
Required Resources
1 PC with operating system of your choice
Virtual Box or VMWare
DEVASC Virtual Machine
CSR1kv Virtual Machine
Instructions
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ ls -1 /usr/bin/python*
/usr/bin/python3
/usr/bin/python3.8
/usr/bin/python3.8-config
/usr/bin/python3-config
/usr/bin/python-argcomplete-check-easy-install-script3
/usr/bin/python-argcomplete-tcsh3
devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
d. Now examine the contents of the venv-created bin subdirectory. Notice there are two files in this
subdirectory, both of which are symlinks. In this case, it is a link to the Python binaries in /usr/bin.
Symlinks are used to link libraries and make sure files there have consistent access to these files without
having to move or create a copy of the original file. There is also a file, activate, that will be discussed
next.
devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ ls -l bin
total 44
-rw-r--r-- 1 devasc devasc 2225 May 31 16:07 activate
-rw-r--r-- 1 devasc devasc 1277 May 31 16:07 activate.csh
-rw-r--r-- 1 devasc devasc 2429 May 31 16:07 activate.fish
-rw-r--r-- 1 devasc devasc 8471 May 31 16:07 Activate.ps1
-rwxrwxr-x 1 devasc devasc 267 May 31 16:07 easy_install
-rwxrwxr-x 1 devasc devasc 267 May 31 16:07 easy_install-3.8
-rwxrwxr-x 1 devasc devasc 258 May 31 16:07 pip
-rwxrwxr-x 1 devasc devasc 258 May 31 16:07 pip3
-rwxrwxr-x 1 devasc devasc 258 May 31 16:07 pip3.8
lrwxrwxrwx 1 devasc devasc 7 May 31 16:07 python -> python3
lrwxrwxrwx 1 devasc devasc 16 May 31 16:07 python3 -> /usr/bin/python3
devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
e. Launch the virtual environment using bin/activate. Notice your prompt is now preceded with (csr1kv). All
the commands done from this point on are within this venv.
devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ source bin/activate
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
Note: The deactivate command is used to exit the venv environment and return to the normal shell
environment.
<output omitted>
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
Commands:
create create scripts and libraries from template
diff Command to diff two snapshots saved to file or directory
dnac Command to learn DNAC features and save to file (Prototype)
learn Command to learn device features and save to file
logs command enabling log archive viewing in local browser
parse Command to parse show commands
run runs the provided script and output corresponding results.
secret utilities for working with secret strings.
shell enter Python shell, loading a pyATS testbed file and/or
pickled data
validate utlities that helps to validate input files
version commands related to version display and manipulation
General Options:
-h, --help Show help
Step 3: Clone and examine the pyATS sample scripts from GitHub.
a. Clone the Github pyATS sample scripts repository CiscoTestAutomation.
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ git clone
https://github.com/CiscoTestAutomation/examples
Cloning into 'examples'...
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 658 (delta 11), reused 18 (delta 4), pack-reused 623
Receiving objects: 100% (658/658), 1.00 MiB | 4.82 MiB/s, done.
Resolving deltas: 100% (338/338), done.
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
b. Verify the copy was successful by listing the files in the current directory. Notice there is a new
subdirectory example.
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ ls -l
total 24
drwxrwxr-x 2 devasc devasc 4096 May 31 16:07 bin
drwxrwxr-x 21 devasc devasc 4096 May 31 16:47 examples
drwxrwxr-x 2 devasc devasc 4096 May 31 16:07 include
drwxrwxr-x 3 devasc devasc 4096 May 31 16:07 lib
lrwxrwxrwx 1 devasc devasc 3 May 31 16:07 lib64 -> lib
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
# of aetest.
###################################################################
###################################################################
### COMMON SETUP SECTION ###
###################################################################
class common_setup(aetest.CommonSetup):
""" Common Setup section """
# First subsection
@aetest.subsection
def sample_subsection_1(self):
""" Common Setup subsection """
log.info("Aetest Common Setup ")
# Second subsection
@aetest.subsection
def sample_subsection_2(self, section):
""" Common Setup subsection """
log.info("Inside %s" % (section))
###################################################################
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
@ aetest.test
def simple_test_1(self):
""" Sample test section. Only print """
log.info("First test section ")
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
#####################################################################
#### COMMON CLEANUP SECTION ###
#####################################################################
@aetest.subsection
def clean_everything(self):
""" Common Cleanup Subsection """
log.info("Aetest Common Cleanup ")
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
import os
from pyats.easypy import run
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
Pro Tip
-------
Use the following command to view your logs locally:
pyats logs view
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
Commands:
create Create Testbed, parser, triggers, ...
diff Command to diff two snapshots saved to file or directory
dnac Command to learn DNAC features and save to file (Prototype)
learn Command to learn device features and save to file
parse Command to parse show commands
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
General Options:
-h, --help Show help
Subcommands:
parser create a new Genie parser from template
testbed create a testbed file automatically
trigger create a new Genie trigger from template
General Options:
-h, --help Show help
-v, --verbose Give more output, additive up to 3 times.
-q, --quiet Give less output, additive up to 3 times, corresponding to
WARNING, ERROR,
and CRITICAL logging levels
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
b. To create your testbed YAML file, enter the command below. The --output parameter will create a
testbed.yml file in a directory named yaml. The directory will be automatically created. The --encode-
password parameter will encode the passwords in the YAML file. The parameter interactive means you
will be asked a series of questions. Answer no to the first three questions. And then provide the following
answers to create the testbed.yaml file.
Device hostname - This must match the hostname of the device, which for this lab is CSR1kv.
IP address - This must match your CSR1kv IPv4 address you discovered earlier in this lab. Shown
here is 192.168.56.101.
Username - This is the local username used for ssh, which is cisco.
Default password - This is the local password used for ssh, which is cisco123!.
Enable password - Leave blank. There is no privileged password configured on the router.
Protocol - SSH along with the key exchange group expected by the router.
OS - The OS on the router.
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ genie create testbed
interactive --output yaml/testbed.yml --encode-password
Start creating Testbed yaml file ...
Do all of the devices have the same username? [y/n] n
Do all of the devices have the same default password? [y/n] n
Do all of the devices have the same enable password? [y/n] n
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
c. Use cat to view the testbed.yml file in the yaml directory. Notice your entries in the YAML file. Your SSH
password is encrypted and the enable password will "ASK" the user to enter the password if one is
required.
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ cat yaml/testbed.yml
devices:
CSR1kv:
connections:
cli:
ip: 192.168.56.101
protocol: ssh -o KexAlgorithms=diffie-hellman-group14-sha1
credentials:
default:
password: '%ENC{w5PDosOUw5fDosKQwpbCmMKH}'
username: cisco
enable:
password: '%ASK{}'
os: iosxe
type: iosxe
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
Step 2: Use Genie to parse output from the show ip interface brief command into JSON.
a. If you have not already completed the Lab - Install the CSR1kv VM, do so now. If you have already
completed that lab, launch the CSR1kv VM now.
b. In the CSR1kv VM, enter the command show ip interface brief from privileged exec mode. Your address
may be incremented to some other address other than 192.168.56.101. Make note of the IPv4 address
for your CSR1kv VM. You will use it later in the lab.
CSR1kv> en
CSR1kv# show ip interface brief
Interface IP-Address OK? Method Status Protocol
GigabitEthernet1 192.168.56.101 YES DHCP up up
CSR1kv#
c. Using your testbed YAML file, invoke Genie to parse unstructured output from the show ip interface
brief command into structured JSON. This command includes the IOS command to be parsed (show ip
interface brief), the YAML testbed file (testbed.yml), and the specified device in the testbed file
(CSR1kv).
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ ^C
Step 3: Use Genie to parse output from the show version command into JSON.
For another example, parse unstructured output from the show version command into structured JSON.
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ genie parse "show
version" --testbed-file yaml/testbed.yml --devices CSR1kv
Enter enable password for device CSR1kv: <Enter>
2020-05-31T18:41:32: %UNICON-WARNING: Device 'CSR1kv' connection 'cli' does not have
IP and/or port specified, ignoring
Device 'CSR1kv' connection 'cli' does not have IP and/or port specified, ignoring
0%|
| 0/1 [00:00<?, ?it/s]{
"version": {
"chassis": "CSR1000V",
"chassis_sn": "9K8P1OFYE3D",
"compiled_by": "mcpre",
"compiled_date": "Thu 30-Jan-20 18:48",
"curr_config_register": "0x2102",
"disks": {
"bootflash:.": {
"disk_size": "7774207",
"type_of_disk": "virtual hard disk"
},
"webui:.": {
"disk_size": "0",
"type_of_disk": "WebUI ODM Files"
}
},
"hostname": "CSR1kv",
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
"image_id": "X86_64_LINUX_IOSD-UNIVERSALK9-M",
"image_type": "production image",
"last_reload_reason": "reload",
"license_level": "ax",
"license_type": "Default. No valid license found.",
"main_mem": "2182252",
"mem_size": {
"non-volatile configuration": "32768",
"physical": "3985032"
},
"next_reload_license_level": "ax",
"number_of_intfs": {
"Gigabit Ethernet": "1"
},
"os": "IOS-XE",
"platform": "Virtual XE",
"processor_type": "VXE",
"returned_to_rom_by": "reload",
"rom": "IOS-XE ROMMON",
"rtr_type": "CSR1000V",
"system_image": "bootflash:packages.conf",
"uptime": "2 days, 6 hours, 26 minutes",
"uptime_this_cp": "2 days, 6 hours, 27 minutes",
"version": "16.9.5",
"version_short": "16.9"
}
}
100%|█████████████████████████████████████████████████████████████████████████████|
1/1 [00:00<00:00, 2.06it/s]
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 16 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 17 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
"joined_group_addresses": [
"FF02::1",
"FF02::1:FF00:101",
"FF02::1:FF73:D79F"
],
"mtu": 1500,
"oper_status": "up"
},
"_exclude": []
}
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 18 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
b. List the files created by Genie in the directory verify-ipv6-2. These are similar to the two files you created
before changing the IPv6 link-local address.
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ ls -l verify-ipv6-2
total 16
-rw-rw-rw- 1 devasc devasc 4536 May 31 20:04 connection_CSR1kv.txt
-rw-rw-r-- 1 devasc devasc 728 May 31 20:04 CSR1kv_show-ipv6-interface-gig-
1_console.txt
-rw-rw-r-- 1 devasc devasc 846 May 31 20:04 CSR1kv_show-ipv6-interface-gig-
1_parsed.txt
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
c. Use the cat to examine the contents each file. The changes are highlighted in the output below.
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ cat verify-ipv6-
2/CSR1kv_show-ipv6-interface-gig-1_console.txt
+++ CSR1kv: executing command 'show ipv6 interface gig 1' +++
show ipv6 interface gig 1
GigabitEthernet1 is up, line protocol is up
IPv6 is enabled, link-local address is FE80::56:1
No Virtual link-local address(es):
Description: VBox
Global unicast address(es):
2001:DB8:ACAD:56::101, subnet is 2001:DB8:ACAD:56::/64
Joined group address(es):
FF02::1
FF02::1:FF00:101
FF02::1:FF56:1
MTU is 1500 bytes
ICMP error messages limited to one every 100 milliseconds
ICMP redirects are enabled
ICMP unreachables are sent
ND DAD is enabled, number of DAD attempts: 1
ND reachable time is 30000 milliseconds (using 30000)
ND NS retransmit interval is 1000 milliseconds
CSR1kv#
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$ cat verify-ipv6-
2/CSR1kv_show-ipv6-interface-gig-1_parsed.txt
{
"GigabitEthernet1": {
"enabled": true,
"ipv6": {
"2001:DB8:ACAD:56::101/64": {
"ip": "2001:DB8:ACAD:56::101",
"prefix_length": "64",
"status": "valid"
},
"FE80::56:1": {
"ip": "FE80::56:1",
"origin": "link_layer",
"status": "valid"
},
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 19 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
"enabled": true,
"icmp": {
"error_messages_limited": 100,
"redirects": true,
"unreachables": "sent"
},
"nd": {
"dad_attempts": 1,
"dad_enabled": true,
"ns_retransmit_interval": 1000,
"reachable_time": 30000,
"suppress": false,
"using_time": 30000
}
},
"joined_group_addresses": [
"FF02::1",
"FF02::1:FF00:101",
"FF02::1:FF56:1"
],
"mtu": 1500,
"oper_status": "up"
},
"_exclude": []
}
(csr1kv) devasc@labvm:~/labs/devnet-src/pyats/csr1kv$
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 20 of 21 www.netacad.com
Lab - Automated Testing Using pyATS and Genie
2020 - 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 21 of 21 www.netacad.com