End-to-end testing
The e2e suite (tests/test_e2e.py, about 70 tests) runs the real server against a LinuxCNC
simulator. It catches what the mock-based tests cannot: real state transitions, command
execution, HAL data from the motion controller, files on a real filesystem, and streams with
live data.
Only against a simulator
The suite connects to whatever gRPC server answers on localhost:50051, and most of its tests
change the machine behind it: they take it out of E-stop, power it on, home it, jog, run MDI
commands and programs, upload and delete files, and freeze LinuxCNC’s task process. Never point
the e2e suite at a machine with hardware. Two checks guard against mistakes.
Tests that change the machine run only with LINUXCNC_GRPC_E2E_SIM=1 set. Without it they
are skipped (“changes the machine; set LINUXCNC_GRPC_E2E_SIM=1 …”), and only the read-only
tests run: status fields, server information, machine configuration, the HAL queries and
streams, and a refused path. These do not power the machine on first.
With the variable set, the suite first reads the INI file the server’s LinuxCNC runs
(status.task.ini_filename), sending nothing before. Its path must have a sim directory
right below configs or sample-configs, as in ~/linuxcnc/configs/sim/axis/axis.ini (a
source tree) or /usr/share/doc/linuxcnc/examples/sample-configs/sim/axis/axis.ini (the
Debian package). Otherwise the whole run stops with an error and sends no command. The path is
checked again, on a fresh status, at the start of every test that changes the machine. That
recheck does not tell one server from another: a different server on the same port is caught
only if its INI path fails the check.
The guard is based on the INI file’s path, not proof of a simulator: a real machine’s
configuration kept under a configs/sim/ directory would pass it.
Running locally
You need the LinuxCNC simulator (the linuxcnc-uspace package on Debian), uv and a checkout
of this repository. Run these steps from the repository root as a normal user, not as root.
# 0. On a machine without a display (a server or a container), start a
# virtual one first (sudo apt install xvfb)
Xvfb :99 -screen 0 1024x768x24 &
export DISPLAY=:99
# 1. Copy the simulator configuration to a path the simulator check accepts
mkdir -p ~/linuxcnc/configs/sim
cp -r /usr/share/doc/linuxcnc/examples/sample-configs/sim/axis ~/linuxcnc/configs/sim/axis
# 2. Run it without a GUI: a "headless" display program that stays alive,
# and DISPLAY = headless in the copied axis.ini. Without this the
# error-channel test (TestE2EErrorChannelWithoutAGui) is skipped.
mkdir -p ~/linuxcnc/configs/sim/axis/bin
printf '#!/bin/bash\ntrap "exit 0" SIGTERM SIGINT SIGHUP\nwhile true; do sleep 60; done\n' \
> ~/linuxcnc/configs/sim/axis/bin/headless
chmod +x ~/linuxcnc/configs/sim/axis/bin/headless
sed -i 's/^DISPLAY[[:space:]]*=.*/DISPLAY = headless/' ~/linuxcnc/configs/sim/axis/axis.ini
# 3. Start LinuxCNC with the copied configuration
linuxcnc ~/linuxcnc/configs/sim/axis/axis.ini > /tmp/linuxcnc-startup.log 2>&1 &
# 4. Create the venv; it must see the system's linuxcnc module, so pin it
# to the system Python (a Python uv downloads would not see the distro's
# linuxcnc module; --clear replaces a .venv made without system site packages)
uv venv --python /usr/bin/python3 --system-site-packages --clear
uv sync --extra dev
# 5. Wait for LinuxCNC to initialize (with the venv's Python, as the server)
uv run python scripts/wait-for-linuxcnc.py --timeout 90
# 6. Start the gRPC server with a directory for the test's G-code files
mkdir -p /tmp/e2e-nc-files
uv run python -m linuxcnc_grpc.server --host 127.0.0.1 --nc-files /tmp/e2e-nc-files &
# 7. Run the e2e tests (the variable lets them change the simulated machine)
LINUXCNC_GRPC_E2E_SIM=1 uv run pytest tests/test_e2e.py -v -m e2e -rsThe linuxcnc launcher looks for the DISPLAY program in the configuration’s own bin/
directory, which is why step 2 puts headless there; a run-in-place build, as in CI, can keep
it in ~/linuxcnc/bin instead. The launcher shuts LinuxCNC down when the DISPLAY program
exits, so headless must keep running.
The server and scripts/wait-for-linuxcnc.py can both be started before LinuxCNC. The wait
script’s --timeout covers the whole run, including the first check that the linuxcnc
module can be imported.
In Docker (macOS, Windows, or any machine without LinuxCNC)
Debian 13 packages the LinuxCNC simulator (linuxcnc-uspace, 2.9.4, also for arm64). Save
this as Dockerfile.sim in the repository root:
# Dockerfile.sim
FROM debian:trixie
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
linuxcnc-uspace python3 python3-venv python3-pip xvfb netcat-openbsd procps sudo \
&& rm -rf /var/lib/apt/lists/*
RUN useradd -m cnc && echo 'cnc ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/cnc
USER cnc
RUN python3 -m venv --system-site-packages /home/cnc/venv \
&& /home/cnc/venv/bin/pip install 'grpcio>=1.76' 'grpcio-health-checking>=1.76' protobuf pytestdocker build -t linuxcnc-sim -f Dockerfile.sim .
docker run -d --init --name linuxcnc-sim \
--cap-add IPC_OWNER --cap-add SYS_NICE --cap-add IPC_LOCK \
-v "$PWD":/repo:ro linuxcnc-sim sleep infinityThe container options matter:
--cap-add IPC_OWNER: LinuxCNC’s realtime helper (rtapi_app) runs as root and must open shared memory created by thecncuser. Without it LinuxCNC fails withrtapi_shmem_new failed due to shmget(...): Permission denied.--cap-add SYS_NICEand--cap-add IPC_LOCK: the realtime helper sets realtime priorities and locks its memory.--init: stopped LinuxCNC processes are reaped. Without it they stay as zombies, and a check for a running LinuxCNC that looks for its processes is fooled.- The image runs as a normal user rather than root, as on a real machine.
Inside the container (docker exec -it linuxcnc-sim bash), follow steps 0 to 3 of
Running locally: start Xvfb, copy the configuration, put headless in
~/linuxcnc/configs/sim/axis/bin/, set DISPLAY = headless and start linuxcnc. The image
already has a venv in /home/cnc/venv, made from the system Python with
--system-site-packages, so skip step 4. Run the rest from /repo with that venv’s Python in
place of uv run, and with PYTHONPATH=/repo/src:/repo/packages/python:
cd /repo
export PYTHONPATH=/repo/src:/repo/packages/python
/home/cnc/venv/bin/python scripts/wait-for-linuxcnc.py --timeout 90
mkdir -p /tmp/e2e-nc-files
/home/cnc/venv/bin/python -m linuxcnc_grpc.server --host 127.0.0.1 --nc-files /tmp/e2e-nc-files &
LINUXCNC_GRPC_E2E_SIM=1 /home/cnc/venv/bin/python -m pytest tests/test_e2e.py -v -m e2e -rs -p no:cacheproviderThe repository is mounted read-only, so -p no:cacheprovider keeps pytest from writing its
cache there.
What the suite covers
| Area | What it checks |
|---|---|
| Status | Version, joints, INI file name, positions and task state. |
| State transitions | E-stop and recovery, on and off, and mode changes. |
| Commands | Homing, MDI G-code, and feed and spindle overrides. |
| WaitComplete | Waiting until a command finishes, the timeout, and waiting on one command by its linuxcnc_serial. |
| G-code programs | Upload, open and run, pause and resume, abort, and single-step. |
| Jogging | Incremental and continuous jogs with stops, inpos and velocity during a jog, and a joint jog refused in teleop. |
| Errors | One message fanned out to two streams, status.errors, replay of buffered messages and a real MDI rejection. |
| Machine settings | Coolant, rapid rate and maximum velocity, override enables, optional stop and block delete. |
| Configuration and server information | GetMachineConfig against the simulator’s INI file, and GetServerInfo. |
| Negative paths | Empty MDI, wrong mode, invalid axis, path traversal and duplicate upload. |
| Streaming | StreamStatus and StreamErrors. |
| HAL | Every HalService RPC, including WatchValues. |
| Files | Upload, list and delete, reading a file back whole and in chunks, and resolving the loaded program. |
Four groups need more than a running simulator. Those that freeze the task need the tests to run on the same host and as the same user as LinuxCNC (as in CI), and are skipped otherwise.
Errors on a machine without a GUI
TestE2EErrorChannelWithoutAGui sends 30 refused MDI lines with no StreamErrors client.
Each must finish well within the 5 seconds LinuxCNC stalls for when nobody reads its full
error queue, and show up in status.errors. It needs the server in its default mode and a
configuration without a listed GUI (DISPLAY = headless). It is skipped when GetServerInfo
reports an error-channel mode other than always.
A task that does not respond
TestE2EUnresponsiveTask freezes LinuxCNC’s task process for about 5 seconds and always
continues it. A command sent meanwhile must return with acknowledged false after about 5
seconds, and GetServerInfo, GetStatus and the health check must answer within 3 seconds
while it waits. It fails against a server started with --in-process-commands, as it should.
A task stopped while it holds the status buffer
TestE2EStatusBufferHeldByStoppedTask stops the task until it holds the status buffer.
GetStatus must answer UNAVAILABLE with status-unreadable, GetServerInfo and the health
check must answer, and WaitComplete must not answer from the unreadable status. Afterwards
the status is readable and the server did not reconnect. It is skipped when LinuxCNC’s
semaphores cannot be read, or when the task is not caught holding the buffer within 60 seconds
(HUNT_SECONDS).
Stops first
TestE2EStopsFirst sends an E-stop behind a blocked command while the task is frozen. The
E-stop must be written next, a command queued behind it must be refused with not_sent, and
GetServerInfo must count the stop as not taken until the task runs again. A second test,
which does not freeze the task, streams feed overrides from one client while another sends 30
jog start and stop pairs, and checks that every jog stop is taken.
In CI
.github/workflows/e2e.yml runs the suite on every push and pull request to main or
master, and by hand (workflow_dispatch). It builds LinuxCNC v2.9.3 in run-in-place mode on
Ubuntu 24.04 (cached), starts it headless from a copy of sim/axis in
/tmp/linuxcnc-sim/configs/sim/axis/, starts the server with --nc-files /tmp/e2e-nc-files,
and runs pytest tests/test_e2e.py -v -m e2e -rs with LINUXCNC_GRPC_E2E_SIM=1.
Tests skip themselves when the server cannot be reached, and pytest counts that as a pass. So
the job reads the JUnit report and fails if no test passed, or if a test was skipped for any
reason other than the tolerated ones (TOLERATED_SKIP_REASONS). A test you add must not skip
in CI.
The Python unit tests run separately, in ci.yml, on Python 3.12 and 3.13.
Adding e2e tests
- Add tests to
tests/test_e2e.pywith@pytest.mark.e2e. Add@reads_onlyonly if the test changes nothing on the machine (no command, upload or delete); every test not marked counts as changing the machine. - Use the
linuxcnc_stuborhal_stubfixtures for gRPC access. - Don’t request
machine_on: it is an autouse fixture, so every test not marked@reads_onlyalready starts with the machine on (ensure_machine_on()does the same inside a test). - Use
home_all(stub)before tests that need motion,wait_complete(stub)to wait for a command, andwait_for_condition()instead of a fixedtime.sleep(). - Use
upload_test_file()anddelete_test_file()for files. - Restore what you change (overrides, modes) at the end of the test.
- Start motion (a jog, an MDI move, a program) inside a
trywhosefinallystops it withstop_axis_jog_quietly()orabort_quietly(). Each attempts a bounded jog stop or abort and logs a failure rather than raising it, so a failing test’s cleanup still runs. Skip that cleanup only once a fresh status has shown the motion stopped (for examplewait_for_motion_stopped()), not when a stop was merely sent. Send no stop after an incremental jog that completed and was seen stopped, since a stop during one cuts it short. Give the motion RPCs a deadline (MOTION_RPC_TIMEOUT).
Mock tests and e2e tests
| Aspect | Mock tests (test_integration.py) | E2E tests (test_e2e.py) |
|---|---|---|
| Server | tests/mock_server.py | Real linuxcnc_grpc.server |
| LinuxCNC | Not needed | Real simulator instance |
| Speed | Seconds | Minutes |
| Determinism | Fully deterministic | Timing-dependent |
| Coverage | API shape and serialization | Real behavior and state machines |