nixos/doc/writing-nixos-tests: Various improvements

Thanks to fricklerhandwerk for the many suggestions, most of which
I have fixupped into preceding commits.
This commit is contained in:
Robert Hensing 2022-07-29 11:50:12 +02:00
parent b2caf7965c
commit 6a78b41476
2 changed files with 19 additions and 18 deletions

View file

@ -39,12 +39,10 @@ It uses two client nodes to test correct locking across server crashes.
## Calling a test {#sec-calling-nixos-tests} ## Calling a test {#sec-calling-nixos-tests}
Tests are invoked a bit differently depending on whether the test lives in NixOS or in another project. Tests are invoked differently depending on whether the test is part of NixOS or lives in a different project.
### Testing within NixOS {#sec-call-nixos-test-in-nixos} ### Testing within NixOS {#sec-call-nixos-test-in-nixos}
Test modules can be instantiated into derivations in multiple ways.
Tests that are part of NixOS are added to [`nixos/tests/all-tests.nix`](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/all-tests.nix). Tests that are part of NixOS are added to [`nixos/tests/all-tests.nix`](https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/all-tests.nix).
```nix ```nix
@ -54,19 +52,22 @@ Tests that are part of NixOS are added to [`nixos/tests/all-tests.nix`](https://
Overrides can be added by defining an anonymous module in `all-tests.nix`. Overrides can be added by defining an anonymous module in `all-tests.nix`.
```nix ```nix
hostname = runTest { imports = [ ./hostname.nix ]; defaults.networking.firewall.enable = false; }; hostname = runTest {
imports = [ ./hostname.nix ];
defaults.networking.firewall.enable = false;
};
``` ```
You can run a test with attribute name `mytest` in `nixos/tests/all-tests.nix` by invoking: You can run a test with attribute name `hostname` in `nixos/tests/all-tests.nix` by invoking:
```shell ```shell
cd /my/git/clone/of/nixpkgs cd /my/git/clone/of/nixpkgs
nix-build -A nixosTests.mytest nix-build -A nixosTests.hostname
``` ```
### Testing outside the NixOS project {#sec-call-nixos-test-outside-nixos} ### Testing outside the NixOS project {#sec-call-nixos-test-outside-nixos}
Outside the `nixpkgs` repository, you can instantiate the test by first acquiring the NixOS library, Outside the `nixpkgs` repository, you can instantiate the test by first importing the NixOS library,
```nix ```nix
# regular nix # regular nix

View file

@ -49,15 +49,11 @@
<section xml:id="sec-calling-nixos-tests"> <section xml:id="sec-calling-nixos-tests">
<title>Calling a test</title> <title>Calling a test</title>
<para> <para>
Tests are invoked a bit differently depending on whether the test Tests are invoked differently depending on whether the test is
lives in NixOS or in another project. part of NixOS or lives in a different project.
</para> </para>
<section xml:id="sec-call-nixos-test-in-nixos"> <section xml:id="sec-call-nixos-test-in-nixos">
<title>Testing within NixOS</title> <title>Testing within NixOS</title>
<para>
Test modules can be instantiated into derivations in multiple
ways.
</para>
<para> <para>
Tests that are part of NixOS are added to Tests that are part of NixOS are added to
<link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/all-tests.nix"><literal>nixos/tests/all-tests.nix</literal></link>. <link xlink:href="https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/all-tests.nix"><literal>nixos/tests/all-tests.nix</literal></link>.
@ -70,22 +66,26 @@
<literal>all-tests.nix</literal>. <literal>all-tests.nix</literal>.
</para> </para>
<programlisting language="bash"> <programlisting language="bash">
hostname = runTest { imports = [ ./hostname.nix ]; defaults.networking.firewall.enable = false; }; hostname = runTest {
imports = [ ./hostname.nix ];
defaults.networking.firewall.enable = false;
};
</programlisting> </programlisting>
<para> <para>
You can run a test with attribute name <literal>mytest</literal> You can run a test with attribute name
in <literal>nixos/tests/all-tests.nix</literal> by invoking: <literal>hostname</literal> in
<literal>nixos/tests/all-tests.nix</literal> by invoking:
</para> </para>
<programlisting> <programlisting>
cd /my/git/clone/of/nixpkgs cd /my/git/clone/of/nixpkgs
nix-build -A nixosTests.mytest nix-build -A nixosTests.hostname
</programlisting> </programlisting>
</section> </section>
<section xml:id="sec-call-nixos-test-outside-nixos"> <section xml:id="sec-call-nixos-test-outside-nixos">
<title>Testing outside the NixOS project</title> <title>Testing outside the NixOS project</title>
<para> <para>
Outside the <literal>nixpkgs</literal> repository, you can Outside the <literal>nixpkgs</literal> repository, you can
instantiate the test by first acquiring the NixOS library, instantiate the test by first importing the NixOS library,
</para> </para>
<programlisting language="bash"> <programlisting language="bash">
# regular nix # regular nix