at 23.05-pre 6.8 kB view raw
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 system.stateVersion = "18.03"; 26 }; 27 }; 28 }; 29 in with pkgs; [ 30 stdenv stdenvNoCC emptyContainer.config.containers.foo.path 31 libxslt desktop-file-utils texinfo docbook5 libxml2 32 docbook_xsl_ns xorg.lndir documentation-highlighter 33 ]; 34 }; 35 36 testScript = let 37 tmpfilesContainerConfig = pkgs.writeText "container-config-tmpfiles" '' 38 { 39 systemd.tmpfiles.rules = [ "d /foo - - - - -" ]; 40 systemd.services.foo = { 41 serviceConfig.Type = "oneshot"; 42 script = "ls -al /foo"; 43 wantedBy = [ "multi-user.target" ]; 44 }; 45 } 46 ''; 47 brokenCfg = pkgs.writeText "broken.nix" '' 48 { 49 assertions = [ 50 { assertion = false; 51 message = "I never evaluate"; 52 } 53 ]; 54 } 55 ''; 56 in '' 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") 59 60 id1, id2 = None, None 61 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}") 65 66 id2 = machine.succeed("nixos-container create foo --ensure-unique-name").rstrip() 67 machine.log(f"created container {id2}") 68 69 assert id1 != id2 70 71 with subtest(f"Put the root of {id2} into a bind mount"): 72 machine.succeed( 73 f"mv /var/lib/nixos-containers/{id2} /id2-bindmount", 74 f"mount --bind /id2-bindmount /var/lib/nixos-containers/{id1}", 75 ) 76 77 ip1 = machine.succeed(f"nixos-container show-ip {id1}").rstrip() 78 ip2 = machine.succeed(f"nixos-container show-ip {id2}").rstrip() 79 assert ip1 != ip2 80 81 with subtest( 82 "Create a directory and a file we can later check if it still exists " 83 + "after destruction of the container" 84 ): 85 machine.succeed("mkdir /nested-bindmount") 86 machine.succeed("echo important data > /nested-bindmount/dummy") 87 88 with subtest( 89 "Create a directory with a dummy file and bind-mount it into both containers." 90 ): 91 for id in id1, id2: 92 important_path = f"/var/lib/nixos-containers/{id}/very/important/data" 93 machine.succeed( 94 f"mkdir -p {important_path}", 95 f"mount --bind /nested-bindmount {important_path}", 96 ) 97 98 with subtest("Start one of them"): 99 machine.succeed(f"nixos-container start {id1}") 100 101 with subtest("Execute commands via the root shell"): 102 assert "Linux" in machine.succeed(f"nixos-container run {id1} -- uname") 103 104 with subtest("Execute a nix command via the root shell. (regression test for #40355)"): 105 machine.succeed( 106 f"nixos-container run {id1} -- nix-instantiate -E " 107 + '\'derivation { name = "empty"; builder = "false"; system = "false"; }\' ' 108 ) 109 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}") 113 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" 118 ) 119 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}") 127 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}") 133 134 with subtest("tmpfiles are present"): 135 machine.log("creating container tmpfiles") 136 machine.succeed( 137 "nixos-container create tmpfiles --config-file ${tmpfilesContainerConfig}" 138 ) 139 machine.log("created, starting") 140 machine.succeed("nixos-container start tmpfiles") 141 machine.log("done starting, investigating") 142 machine.succeed( 143 "echo $(nixos-container run tmpfiles -- systemctl is-active foo.service) | grep -q active;" 144 ) 145 machine.succeed("nixos-container destroy tmpfiles") 146 147 with subtest("Execute commands via the root shell"): 148 assert "Linux" in machine.succeed(f"nixos-container run {id1} -- uname") 149 150 with subtest("Destroy the containers"): 151 for id in id1, id2: 152 machine.succeed(f"nixos-container destroy {id}") 153 154 with subtest("Check whether destruction of any container has killed important data"): 155 machine.succeed("grep -qF 'important data' /nested-bindmount/dummy") 156 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}") 160 161 with subtest("Ensure that a failed container creation doesn'leave any state"): 162 machine.fail( 163 "nixos-container create b0rk --config-file ${brokenCfg}" 164 ) 165 machine.succeed("test ! -e /var/lib/nixos-containers/b0rk") 166 ''; 167})