From fab4acccedbca68694602e12b50a475f3b3ccd0a Mon Sep 17 00:00:00 2001 From: Piotr Kwiecinski <2151333+piotrkwiecinski@users.noreply.github.com> Date: Tue, 1 Apr 2025 21:38:41 +0200 Subject: [PATCH] nixosTests.binary-cache{no-compression,xz}: migrate to runTest Part of #386873 --- nixos/tests/all-tests.nix | 15 +++- nixos/tests/binary-cache.nix | 147 +++++++++++++++++------------------ 2 files changed, 83 insertions(+), 79 deletions(-) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index d1fe552bbc1f..82fef1866ba7 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -231,9 +231,18 @@ in beanstalkd = handleTest ./beanstalkd.nix { }; bees = handleTest ./bees.nix { }; benchexec = handleTest ./benchexec.nix { }; - binary-cache = handleTest ./binary-cache.nix { compression = "zstd"; }; - binary-cache-no-compression = handleTest ./binary-cache.nix { compression = "none"; }; - binary-cache-xz = handleTest ./binary-cache.nix { compression = "xz"; }; + binary-cache = runTest { + imports = [ ./binary-cache.nix ]; + _module.args.compression = "zstd"; + }; + binary-cache-no-compression = runTest { + imports = [ ./binary-cache.nix ]; + _module.args.compression = "none"; + }; + binary-cache-xz = runTest { + imports = [ ./binary-cache.nix ]; + _module.args.compression = "xz"; + }; bind = handleTest ./bind.nix { }; bird = handleTest ./bird.nix { }; birdwatcher = handleTest ./birdwatcher.nix { }; diff --git a/nixos/tests/binary-cache.nix b/nixos/tests/binary-cache.nix index fda0b9a2096b..c74595bd90f0 100644 --- a/nixos/tests/binary-cache.nix +++ b/nixos/tests/binary-cache.nix @@ -1,88 +1,83 @@ -{ compression, ... }@args: +{ lib, compression, ... }: +{ + name = "binary-cache-" + compression; + meta.maintainers = with lib.maintainers; [ thomasjm ]; -import ./make-test-python.nix ( - { lib, pkgs, ... }: + nodes.machine = + { pkgs, ... }: + { + imports = [ ../modules/installer/cd-dvd/channel.nix ]; + environment.systemPackages = with pkgs; [ + openssl + python3 + ]; - { - name = "binary-cache-" + compression; - meta.maintainers = with lib.maintainers; [ thomasjm ]; + # We encrypt the binary cache before putting it on the machine so Nix + # doesn't bring any references along. + environment.etc."binary-cache.tar.gz.encrypted".source = + with pkgs; + runCommand "binary-cache.tar.gz.encrypted" + { + allowReferences = [ ]; + nativeBuildInputs = [ openssl ]; + } + '' + tar -czf tmp.tar.gz -C "${ + mkBinaryCache { + rootPaths = [ hello ]; + inherit compression; + } + }" . + openssl enc -aes-256-cbc -salt -in tmp.tar.gz -out $out -k mysecretpassword + ''; - nodes.machine = - { pkgs, ... }: - { - imports = [ ../modules/installer/cd-dvd/channel.nix ]; - environment.systemPackages = with pkgs; [ - openssl - python3 - ]; + nix.extraOptions = '' + experimental-features = nix-command + ''; + }; - # We encrypt the binary cache before putting it on the machine so Nix - # doesn't bring any references along. - environment.etc."binary-cache.tar.gz.encrypted".source = - with pkgs; - runCommand "binary-cache.tar.gz.encrypted" - { - allowReferences = [ ]; - nativeBuildInputs = [ openssl ]; - } - '' - tar -czf tmp.tar.gz -C "${ - mkBinaryCache { - rootPaths = [ hello ]; - inherit compression; - } - }" . - openssl enc -aes-256-cbc -salt -in tmp.tar.gz -out $out -k mysecretpassword - ''; + testScript = '' + # Decrypt the cache into /tmp/binary-cache.tar.gz + machine.succeed("openssl enc -d -aes-256-cbc -in /etc/binary-cache.tar.gz.encrypted -out /tmp/binary-cache.tar.gz -k mysecretpassword") - nix.extraOptions = '' - experimental-features = nix-command - ''; - }; + # Untar the cache into /tmp/cache + machine.succeed("mkdir /tmp/cache") + machine.succeed("tar -C /tmp/cache -xf /tmp/binary-cache.tar.gz") - testScript = '' - # Decrypt the cache into /tmp/binary-cache.tar.gz - machine.succeed("openssl enc -d -aes-256-cbc -in /etc/binary-cache.tar.gz.encrypted -out /tmp/binary-cache.tar.gz -k mysecretpassword") + # Sanity test of cache structure + status, stdout = machine.execute("ls /tmp/cache") + cache_files = stdout.split() + assert ("nix-cache-info" in cache_files) + assert ("nar" in cache_files) - # Untar the cache into /tmp/cache - machine.succeed("mkdir /tmp/cache") - machine.succeed("tar -C /tmp/cache -xf /tmp/binary-cache.tar.gz") + # Nix store ping should work + machine.succeed("nix store ping --store file:///tmp/cache") - # Sanity test of cache structure - status, stdout = machine.execute("ls /tmp/cache") - cache_files = stdout.split() - assert ("nix-cache-info" in cache_files) - assert ("nar" in cache_files) + # Cache should contain a .narinfo referring to "hello" + grepLogs = machine.succeed("grep -l 'StorePath: /nix/store/[[:alnum:]]*-hello-.*' /tmp/cache/*.narinfo") - # Nix store ping should work - machine.succeed("nix store ping --store file:///tmp/cache") + # Get the store path referenced by the .narinfo + narInfoFile = grepLogs.strip() + narInfoContents = machine.succeed("cat " + narInfoFile) + import re + match = re.match(r"^StorePath: (/nix/store/[a-z0-9]*-hello-.*)$", narInfoContents, re.MULTILINE) + if not match: raise Exception("Couldn't find hello store path in cache") + storePath = match[1] - # Cache should contain a .narinfo referring to "hello" - grepLogs = machine.succeed("grep -l 'StorePath: /nix/store/[[:alnum:]]*-hello-.*' /tmp/cache/*.narinfo") + # Make sure the store path doesn't exist yet + machine.succeed("[ ! -d %s ] || exit 1" % storePath) - # Get the store path referenced by the .narinfo - narInfoFile = grepLogs.strip() - narInfoContents = machine.succeed("cat " + narInfoFile) - import re - match = re.match(r"^StorePath: (/nix/store/[a-z0-9]*-hello-.*)$", narInfoContents, re.MULTILINE) - if not match: raise Exception("Couldn't find hello store path in cache") - storePath = match[1] + # Should be able to build hello using the cache + logs = machine.succeed("nix-build -A hello '' --option require-sigs false --option trusted-substituters file:///tmp/cache --option substituters file:///tmp/cache 2>&1") + logLines = logs.split("\n") + if not "this path will be fetched" in logLines[0]: raise Exception("Unexpected first log line") + def shouldBe(got, desired): + if got != desired: raise Exception("Expected '%s' but got '%s'" % (desired, got)) + shouldBe(logLines[1], " " + storePath) + shouldBe(logLines[2], "copying path '%s' from 'file:///tmp/cache'..." % storePath) + shouldBe(logLines[3], storePath) - # Make sure the store path doesn't exist yet - machine.succeed("[ ! -d %s ] || exit 1" % storePath) - - # Should be able to build hello using the cache - logs = machine.succeed("nix-build -A hello '' --option require-sigs false --option trusted-substituters file:///tmp/cache --option substituters file:///tmp/cache 2>&1") - logLines = logs.split("\n") - if not "this path will be fetched" in logLines[0]: raise Exception("Unexpected first log line") - def shouldBe(got, desired): - if got != desired: raise Exception("Expected '%s' but got '%s'" % (desired, got)) - shouldBe(logLines[1], " " + storePath) - shouldBe(logLines[2], "copying path '%s' from 'file:///tmp/cache'..." % storePath) - shouldBe(logLines[3], storePath) - - # Store path should exist in the store now - machine.succeed("[ -d %s ] || exit 1" % storePath) - ''; - } -) args + # Store path should exist in the store now + machine.succeed("[ -d %s ] || exit 1" % storePath) + ''; +}