Files
napalm-ubiquiti/sim/README.md
T
Christian Manivong 0886ea80c6 feat: a simulated controller and switch, and the drivers against them
sim/unifi_sim plays a UniFi Network controller on HTTPS (UniFi OS or
standalone, Integration API by version, classic API, logins with CSRF, API
keys, MFA, rate limit, wrong site, redirects, port override writes) and a
switch on SSH, from the fixtures. The end-to-end tests run all three drivers
against it over real TLS and SSH, certificate and host key checks included.
A command line and a Dockerfile run it by hand or on a test server.
2026-12-05 17:12:10 +01:00

2.1 KiB

UniFi simulator

A UniFi Network controller and a UniFi switch, simulated so the drivers can be tested without hardware. It is not a UniFi implementation. It answers what the drivers ask, by the rules a real controller answers by, from the recorded fixtures in tests/fixtures.

What it plays

The controller on HTTPS, with a self-signed certificate:

  • UniFi OS (/proxy/network/...) or a standalone Network application (--standalone).
  • The Integration API (/v1/info, sites, sites/{id}/devices, and pending-devices from 10.0) with an API key, from Network 9.0.108.
  • The classic API (self/sites, stat/device[/<mac>], stat/sta, stat/sysinfo, rest/networkconf|portconf|wlanconf, and PUT rest/device/<id>) with an API key or a session.
  • Logins: UniFi OS (TOKEN cookie, X-CSRF-Token) or standalone (unifises and csrf_token cookies). A write on a session needs its CSRF token.
  • Failures: a wrong key or password (401), a cloud account with MFA (499), too many failed logins (429 with Retry-After), an unknown site (api.err.NoSiteContext), and every answer a redirect (redirect=True).
  • Data: two sites. default holds a US-16-150W, a USW Flex Mini, a UAP-AC-Pro and a U6-LR waiting for adoption. branch holds a US-8.

A switch without a controller on SSH (US-24, 7.1.26, login ubnt/ubnt). It answers mca-dump and cat /tmp/running.cfg; any other command exits 127. Its Ed25519 host key is new on every start.

Use

In tests (tests/test_sim_e2e.py):

from unifi_sim import SimController, SimDevice, serve_controller, serve_device

controller = serve_controller(SimController.from_fixtures(version="9.1.120"))
switch = serve_device(SimDevice.us24())
...
controller.stop()
switch.stop()

By hand, or for netOrk on a test server:

PYTHONPATH=sim python -m unifi_sim --port 8443 --ssh-port 2222 [--version 10.1.84] [--standalone]
docker build -f sim/Dockerfile -t unifi-sim . && docker run -p 8443:8443 -p 2222:2222 unifi-sim --api-key <key>

It prints the controller's URL, certificate fingerprint, login and API key, and the switch's SSH port and host key.