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

test: add integration nixos tests #63

Merged
merged 1 commit into from
Sep 26, 2024
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
17 changes: 12 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
vendorHash = null;
pname = "leng";
version = "1.5.3";
src = ./.;
src = nixpkgs.lib.sources.cleanSource ./.;
};
default = leng;
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ];
};

Expand Down Expand Up @@ -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.
'';
}
];
Expand Down
44 changes: 44 additions & 0 deletions nixos-tests/custom-dns.nix
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions nixos-tests/metrics-api.nix
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions nixos-tests/systemctl-start.nix
Original file line number Diff line number Diff line change
@@ -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
Loading