Skip to content

Instantly share code, notes, and snippets.

@573
Last active September 16, 2020 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 573/5df7e2e6d72fe31f85a26dd700d08ded to your computer and use it in GitHub Desktop.
Save 573/5df7e2e6d72fe31f85a26dd700d08ded to your computer and use it in GitHub Desktop.
Running integration tests the nix way #howto

This demonstrates using the integration test framework NixOS provides - even on non-NixOS** systems provided nixpkgs is installed.

** Might confuse the reader (intended for novices like me) to go into much detail here, but when running the tests a NixOS instance is indeed spawned, it's just that your host doesn't need to run NixOS on bare metal to have that.

I needed that for github-actions to run an integration test of some haskell redis client.

Without the help of a very active community this wouldn't be feasible for a nix-novice like me to name a few:

Attached are my adaptions of default.nix, server.nix and test.nix (basically a retrofitted redis.nix - link also above - using perl instead of python) from the above tutorial. The config change suggested by Josef goes to ~/.config/nix/nix.conf

BUGS

My basic goal for now is reached though there is still work to do i. e. the server host runs but the redis instance therein not yet runs:

machine# [ 123.548193] byqcih968x56v5p9k2nbn8afzph3ibs0-unit-script-redis-pre-start[673]: install: cannot change owner and permissions of ‘/var/lib/redis’: No such file or directory
machine# [ 123.969505] systemd[1]: redis.service: Control process exited, code=exited, status=1/FAILURE
machine# [ 123.996381] systemd[1]: redis.service: Failed with result 'exit-code'.
machine# [ 124.010802] systemd[1]: Failed to start Redis Server.
machine# [ 124.031742] systemd[1]: redis.service: Consumed 218ms CPU time, no IP traffic.
machine: running command: systemctl --no-pager show "redis"
machine: exit status 0
(1.36 seconds)

{ pkgs ? import <nixpkgs> {} }: {
test = pkgs.nixosTest ./test.nix;
}
# i. e. ~/.config/nix/nix.conf
# added just nixos-test
system-features = kvm nixos-test
{ pkgs, ... }:
{
services.redis.enable = true;
services.redis.unixSocket = "/run/redis/redis.sock";
}
{ ... }:
{
name = "redis";
nodes.machine = ./server.nix;
# this is backported to perl
testScript = ''
startAll;
$machine->waitForUnit("redis");
$machine->waitForOpenPort("6379");
$machine->succeed("redis-cli ping | grep PONG");
$machine->succeed("redis-cli -s /run/redis/redis.sock ping | grep PONG");
'';
}