Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move nixos tests #7648

Merged
merged 3 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Maintainers: tick if completed or explain if not relevant
- [ ] tests, as appropriate
- functional tests - `tests/**.sh`
- unit tests - `src/*/tests`
- integration tests
- integration tests - `tests/nixos/*`
- [ ] documentation in the manual
- [ ] code and comments are self-explanatory
- [ ] commit message explains why the change was made
Expand Down
55 changes: 20 additions & 35 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,18 @@
};
};

nixos-lib = import (nixpkgs + "/nixos/lib") { };

# https://nixos.org/manual/nixos/unstable/index.html#sec-calling-nixos-tests
runNixOSTestFor = system: test: nixos-lib.runTest {
imports = [ test ];
hostPkgs = nixpkgsFor.${system};
defaults = {
nixpkgs.pkgs = nixpkgsFor.${system};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with the assertion

          assertions = [ { assertion = config.nix.package.drvPath == self.packages.${system}.default.drvPath; message = "Unexpected nix.package"; } ];

and checked that the assertion runs. ✔️
I'm not adding the assertion, because we may want to write a test which uses an older nix daemon. In other words the validity of the assertion is somewhat coincidental.

};
_module.args.nixpkgs = nixpkgs;
};

in {

# A Nixpkgs overlay that overrides the 'nix' and
Expand Down Expand Up @@ -475,49 +487,22 @@
};

# System tests.
tests.remoteBuilds = import ./tests/remote-builds.nix {
system = "x86_64-linux";
inherit nixpkgs;
overlay = self.overlays.default;
};
tests.remoteBuilds = runNixOSTestFor "x86_64-linux" ./tests/nixos/remote-builds.nix;

tests.nix-copy-closure = import ./tests/nix-copy-closure.nix {
system = "x86_64-linux";
inherit nixpkgs;
overlay = self.overlays.default;
};
tests.nix-copy-closure = runNixOSTestFor "x86_64-linux" ./tests/nixos/nix-copy-closure.nix;

tests.nssPreload = (import ./tests/nss-preload.nix rec {
system = "x86_64-linux";
inherit nixpkgs;
overlay = self.overlays.default;
});
tests.nssPreload = runNixOSTestFor "x86_64-linux" ./tests/nixos/nss-preload.nix;

tests.githubFlakes = (import ./tests/github-flakes.nix rec {
system = "x86_64-linux";
inherit nixpkgs;
overlay = self.overlays.default;
});
tests.githubFlakes = runNixOSTestFor "x86_64-linux" ./tests/nixos/github-flakes.nix;

tests.sourcehutFlakes = (import ./tests/sourcehut-flakes.nix rec {
system = "x86_64-linux";
inherit nixpkgs;
overlay = self.overlays.default;
});
tests.sourcehutFlakes = runNixOSTestFor "x86_64-linux" ./tests/nixos/sourcehut-flakes.nix;

tests.containers = (import ./tests/containers.nix rec {
system = "x86_64-linux";
inherit nixpkgs;
overlay = self.overlays.default;
});
tests.containers = runNixOSTestFor "x86_64-linux" ./tests/nixos/containers/containers.nix;

tests.setuid = nixpkgs.lib.genAttrs
["i686-linux" "x86_64-linux"]
(system:
import ./tests/setuid.nix rec {
inherit nixpkgs system;
overlay = self.overlays.default;
});
(system: runNixOSTestFor system ./tests/nixos/setuid.nix);


# Make sure that nix-env still produces the exact same result
# on a particular version of Nixpkgs.
Expand Down
11 changes: 3 additions & 8 deletions tests/containers.nix → tests/nixos/containers/containers.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# Test whether we can run a NixOS container inside a Nix build using systemd-nspawn.
{ nixpkgs, system, overlay }:
{ lib, nixpkgs, ... }:

with import (nixpkgs + "/nixos/lib/testing-python.nix") {
inherit system;
extraConfigurations = [ { nixpkgs.overlays = [ overlay ]; } ];
};

