Test_driver package

Hi,

I am trying to familiarize with nixosTest, ie:

checks.${system} = {
  os = nixpkgs.legacyPackages.${system}.testers.nixosTest {
    name = "basic-test";
    nodes = {
      machine = { ... }: {
        imports = [ ./foo.nix ];
      };
    };
    testScript = builtins.readFile ./tests/os.py;
  };
};

The testScript option takes in a python script, ie:

machine.start(allow_reboot=True)

with subtest("wait for boot"):
    machine.wait_for_unit("default.target")

As machine and subtest are not defined it makes very difficult to discover methods, arguments viat the IDE, as it does not know where to load them from.

If I use:

from test_driver import subtest

machine.start(allow_reboot=True)

with subtest("wait for boot"):
    machine.wait_for_unit("default.target")

it fails, as it is already defined:

nixos-test-driver-os> Running type check (enable/disable: config.skipTypeCheck)
nixos-test-driver-os> See https://nixos.org/manual/nixos/stable/#test-opt-skipTypeCheck
nixos-test-driver-os> testScriptWithTypes:61: error: Module "test_driver" has no attribute "subtest"
nixos-test-driver-os> [attr-defined]
nixos-test-driver-os>     from test_driver import subtest
nixos-test-driver-os>     ^
nixos-test-driver-os> testScriptWithTypes:61: error: Name "subtest" already defined on line 42
nixos-test-driver-os> [no-redef]
nixos-test-driver-os>     from test_driver import subtest
nixos-test-driver-os>     ^
nixos-test-driver-os> Found 2 errors in 1 file (checked 1 source file)

therefore I am assuming that the “input” script is the suffix of a predefined one.

Are there any best-practise on how to handle these scripts or install the actual package?

Thanks

Test script and its Python environment are assembled here:

All the available methods are listed in the NixOS manual:
https://nixos.org/manual/nixos/stable/#sec-nixos-tests

Assuming from your post, that you want your Python IDE to recognise the test module for code completion, a quick way should be opening a nix-shell with the test driver package (no need to install it system wide) and launching the IDE from that shell. The IDE then should (if it is smart enough) discover the Python module.
If not, try a shell that uses python.with …

1 Like