1 import ./make-test-python.nix ({ pkgs, lib, ... }: {
2 name = "containers-imperative";
4 maintainers = with lib.maintainers; [ aristid aszlig eelco kampfschlaefer ];
8 { config, pkgs, lib, ... }:
9 { imports = [ ../modules/installer/cd-dvd/channel.nix ];
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
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; };
24 containers.foo.config = {
25 system.stateVersion = "18.03";
30 stdenv stdenvNoCC emptyContainer.config.containers.foo.path
31 libxslt desktop-file-utils texinfo docbook5 libxml2
32 docbook_xsl_ns xorg.lndir documentation-highlighter
37 tmpfilesContainerConfig = pkgs.writeText "container-config-tmpfiles" ''
39 systemd.tmpfiles.rules = [ "d /foo - - - - -" ];
40 systemd.services.foo = {
41 serviceConfig.Type = "oneshot";
42 script = "ls -al /foo";
43 wantedBy = [ "multi-user.target" ];
47 brokenCfg = pkgs.writeText "broken.nix" ''
51 message = "I never evaluate";
57 with subtest("Make sure we have a NixOS tree (required by ‘nixos-container create’)"):
58 machine.succeed("PAGER=cat nix-env -qa -A nixos.hello >&2")
62 with subtest("Create some containers imperatively"):
63 id1 = machine.succeed("nixos-container create foo --ensure-unique-name").rstrip()
64 machine.log(f"created container {id1}")
66 id2 = machine.succeed("nixos-container create foo --ensure-unique-name").rstrip()
67 machine.log(f"created container {id2}")
71 with subtest(f"Put the root of {id2} into a bind mount"):
73 f"mv /var/lib/nixos-containers/{id2} /id2-bindmount",
74 f"mount --bind /id2-bindmount /var/lib/nixos-containers/{id1}",
77 ip1 = machine.succeed(f"nixos-container show-ip {id1}").rstrip()
78 ip2 = machine.succeed(f"nixos-container show-ip {id2}").rstrip()
82 "Create a directory and a file we can later check if it still exists "
83 + "after destruction of the container"
85 machine.succeed("mkdir /nested-bindmount")
86 machine.succeed("echo important data > /nested-bindmount/dummy")
89 "Create a directory with a dummy file and bind-mount it into both containers."
92 important_path = f"/var/lib/nixos-containers/{id}/very/important/data"
94 f"mkdir -p {important_path}",
95 f"mount --bind /nested-bindmount {important_path}",
98 with subtest("Start one of them"):
99 machine.succeed(f"nixos-container start {id1}")
101 with subtest("Execute commands via the root shell"):
102 assert "Linux" in machine.succeed(f"nixos-container run {id1} -- uname")
104 with subtest("Execute a nix command via the root shell. (regression test for #40355)"):
106 f"nixos-container run {id1} -- nix-instantiate -E "
107 + '\'derivation { name = "empty"; builder = "false"; system = "false"; }\' '
110 with subtest("Stop and start (regression test for #4989)"):
111 machine.succeed(f"nixos-container stop {id1}")
112 machine.succeed(f"nixos-container start {id1}")
114 # clear serial backlog for next tests
115 machine.succeed("logger eat console backlog 3ea46eb2-7f82-4f70-b810-3f00e3dd4c4d")
116 machine.wait_for_console_text(
117 "eat console backlog 3ea46eb2-7f82-4f70-b810-3f00e3dd4c4d"
120 with subtest("Stop a container early"):
121 machine.succeed(f"nixos-container stop {id1}")
122 machine.succeed(f"nixos-container start {id1} >&2 &")
123 machine.wait_for_console_text("Stage 2")
124 machine.succeed(f"nixos-container stop {id1}")
125 machine.wait_for_console_text(f"Container {id1} exited successfully")
126 machine.succeed(f"nixos-container start {id1}")
128 with subtest("Stop a container without machined (regression test for #109695)"):
129 machine.systemctl("stop systemd-machined")
130 machine.succeed(f"nixos-container stop {id1}")
131 machine.wait_for_console_text(f"Container {id1} has been shut down")
132 machine.succeed(f"nixos-container start {id1}")
134 with subtest("tmpfiles are present"):
135 machine.log("creating container tmpfiles")
137 "nixos-container create tmpfiles --config-file ${tmpfilesContainerConfig}"
139 machine.log("created, starting…")
140 machine.succeed("nixos-container start tmpfiles")
141 machine.log("done starting, investigating…")
143 "echo $(nixos-container run tmpfiles -- systemctl is-active foo.service) | grep -q active;"
145 machine.succeed("nixos-container destroy tmpfiles")
147 with subtest("Execute commands via the root shell"):
148 assert "Linux" in machine.succeed(f"nixos-container run {id1} -- uname")
150 with subtest("Destroy the containers"):
152 machine.succeed(f"nixos-container destroy {id}")
154 with subtest("Check whether destruction of any container has killed important data"):
155 machine.succeed("grep -qF 'important data' /nested-bindmount/dummy")
157 with subtest("Ensure that the container path is gone"):
158 print(machine.succeed("ls -lsa /var/lib/nixos-containers"))
159 machine.succeed(f"test ! -e /var/lib/nixos-containers/{id1}")
161 with subtest("Ensure that a failed container creation doesn'leave any state"):
163 "nixos-container create b0rk --config-file ${brokenCfg}"
165 machine.succeed("test ! -e /var/lib/nixos-containers/b0rk")