-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
/
switch-test.nix
1449 lines (1295 loc) · 68.3 KB
/
switch-test.nix
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Test configuration switching.
import ./make-test-python.nix ({ lib, pkgs, ng, ...} : let
# Simple service that can either be socket-activated or that will
# listen on port 1234 if not socket-activated.
# A connection to the socket causes 'hello' to be written to the client.
socketTest = pkgs.writeScript "socket-test.py" /* python */ ''
#!${pkgs.python3}/bin/python3
from socketserver import TCPServer, StreamRequestHandler
import socket
import os
class Handler(StreamRequestHandler):
def handle(self):
self.wfile.write("hello".encode("utf-8"))
class Server(TCPServer):
def __init__(self, server_address, handler_cls):
listenFds = os.getenv('LISTEN_FDS')
if listenFds is None or int(listenFds) < 1:
print(f'Binding to {server_address}')
TCPServer.__init__(
self, server_address, handler_cls, bind_and_activate=True)
else:
TCPServer.__init__(
self, server_address, handler_cls, bind_and_activate=False)
# Override socket
print(f'Got activated by {os.getenv("LISTEN_FDNAMES")} '
f'with {listenFds} FDs')
self.socket = socket.fromfd(3, self.address_family,
self.socket_type)
if __name__ == "__main__":
server = Server(("localhost", 1234), Handler)
server.serve_forever()
'';
in {
name = "switch-test";
meta = with pkgs.lib.maintainers; {
maintainers = [ gleber das_j ];
};
nodes = {
machine = { pkgs, lib, ... }: {
system.switch.enableNg = ng;
environment.systemPackages = [ pkgs.socat ]; # for the socket activation stuff
users.mutableUsers = false;
# Test that no boot loader still switches, e.g. in the ISO
boot.loader.grub.enable = false;
specialisation = rec {
brokenInitInterface.configuration.config.system.extraSystemBuilderCmds = ''
echo "systemd 0" > $out/init-interface-version
'';
modifiedSystemConf.configuration.systemd.extraConfig = ''
# Hello world!
'';
addedMount.configuration.virtualisation.fileSystems."/test" = {
device = "tmpfs";
fsType = "tmpfs";
};
addedMountOptsModified.configuration = {
imports = [ addedMount.configuration ];
virtualisation.fileSystems."/test".options = [ "x-test" ];
};
addedMountDevModified.configuration = {
imports = [ addedMountOptsModified.configuration ];
virtualisation.fileSystems."/test".device = lib.mkForce "ramfs";
};
storeMountModified.configuration = {
virtualisation.fileSystems."/".device = lib.mkForce "auto";
};
swap.configuration.swapDevices = lib.mkVMOverride [
{ device = "/swapfile"; size = 1; }
];
simpleService.configuration = {
systemd.services.test = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
ExecReload = "${pkgs.coreutils}/bin/true";
};
};
};
simpleServiceSeparateActivationScript.configuration = {
system.activatable = false;
systemd.services.test = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
ExecReload = "${pkgs.coreutils}/bin/true";
};
};
};
simpleServiceDifferentDescription.configuration = {
imports = [ simpleService.configuration ];
systemd.services.test.description = "Test unit";
};
simpleServiceModified.configuration = {
imports = [ simpleService.configuration ];
systemd.services.test.serviceConfig.X-Test = true;
};
simpleServiceNostop.configuration = {
imports = [ simpleService.configuration ];
systemd.services.test.stopIfChanged = false;
};
simpleServiceReload.configuration = {
imports = [ simpleService.configuration ];
systemd.services.test = {
reloadIfChanged = true;
serviceConfig.ExecReload = "${pkgs.coreutils}/bin/true";
};
};
simpleServiceNorestart.configuration = {
imports = [ simpleService.configuration ];
systemd.services.test.restartIfChanged = false;
};
simpleServiceFailing.configuration = {
imports = [ simpleServiceModified.configuration ];
systemd.services.test.serviceConfig.ExecStart = lib.mkForce "${pkgs.coreutils}/bin/false";
};
autorestartService.configuration = {
# A service that immediately goes into restarting (but without failing)
systemd.services.autorestart = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
Restart = "always";
RestartSec = "20y"; # Should be long enough
ExecStart = "${pkgs.coreutils}/bin/true";
};
};
};
autorestartServiceFailing.configuration = {
imports = [ autorestartService.configuration ];
systemd.services.autorestart.serviceConfig = {
ExecStart = lib.mkForce "${pkgs.coreutils}/bin/false";
};
};
simpleServiceWithExtraSection.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.packages = [ (pkgs.writeTextFile {
name = "systemd-extra-section";
destination = "/etc/systemd/system/test.service";
text = ''
[X-Test]
X-Test-Value=a
'';
}) ];
};
simpleServiceWithExtraSectionOtherName.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.packages = [ (pkgs.writeTextFile {
name = "systemd-extra-section";
destination = "/etc/systemd/system/test.service";
text = ''
[X-Test2]
X-Test-Value=a
'';
}) ];
};
simpleServiceWithInstallSection.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.packages = [ (pkgs.writeTextFile {
name = "systemd-extra-section";
destination = "/etc/systemd/system/test.service";
text = ''
[Install]
WantedBy=multi-user.target
'';
}) ];
};
simpleServiceWithExtraKey.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.services.test.serviceConfig."X-Test" = "test";
};
simpleServiceWithExtraKeyOtherValue.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.services.test.serviceConfig."X-Test" = "test2";
};
simpleServiceWithExtraKeyOtherName.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.services.test.serviceConfig."X-Test2" = "test";
};
simpleServiceReloadTrigger.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.services.test.reloadTriggers = [ "/dev/null" ];
};
simpleServiceReloadTriggerModified.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.services.test.reloadTriggers = [ "/dev/zero" ];
};
simpleServiceReloadTriggerModifiedAndSomethingElse.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.services.test = {
reloadTriggers = [ "/dev/zero" ];
serviceConfig."X-Test" = "test";
};
};
simpleServiceReloadTriggerModifiedSomethingElse.configuration = {
imports = [ simpleServiceNostop.configuration ];
systemd.services.test.serviceConfig."X-Test" = "test";
};
unitWithBackslash.configuration = {
systemd.services."escaped\\x2ddash" = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
ExecReload = "${pkgs.coreutils}/bin/true";
};
};
};
unitWithBackslashModified.configuration = {
imports = [ unitWithBackslash.configuration ];
systemd.services."escaped\\x2ddash".serviceConfig.X-Test = "test";
};
unitWithMultilineValue.configuration = {
systemd.services.test.serviceConfig.ExecStart = ''
${pkgs.coreutils}/bin/true \
# ignored
; ignored
blah blah
'';
};
unitStartingWithDash.configuration = {
systemd.services."-" = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
};
};
};
unitStartingWithDashModified.configuration = {
imports = [ unitStartingWithDash.configuration ];
systemd.services."-" = {
reloadIfChanged = true;
serviceConfig.ExecReload = "${pkgs.coreutils}/bin/true";
};
};
unitWithRequirement.configuration = {
systemd.services.required-service = {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
ExecReload = "${pkgs.coreutils}/bin/true";
};
};
systemd.services.test-service = {
wantedBy = [ "multi-user.target" ];
requires = [ "required-service.service" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
ExecReload = "${pkgs.coreutils}/bin/true";
};
};
};
unitWithRequirementModified.configuration = {
imports = [ unitWithRequirement.configuration ];
systemd.services.required-service.serviceConfig.X-Test = "test";
systemd.services.test-service.reloadTriggers = [ "test" ];
};
unitWithRequirementModifiedNostart.configuration = {
imports = [ unitWithRequirement.configuration ];
systemd.services.test-service.unitConfig.RefuseManualStart = true;
};
unitWithTemplate.configuration = {
systemd.services."instantiated@".serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
ExecReload = "${pkgs.coreutils}/bin/true";
};
systemd.services."instantiated@one" = {
wantedBy = [ "multi-user.target" ];
overrideStrategy = "asDropin";
};
systemd.services."instantiated@two" = {
wantedBy = [ "multi-user.target" ];
overrideStrategy = "asDropin";
};
};
unitWithTemplateModified.configuration = {
imports = [ unitWithTemplate.configuration ];
systemd.services."instantiated@".serviceConfig.X-Test = "test";
};
restart-and-reload-by-activation-script.configuration = {
systemd.services = rec {
simple-service = {
# No wantedBy so we can check if the activation script restart triggers them
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
ExecReload = "${pkgs.coreutils}/bin/true";
};
};
"templated-simple-service@" = simple-service;
"templated-simple-service@instance".overrideStrategy = "asDropin";
simple-restart-service = simple-service // {
stopIfChanged = false;
};
"templated-simple-restart-service@" = simple-restart-service;
"templated-simple-restart-service@instance".overrideStrategy = "asDropin";
simple-reload-service = simple-service // {
reloadIfChanged = true;
};
"templated-simple-reload-service@" = simple-reload-service;
"templated-simple-reload-service@instance".overrideStrategy = "asDropin";
no-restart-service = simple-service // {
restartIfChanged = false;
};
"templated-no-restart-service@" = no-restart-service;
"templated-no-restart-service@instance".overrideStrategy = "asDropin";
reload-triggers = simple-service // {
wantedBy = [ "multi-user.target" ];
};
"templated-reload-triggers@" = simple-service;
"templated-reload-triggers@instance" = {
overrideStrategy = "asDropin";
wantedBy = [ "multi-user.target" ];
};
reload-triggers-and-restart-by-as = simple-service;
"templated-reload-triggers-and-restart-by-as@" = reload-triggers-and-restart-by-as;
"templated-reload-triggers-and-restart-by-as@instance".overrideStrategy = "asDropin";
reload-triggers-and-restart = simple-service // {
stopIfChanged = false; # easier to check for this
wantedBy = [ "multi-user.target" ];
};
"templated-reload-triggers-and-restart@" = simple-service;
"templated-reload-triggers-and-restart@instance" = {
overrideStrategy = "asDropin";
stopIfChanged = false; # easier to check for this
wantedBy = [ "multi-user.target" ];
};
};
system.activationScripts.restart-and-reload-test = {
supportsDryActivation = true;
deps = [];
text = ''
if [ "$NIXOS_ACTION" = dry-activate ]; then
f=/run/nixos/dry-activation-restart-list
g=/run/nixos/dry-activation-reload-list
else
f=/run/nixos/activation-restart-list
g=/run/nixos/activation-reload-list
fi
cat <<EOF >> "$f"
simple-service.service
simple-restart-service.service
simple-reload-service.service
no-restart-service.service
reload-triggers-and-restart-by-as.service
templated-simple-service@instance.service
templated-simple-restart-service@instance.service
templated-simple-reload-service@instance.service
templated-no-restart-service@instance.service
templated-reload-triggers-and-restart-by-as@instance.service
EOF
cat <<EOF >> "$g"
reload-triggers.service
reload-triggers-and-restart-by-as.service
reload-triggers-and-restart.service
templated-reload-triggers@instance.service
templated-reload-triggers-and-restart-by-as@instance.service
templated-reload-triggers-and-restart@instance.service
EOF
'';
};
};
restart-and-reload-by-activation-script-modified.configuration = {
imports = [ restart-and-reload-by-activation-script.configuration ];
systemd.services.reload-triggers-and-restart.serviceConfig.X-Modified = "test";
systemd.services."templated-reload-triggers-and-restart@instance" = {
overrideStrategy = "asDropin";
serviceConfig.X-Modified = "test";
};
};
simple-socket.configuration = {
systemd.services.socket-activated = {
description = "A socket-activated service";
stopIfChanged = lib.mkDefault false;
serviceConfig = {
ExecStart = socketTest;
ExecReload = "${pkgs.coreutils}/bin/true";
};
};
systemd.sockets.socket-activated = {
wantedBy = [ "sockets.target" ];
listenStreams = [ "/run/test.sock" ];
socketConfig.SocketMode = lib.mkDefault "0777";
};
};
simple-socket-service-modified.configuration = {
imports = [ simple-socket.configuration ];
systemd.services.socket-activated.serviceConfig.X-Test = "test";
};
simple-socket-stop-if-changed.configuration = {
imports = [ simple-socket.configuration ];
systemd.services.socket-activated.stopIfChanged = true;
};
simple-socket-stop-if-changed-and-reloadtrigger.configuration = {
imports = [ simple-socket.configuration ];
systemd.services.socket-activated = {
stopIfChanged = true;
reloadTriggers = [ "test" ];
};
};
mount.configuration = {
systemd.mounts = [
{
description = "Testmount";
what = "tmpfs";
type = "tmpfs";
where = "/testmount";
options = "size=1M";
wantedBy = [ "local-fs.target" ];
}
];
};
mountOptionsModified.configuration = {
systemd.mounts = [
{
description = "Testmount";
what = "tmpfs";
type = "tmpfs";
where = "/testmount";
options = "size=10M";
wantedBy = [ "local-fs.target" ];
}
];
};
mountModified.configuration = {
systemd.mounts = [
{
description = "Testmount";
what = "ramfs";
type = "ramfs";
where = "/testmount";
options = "size=10M";
wantedBy = [ "local-fs.target" ];
}
];
};
timer.configuration = {
systemd.timers.test-timer = {
wantedBy = [ "timers.target" ];
timerConfig.OnCalendar = "@1395716396"; # chosen by fair dice roll
};
systemd.services.test-timer = {
serviceConfig = {
Type = "oneshot";
ExecStart = "${pkgs.coreutils}/bin/true";
};
};
};
timerModified.configuration = {
imports = [ timer.configuration ];
systemd.timers.test-timer.timerConfig.OnCalendar = lib.mkForce "Fri 2012-11-23 16:00:00";
};
hybridSleepModified.configuration = {
systemd.targets.hybrid-sleep.unitConfig.X-Test = true;
};
target.configuration = {
systemd.targets.test-target.wantedBy = [ "multi-user.target" ];
# We use this service to figure out whether the target was modified.
# This is the only way because targets are filtered and therefore not
# printed when they are started/stopped.
systemd.services.test-service = {
bindsTo = [ "test-target.target" ];
serviceConfig.ExecStart = "${pkgs.coreutils}/bin/sleep infinity";
};
};
targetModified.configuration = {
imports = [ target.configuration ];
systemd.targets.test-target.unitConfig.X-Test = true;
};
targetModifiedStopOnReconfig.configuration = {
imports = [ target.configuration ];
systemd.targets.test-target.unitConfig.X-StopOnReconfiguration = true;
};
path.configuration = {
systemd.paths.test-watch = {
wantedBy = [ "paths.target" ];
pathConfig.PathExists = "/testpath";
};
systemd.services.test-watch = {
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/touch /testpath-modified";
};
};
};
pathModified.configuration = {
imports = [ path.configuration ];
systemd.paths.test-watch.pathConfig.PathExists = lib.mkForce "/testpath2";
};
slice.configuration = {
systemd.slices.testslice.sliceConfig.MemoryMax = "1"; # don't allow memory allocation
systemd.services.testservice = {
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${pkgs.coreutils}/bin/true";
Slice = "testslice.slice";
};
};
};
sliceModified.configuration = {
imports = [ slice.configuration ];
systemd.slices.testslice.sliceConfig.MemoryMax = lib.mkForce null;
};
dbusReload.configuration = { config, ... }: let
dbusService = {
"dbus" = "dbus";
"broker" = "dbus-broker";
}.${config.services.dbus.implementation};
in {
# We want to make sure that stc catches this as a reload,
# not a restart.
systemd.services.${dbusService}.restartTriggers = [
(pkgs.writeText "dbus-reload-dummy" "dbus reload dummy")
];
};
};
};
other = {
system.switch.enable = true;
users.mutableUsers = true;
};
};
testScript = { nodes, ... }: let
originalSystem = nodes.machine.system.build.toplevel;
otherSystem = nodes.other.system.build.toplevel;
machine = nodes.machine.system.build.toplevel;
# Ensures failures pass through using pipefail, otherwise failing to
# switch-to-configuration is hidden by the success of `tee`.
stderrRunner = pkgs.writeScript "stderr-runner" ''
#! ${pkgs.runtimeShell}
set -e
set -o pipefail
exec env -i "$@" | tee /dev/stderr
'';
# Returns a comma separated representation of the given list in sorted
# order, that matches the output format of switch-to-configuration.pl
sortedUnits = xs: lib.concatStringsSep ", " (builtins.sort builtins.lessThan xs);
dbusService = {
"dbus" = "dbus.service";
"broker" = "dbus-broker.service";
}.${nodes.machine.services.dbus.implementation};
in /* python */ ''
def switch_to_specialisation(system, name, action="test", fail=False):
if name == "":
switcher = f"{system}/bin/switch-to-configuration"
else:
switcher = f"{system}/specialisation/{name}/bin/switch-to-configuration"
return run_switch(switcher, action, fail)
# like above but stc = switcher
def run_switch(switcher, action="test", fail=False):
out = machine.fail(f"{switcher} {action} 2>&1") if fail \
else machine.succeed(f"{switcher} {action} 2>&1")
assert_lacks(out, "switch-to-configuration line") # Perl warnings
return out
def assert_contains(haystack, needle):
if needle not in haystack:
print("The haystack that will cause the following exception is:")
print("---")
print(haystack)
print("---")
raise Exception(f"Expected string '{needle}' was not found")
def assert_lacks(haystack, needle):
if needle in haystack:
print("The haystack that will cause the following exception is:")
print("---")
print(haystack, end="")
print("---")
raise Exception(f"Unexpected string '{needle}' was found")
machine.wait_for_unit("multi-user.target")
machine.succeed(
"${stderrRunner} ${originalSystem}/bin/switch-to-configuration test"
)
# This tests whether the /etc/os-release parser works which is a fallback
# when /etc/NIXOS is missing. If the parser does not work, switch-to-configuration
# would fail.
machine.succeed("rm /etc/NIXOS")
machine.succeed(
"${stderrRunner} ${otherSystem}/bin/switch-to-configuration test"
)
boot_loader_text = "Warning: do not know how to make this configuration bootable; please enable a boot loader."
with subtest("actions"):
# boot action
out = switch_to_specialisation("${machine}", "simpleService", action="boot")
assert_contains(out, boot_loader_text)
assert_lacks(out, "activating the configuration...") # good indicator of a system activation
# switch action
out = switch_to_specialisation("${machine}", "", action="switch")
assert_contains(out, boot_loader_text)
assert_contains(out, "activating the configuration...") # good indicator of a system activation
# test and dry-activate actions are tested further down below
# invalid action fails the script
switch_to_specialisation("${machine}", "", action="broken-action", fail=True)
# no action fails the script
assert "Usage:" in machine.fail("${machine}/bin/switch-to-configuration 2>&1")
with subtest("init interface version"):
# Do not try to switch to an invalid init interface version
assert "incompatible" in switch_to_specialisation("${machine}", "brokenInitInterface", fail=True)
with subtest("systemd restarts"):
# systemd is restarted when its system.conf changes
out = switch_to_specialisation("${machine}", "modifiedSystemConf")
assert_contains(out, "restarting systemd...")
with subtest("continuing from an aborted switch"):
# An aborted switch will write into a file what it tried to start
# and a second switch should continue from this
machine.succeed("echo ${dbusService} > /run/nixos/start-list")
out = switch_to_specialisation("${machine}", "modifiedSystemConf")
assert_contains(out, "starting the following units: ${dbusService}\n")
with subtest("fstab mounts"):
switch_to_specialisation("${machine}", "")
# add a mountpoint
out = switch_to_specialisation("${machine}", "addedMount")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: test.mount\n")
# modify the mountpoint's options
out = switch_to_specialisation("${machine}", "addedMountOptsModified")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_contains(out, "reloading the following units: test.mount\n")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# modify the device
out = switch_to_specialisation("${machine}", "addedMountDevModified")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_contains(out, "\nrestarting the following units: test.mount\n")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# modify both
out = switch_to_specialisation("${machine}", "addedMount")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_contains(out, "\nrestarting the following units: test.mount\n")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# remove the mount
out = switch_to_specialisation("${machine}", "")
assert_contains(out, "stopping the following units: test.mount\n")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# change something about the / mount
out = switch_to_specialisation("${machine}", "storeMountModified")
assert_lacks(out, "stopping the following units:")
assert_contains(out, "NOT restarting the following changed units: -.mount")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
with subtest("swaps"):
switch_to_specialisation("${machine}", "")
# add a swap
out = switch_to_specialisation("${machine}", "swap")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: swapfile.swap")
# remove it
out = switch_to_specialisation("${machine}", "")
assert_contains(out, "stopping swap device: /swapfile")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
with subtest("services"):
switch_to_specialisation("${machine}", "")
# Nothing happens when nothing is changed
out = switch_to_specialisation("${machine}", "")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# Start a simple service
out = switch_to_specialisation("${machine}", "simpleService")
assert_lacks(out, boot_loader_text) # test does not install a bootloader
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: test.service\n")
# Not changing anything doesn't do anything
out = switch_to_specialisation("${machine}", "simpleService")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# Only changing the description does nothing
out = switch_to_specialisation("${machine}", "simpleServiceDifferentDescription")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# Restart the simple service
out = switch_to_specialisation("${machine}", "simpleServiceModified")
assert_contains(out, "stopping the following units: test.service\n")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_contains(out, "\nstarting the following units: test.service\n")
assert_lacks(out, "the following new units were started:")
# Restart the service with stopIfChanged=false
out = switch_to_specialisation("${machine}", "simpleServiceNostop")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_contains(out, "\nrestarting the following units: test.service\n")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# Reload the service with reloadIfChanged=true
out = switch_to_specialisation("${machine}", "simpleServiceReload")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_contains(out, "reloading the following units: test.service\n")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# Nothing happens when restartIfChanged=false
out = switch_to_specialisation("${machine}", "simpleServiceNorestart")
assert_lacks(out, "stopping the following units:")
assert_contains(out, "NOT restarting the following changed units: test.service\n")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# Dry mode shows different messages
out = switch_to_specialisation("${machine}", "simpleService", action="dry-activate")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
assert_contains(out, "would start the following units: test.service\n")
out = switch_to_specialisation("${machine}", "", action="test")
# Ensure the service can be started when the activation script isn't in toplevel
# This is a lot like "Start a simple service", except activation-only deps could be gc-ed
out = run_switch("${nodes.machine.specialisation.simpleServiceSeparateActivationScript.configuration.system.build.separateActivationScript}/bin/switch-to-configuration");
assert_lacks(out, boot_loader_text) # test does not install a bootloader
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: test.service\n")
machine.succeed("! test -e /run/current-system/activate")
machine.succeed("! test -e /run/current-system/dry-activate")
machine.succeed("! test -e /run/current-system/bin/switch-to-configuration")
# Ensure units with multiline values work
out = switch_to_specialisation("${machine}", "unitWithMultilineValue")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "restarting the following units:")
assert_lacks(out, "the following new units were started:")
assert_contains(out, "starting the following units: test.service")
# Ensure \ works in unit names
out = switch_to_specialisation("${machine}", "unitWithBackslash")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: escaped\\x2ddash.service\n")
out = switch_to_specialisation("${machine}", "unitWithBackslashModified")
assert_contains(out, "stopping the following units: escaped\\x2ddash.service\n")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_contains(out, "\nstarting the following units: escaped\\x2ddash.service\n")
assert_lacks(out, "the following new units were started:")
# Ensure units can start with a dash
out = switch_to_specialisation("${machine}", "unitStartingWithDash")
assert_contains(out, "stopping the following units: escaped\\x2ddash.service\n")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: -.service\n")
# The regression only occurs when reloading units
out = switch_to_specialisation("${machine}", "unitStartingWithDashModified")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_contains(out, "reloading the following units: -.service")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_lacks(out, "the following new units were started:")
# Ensure units that require changed units are properly reloaded
out = switch_to_specialisation("${machine}", "unitWithRequirement")
assert_contains(out, "stopping the following units: -.service\n")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: required-service.service, test-service.service\n")
out = switch_to_specialisation("${machine}", "unitWithRequirementModified")
assert_contains(out, "stopping the following units: required-service.service\n")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_contains(out, "\nstarting the following units: required-service.service, test-service.service\n")
assert_lacks(out, "the following new units were started:")
# Unless the unit asks to be not restarted
out = switch_to_specialisation("${machine}", "unitWithRequirementModifiedNostart")
assert_contains(out, "stopping the following units: required-service.service\n")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_contains(out, "\nstarting the following units: required-service.service\n")
assert_lacks(out, "the following new units were started:")
# Ensure templated units are restarted when the base unit changes
switch_to_specialisation("${machine}", "unitWithTemplate")
out = switch_to_specialisation("${machine}", "unitWithTemplateModified")
assert_contains(out, "stopping the following units: instantiated@one.service, instantiated@two.service\n")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_contains(out, "\nstarting the following units: instantiated@one.service, instantiated@two.service\n")
assert_lacks(out, "the following new units were started:")
with subtest("failing units"):
# Let the simple service fail
switch_to_specialisation("${machine}", "simpleServiceModified")
out = switch_to_specialisation("${machine}", "simpleServiceFailing", fail=True)
assert_contains(out, "stopping the following units: test.service\n")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_contains(out, "\nstarting the following units: test.service\n")
assert_lacks(out, "the following new units were started:")
assert_contains(out, "warning: the following units failed: test.service\n")
assert_contains(out, "Main PID:") # output of systemctl
# A unit that gets into autorestart without failing is not treated as failed
out = switch_to_specialisation("${machine}", "autorestartService")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: autorestart.service\n")
machine.systemctl('stop autorestart.service') # cancel the 20y timer
# Switching to the same system should do nothing (especially not treat the unit as failed)
out = switch_to_specialisation("${machine}", "autorestartService")
assert_lacks(out, "stopping the following units:")
assert_lacks(out, "NOT restarting the following changed units:")
assert_lacks(out, "reloading the following units:")
assert_lacks(out, "\nrestarting the following units:")
assert_lacks(out, "\nstarting the following units:")
assert_contains(out, "the following new units were started: autorestart.service\n")