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

nixos-module: Test stop behavior, Type=oneshot? #251

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
20 changes: 16 additions & 4 deletions nixos-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,22 @@ let
cfg.docker.client.package
];
environment.ARION_PREBUILT = config.settings.out.dockerComposeYaml;
script = ''
echo 1>&2 "docker compose file: $ARION_PREBUILT"
arion --prebuilt-file "$ARION_PREBUILT" up
'';
serviceConfig.Type = "oneshot";
serviceConfig.RemainAfterExit = true;
serviceConfig.ExecStart = [
(lib.getExe (pkgs.writeScriptBin "nixos-arion-start" ''
#!${pkgs.runtimeShell}
echo 1>&2 "starting arion project: $ARION_PREBUILT"
arion --prebuilt-file "$ARION_PREBUILT" up --detach
''))
];
serviceConfig.ExecStop = [
(lib.getExe (pkgs.writeScriptBin "nixos-arion-stop" ''
#!${pkgs.runtimeShell}
echo 1>&2 "stopping arion project: $ARION_PREBUILT"
arion --prebuilt-file "$ARION_PREBUILT" down
''))
];
};
};
};
Expand Down
8 changes: 4 additions & 4 deletions tests/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
};

# Currently broken; kafka can't reach zookeeper
# nixosModuleWithPodman =
# import ./nixos-virtualization-arion-test/test.nix final {
# virtualisation.arion.backend = "podman-socket";
# };
nixosModuleWithPodman =
import ./nixos-virtualization-arion-test/test.nix final {
virtualisation.arion.backend = "podman-socket";
};

testWithPodman =
nixosTest (import ./arion-test { usePodman = true; pkgs = final; });
Expand Down
26 changes: 25 additions & 1 deletion tests/nixos-virtualization-arion-test/arion-compose.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, ... }: {
{ lib, pkgs, ... }: {
project.name = "whale";

docker-compose.raw = {
Expand Down Expand Up @@ -59,4 +59,28 @@
"start-foreground"
];
};

services.stop-probe = {
image.command = [
(lib.getExe (pkgs.writeScriptBin "stop-probe" ''
#!${pkgs.runtimeShell}
touch /diagnostics/stop-probe-started
onSIGTERM() {
echo "Handling SIGTERM"
touch /diagnostics/stop-probe-terminated-cleanly
echo "Bye!"
}
echo "Registering SIGTERM handler"
trap onSIGTERM SIGTERM
sleep 3600
''))
];
service.useHostStore = true;
service.volumes = [
"/tmp/shared:/diagnostics"
];
service.environment = {
"PATH" = lib.makeBinPath [ pkgs.busybox ];
};
};
}
40 changes: 36 additions & 4 deletions tests/nixos-virtualization-arion-test/test.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pkgs: module:

pkgs.nixosTest {
pkgs.testers.runNixOSTest ({ lib, ... }:{
name = "test-basic-arion-kafka";
nodes = {
machine = { ... }: {
Expand All @@ -16,10 +16,13 @@ pkgs.nixosTest {
};
};
};
testScript = ''
testScript = { nodes, ... }: ''
machine.wait_for_unit("sockets.target")
machine.wait_for_unit("arion-whale.service")

${# TODO: make the kafka service work on podman-socket; some networking issue
lib.optionalString (nodes.machine.virtualisation.arion.backend != "podman-socket") ''

machine.succeed("""
(echo "hello"; echo "world") \
| ${pkgs.apacheKafka}/bin/kafka-console-producer.sh \
Expand All @@ -35,6 +38,35 @@ pkgs.nixosTest {
) | grep --line-buffered hello | { read; kill $(<pid); rm pid; }
) 2>/dev/console
""")


# make sure logs were captured
machine.succeed("""
# Check that messages were logged with field "CONTAINER_NAME" set to "whale-zookeeper-1"
journalctl --output json | ${pkgs.jq}/bin/jq 'select(.CONTAINER_NAME=="whale-zookeeper-1") | .MESSAGE' | grep -F 'org.apache.zookeeper'
""")

''}

machine.wait_until_succeeds("""
journalctl --grep 'Registering SIGTERM handler' >/dev/null
""")

# explore the shared mounts, as they're undocumented
machine.succeed("""
mount >&2
touch /tmp/xchg/this-is-xchg
touch /tmp/shared/this-is-shared
""")

machine.shutdown()

# show what's in machine.shared_dir by running `ls` on the host
dir = machine.shared_dir
import os
print(f'Contents of {dir}:')
os.system(f'ls -l {dir}')
# dir/stop-probe-terminated-cleanly must exist
assert os.path.exists(f'{dir}/stop-probe-terminated-cleanly'), f'{dir}/stop-probe-terminated-cleanly does not exist'

'';
}
})