# `vmTests..container-deferred-start`: a service container whose daemon # takes longer to start than the container's one-minute start limit, with and # without the daemon named in `services.hyperhive.swarmContainer.startAfterReady` # (../container-modules/swarm-container.nix). The daemon stands in for tuwunel: # a `Type=notify` unit wanted by `multi-user.target` whose start, a pause of 90 s # before it reports ready, is longer than the limit. # # What it pins: # - `old`, the daemon in the boot as nixpkgs ships it: the container's start # job fails with `start operation timed out`, which is what pr1ma's # homeserver does at every attempt (the control); # - `new`, the daemon named in `startAfterReady`: the container's start job # ends inside the limit with the daemon still `activating`, the daemon is # `active` once its pause is over, and the container's unit was never # restarted; # - a unit that depends on the daemon and retries (the shape of the homeserver's # clients) fails while the daemon starts and succeeds once it is ready. # # The module-eval suite pins the same wiring on the real homeserver. { pkgs }: let # What each container's daemon waits before it reports ready: past the # container's 60 s limit with the guest's own boot added. pause = 90; daemon = pkgs.writeShellScript "slow-daemon" '' ${pkgs.coreutils}/bin/sleep ${toString pause} ${pkgs.systemd}/bin/systemd-notify --ready exec ${pkgs.coreutils}/bin/sleep infinity ''; guest = { deferred }: { imports = [ ../container-modules/swarm-container.nix ]; system.stateVersion = "26.05"; services.hyperhive.swarmContainer = { privateNetwork = false; startAfterReady = pkgs.lib.optional deferred "slow"; }; systemd.services.slow = { wantedBy = [ "multi-user.target" ]; serviceConfig = { Type = "notify"; NotifyAccess = "all"; ExecStart = "${daemon}"; TimeoutStartSec = "30min"; }; }; # Retries until the daemon is up, the way the homeserver's clients do. systemd.services.client = { wantedBy = [ "multi-user.target" ]; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; TimeoutStartSec = 20; ExecStart = "${pkgs.systemd}/bin/systemctl is-active --quiet slow.service"; Restart = "on-failure"; RestartSec = 5; }; }; }; in pkgs.testers.runNixOSTest { name = "container-deferred-start"; # Without this the driver allows an hour, the runner's whole job limit. globalTimeout = 900; nodes.host = { ... }: { containers.old = { autoStart = false; config = guest { deferred = false; }; }; containers.new = { autoStart = false; config = guest { deferred = true; }; }; }; testScript = '' host.start() host.wait_for_unit("multi-user.target") def inside(name: str, cmd: str) -> str: return host.succeed(f"nixos-container run {name} -- {cmd}").strip() with subtest("control: a daemon in the boot that outlasts the limit fails the container's start"): host.fail("systemctl start container@old.service") out = host.succeed("journalctl -u container@old.service --no-pager -o cat") assert "start operation timed out" in out, out assert "Failed with result 'timeout'" in out, out # The failed start leaves nothing to reset: stopping unloads the unit. host.succeed("systemctl stop container@old.service") state = host.execute("systemctl is-active container@old.service")[1].strip() assert state == "inactive", state with subtest("a daemon named in startAfterReady: the container's start ends inside the limit"): host.succeed("systemctl start container@new.service") out = host.succeed("journalctl -u container@new.service --no-pager -o cat") assert "start operation timed out" not in out, out host.succeed("systemctl is-active container@new.service") # The guest queues the daemon's start job once its boot is over, which is # just after the container's start ends. host.wait_until_succeeds( "test \"$(nixos-container run new -- systemctl show -p ActiveState --value slow.service)\" = activating", timeout=60, ) # The starter says which state the boot ended in, next to the start. out = inside("new", "journalctl -u swarm-container-deferred-start.service --no-pager -o cat") assert "swarm-container-deferred-start: the boot ended " in out, out assert "starting slow" in out, out with subtest("the dependant fails while the daemon starts"): assert inside("new", "systemctl show -p ActiveState --value client.service") != "active" with subtest("the daemon finishes its start, and the dependant recovers"): host.wait_until_succeeds("nixos-container run new -- systemctl is-active --quiet slow.service", timeout=300) host.wait_until_succeeds("nixos-container run new -- systemctl is-active --quiet client.service", timeout=60) out = host.succeed("journalctl -u container@new.service --no-pager -o cat") assert "start operation timed out" not in out, out host.succeed("systemctl is-active container@new.service") restarts = host.succeed("systemctl show -p NRestarts --value container@new.service").strip() assert restarts == "0", restarts host.succeed("systemctl stop container@new.service") ''; }