NixOS VM tests: Difference between revisions

m Tomodachi94 moved page NixOS Testing library to NixOS VM tests: title is clearer, "VM tests" is fairly common
6543 (talk | contribs)
pictures just describe things better sometime
 
(5 intermediate revisions by 5 users not shown)
Line 1: Line 1:
WARNING: this page is mostly outdated as https://nixos.org/manual/nixos/stable/#chap-developing-the-test-driver mentions that  <code>pkgs.nixosTest</code>, <code>testing-python.nix</code> and <code>make-test-python.nix</code> are outdated. Beside the documentation, you may also like https://nix.dev/tutorials/nixos/integration-testing-using-virtual-machines.html
The primary documentation for the [https://nixos.org/manual/nixos/stable/index.html#sec-nixos-tests NixOS VM testing framework] is in [https://nixos.org/manual/nixos/stable/index.html#sec-nixos-tests the NixOS manual], and in [https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest the Nixpkgs manual]. A tutorial can be found at [https://nix.dev/tutorials/nixos/integration-testing-using-virtual-machines].
The primary documentation for the [https://nixos.org/manual/nixos/stable/index.html#sec-nixos-tests NixOS VM testing framework] is in [https://nixos.org/manual/nixos/stable/index.html#sec-nixos-tests the NixOS manual], and in [https://nixos.org/manual/nixpkgs/unstable/#tester-runNixOSTest the Nixpkgs manual]. A tutorial can be found at [https://nix.dev/tutorials/nixos/integration-testing-using-virtual-machines].


The test infrastructure entry point is nixos/lib/testing.nix. Alternatively, for out-of-tree tests you can invoke it via Nixpkgs as the nixosTest function, which reuses your already evaluated Nixpkgs to generate your node configurations.
The test infrastructure entry point is nixos/lib/testing.nix. Alternatively, for out-of-tree tests you can invoke it via Nixpkgs as the nixosTest function, which reuses your already evaluated Nixpkgs to generate your node configurations.
The test infra relies on the qemu build-vm code to generate virtual machines.
The test infra relies on the qemu build-vm code to generate virtual machines.


It will generate a test driver (a wrapper of nixos/lib/test-driver/test-driver.py) in charge of creating the network.
It will generate a test driver (a wrapper of nixos/lib/test-driver/test-driver.py) in charge of creating the network.
Line 11: Line 16:
The wrapper `bin/nixos-run-vms` is in charge to start the driver with the correct VM script as arguments.
The wrapper `bin/nixos-run-vms` is in charge to start the driver with the correct VM script as arguments.


Once the driver is loaded, depending on the environment variables `tests` it will run in an interactive mode or run some perl code (`testScript`).
Once the driver is loaded, depending on the environment variables `tests` it will run in an interactive mode or run some python code (`test_script()`).
In interactive mode, you can run `start_all` followed by `join_all` to start and keep the VM alive
In interactive mode, you can run `start_all()` to start and keep the VM alive


== Connecting to an interactive VM via SSH ==
== Connecting to an interactive VM via SSH ==
Line 18: Line 23:
Add this to your test config:
Add this to your test config:


  interactive.nodes.machine = {
  interactive.sshBackdoor.enable = true;
    services.openssh = {
      enable = true;
      settings = {
        PermitRootLogin = "yes";
        PermitEmptyPasswords = "yes";
      };
    };
   
    security.pam.services.sshd.allowNullPassword = true;
   
    virtualisation.forwardPorts = [
      { from = "host"; host.port = 2000; guest.port = 22; }
    ];
};


Now you can connect to this VM via  
Now you can connect to this VM via (on linux):
  ssh root@localhost -p 2000
  ssh -o User=root vsock/3
If you are on MacOS or have multiple VMs look at the shell output that do run the interactive tests:
[[File:Screenshot of interactive test just started.png|none|thumb|444x444px|Screenshot of just started nixos test for meilisearch (interactive)]]


== How to debug tests ? ==
== How to debug tests ? ==
Line 47: Line 40:
== I don't see any prompt ? (qemu window pitch black) ==
== I don't see any prompt ? (qemu window pitch black) ==


Check the output for `malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "\x{0}\x{0}\x{0}\x{0}...") at /nix/store/1hkp2n6hz3ybf2rvkjkwrzgbjkrrakzl-update-users-groups.pl line 11`.
Check the output for  
 
<code>malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "\x{0}\x{0}\x{0}\x{0}...") at /nix/store/1hkp2n6hz3ybf2rvkjkwrzgbjkrrakzl-update-users-groups.pl line 11`</code>
 
You should purge the state present in rm -rf /tmp/vm-state-<VM_NAME>
You should purge the state present in rm -rf /tmp/vm-state-<VM_NAME>


Line 391: Line 387:
   (0.00 seconds)
   (0.00 seconds)
   /nix/store/bx7z3imvxxpwkkza10vb23czhw7873w2-vm-test-run-unnamed
   /nix/store/bx7z3imvxxpwkkza10vb23czhw7873w2-vm-test-run-unnamed
[[Category:NixOS]]