Course 14 - Students Guide
Course 14 - Students Guide
Introduction to pyATS
pyATS - Python Automated Test Systems is a Python3 based test automation infrastructure. It
is developed as the next-generation Cisco mainstream automation infrastructure, is
compatible to existing tooling & infrastructure, and is officially endorsed by Cisco executive
team.
pyATS is 100% fully developed and implemented using Python and is designed to be usable
independent of any other existing infrastructure. It is engineered to be suitable for agile, rapid
development iterations, with the ability to handle a wide variety of testing scenarios and
requirements, from white-box unit-testing, to black-box, feature, performance/scale testing &
beyond.
Release to DevNet
pyATS started its journey initially as an internal Cisco Engineering tool. Over the years, it
amassed thousands of test developers and millions of lines of test scripts and libraries. As
pyATS gathered momentum within Cisco, teams began inquiring whether it was possible to
share their automation with customers as part of collaborative development and release.
As a result, pyATS has been made available externally to Cisco’s end customers, through
DevNet. Today, pyATS is downloadable through Python PyPI:
What is pyATS?
pyATS is the test framework foundation for the ecosystem. pyATS specializes in data-driven
and reusable testing for agile, rapid development iterations.
This powerful, highly pluggable Python® framework enables developers to start with small,
simple and linear test cases, and then scale up to large, complex and asynchronous test suites.
pyATS Key Benefits
The pyATS ecosystem provides an agnostic infrastructure for rapid test development and
execution. All OS/Platform and management protocol support is defined and injected through
plugins, library implementations, and extensions. It includes support for third-party platforms
and protocols.
Network engineers and NetDevOps can be productive at day one with the pyATS Library’s
readily available, holistic, and model-driven libraries, which:
Connect to devices and make sure that they are up, running and pingable.
Retrieve information about the network state.
Take before and after snapshots of the network, and then compare them.
Parse device output and store a snapshot, and then automatically run the same
commands at specified time intervals to compare the current and previous states.
Use an available test case to verify a stable network topology after an image upgrade,
for example.
Perform typical actions such as Reload Devices, Perform Switchover, ShutNoShutBgp,
and ConfigUnconfigOspf.
pyATS Ecosystem
pyATS and the pyATS Library together define an ecosystem that streamlines and standardizes
how you set up and run automated network tests. pyATS and the pyATS library provide sanity,
feature, solution, system, and scale test automation for any type of device or virtual device.
pyATS is currently used with devices such as routers and switches, access points, firewalls,
Linux servers, phones, cable CPEs, and many more.
Originally developed for internal Cisco engineers, the pyATS ecosystem is at the core of Cisco’s
Test Automation Solution. It’s currently used by Cisco engineering, DevNet engineers, network
engineers, and developers.
The pyATS ecosystem empowers your team to create and run consistent, repeatable, iterative,
and reusable tests. pyATS provides the test framework, and the pyATS Library offers ready-to-
use test components.
The pyATS ecosystem can learn and profile an entire feature’s configuration and operational
status. For example, with just a few commands, you can profile your system before and after a
configuration change and get a detailed summary of exactly what changed.
The pyATS Library (a.k.a. Genie) provides all the tools needed for network test automation,
including
Use the pyATS Library when you want to automate your day-to-day DevOps activities, perform
stateful validation of your network devices, or build a safety net of scalable, data-driven, and
reusable test cases around your network requirements. You can:
Profile the current state of your network and take a snapshot for later comparison.
Set up automated monitoring of the operational state of your devices.
Automate configuration and upgrade tasks.
Introduce changes – such as new products or releases – and see the effects on your
network.
Use the power of our Pythonic library to efficiently write your own, reusable, OS-
agnostic scripts.
Run a Testscript
Create a Testbed
Because we are in the business of network test automation, pyATS is designed around the
concept of testbeds: where you describe your devices under testing in YAML format.
This testbed YAML file provides many sections for you to describe your physical devices, and
how they link together to form the topology.
Once a testbed YAML file is written, you can load it, query your topology, connect & issue
commands to your devices using the APIs.
This is the best way to validate whether your topology file is well formed, and your devices
connectable.
Create a Testbed
The devices block contains a description of each network device and must include the
following keys. (Platform is recommended but not required.)
Key Description
hostname This must be the configured hostname of the device.
alias The pyATS Library uses the alias to identify the device during script
execution. This makes the script reusable on another topology, when a
device is assigned the same alias, such as uut (unit under test).
os Device operating system
platform (Recommended) The name of the hardware or chassis, or the name of the
virtual hardware. The system uses the os/platform values to determine the
connection plugin and services.
credentials The username, password, and any other credentials required to log in to
the device.
The following example shows a YAML file with two devices defined:
1. The device name must match the hostname of the device, otherwise, the connection
will hang.
2. At least one device needs to have the alias ‘uut’ in the testbed YAML file.
You can define all your device data in a CSV (.csv) or Excel (.xls, .xlsx) file. The pyATS create
testbed command automatically converts the input and creates an equivalent YAML file.
You can also create a testbed YAML from an excel sheet with pyATS create testbed command,
here’s an example:
The following example shows an Excel file with the required columns. Here is an excel sheet
containing device data:
we can turn it into a testbed YAML file by running the following command:
Genie testbed also support creating a testbed directly from a dictionary without a YAML file.
This feature is convenient for quick testing without complicated configurations, and it allows
seamless transition from JSON if the device data obtained from a remote server.
The below code shows the minimum data required for a successful creation, please consult
pyATS create testbed for the minimum key requirement.
Handling Connection to Devices
This topic describes how to connect to network devices using pyATS. It also gives you a quick
example to try using mocked devices.
Supported Platforms
At the moment unicon.plugins supports the following network device types, described as their
OS (network operation system), platform and model (specific model support).
These values help Unicon load the most accurate connection plugin for the given network
device and corresponds to the pyATS testbed YAML counterparts.
For example, if os=iosxe and platform=abc, since abc is not found in the iosxe table, it will fall
back to use the generic iosxe plugin. If os=iosxe and platform=cat3k, it will use the specific
plugin iosxe/cat3k.
1. Load the pyATS Library testbed API so that you can create the testbed and device
objects:
2. Create a testbed object tb based on your testbed YAML file. Specify the absolute or
relative path, in this case, mock/mock.yaml:
Result: The system creates a variable tb that points to the testbed object. This
command also creates tb.devices, which contains the YAML device information in the
form of key-value pairs.
3. Create an object dev for the device that you want to connect to:
Result: The pyATS Library finds the device named nx-osv-1 in tb.devices and stores the
information in the dev object.
Result: The system connects to the device and displays the connection details. Once
you’re connected, you can run show commands and parse the output.
Depending on your device terminal settings, when you connect to a device using a CLI and
execute a command, you would sometimes see “press any key to continue”. For humans, this
breakpoint gives a possibility to analyze output. However, from automation point of view it
would break parsers, as they change output data.
To avoid those, Unicon (#pyATS connection implementation) issues the following commands
on connection established:
no logging console
terminal width 511
etc (depending on platform, which can be specified in testbed file)
All these commands affect the terminal behavior, not your device’s functionality.
Here are some use cases where these objects are useful:
This topic describes how to use the Python interpreter or a Python script to use the Conf
module functionality. Because you use it primarily for automated test scripts, we have not
provided a command line option.
This example shows you how to configure a single feature on a single device. You can use the
device hostname or the device alias (defined in the testbed YAML file). In the following
example, uut is the alias “unit under test” for the host nx-osv-1.
1. Get the pyATS Library Interface functionality, to configure an interface on the uut
device:
The argument (apply=False) shows you what will be applied on the device if you go
ahead with the build. The system displays the following configuration information:
Think of what you want to apply on your device, not how to do it on this platform. Genie conf
provides a structure which is feature based and not platform based. This mean, it stays the
same for all platform and figure out how to apply on the device.
Once you have this structured data you can do so much with it:
Instead of learning a few cli at the time, you can learn the whole feature and have it into 1
structured output (JSON/Dictionary). This structure is agnostic between all OS (Identical
between all the OS).
Profile the Network
This topic describes how to use the learn function of the pyATS Library Ops module for stateful
network validation of device features, such as protocols, interfaces, line cards, and other
hardware.
The term feature typically refers to a network protocol, represented by the pyATS Library as a
Python object, with attributes that represent the feature (protocol) configuration on a device.
Many networks use a combination of different features, such as BGP, OSPF, and Interface.
The pyATS Library Ops module provides a representation of the current operational state of a
device, per feature (protocol). It “learns” the operational state by executing a series of
commands and parsing the output into a common data structure across different operating
systems.
The output is stored with the same key-value pair structure across devices. The stored output
makes it possible for you to take a snapshot of the network state at different points in time,
and then to compare network states.
Why use learn instead of a parser? The parsed output for different devices can result in
different data structures. The learn function, by contrast, results in a consistent set of keys,
which means that you can write one script that works on different devices.
Learn Examples
This topic describes how you can tell the system to learn one or more features.
To learn one feature on a single device, you can use the device hostname or the device alias
(defined in the testbed YAML file). In the following example, uut is the alias “unit under test”
for the host nx-osv-1.
Use pyATS shell to load the testbed API and create your testbed and device objects. Then,
connect to the device and tell the system to learn the feature. In this example, the system
stores the output as a Python dictionary in the variable output:
Use pyATS shell to load the testbed API and create your testbed and device objects. Then, tell
the system to connect to each device and to learn the specified features. In this example, the
system stores the output as a Python dictionary in the variable learnt and displays the output:
This example uses a Python for loop to execute each statement on all devices in the testbed.
The system stores the feature information in Python dictionaries, each identified by the device
name.
Use the learn all functionality to learn all the supported features on a device. The system
returns the results in a format with key-value pairs and notifies you of any exceptions for
features it did not learn.
Use pyATS shell to load the testbed API and create your testbed and device objects. Then,
connect to the device and tell the system to learn all of the features. In this example, the
system stores the output as a Python dictionary in the variable output:
Ops object learns everything to know about the operation state of a device feature such as
OSPF, interface, etc.
This sends multiple commands on the device and creates a structure for all the information.
Compare Network State
This topic describes how to use the Diff functionality to monitor the network and to verify that
network is performing as expected.
The pyATS Library Diff functionality enables you to compare network snapshots so that it is
easy to see any changes. For any two Python dictionaries or objects with a consistent data
structure (the set of key-value pairs), the system compares the keys and identifies differences.
You can exclude keys that you don’t want to compare.
Using the pyATS Library, you can collect and compare network state information on demand,
or as scheduled jobs that run your automation scripts. The process is simple:
For example, you can set up a scheduled job to take a new snapshot every day, to make sure
your network is running as it should and to see any configuration changes. The result is a Linux-
style list of additions, deletions, and changes.
In this example, you’ll see how to take snapshots with the learn function, save the snapshots
to different directories, and then compare the data.
1. With your devices already configured and running, take a snapshot of the BGP feature
and save it to the directory (or variable) output1. You can use a Python interpreter or
the Library command line.
Use pyATS shell to load the testbed API and create your testbed and device objects. Then, tell
the system to connect to each device and to learn the specified feature:
This example uses a Python for loop to execute each statement on all devices in the testbed.
Result: The system stores the feature information in a Python dictionary, which includes the
data for all devices.
In this section we will take a look at how pyATS CLI can help expedite network automation.
pyATS CLI is a powerful Linux-based command-line utility offering Genie Python functionality
directly from a Linux terminal. It requires no previous knowledge of Python or network
programming, making it a great way to start getting acquainted with Genie.
Genie is the top-level command-line entry point for Genie. All other functions are loaded as
subcommands of this command. Ensure Genie is installed prior to using genie CLI.
For a complete list of built-in functions available within genie CLI, execute the following in your
Linux terminal:
Genie Parse
Genie parse is used to execute and parse show-commands on your device directly from a Linux
terminal.
The following is an example of using genie parse to execute and parse command “show
version” on device nx-osv-1 of the testbed.
Genie Learn
genie learn is a powerful tool that can be extensively used to accomplish stateful network
validation across multiple devices, with one Linux command.
The following is an example of how to learn one feature (BGP) on the device uut:
Genie Diff
genie diff is used to compare Python data structures generated by genie parse and genie learn.
It is extremely useful in performing stateful network validation before and after any event
occurs in a network that could cause the operational state to change.
The following is an example of using genie diff with the default exclude list during comparison:
With pyATS, we’ve taken this complex topic and made it simple. We’ve taken a divide and
conquer approach to this problem. Multiple packages and libraries were created with Genie
harness being the last piece to create a resilient and future-proof automation solution. In
short, we’ve made lasagna with many modules stacked on top of each other.
Genie harness provides comprehensive common tools so that users can focus on writing tests
without having to reinvent the wheel!
Once the tests are written, the harness is then driven by YAML datafiles. The files control the
whole flow of the execution:
Configure a device.
Parse the device output to verify if the device state is as expected.
Unconfig or modify the initial configuration.
Learn a feature and verify the result of the action
Calling different APIs and use their outputs on other actions and other devices
Yang integration
It is fully customizable and new actions can be added
To use the Blitz, add the YAML content to a trigger datafile, or create a new YAML file if you
don’t have one. Using a job file execute tests in the given order. Each trigger datafile can
contains of multiple testcases.
Introduce Network Visualization with Expresso
XPRESSO is the standard pyATS UI dashboard that manages test suites, test resources and test
results, providing network insights through Cisco pyATS.
XPRESSO provides a unique set of at-a-glance, simple-to-use set of features to help to prepare,
schedule, and execute network test automation scripts while providing a comprehensive and
user-friendly way to manage and search test results so users can quickly isolate, interpret, and
analyze test results.
XPRESSO provides an all-in-one web-based dashboard that takes the pyATS experience to the
next level by drastically reducing network testing/certification/validation cycle-time by: