From 1e67e0badbb88076846fbe3d50081079e1f7221f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Sun, 19 Nov 2023 09:23:50 +0100 Subject: [PATCH] add nixos test --- checks/flake-module.nix | 15 +++++++++++++++ checks/lib.nix | 20 ++++++++++++++++++++ checks/test.nix | 35 +++++++++++++++++++++++++++++++++++ flake.nix | 1 + 4 files changed, 71 insertions(+) create mode 100644 checks/flake-module.nix create mode 100644 checks/lib.nix create mode 100644 checks/test.nix diff --git a/checks/flake-module.nix b/checks/flake-module.nix new file mode 100644 index 0000000..d205b1b --- /dev/null +++ b/checks/flake-module.nix @@ -0,0 +1,15 @@ +{ self, ... }: { + perSystem = + { pkgs + , ... + }: { + checks = + let + # this gives us a reference to our flake but also all flake inputs + checkArgs = { inherit self pkgs; }; + in + { + test = import ./test.nix checkArgs; + }; + }; +} diff --git a/checks/lib.nix b/checks/lib.nix new file mode 100644 index 0000000..f930aa7 --- /dev/null +++ b/checks/lib.nix @@ -0,0 +1,20 @@ +# tests/lib.nix +# 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/checks/test.nix b/checks/test.nix new file mode 100644 index 0000000..07c2b6f --- /dev/null +++ b/checks/test.nix @@ -0,0 +1,35 @@ +(import ./lib.nix) { + name = "nixos-wiki"; + nodes = { + # `self` here is set by using specialArgs in `lib.nix` + wiki = { self, pkgs, config, ... }: { + imports = [ + self.nixosModules.nixos-wiki + ]; + security.acme.defaults.email = "admin@example.com"; + services.nixos-wiki = { + hostname = "nixos-wiki.example.com"; + adminPasswordFile = pkgs.writeText "adminPasswordFile" "Creation-Fabric-Untrimmed3"; + githubClientId = "Iv1.95ed182c83df1d22"; + githubClientSecretFile = pkgs.writeText "githubClientSecretFile" "secret"; + emergencyContact = "nixos-wiki@thalheim.io"; + passwordSender = "nixos-wiki@thalheim.io"; + noReplyAddress = "nixos-wiki-no-reply@thalheim.io"; + }; + services.nginx.virtualHosts.${config.services.mediawiki.nginx.hostName} = { + enableACME = false; + forceSSL = false; + }; + }; + }; + # This is the test code that will check if our service is running correctly: + testScript = '' + start_all() + + machine.wait_for_unit("phpfpm-mediawiki.service") + machine.wait_for_unit("nginx.service") + + page = machine.succeed("curl -vL http://localhost/") + assert "MediaWiki has been installed" in page + ''; +} diff --git a/flake.nix b/flake.nix index e96c281..58c20dd 100644 --- a/flake.nix +++ b/flake.nix @@ -34,6 +34,7 @@ inputs.treefmt-nix.flakeModule ./targets/flake-module.nix ./modules/flake-module.nix + ./checks/flake-module.nix ]; perSystem = { config, pkgs, ... }: { treefmt = {