Example: Creating the Junos Snapshot Administrator in Python Test Files
This example demonstrates how to create some basic Junos Snapshot Administrator in Python (jsnapy) test files. The examples in this topic use specific numbers of spaces sometimes before any other text is entered on a line. These spaces are required for proper formatting of the YAML file.
Requirements
Junos Snapshot Administrator in Python Release 1.0 is installed on the server.
Overview
This topic describes how to create two test files using various elements available for test file creation:
A test file named
test_sw_version.yml
that just captures the Junos OS software release versionA test file named
filter_config_info.yml
that requests filtered information from theget_config
RPC command.
Jsnapy test files contain the detailed commands, tests, hierarchy locations, and test elements necessary to create or compare snapshots of the runtime environments of Junos OS devices. You can create as many test files as you want, store them wherever you want on the jsnapy server, and name them as you see fit (with the exception of spaces and special characters). There are two primary parts to a jsnapy test file: the named list of tests under a tests_include heading, and the detail sections, one detail section for each named test under the tests_include section.
Comments can be inserted into the test file at any location by starting the comment with the pound sign (#).
Capture Junos OS Software Release Version
Procedure
Step-by-Step Procedure
For this example, use a text editor to create a new file
named test_sw_version.yml
. Create the file in the same
directory as the configuration file that you intend to use or in the /etc/jsnapy/testfiles/
directory.
Create the tests_include section.
tests_include:
Set the test name.
- test_sw_version
Start the test details section.
test_sw_version:
Set the command to be sent. In this case it is the Junos OS command
show version
.- command: show version
Specify whether your tests look for the first instance of a value or all instances of a value. In this case we are looking for only the first instance.
- item:
Specify the XPATH location from which to start the snapshot or search.
xpath: '//software-information'
Begin the tests section, if there is one. In this case there will be a tests section.
tests:
Specify what tests will be run. In this case we will enable this test file to check whether the Junos OS version is the same between snapshots.
- all-same: junos-version
Set the error message to display. In the even that the test fails, this message will be displayed.
err: "Test Failed!!! The versions are not the same. From the PRE snapshot, the version is: <{{pre['junos-version']}}>. From the POST snapshot, the version is <{{post['junos-version']}}>!! "
Set the info message to display. In the event that the test succeeds, this message will be displayed.
info: "Test Succeeded!! The Junos OS version is: <{{post['junos-version']}}>!!!"
Results
The resulting test file can be shown in the terminal
using the cat
command.
user@jsnapy-server:~> cat test_sw_version.yml tests_include: - test_sw_version test_sw_version: - command: show version - item: xpath: '//software-information' tests: - all-same: junos-version err: "Test Failed!!! The versions are not the same. From the PRE snapshot, the version is: <{{pre['junos-version']}}>. From the POST snapshot, the version is <{{post['junos-version']}}>!! " info: "Test Succeeded!! The Junos OS version is: <{{post['junos-version']}}>!!!"
Request Filtered Configuration Information
Procedure
Step-by-Step Procedure
This example creates a test file that uses an RPC command, get-config
, to retrieve the device configuration. An argument
is provided to the RPC command so that jsnapy filters that device
configuration information so that the snapshot contains only the filtered
information rather than the entire configuration. In this example
we will configure the test file to return only the host-name
information.
Create the tests_include section.
tests_include:
Set the test name.
- test_rpc_filtering
Begin the test details section.
test_rpc_filterin:
Set the RPC command to be used.
- rpc: get-config
Provide the arguments keyword to tell jsnapy that arguments follow.
- kwargs:
Enter the argument name and value. In this case we are filtering for the host name.
filter_xml: configuration/system/host-name
Results
The resulting configuration can be shown in the terminal
using the cat
command.
user@jsnapy-server:~> cat filter_config_info.yml tests_include: - test_rpc_filtering test_rpc_filtering: - rpc: get-config - kwargs: filter_xml: configuration/system/host-name