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