Network automation lab 16 of 27

Testing network automation: unit tests, mocks, pre/post snapshots and a pipeline

Write the tests first: unit-test a small network library with saved device output and a mocked connection, then validate a real OSPF change with before-and-after snapshots, a structured diff and explicit checks, all chained into a pipeline that stops at the first failure.

Run this lab

It needs Lite: its 4 nodes are set to 4 vCPU and 2,560 MB in total. That fits the free demo too.

In your workspace: Shared, then Network Automation - CCNA and CCNP Automation guided labs, then 16 - Testing network automation - unit tests mocks pre-post snapshots and a pipeline

Try it free for 30 minutesRun it on Lite from £3 a day

The free demo needs no card and no email. Every lab plan includes every guided lab.

Level
Advanced
Time
About 85 minutes
Nodes need
4 vCPU, 2,560 MB
Smallest plan
Lite

The scenario

Kestrel Freight's change board has a new rule: no automated change goes out without tests. The change today is small (r1 should reach r2's loopback through r3, so the r1–r2 link can be upgraded next week), but you'll build the safety net properly: unit tests for the team's netcheck library that run without any router, snapshots before and after the change, a diff that shows exactly what moved, a validation script that says PASS only if the network did what was intended and nothing else, and a pipeline that chains it all.

RouterMgmtLinksLoopback0
r110.10.20.11Gi0/1 10.1.12.1 → r2 · Gi0/2 10.1.13.1 → r310.255.0.1
r210.10.20.12Gi0/1 10.1.12.2 → r1 · Gi0/2 10.1.23.1 → r310.255.0.2
r310.10.20.13Gi0/1 10.1.13.2 → r1 · Gi0/2 10.1.23.2 → r210.255.0.3

OSPF area 0 runs on every link and loopback. Files in ~/lab: netcheck.py (function stubs), tests/test_netcheck.py (two finished tests and three TODOs), tests/data/ (saved r1 output), snapshot.py and diff.py (ready to use). Login netops / NetOps-Lab1.

What you’ll practise

  • Apply test-driven development: write a failing test, make it pass, refactor
  • Unit-test parsing and logic with saved output and unittest.mock instead of live devices
  • Capture structured pre- and post-change snapshots and diff them
  • Validate that a change did exactly what was intended and nothing else
  • Chain lint, test, pre-check, deploy, post-check and validation into a CI/CD-style pipeline

The tasks

  1. Red, green, refactor
  2. Write the test, then the code: ospf_healthy()
  3. Test a parser with saved output
  4. Replace the router with a mock
  5. Snapshot the network before the change
  6. Make the change, snapshot again, diff
  7. Validate: intended changes only
  8. Chain it into a pipeline

The Lab Tasks pane has the scenario, these tasks, the checks to run, hints and the full solution.

Topology and devices

The network as it opens in EVE-NG, drawn from the lab’s own canvas, and the image each node runs.

Topology of Testing network automation - unit tests mocks pre-post snapshots and a pipeline4 nodes and 1 network, 7 links. Every link is listed as text below the diagram.devboxr1r2r3Mgmteth0Gi0/1Gi0/1Gi0/2Gi0/2Gi0/0Gi0/0Gi0/2Gi0/1Gi0/0
4 nodes and 1 network, with the interface names each link uses. On a narrow screen, scroll the diagram sideways.
Nodes in Testing network automation: unit tests, mocks, pre/post snapshots and a pipeline, with the image each runs and the vCPU and RAM it’s set to
NodeDeviceImage in Add NodeSet to
devboxEVE-NG GUI server (Docker)eve-gui-server:latest1 vCPU, 1,024 MB
r1Cisco IOSvvios-adventerprisek9-m.spa.159-3.m91 vCPU, 512 MB
r2Cisco IOSvvios-adventerprisek9-m.spa.159-3.m91 vCPU, 512 MB
r3Cisco IOSvvios-adventerprisek9-m.spa.159-3.m91 vCPU, 512 MB

How to open this lab

  1. Sign in to your workspace at lab.cloudlabbox.com.

  2. In File Manager, open Shared, then the folder Network Automation - CCNA and CCNP Automation guided labs.

  3. Open 16 - Testing network automation - unit tests mocks pre-post snapshots and a pipeline and start its nodes, then open Lab Tasks for the scenario and the steps.

The lab is read-only. You can start the nodes and configure the devices, but changes to the topology can’t be saved. To change it, rebuild it in your own folder.

More Network automation labs

All Network automation labs

Related labs in other courses

CloudLabBox is independent of Cisco. The lab follows exam topics and isn’t vendor training material; vendor names identify the exams and the images in the workspace.