1# the ntfy-sh module was switching to DynamicUser=true. this test assures that
2# the migration does not break existing setups.
3#
4# this test works doing a migration and asserting ntfy-sh runs properly. first,
5# ntfy-sh is configured to use a static user and group. then ntfy-sh is
6# started and tested. after that, ntfy-sh is shut down and a systemd drop
7# in configuration file is used to update the service configuration to use
8# DynamicUser=true. then the ntfy-sh is started again and tested.
9
10import ./make-test-python.nix {
11 name = "ntfy-sh";
12
13 nodes.machine =
14 {
15 lib,
16 pkgs,
17 ...
18 }:
19 {
20 environment.etc."ntfy-sh-dynamic-user.conf".text = ''
21 [Service]
22 Group=new-ntfy-sh
23 User=new-ntfy-sh
24 DynamicUser=true
25 '';
26
27 services.ntfy-sh.enable = true;
28 services.ntfy-sh.settings.base-url = "http://localhost:2586";
29
30 systemd.services.ntfy-sh.serviceConfig = {
31 DynamicUser = lib.mkForce false;
32 ExecStartPre = [
33 "${pkgs.coreutils}/bin/id"
34 "${pkgs.coreutils}/bin/ls -lahd /var/lib/ntfy-sh/"
35 "${pkgs.coreutils}/bin/ls -lah /var/lib/ntfy-sh/"
36 ];
37 Group = lib.mkForce "old-ntfy-sh";
38 User = lib.mkForce "old-ntfy-sh";
39 };
40
41 users.users.old-ntfy-sh = {
42 isSystemUser = true;
43 group = "old-ntfy-sh";
44 };
45
46 users.groups.old-ntfy-sh = { };
47 };
48
49 testScript = ''
50 import json
51
52 msg = "Test notification"
53
54 def test_ntfysh():
55 machine.wait_for_unit("ntfy-sh.service")
56 machine.wait_for_open_port(2586)
57
58 machine.succeed(f"curl -d '{msg}' localhost:2586/test")
59
60 text = machine.succeed("curl -s localhost:2586/test/json?poll=1")
61 for line in text.splitlines():
62 notif = json.loads(line)
63 assert msg == notif["message"], "Wrong message"
64
65 machine.succeed("ntfy user list")
66
67 machine.wait_for_unit("multi-user.target")
68
69 test_ntfysh()
70
71 machine.succeed("systemctl stop ntfy-sh.service")
72 machine.succeed("mkdir -p /run/systemd/system/ntfy-sh.service.d")
73 machine.succeed("cp /etc/ntfy-sh-dynamic-user.conf /run/systemd/system/ntfy-sh.service.d/dynamic-user.conf")
74 machine.succeed("systemctl daemon-reload")
75 machine.succeed("systemctl start ntfy-sh.service")
76
77 test_ntfysh()
78 '';
79}