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 13::: {.note} 14By executing the test driver in this way, 15the VMs executed may gain network & Internet access via their backdoor control interface, 16typically recognized as `eth0`. 17::: 18 19You can then take any Python statement, e.g. 20 21```py 22>>> start_all() 23>>> test_script() 24>>> machine.succeed("touch /tmp/foo") 25>>> print(machine.succeed("pwd")) # Show stdout of command 26``` 27 28The function `test_script` executes the entire test script and drops you 29back into the test driver command line upon its completion. This allows 30you to inspect the state of the VMs after the test (e.g. to debug the 31test script). 32 33## Shell access in interactive mode {#sec-nixos-test-shell-access} 34 35The function `<yourmachine>.shell_interact()` grants access to a shell running 36inside a virtual machine. To use it, replace `<yourmachine>` with the name of a 37virtual machine defined in the test, for example: `machine.shell_interact()`. 38Keep in mind that this shell may not display everything correctly as it is 39running within an interactive Python REPL, and logging output from the virtual 40machine may overwrite input and output from the guest shell: 41 42```py 43>>> machine.shell_interact() 44machine: Terminal is ready (there is no initial prompt): 45$ hostname 46machine 47``` 48 49As an alternative, you can proxy the guest shell to a local TCP server by first 50starting a TCP server in a terminal using the command: 51 52```ShellSession 53$ socat 'READLINE,PROMPT=$ ' tcp-listen:4444,reuseaddr 54``` 55 56In the terminal where the test driver is running, connect to this server by 57using: 58 59```py 60>>> machine.shell_interact("tcp:127.0.0.1:4444") 61``` 62 63Once the connection is established, you can enter commands in the socat terminal 64where socat is running. 65 66## SSH Access for test machines {#sec-nixos-test-ssh-access} 67 68An SSH-based backdoor to log into machines can be enabled with 69 70```nix 71{ 72 name = "…"; 73 nodes.machines = { /* … */ }; 74 interactive.sshBackdoor.enable = true; 75} 76``` 77 78::: {.warning} 79Make sure to only enable the backdoor for interactive tests 80(i.e. by using `interactive.sshBackdoor.enable`)! This is the only 81supported configuration. 82 83Running a test in a sandbox with this will fail because `/dev/vhost-vsock` isn't available 84in the sandbox. 85::: 86 87This creates a [vsock socket](https://man7.org/linux/man-pages/man7/vsock.7.html) 88for each VM to log in with SSH. This configures root login with an empty password. 89 90When the VMs get started interactively with the test-driver, it's possible to 91connect to `machine` with 92 93``` 94$ ssh vsock/3 -o User=root 95``` 96 97The socket numbers correspond to the node number of the test VM, but start 98at three instead of one because that's the lowest possible 99vsock number. The exact SSH commands are also printed out when starting 100`nixos-test-driver`. 101 102On non-NixOS systems you'll probably need to enable 103the SSH config from {manpage}`systemd-ssh-proxy(1)` yourself. 104 105If starting VM fails with an error like 106 107``` 108qemu-system-x86_64: -device vhost-vsock-pci,guest-cid=3: vhost-vsock: unable to set guest cid: Address already in use 109``` 110 111it means that the vsock numbers for the VMs are already in use. This can happen 112if another interactive test with SSH backdoor enabled is running on the machine. 113 114In that case, you need to assign another range of vsock numbers. You can pick another 115offset with 116 117```nix 118{ 119 sshBackdoor = { 120 enable = true; 121 vsockOffset = 23542; 122 }; 123} 124``` 125 126## Port forwarding to NixOS test VMs {#sec-nixos-test-port-forwarding} 127 128If your test has only a single VM, you may use e.g. 129 130```ShellSession 131$ QEMU_NET_OPTS="hostfwd=tcp:127.0.0.1:2222-:22" ./result/bin/nixos-test-driver 132``` 133 134to port-forward a port in the VM (here `22`) to the host machine (here port `2222`). 135 136This naturally does not work when multiple machines are involved, 137since a single port on the host cannot forward to multiple VMs. 138 139If the test defines multiple machines, you may opt to _temporarily_ set 140`virtualisation.forwardPorts` in the test definition for debugging. 141 142Such port forwardings connect via the VM's virtual network interface. 143Thus they cannot connect to ports that are only bound to the VM's 144loopback interface (`127.0.0.1`), and the VM's NixOS firewall 145must be configured to allow these connections. 146 147## Reuse VM state {#sec-nixos-test-reuse-vm-state} 148 149You can re-use the VM states coming from a previous run by setting the 150`--keep-vm-state` flag. 151 152```ShellSession 153$ ./result/bin/nixos-test-driver --keep-vm-state 154``` 155 156The machine state is stored in the `$TMPDIR/vm-state-machinename` 157directory. 158 159## Interactive-only test configuration {#sec-nixos-test-interactive-configuration} 160 161The `.driverInteractive` attribute combines the regular test configuration with 162definitions from the [`interactive` submodule](#test-opt-interactive). This gives you 163a more usable, graphical, but slightly different configuration. 164 165You can add your own interactive-only test configuration by adding extra 166configuration to the [`interactive` submodule](#test-opt-interactive). 167 168To interactively run only the regular configuration, build the `<test>.driver` attribute 169instead, and call it with the flag `result/bin/nixos-test-driver --interactive`.