From a711e445cc057ac06160816b848f5752a6b4b753 Mon Sep 17 00:00:00 2001 From: Leon Barrett Date: Sat, 15 Apr 2023 17:27:20 -0700 Subject: [PATCH 1/2] nixos/tests/ec2: Fix test tooling This change fixes two problems with the qemu testing code: 1. Previously, the qemu-img command was missing a disk image format argument. 2. Previously, if a test assertion failed, the test hung because the VM was not torn down. --- nixos/tests/common/ec2.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/nixos/tests/common/ec2.nix b/nixos/tests/common/ec2.nix index 6ed420e0aae7..1a64c464039b 100644 --- a/nixos/tests/common/ec2.nix +++ b/nixos/tests/common/ec2.nix @@ -17,6 +17,7 @@ with pkgs.lib; ln -s ${pkgs.writeText "sshPublicKey" sshPublicKey} $out/1.0/meta-data/public-keys/0/openssh-key ''; }; + indentLines = str: concatLines (map (s: " " + s) (splitString "\n" str)); in makeTest { name = "ec2-" + name; nodes = {}; @@ -36,6 +37,8 @@ with pkgs.lib; "create", "-f", "qcow2", + "-F", + "qcow2", "-o", "backing_file=${image}", disk_image, @@ -59,7 +62,11 @@ with pkgs.lib; ) machine = create_machine({"startCommand": start_command}) - '' + script; + try: + '' + indentLines script + '' + finally: + machine.shutdown() + ''; inherit meta; }; From 15c760d6b8fd425c4f10cda6f82959dd98ea191c Mon Sep 17 00:00:00 2001 From: Leon Barrett Date: Fri, 31 Mar 2023 14:51:38 -0700 Subject: [PATCH 2/2] nixos/make-disk-image: fix contents dir paths `make-disk-image` is a tool for creating VM images. It takes an argument `contents` that allows one to specify files and directories that should be copied into the VM image. However, directories end up not at the specified target, but instead at a subdirectory of the target, with a nix-store-like path, e.g. `/target/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-source`. See issue https://github.com/NixOS/nixpkgs/issues/226203 . This change adds a test for make-disk-image's contents directory handling and adds a fix (appending `/` to rsync input directory names). This closes issue https://github.com/NixOS/nixpkgs/issues/226203 . --- .../manual/release-notes/rl-2305.section.md | 2 ++ nixos/lib/make-disk-image.nix | 11 +++++--- nixos/tests/image-contents.nix | 25 +++++++++++++------ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/nixos/doc/manual/release-notes/rl-2305.section.md b/nixos/doc/manual/release-notes/rl-2305.section.md index d60546fb74f8..713b2556a413 100644 --- a/nixos/doc/manual/release-notes/rl-2305.section.md +++ b/nixos/doc/manual/release-notes/rl-2305.section.md @@ -415,6 +415,8 @@ In addition to numerous new and upgraded packages, this release has the followin - The `bind` module now allows the per-zone `allow-query` setting to be configured (previously it was hard-coded to `any`; it still defaults to `any` to retain compatibility). +- `make-disk-image` handles `contents` arguments that are directories better, fixing a bug where it used to put them in a subdirectory of the intended `target`. + ## Detailed migration information {#sec-release-23.05-migration} ### Pipewire configuration overrides {#sec-release-23.05-migration-pipewire} diff --git a/nixos/lib/make-disk-image.nix b/nixos/lib/make-disk-image.nix index d641d1289fe4..db53bb98ee4e 100644 --- a/nixos/lib/make-disk-image.nix +++ b/nixos/lib/make-disk-image.nix @@ -402,11 +402,16 @@ let format' = format; in let done else mkdir -p $root/$(dirname $target) - if ! [ -e $root/$target ]; then - rsync $rsync_flags $source $root/$target - else + if [ -e $root/$target ]; then echo "duplicate entry $target -> $source" exit 1 + elif [ -d $source ]; then + # Append a slash to the end of source to get rsync to copy the + # directory _to_ the target instead of _inside_ the target. + # (See `man rsync`'s note on a trailing slash.) + rsync $rsync_flags $source/ $root/$target + else + rsync $rsync_flags $source $root/$target fi fi done diff --git a/nixos/tests/image-contents.nix b/nixos/tests/image-contents.nix index 90908968a7e2..858f7d8c68f4 100644 --- a/nixos/tests/image-contents.nix +++ b/nixos/tests/image-contents.nix @@ -27,13 +27,19 @@ let inherit pkgs config; lib = pkgs.lib; format = "qcow2"; - contents = [{ - source = pkgs.writeText "testFile" "contents"; - target = "/testFile"; - user = "1234"; - group = "5678"; - mode = "755"; - }]; + contents = [ + { + source = pkgs.writeText "testFile" "contents"; + target = "/testFile"; + user = "1234"; + group = "5678"; + mode = "755"; + } + { + source = ./.; + target = "/testDir"; + } + ]; }) + "/nixos.qcow2"; in makeEc2Test { @@ -42,10 +48,15 @@ in makeEc2Test { userData = null; script = '' machine.start() + # Test that if contents includes a file, it is copied to the target. assert "content" in machine.succeed("cat /testFile") fileDetails = machine.succeed("ls -l /testFile") assert "1234" in fileDetails assert "5678" in fileDetails assert "rwxr-xr-x" in fileDetails + + # Test that if contents includes a directory, it is copied to the target. + dirList = machine.succeed("ls /testDir") + assert "image-contents.nix" in dirList ''; }