From 54c0e8a46b1157bf6b531c7345b25b16d439fadc Mon Sep 17 00:00:00 2001 From: kalipso Date: Tue, 31 Oct 2023 15:01:35 +0100 Subject: [PATCH] [nix] add base integration test run it using 'nix flake check -L' --- flake.nix | 9 +++++++++ test/lib.nix | 21 +++++++++++++++++++++ test/test.nix | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 test/lib.nix create mode 100644 test/test.nix diff --git a/flake.nix b/flake.nix index 9547c5c..ddee458 100644 --- a/flake.nix +++ b/flake.nix @@ -176,5 +176,14 @@ program = builtins.toString (nixpkgs.legacyPackages."x86_64-linux".writeScript "docs" '' ${pkgs.python3}/bin/python3 -m http.server --directory ${self.packages."x86_64-linux".docs}/share/doc''); }; + + checks = forAllSystems (system: let + checkArgs = { + pkgs = nixpkgs.legacyPackages.${system}; + inherit self; + }; + in { + gokill = import ./test/test.nix checkArgs; + }); }; } diff --git a/test/lib.nix b/test/lib.nix new file mode 100644 index 0000000..6e46893 --- /dev/null +++ b/test/lib.nix @@ -0,0 +1,21 @@ +# tests/lib.nix +# based on https://blog.thalheim.io/2023/01/08/how-to-use-nixos-testing-framework-with-flakes/ +# The first argument to this function is the test module itself +test: +# These arguments are provided by `flake.nix` on import, see checkArgs +{ pkgs, self}: +let + inherit (pkgs) lib; + # this imports the nixos library that contains our testing framework + nixos-lib = import (pkgs.path + "/nixos/lib") {}; +in +(nixos-lib.runTest { + hostPkgs = pkgs; + # This speeds up the evaluation by skipping evaluating documentation (optional) + defaults.documentation.enable = lib.mkDefault false; + # This makes `self` available in the NixOS configuration of our virtual machines. + # This is useful for referencing modules or packages from your own flake + # as well as importing from other flakes. + node.specialArgs = { inherit self; }; + imports = [ test ]; +}).config.result diff --git a/test/test.nix b/test/test.nix new file mode 100644 index 0000000..130eb8b --- /dev/null +++ b/test/test.nix @@ -0,0 +1,42 @@ +# ./tests/hello-world-server.nix +(import ./lib.nix) { + name = "gokill-base-test"; + nodes = { + # `self` here is set by using specialArgs in `lib.nix` + node1 = { self, pkgs, ... }: { + imports = [ self.nixosModules.gokill ]; + + services.gokill = { + enable = true; + triggers = [ + { + type = "Timeout"; + name = "custom timeout"; + options = { + duration = 10; + }; + actions = [ + { + type = "Command"; + options = { + command = "echo hello world"; + }; + stage = 2; + } + ]; + } + ]; + }; + }; + }; + + testScript = '' + import time + start_all() # wait for our service to start + node1.wait_for_unit("gokill") + time.sleep(11) + output = node1.succeed("journalctl -u gokill.service | tail -n 2 | head -n 1") + # Check if our webserver returns the expected result + assert "hellow world" in output + ''; +}