summaryrefslogtreecommitdiffstats
path: root/lib/services/test.nix
blob: 624e34ff855e5ee7c40f24d14c13a71c502473d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Run:
#   nix-instantiate --eval lib/services/test.nix
let
  lib = import ../.;

  inherit (lib) mkOption types;

  portable-lib = lib.services;

  dummyPkg =
    name:
    derivation {
      system = "dummy";
      name = name;
      builder = "/bin/false";
    };

  configured = portable-lib.configure {
    # `coreutils` is the only package the portable layer legitimately needs, to derive
    # `process.reloadCommand` from `process.reloadSignal`. Anything else reaching for
    # `pkgs` fails on the missing attribute.
    serviceManagerPkgs = {
      coreutils = dummyPkg "coreutils";
    };
    extraRootModules = [ ];
    extraRootSpecialArgs = { };
  };

  exampleConfig = {
    services = {
      service1 = {
        process = {
          argv = [
            "/usr/bin/echo" # *giggles*
            "hello"
          ];
        };
        assertions = [
          {
            assertion = false;
            message = "you can't enable this for that reason";
          }
        ];
        warnings = [
          "The `foo' service is deprecated and will go away soon!"
        ];
      };
      service2 = {
        process = {
          # No meta.mainProgram, because it's supposedly an executable script _file_,
          # not a directory with a bin directory containing the main program.
          argv = [
            (dummyPkg "cowsay.sh")
            "world"
          ];
          reloadCommand = "${dummyPkg "cowsay.sh"} reload";
        };
      };
      service3 = {
        process = {
          argv = [ "/bin/false" ];
        };
        services.exclacow = {
          process = {
            argv = [
              (lib.getExe (
                dummyPkg "cowsay-ng"
                // {
                  meta.mainProgram = "cowsay";
                }
              ))
              "!"
            ];
            reloadSignal = "HUP";
          };
          assertions = [
            {
              assertion = false;
              message = "you can't enable this for such reason";
            }
          ];
          warnings = [
            "The `bar' service is deprecated and will go away soon!"
          ];
        };
      };
      # The default `flagFormat`, and one flag of every supported value kind.
      flagsDefault = {
        process = {
          argv = [ "/bin/flagged" ];
          flags = {
            "--bool-off" = false;
            "--bool-on" = true;
            "--config" = ./test.nix;
            "--count" = 3;
            "--name" = "example";
            "--unset" = null;
          };
        };
      };
      # A `flagFormat` that joins with `=` and spells out booleans.
      flagsCustomFormat = {
        process = {
          argv = [ "/bin/flagged" ];
          flagFormat = name: {
            option = "--${name}";
            sep = "=";
            explicitBool = true;
          };
          flags = {
            port = 8080;
            quiet = false;
            verbose = true;
          };
        };
      };
      # The list form, which allows a flag to be repeated.
      flagsRepeated = {
        process = {
          argv = [ "/bin/flagged" ];
          flags = [
            { "--host" = "a"; }
            { "--host" = "b"; }
          ];
        };
      };
      # `argv` and `flags` share one `lib.mkOrder` space.
      flagsOrdering = {
        process = {
          argv = lib.mkMerge [
            (lib.mkBefore [ "/bin/gt" ])
            (lib.mkOrder 800 [ "server" ])
            (lib.mkAfter [ "TRAILING" ])
          ];
          flags = lib.mkMerge [
            { "--listen" = "a"; }
            { "--disable-landlock" = lib.mkOrder 600 true; }
          ];
        };
      };
    };
  };

  exampleEval = lib.evalModules {
    modules = [
      {
        options.services = mkOption {
          type = types.attrsOf configured.serviceSubmodule;
        };
      }
      exampleConfig
    ];
  };

  # Every service carries some assertions that hold; only the violated ones are of interest here.
  failures = lib.filter (a: !a.assertion);

  # The goal of this test is to perform and check a complete evaluation of all options.
  # Only filter out values that are truly infeasible to check, such as function values.
  filterEval =
    config:
    lib.optionalAttrs (config ? process) {
      inherit (config) warnings;
      assertions = failures config.assertions;
      # `flagFormat` is a function and cannot be compared; the rest of `process` is checked.
      process = { inherit (config.process) argv reloadCommand reloadSignal; };
    }
    // {
      services = lib.mapAttrs (k: filterEval) config.services;
    };

  test =
    assert
      filterEval exampleEval.config == {
        services = {
          service1 = {
            process = {
              argv = [
                "/usr/bin/echo"
                "hello"
              ];
              reloadCommand = null;
              reloadSignal = null;
            };
            services = { };
            assertions = [
              {
                assertion = false;
                message = "you can't enable this for that reason";
              }
            ];
            warnings = [
              "The `foo' service is deprecated and will go away soon!"
            ];
          };
          service2 = {
            process = {
              argv = [
                "${dummyPkg "cowsay.sh"}"
                "world"
              ];
              reloadCommand = "${dummyPkg "cowsay.sh"} reload";
              reloadSignal = null;
            };
            services = { };
            assertions = [ ];
            warnings = [ ];
          };
          service3 = {
            process = {
              argv = [ "/bin/false" ];
              reloadCommand = null;
              reloadSignal = null;
            };
            services.exclacow = {
              process = {
                argv = [
                  "${dummyPkg "cowsay-ng"}/bin/cowsay"
                  "!"
                ];
                reloadCommand = "${dummyPkg "coreutils"}/bin/kill -HUP $MAINPID";
                reloadSignal = "HUP";
              };
              services = { };
              assertions = [
                {
                  assertion = false;
                  message = "you can't enable this for such reason";
                }
              ];
              warnings = [ "The `bar' service is deprecated and will go away soon!" ];
            };
            assertions = [ ];
            warnings = [ ];
          };
          flagsDefault = {
            process = {
              argv = [
                "/bin/flagged"
                "--bool-on"
                "--config"
                "${./test.nix}"
                "--count"
                "3"
                "--name"
                "example"
              ];
              reloadCommand = null;
              reloadSignal = null;
            };
            services = { };
            assertions = [ ];
            warnings = [ ];
          };
          flagsCustomFormat = {
            process = {
              argv = [
                "/bin/flagged"
                "--port=8080"
                "--quiet=false"
                "--verbose=true"
              ];
              reloadCommand = null;
              reloadSignal = null;
            };
            services = { };
            assertions = [ ];
            warnings = [ ];
          };
          flagsRepeated = {
            process = {
              argv = [
                "/bin/flagged"
                "--host"
                "a"
                "--host"
                "b"
              ];
              reloadCommand = null;
              reloadSignal = null;
            };
            services = { };
            assertions = [ ];
            warnings = [ ];
          };
          flagsOrdering = {
            process = {
              argv = [
                "/bin/gt"
                "--disable-landlock"
                "server"
                "--listen"
                "a"
                "TRAILING"
              ];
              reloadCommand = null;
              reloadSignal = null;
            };
            services = { };
            assertions = [ ];
            warnings = [ ];
          };
        };
      };

    assert
      portable-lib.getWarnings [ "service1" ] exampleEval.config.services.service1 == [
        "in service1: The `foo' service is deprecated and will go away soon!"
      ];

    assert
      failures (portable-lib.getAssertions [ "service1" ] exampleEval.config.services.service1) == [
        {
          message = "in service1: you can't enable this for that reason";
          assertion = false;
        }
      ];

    assert
      portable-lib.getWarnings [ "service3" ] exampleEval.config.services.service3 == [
        "in service3.services.exclacow: The `bar' service is deprecated and will go away soon!"
      ];
    assert
      failures (portable-lib.getAssertions [ "service3" ] exampleEval.config.services.service3) == [
        {
          message = "in service3.services.exclacow: you can't enable this for such reason";
          assertion = false;
        }
      ];

    "ok";

in
test