1# Running Tests interactively {#sec-running-nixos-tests-interactively}
2
3The test itself can be run interactively. This is particularly useful
4when developing or debugging a test:
5
6```ShellSession
7$ nix-build . -A nixosTests.login.driverInteractive
8$ ./result/bin/nixos-test-driver
9[...]
10>>>
11```
12
13You can then take any Python statement, e.g.
14
15```py
16>>> start_all()
17>>> test_script()
18>>> machine.succeed("touch /tmp/foo")
19>>> print(machine.succeed("pwd")) # Show stdout of command
20```
21
22The function `test_script` executes the entire test script and drops you
23back into the test driver command line upon its completion. This allows
24you to inspect the state of the VMs after the test (e.g. to debug the
25test script).
26
27## Shell access in interactive mode {#sec-nixos-test-shell-access}
28
29The function `<yourmachine>.shell_interact()` grants access to a shell running
30inside a virtual machine. To use it, replace `<yourmachine>` with the name of a
31virtual machine defined in the test, for example: `machine.shell_interact()`.
32Keep in mind that this shell may not display everything correctly as it is
33running within an interactive Python REPL, and logging output from the virtual
34machine may overwrite input and output from the guest shell:
35
36```py
37>>> machine.shell_interact()
38machine: Terminal is ready (there is no initial prompt):
39$ hostname
40machine
41```
42
43As an alternative, you can proxy the guest shell to a local TCP server by first
44starting a TCP server in a terminal using the command:
45
46```ShellSession
47$ socat 'READLINE,PROMPT=$ ' tcp-listen:4444,reuseaddr`
48```
49
50In the terminal where the test driver is running, connect to this server by
51using:
52
53```py
54>>> machine.shell_interact("tcp:127.0.0.1:4444")
55```
56
57Once the connection is established, you can enter commands in the socat terminal
58where socat is running.
59
60## Port forwarding to NixOS test VMs {#sec-nixos-test-port-forwarding}
61
62If your test has only a single VM, you may use e.g.
63
64```ShellSession
65$ QEMU_NET_OPTS="hostfwd=tcp:127.0.0.1:2222-:22" ./result/bin/nixos-test-driver
66```
67
68to port-forward a port in the VM (here `22`) to the host machine (here port `2222`).
69
70This naturally does not work when multiple machines are involved,
71since a single port on the host cannot forward to multiple VMs.
72
73If the test defines multiple machines, you may opt to _temporarily_ set
74`virtualisation.forwardPorts` in the test definition for debugging.
75
76Such port forwardings connect via the VM's virtual network interface.
77Thus they cannot connect to ports that are only bound to the VM's
78loopback interface (`127.0.0.1`), and the VM's NixOS firewall
79must be configured to allow these connections.
80
81## Reuse VM state {#sec-nixos-test-reuse-vm-state}
82
83You can re-use the VM states coming from a previous run by setting the
84`--keep-vm-state` flag.
85
86```ShellSession
87$ ./result/bin/nixos-test-driver --keep-vm-state
88```
89
90The machine state is stored in the `$TMPDIR/vm-state-machinename`
91directory.
92
93## Interactive-only test configuration {#sec-nixos-test-interactive-configuration}
94
95The `.driverInteractive` attribute combines the regular test configuration with
96definitions from the [`interactive` submodule](#test-opt-interactive). This gives you
97a more usable, graphical, but slightly different configuration.
98
99You can add your own interactive-only test configuration by adding extra
100configuration to the [`interactive` submodule](#test-opt-interactive).
101
102To interactively run only the regular configuration, build the `<test>.driver` attribute
103instead, and call it with the flag `result/bin/nixos-test-driver --interactive`.