[Optional] Lab Task 8: Robot Framework pyATS
In network automation and testing, Robot Framework and pyATS are two powerful tools that serve different but complementary purposes.
Robot Framework – Keyword-Driven Test Automation
Robot Framework is a generic automation framework that is widely used for test automation in various domains, including network infrastructure. It follows a keyword-driven approach, making it easy to write human-readable test cases.
Key Features:
- Uses a simple, readable syntax (based on keywords).
- Supports web, API, and network automation.
- Extensible with custom Python libraries (e.g., for network testing).
- Generates detailed HTML reports.
pyATS – Cisco’s Network Testing Framework
pyATS (Python Automated Test System) is a Python-based framework built by Cisco for network automation and validation. It is designed for testing network configurations, verifying state, and automating changes across devices.
Key Features:
- Focused on network testing and automation.
- Supports CLI, API, and telemetry-based network validation.
- Integrates with Genie, a library for parsing and validating network outputs.
- Scales well for pre- and post-change validation in CI/CD pipelines.
Step 1: Create folder/file structure
First, you need to create the folder/file structure. Everything marked as a star needs to be created:
1Repository folder pod01 (Folder)
2
3├── ndfc (Folder)
4
5└──── robot_framework (*Folder)
6
7 └── nxos9kv-01.yaml (*File)
8
9 └── simple_testcase.robot (*File)
10nxos9kv-01.yaml .yaml
Add the following content to the nxos9kv-01.yaml . This defines how pyATS can access the device. Change the IP in line 12:
1devices:
2 POD01-N9KV-01:
3 type: nexus
4 os: nxos
5 credentials:
6 default:
7 username:
8 password:
9 connections:
10 cli:
11 protocol: ssh
12 ip: 198.18.1.12
13 port: 22simple_testcase.robot
Add the following content to the simple_testcase.robot, which describes what will be tested.
In these tests you connect to the device defined in the testbed, you will verify that:
- VLAN 1001 is created
- VLAN Interface 1001 is existent
- Branch and Datacenter clients are reachable
1*** Settings ***
2# Importing test libraries, resource files and variable files.
3Library pyats.robot.pyATSRobot
4Library unicon.robot.UniconRobot
5Library genie.libs.robot.GenieRobot
6
7*** Variables ***
8# Define the pyATS testbed file to use for this run
9${testbed} nxos9kv-01.yaml
10${vlan} 1001
11${branch_client} 10.0.0.10
12${datacenter_client} 10.0.1.10
13${source_interface} 10.0.1.1
14
15*** Test Cases ***
16# Creating test cases from available keywords.
17
18Connect
19 # Initializes the pyATS/Genie Testbed
20 use testbed "${testbed}"
21
22 # Connect to both device
23 connect to device "POD01-N9KV-01"
24
25# Verify Interfaces
26Verify the counts of 'up' Interace for POD01-N9KV-01
27 verify count "9" "interface up" on device "POD01-N9KV-01"
28
29Validate VLAN on device
30 ${output}= parse "show vlan" on device "POD01-N9KV-01"
31 ${response}= dq query data=${output} filters=contains('1001')
32
33 Should Not Be Empty ${response} msg=VLAN not found
34
35Validate VLAN interface on device
36 ${output}= parse "show interface vlan ${vlan}" on device "POD01-N9KV-01"
37 Should be Equal ${output}[Vlan${vlan}][link_state] up
38 Should Not Be Empty ${output} msg=VLAN interface not found
39
40Validate reachability of branch client
41 ${output}= parse "ping ${branch_client} source ${source_interface} count 10" on device "POD01-N9KV-01"
42 ${success_rate}= Set Variable ${output}[ping][statistics][success_rate_percent]
43 Log Success rate: ${success_rate}%
44 Should Be Equal as Numbers ${success_rate} 100.0 msg=Ping success rate is not 100%
45
46Validate reachability of datacenter client
47 ${output}= parse "ping ${datacenter_client} source ${source_interface} count 10" on device "POD01-N9KV-01"
48 ${success_rate}= Set Variable ${output}[ping][statistics][success_rate_percent]
49 Log Success rate: ${success_rate}%
50 Should Be Equal as Numbers ${success_rate} 100.0 msg=Ping success rate is not 100%Step 2: Run the pipeline
Adjust the pipeline file:
.gitlab-ci.yml
1stages:
2 - robot_framework_test
3
4robot_framework_test:
5 stage: robot_framework_test
6 tags:
7 - docker-runner
8 image: cbeye592/ltrato-2600:ndfc
9 id_tokens:
10 VAULT_ID_TOKEN:
11 aud: https://198.18.133.99:8200
12 secrets:
13 ansible_user:
14 vault: NDFC/ansible_user@pod01
15 file: false
16 token: $VAULT_ID_TOKEN
17 ansible_password:
18 vault: NDFC/ansible_password@pod01
19 file: false
20 token: $VAULT_ID_TOKEN
21 DEVICE_USER:
22 vault: NDFC/DEVICE_USER@pod01
23 file: false
24 token: $VAULT_ID_TOKEN
25 DEVICE_PASSWORD:
26 vault: NDFC/DEVICE_PASSWORD@pod01
27 file: false
28 token: $VAULT_ID_TOKEN
29 api_token:
30 vault: NETBOX/api_token@pod01
31 file: false
32 token: $VAULT_ID_TOKEN
33 before_script:
34 - source /root/ansible/bin/activate
35 - chmod -R 700 ndfc
36 - cd ndfc/robot_framework
37 script:
38 - "sed -i 's/username: /username: '${DEVICE_USER}'/g' nxos9kv-01.yaml"
39 - "sed -i 's/password: /password: '${DEVICE_PASSWORD}'/g' nxos9kv-01.yaml"
40 - robot --outputdir results simple_testcase.robot
41 artifacts:
42 untracked: false
43 when: always
44 expire_in: "1 days"
45 paths:
46 - "ndfc/robot_framework/results/log.html"
47 - "ndfc/robot_framework/results/report.html"Validation
Check the pipeline status and download the job artifact, which contains the Robot Framework report: