1# Test for NixOS' container support.
2
3import ./make-test.nix ({ pkgs, ...} : {
4 name = "containers-imperative";
5 meta = with pkgs.stdenv.lib.maintainers; {
6 maintainers = [ aristid aszlig eelco chaoflow kampfschlaefer ];
7 };
8
9 machine =
10 { config, pkgs, lib, ... }:
11 { imports = [ ../modules/installer/cd-dvd/channel.nix ];
12 virtualisation.writableStore = true;
13 virtualisation.memorySize = 768;
14 # Make sure we always have all the required dependencies for creating a
15 # container available within the VM, because we don't have network access.
16 virtualisation.pathsInNixDB = let
17 emptyContainer = import ../lib/eval-config.nix {
18 inherit (config.nixpkgs) system;
19 modules = lib.singleton {
20 containers.foo.config = {};
21 };
22 };
23 in [ pkgs.stdenv emptyContainer.config.containers.foo.path ];
24 };
25
26 testScript =
27 ''
28 # Make sure we have a NixOS tree (required by ‘nixos-container create’).
29 $machine->succeed("PAGER=cat nix-env -qa -A nixos.hello >&2");
30
31 # Create some containers imperatively.
32 my $id1 = $machine->succeed("nixos-container create foo --ensure-unique-name");
33 chomp $id1;
34 $machine->log("created container $id1");
35
36 my $id2 = $machine->succeed("nixos-container create foo --ensure-unique-name");
37 chomp $id2;
38 $machine->log("created container $id2");
39
40 die if $id1 eq $id2;
41
42 # Put the root of $id2 into a bind mount.
43 $machine->succeed(
44 "mv /var/lib/containers/$id2 /id2-bindmount",
45 "mount --bind /id2-bindmount /var/lib/containers/$id1"
46 );
47
48 my $ip1 = $machine->succeed("nixos-container show-ip $id1");
49 chomp $ip1;
50 my $ip2 = $machine->succeed("nixos-container show-ip $id2");
51 chomp $ip2;
52 die if $ip1 eq $ip2;
53
54 # Create a directory and a file we can later check if it still exists
55 # after destruction of the container.
56 $machine->succeed(
57 "mkdir /nested-bindmount",
58 "echo important data > /nested-bindmount/dummy",
59 );
60
61 # Create a directory with a dummy file and bind-mount it into both
62 # containers.
63 foreach ($id1, $id2) {
64 my $importantPath = "/var/lib/containers/$_/very/important/data";
65 $machine->succeed(
66 "mkdir -p $importantPath",
67 "mount --bind /nested-bindmount $importantPath"
68 );
69 }
70
71 # Start one of them.
72 $machine->succeed("nixos-container start $id1");
73
74 # Execute commands via the root shell.
75 $machine->succeed("nixos-container run $id1 -- uname") =~ /Linux/ or die;
76
77 # Stop and start (regression test for #4989)
78 $machine->succeed("nixos-container stop $id1");
79 $machine->succeed("nixos-container start $id1");
80
81 # Execute commands via the root shell.
82 $machine->succeed("nixos-container run $id1 -- uname") =~ /Linux/ or die;
83
84 # Destroy the containers.
85 $machine->succeed("nixos-container destroy $id1");
86 $machine->succeed("nixos-container destroy $id2");
87
88 $machine->succeed(
89 # Check whether destruction of any container has killed important data
90 "grep -qF 'important data' /nested-bindmount/dummy",
91 # Ensure that the container path is gone
92 "test ! -e /var/lib/containers/$id1"
93 );
94 '';
95
96})