makeTest ({
{
name = "containers";

nodes =
Expand Down Expand Up @@ -65,4 +60,4 @@ makeTest ({
host.succeed("[[ $(cat ./result/msg) = 'Hello World' ]]")
'';

})
}
File renamed without changes.
File renamed without changes.
15 changes: 4 additions & 11 deletions tests/github-flakes.nix → tests/nixos/github-flakes.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
{ nixpkgs, system, overlay }:

with import (nixpkgs + "/nixos/lib/testing-python.nix") {
inherit system;
extraConfigurations = [ { nixpkgs.overlays = [ overlay ]; } ];
};

{ lib, config, nixpkgs, ... }:
let
pkgs = config.nodes.client.nixpkgs.pkgs;

# Generate a fake root CA and a fake api.github.com / github.com / channels.nixos.org certificate.
cert = pkgs.runCommand "cert" { buildInputs = [ pkgs.openssl ]; }
cert = pkgs.runCommand "cert" { nativeBuildInputs = [ pkgs.openssl ]; }
''
mkdir -p $out

Expand Down Expand Up @@ -92,8 +87,6 @@ let
'';
in

makeTest (

{
name = "github-flakes";

Expand Down Expand Up @@ -207,4 +200,4 @@ makeTest (
client.succeed("nix build nixpkgs#fuse --tarball-ttl 0")
'';

})
}
17 changes: 10 additions & 7 deletions tests/nix-copy-closure.nix → tests/nixos/nix-copy-closure.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Test ‘nix-copy-closure’.

{ nixpkgs, system, overlay }:
{ lib, config, nixpkgs, hostPkgs, ... }:

with import (nixpkgs + "/nixos/lib/testing-python.nix") {
inherit system;
extraConfigurations = [ { nixpkgs.overlays = [ overlay ]; } ];
};
let
pkgs = config.nodes.client.nixpkgs.pkgs;

makeTest (let pkgA = pkgs.cowsay; pkgB = pkgs.wget; pkgC = pkgs.hello; pkgD = pkgs.tmux; in {
pkgA = pkgs.cowsay;
pkgB = pkgs.wget;
pkgC = pkgs.hello;
pkgD = pkgs.tmux;

in {
name = "nix-copy-closure";

nodes =
Expand Down Expand Up @@ -74,4 +77,4 @@ makeTest (let pkgA = pkgs.cowsay; pkgB = pkgs.wget; pkgC = pkgs.hello; pkgD = pk
# )
# client.succeed("nix-store --check-validity ${pkgC}")
'';
})
}
16 changes: 6 additions & 10 deletions tests/nss-preload.nix → tests/nixos/nss-preload.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{ nixpkgs, system, overlay }:

with import (nixpkgs + "/nixos/lib/testing-python.nix") {
inherit system;
extraConfigurations = [ { nixpkgs.overlays = [ overlay ]; } ];
};
{ lib, config, nixpkgs, ... }:

let

pkgs = config.nodes.client.nixpkgs.pkgs;

nix-fetch = pkgs.writeText "fetch.nix" ''
derivation {
# This derivation is an copy from what is available over at
Expand Down Expand Up @@ -41,9 +39,7 @@ let
'';
in

makeTest (

rec {
{
name = "nss-preload";

nodes = {
Expand Down Expand Up @@ -122,4 +118,4 @@ rec {
nix-build ${nix-fetch} >&2
""")
'';
})
}
14 changes: 4 additions & 10 deletions tests/remote-builds.nix → tests/nixos/remote-builds.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
# Test Nix's remote build feature.

{ nixpkgs, system, overlay }:

with import (nixpkgs + "/nixos/lib/testing-python.nix") {
inherit system;
extraConfigurations = [ { nixpkgs.overlays = [ overlay ]; } ];
};

makeTest (
{ config, lib, hostPkgs, ... }:

let
pkgs = config.nodes.client.nixpkgs.pkgs;

# The configuration of the remote builders.
builder =
Expand Down Expand Up @@ -75,7 +69,7 @@ in

# Create an SSH key on the client.
subprocess.run([
"${pkgs.openssh}/bin/ssh-keygen", "-t", "ed25519", "-f", "key", "-N", ""
"${hostPkgs.openssh}/bin/ssh-keygen", "-t", "ed25519", "-f", "key", "-N", ""
], capture_output=True, check=True)
client.succeed("mkdir -p -m 700 /root/.ssh")
client.copy_from_host("key", "/root/.ssh/id_ed25519")
Expand Down Expand Up @@ -109,4 +103,4 @@ in
builder1.block()
client.succeed("nix-build ${expr nodes.client.config 4}")
'';
})
}
11 changes: 5 additions & 6 deletions tests/setuid.nix → tests/nixos/setuid.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# Verify that Linux builds cannot create setuid or setgid binaries.

{ nixpkgs, system, overlay }:
{ lib, config, nixpkgs, ... }:

with import (nixpkgs + "/nixos/lib/testing-python.nix") {
inherit system;
extraConfigurations = [ { nixpkgs.overlays = [ overlay ]; } ];
};
let
pkgs = config.nodes.machine.nixpkgs.pkgs;

makeTest {
in
{
name = "setuid";

nodes.machine =
Expand Down
14 changes: 4 additions & 10 deletions tests/sourcehut-flakes.nix → tests/nixos/sourcehut-flakes.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{ nixpkgs, system, overlay }:

with import (nixpkgs + "/nixos/lib/testing-python.nix")
{
inherit system;
extraConfigurations = [{ nixpkgs.overlays = [ overlay ]; }];
};
{ lib, config, hostPkgs, nixpkgs, ... }:

let
pkgs = config.nodes.sourcehut.nixpkgs.pkgs;

# Generate a fake root CA and a fake git.sr.ht certificate.
cert = pkgs.runCommand "cert" { buildInputs = [ pkgs.openssl ]; }
''
Expand Down Expand Up @@ -64,8 +60,6 @@ let

in

makeTest (

{
name = "sourcehut-flakes";

Expand Down Expand Up @@ -164,4 +158,4 @@ makeTest (
client.succeed("nix build nixpkgs#fuse --tarball-ttl 0")
'';

})
}