From d6e54c240979b13d6be8bd6aa75e73325ecbab57 Mon Sep 17 00:00:00 2001 From: Cottand Date: Fri, 27 Sep 2024 00:20:11 +0100 Subject: [PATCH] add nixos tests --- flake.nix | 17 +++++++++---- nixos-tests/custom-dns.nix | 44 +++++++++++++++++++++++++++++++++ nixos-tests/metrics-api.nix | 42 +++++++++++++++++++++++++++++++ nixos-tests/systemctl-start.nix | 34 +++++++++++++++++++++++++ 4 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 nixos-tests/custom-dns.nix create mode 100644 nixos-tests/metrics-api.nix create mode 100644 nixos-tests/systemctl-start.nix diff --git a/flake.nix b/flake.nix index fe905df..22c02a3 100644 --- a/flake.nix +++ b/flake.nix @@ -17,7 +17,7 @@ vendorHash = null; pname = "leng"; version = "1.5.3"; - src = ./.; + src = nixpkgs.lib.sources.cleanSource ./.; }; default = leng; }; @@ -49,8 +49,15 @@ default = leng; }; - })) // + checks = { + inherit (self.packages.${system}) leng; + metrics-api = pkgs.callPackage ./nixos-tests/metrics-api.nix { inherit self; }; + systemctl-start = pkgs.callPackage ./nixos-tests/systemctl-start.nix { inherit self; }; + custom-dns = pkgs.callPackage ./nixos-tests/custom-dns.nix { inherit self; }; + }; + })) + // { nixosModules.default = { pkgs, lib, config, ... }: with lib; @@ -91,7 +98,7 @@ ## implementation config = mkIf cfg.enable { environment = { - etc."leng.toml".source = toml.generate "leng.toml" cfg.configuration; + etc."leng.toml".source = { blocking.sourcesStore = "/var/lib/leng-sources";} // (toml.generate "leng.toml" cfg.configuration); systemPackages = [ cfg.package ]; }; @@ -122,9 +129,9 @@ }; assertions = [ { - assertion = cfg.configuration.blocking.sourcesStore == "/var/lib/leng-sources"; + assertion = (cfg.configuration ? "blocking" && cfg.configuration.blocking ? "sourcesStore"); message = '' - `services.leng.configuration.blocking.sourcesStore` should be set to `var/lib/leng-sources`, but it is set to ${cfg.configuration.blocking.sourcesStore}. + `services.leng.configuration.blocking.sourcesStore` should be set to a directory leng can write to, but it is not set. ''; } ]; diff --git a/nixos-tests/custom-dns.nix b/nixos-tests/custom-dns.nix new file mode 100644 index 0000000..33a167d --- /dev/null +++ b/nixos-tests/custom-dns.nix @@ -0,0 +1,44 @@ +{ self, pkgs, home-manager, ... }: +let + nixpkgs = self.inputs.nixpkgs; +in +(nixpkgs.lib.nixos.runTest { + hostPkgs = pkgs; + defaults.documentation.enable = false; + node.specialArgs = { inherit self; }; + + name = "leng-custom-dns"; + + nodes = { + server = { config, pkgs, ... }: { + imports = [ self.nixosModules.default ]; + # Open the default port for `postgrest` in the firewall + networking.firewall.allowedUDPPorts = [ 53 ]; + + services.leng.enable = true; + services.leng.configuration = { + blocking.sourcesStore = "/tmp"; + customdnsrecords = [ + "example.com IN A 1.2.3.4" + ]; + }; + }; + + client = { pkgs, ... }: { + environment.systemPackages = [ pkgs.dig ]; + }; + }; + + testScript = + '' + start_all() + + server.wait_for_unit("leng", timeout=10) + server.wait_for_open_port(53) + + client.succeed( + "dig @server example.com | grep -o '1.2.3.4' " + ) + ''; + +}).config.result diff --git a/nixos-tests/metrics-api.nix b/nixos-tests/metrics-api.nix new file mode 100644 index 0000000..52acce9 --- /dev/null +++ b/nixos-tests/metrics-api.nix @@ -0,0 +1,42 @@ +{ self, pkgs, home-manager, ... }: +let + nixpkgs = self.inputs.nixpkgs; + httpPort = 9243; +in +(nixpkgs.lib.nixos.runTest { + hostPkgs = pkgs; + defaults.documentation.enable = false; + node.specialArgs = { inherit self; }; + + name = "leng-systemctl-start"; + + nodes = { + server = { config, pkgs, ... }: { + imports = [ self.nixosModules.default ]; + # Open the default port for `postgrest` in the firewall + networking.firewall.allowedTCPPorts = [ httpPort ]; + + services.leng.enable = true; + services.leng.configuration = { + metrics.enabled = true; + api = "0.0.0.0:${toString httpPort}"; + blocking.sourcesStore="/tmp"; + }; + }; + + client = {}; + }; + + testScript = + '' + start_all() + + server.wait_for_unit("leng", timeout=10) + server.wait_for_open_port(${toString httpPort}) + + actual = client.succeed( + "curl http://server:${toString httpPort}/metrics | grep -o 'go_gc_duration_seconds' " + ) + ''; + +}).config.result \ No newline at end of file diff --git a/nixos-tests/systemctl-start.nix b/nixos-tests/systemctl-start.nix new file mode 100644 index 0000000..3a759c2 --- /dev/null +++ b/nixos-tests/systemctl-start.nix @@ -0,0 +1,34 @@ +{ self, pkgs, home-manager, ... }: +let + nixpkgs = self.inputs.nixpkgs; +in +(nixpkgs.lib.nixos.runTest { + hostPkgs = pkgs; + defaults.documentation.enable = false; + node.specialArgs = { inherit self; }; + + name = "leng-metrics-api"; + + nodes = { + server = { config, pkgs, ... }: { + imports = [ self.nixosModules.default ]; + + services.leng.enable = true; + services.leng.configuration = { + blocking.sourcesStore="/tmp"; + }; + }; + + client = { }; + }; + + testScript = + '' + start_all() + + server.wait_for_unit("leng", timeout=10) + + server.succeed("systemctl status leng") + ''; + +}).config.result