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