1import ./make-test-python.nix ({ pkgs, lib, ... }: {
2 name = "containers-imperative";
3 meta = {
4 maintainers = with lib.maintainers; [ aristid aszlig eelco kampfschlaefer ];
5 };
6
7 nodes.machine =
8 { config, pkgs, lib, ... }:
9 { imports = [ ../modules/installer/cd-dvd/channel.nix ];
10
11 # XXX: Sandbox setup fails while trying to hardlink files from the host's
12 # store file system into the prepared chroot directory.
13 nix.settings.sandbox = false;
14 nix.settings.substituters = []; # don't try to access cache.nixos.org
15
16 virtualisation.writableStore = true;
17 # Make sure we always have all the required dependencies for creating a
18 # container available within the VM, because we don't have network access.
19 virtualisation.additionalPaths = let
20 emptyContainer = import ../lib/eval-config.nix {
21 modules = lib.singleton {
22 nixpkgs = { inherit (config.nixpkgs) localSystem; };
23
24 containers.foo.config = {};
25 };
26
27 # The system is inherited from the host above.
28 # Set it to null, to remove the "legacy" entrypoint's non-hermetic default.
29 system = null;
30 };
31 in with pkgs; [
32 stdenv stdenvNoCC emptyContainer.config.containers.foo.path
33 libxslt desktop-file-utils texinfo docbook5 libxml2
34 docbook_xsl_ns xorg.lndir documentation-highlighter
35 ];
36 };
37
38 testScript = let
39 tmpfilesContainerConfig = pkgs.writeText "container-config-tmpfiles" ''
40 {
41 systemd.tmpfiles.rules = [ "d /foo - - - - -" ];
42 systemd.services.foo = {
43 serviceConfig.Type = "oneshot";
44 script = "ls -al /foo";
45 wantedBy = [ "multi-user.target" ];
46 };
47 }
48 '';
49 brokenCfg = pkgs.writeText "broken.nix" ''
50 {
51 assertions = [
52 { assertion = false;
53 message = "I never evaluate";
54 }
55 ];
56 }
57 '';
58 in ''
59 with subtest("Make sure we have a NixOS tree (required by ‘nixos-container create’)"):
60 machine.succeed("PAGER=cat nix-env -qa -A nixos.hello >&2")
61
62 id1, id2 = None, None
63
64 with subtest("Create some containers imperatively"):
65 id1 = machine.succeed("nixos-container create foo --ensure-unique-name").rstrip()
66 machine.log(f"created container {id1}")
67
68 id2 = machine.succeed("nixos-container create foo --ensure-unique-name").rstrip()
69 machine.log(f"created container {id2}")
70
71 assert id1 != id2
72
73 with subtest(f"Put the root of {id2} into a bind mount"):
74 machine.succeed(
75 f"mv /var/lib/nixos-containers/{id2} /id2-bindmount",
76 f"mount --bind /id2-bindmount /var/lib/nixos-containers/{id1}",
77 )
78
79 ip1 = machine.succeed(f"nixos-container show-ip {id1}").rstrip()
80 ip2 = machine.succeed(f"nixos-container show-ip {id2}").rstrip()
81 assert ip1 != ip2
82
83 with subtest(
84 "Create a directory and a file we can later check if it still exists "
85 + "after destruction of the container"
86 ):
87 machine.succeed("mkdir /nested-bindmount")
88 machine.succeed("echo important data > /nested-bindmount/dummy")
89
90 with subtest(
91 "Create a directory with a dummy file and bind-mount it into both containers."
92 ):
93 for id in id1, id2:
94 important_path = f"/var/lib/nixos-containers/{id}/very/important/data"
95 machine.succeed(
96 f"mkdir -p {important_path}",
97 f"mount --bind /nested-bindmount {important_path}",
98 )
99
100 with subtest("Start one of them"):
101 machine.succeed(f"nixos-container start {id1}")
102
103 with subtest("Execute commands via the root shell"):
104 assert "Linux" in machine.succeed(f"nixos-container run {id1} -- uname")
105
106 with subtest("Execute a nix command via the root shell. (regression test for #40355)"):
107 machine.succeed(
108 f"nixos-container run {id1} -- nix-instantiate -E "
109 + '\'derivation { name = "empty"; builder = "false"; system = "false"; }\' '
110 )
111
112 with subtest("Stop and start (regression test for #4989)"):
113 machine.succeed(f"nixos-container stop {id1}")
114 machine.succeed(f"nixos-container start {id1}")
115
116 # clear serial backlog for next tests
117 machine.succeed("logger eat console backlog 3ea46eb2-7f82-4f70-b810-3f00e3dd4c4d")
118 machine.wait_for_console_text(
119 "eat console backlog 3ea46eb2-7f82-4f70-b810-3f00e3dd4c4d"
120 )
121
122 with subtest("Stop a container early"):
123 machine.succeed(f"nixos-container stop {id1}")
124 machine.succeed(f"nixos-container start {id1} >&2 &")
125 machine.wait_for_console_text("Stage 2")
126 machine.succeed(f"nixos-container stop {id1}")
127 machine.wait_for_console_text(f"Container {id1} exited successfully")
128 machine.succeed(f"nixos-container start {id1}")
129
130 with subtest("Stop a container without machined (regression test for #109695)"):
131 machine.systemctl("stop systemd-machined")
132 machine.succeed(f"nixos-container stop {id1}")
133 machine.wait_for_console_text(f"Container {id1} has been shut down")
134 machine.succeed(f"nixos-container start {id1}")
135
136 with subtest("tmpfiles are present"):
137 machine.log("creating container tmpfiles")
138 machine.succeed(
139 "nixos-container create tmpfiles --config-file ${tmpfilesContainerConfig}"
140 )
141 machine.log("created, starting…")
142 machine.succeed("nixos-container start tmpfiles")
143 machine.log("done starting, investigating…")
144 machine.succeed(
145 "echo $(nixos-container run tmpfiles -- systemctl is-active foo.service) | grep -q active;"
146 )
147 machine.succeed("nixos-container destroy tmpfiles")
148
149 with subtest("Execute commands via the root shell"):
150 assert "Linux" in machine.succeed(f"nixos-container run {id1} -- uname")
151
152 with subtest("Destroy the containers"):
153 for id in id1, id2:
154 machine.succeed(f"nixos-container destroy {id}")
155
156 with subtest("Check whether destruction of any container has killed important data"):
157 machine.succeed("grep -qF 'important data' /nested-bindmount/dummy")
158
159 with subtest("Ensure that the container path is gone"):
160 print(machine.succeed("ls -lsa /var/lib/nixos-containers"))
161 machine.succeed(f"test ! -e /var/lib/nixos-containers/{id1}")
162
163 with subtest("Ensure that a failed container creation doesn'leave any state"):
164 machine.fail(
165 "nixos-container create b0rk --config-file ${brokenCfg}"
166 )
167 machine.succeed("test ! -e /var/lib/nixos-containers/b0rk")
168 '';
169})