about summary refs log tree commit diff
path: root/nixos/tests/bootspec.nix
blob: 14928b22062510b4669cf1dc3ac26a1682aa77c9 (plain) (blame)
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
{ system ? builtins.currentSystem,
  config ? {},
  pkgs ? import ../.. { inherit system config; }
}:

with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib;

let
  baseline = {
    virtualisation.useBootLoader = true;
  };
  grub = {
    boot.loader.grub.enable = true;
  };
  systemd-boot = {
    boot.loader.systemd-boot.enable = true;
  };
  uefi = {
    virtualisation.useEFIBoot = true;
    boot.loader.efi.canTouchEfiVariables = true;
    boot.loader.grub.efiSupport = true;
    environment.systemPackages = [ pkgs.efibootmgr ];
  };
  standard = {
    boot.bootspec.enable = true;

    imports = [
      baseline
      systemd-boot
      uefi
    ];
  };
in
{
  basic = makeTest {
    name = "systemd-boot-with-bootspec";
    meta.maintainers = with pkgs.lib.maintainers; [ raitobezarius ];

    nodes.machine = standard;

    testScript = ''
      machine.start()
      machine.wait_for_unit("multi-user.target")

      machine.succeed("test -e /run/current-system/boot.json")
    '';
  };

  grub = makeTest {
    name = "grub-with-bootspec";
    meta.maintainers = with pkgs.lib.maintainers; [ raitobezarius ];

    nodes.machine = {
      boot.bootspec.enable = true;

      imports = [
        baseline
        grub
        uefi
      ];
    };

    testScript = ''
      machine.start()
      machine.wait_for_unit("multi-user.target")

      machine.succeed("test -e /run/current-system/boot.json")
    '';
  };

  legacy-boot = makeTest {
    name = "legacy-boot-with-bootspec";
    meta.maintainers = with pkgs.lib.maintainers; [ raitobezarius ];

    nodes.machine = {
      boot.bootspec.enable = true;

      imports = [
        baseline
        grub
      ];
    };

    testScript = ''
      machine.start()
      machine.wait_for_unit("multi-user.target")

      machine.succeed("test -e /run/current-system/boot.json")
    '';
  };

  # Check that initrd create corresponding entries in bootspec.
  initrd = makeTest {
    name = "bootspec-with-initrd";
    meta.maintainers = with pkgs.lib.maintainers; [ raitobezarius ];

    nodes.machine = {
      imports = [ standard ];
      environment.systemPackages = [ pkgs.jq ];
      # It's probably the case, but we want to make it explicit here.
      boot.initrd.enable = true;
    };

    testScript = ''
      import json

      machine.start()
      machine.wait_for_unit("multi-user.target")

      machine.succeed("test -e /run/current-system/boot.json")

      bootspec = json.loads(machine.succeed("jq -r '.\"org.nixos.bootspec.v1\"' /run/current-system/boot.json"))

      assert 'initrd' in bootspec, "Bootspec should contain initrd field when initrd is enabled"
      assert 'initrdSecrets' not in bootspec, "Bootspec should not contain initrdSecrets when there's no initrdSecrets"
    '';
  };

  # Check that initrd secrets create corresponding entries in bootspec.
  initrd-secrets = makeTest {
    name = "bootspec-with-initrd-secrets";
    meta.maintainers = with pkgs.lib.maintainers; [ raitobezarius ];

    nodes.machine = {
      imports = [ standard ];
      environment.systemPackages = [ pkgs.jq ];
      # It's probably the case, but we want to make it explicit here.
      boot.initrd.enable = true;
      boot.initrd.secrets."/some/example" = pkgs.writeText "example-secret" "test";
    };

    testScript = ''
      import json

      machine.start()
      machine.wait_for_unit("multi-user.target")

      machine.succeed("test -e /run/current-system/boot.json")

      bootspec = json.loads(machine.succeed("jq -r '.\"org.nixos.bootspec.v1\"' /run/current-system/boot.json"))

      assert 'initrdSecrets' in bootspec, "Bootspec should contain an 'initrdSecrets' field given there's an initrd secret"
    '';
  };


  # Check that specialisations create corresponding entries in bootspec.
  specialisation = makeTest {
    name = "bootspec-with-specialisation";
    meta.maintainers = with pkgs.lib.maintainers; [ raitobezarius ];

    nodes.machine = {
      imports = [ standard ];
      environment.systemPackages = [ pkgs.jq ];
      specialisation.something.configuration = {};
    };

    testScript = ''
      import json

      machine.start()
      machine.wait_for_unit("multi-user.target")

      machine.succeed("test -e /run/current-system/boot.json")
      machine.succeed("test -e /run/current-system/specialisation/something/boot.json")

      sp_in_parent = json.loads(machine.succeed("jq -r '.\"org.nixos.specialisation.v1\".something' /run/current-system/boot.json"))
      sp_in_fs = json.loads(machine.succeed("cat /run/current-system/specialisation/something/boot.json"))

      assert sp_in_parent['org.nixos.bootspec.v1'] == sp_in_fs['org.nixos.bootspec.v1'], "Bootspecs of the same specialisation are different!"
    '';
  };

  # Check that extensions are propagated.
  extensions = makeTest {
    name = "bootspec-with-extensions";
    meta.maintainers = with pkgs.lib.maintainers; [ raitobezarius ];

    nodes.machine = { config, ... }: {
      imports = [ standard ];
      environment.systemPackages = [ pkgs.jq ];
      boot.bootspec.extensions = {
        "org.nix-tests.product" = {
          osRelease = config.environment.etc."os-release".source;
        };
      };
    };

    testScript = ''
      machine.start()
      machine.wait_for_unit("multi-user.target")

      current_os_release = machine.succeed("cat /etc/os-release")
      bootspec_os_release = machine.succeed("cat $(jq -r '.\"org.nix-tests.product\".osRelease' /run/current-system/boot.json)")

      assert current_os_release == bootspec_os_release, "Filename referenced by extension has unexpected contents"
    '';
  };

}