From 32a96169d650f5e9e822c743947cd8d8025cd50a Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Thu, 26 Feb 2026 20:30:45 +0100 Subject: [PATCH] testing: migrate optimise-store.sh Change-Id: I900183e11ba1405c9aa72028a8902ad8cf023e69 --- tests/functional/meson.build | 1 - tests/functional/optimise-store.sh | 44 ------------------ .../functional2/store/test_optimise_store.py | 46 +++++++++++++++++++ 3 files changed, 46 insertions(+), 45 deletions(-) delete mode 100644 tests/functional/optimise-store.sh create mode 100644 tests/functional2/store/test_optimise_store.py diff --git a/tests/functional/meson.build b/tests/functional/meson.build index 415db56d1..efaf33954 100644 --- a/tests/functional/meson.build +++ b/tests/functional/meson.build @@ -59,7 +59,6 @@ functional_tests_scripts = [ 'fetchTree-file.sh', 'simple.sh', 'referrers.sh', - 'optimise-store.sh', 'substitute-with-invalid-ca.sh', 'signing.sh', 'gc-non-blocking.sh', diff --git a/tests/functional/optimise-store.sh b/tests/functional/optimise-store.sh deleted file mode 100644 index 8c2d05cd5..000000000 --- a/tests/functional/optimise-store.sh +++ /dev/null @@ -1,44 +0,0 @@ -source common.sh - -clearStore - -outPath1=$(echo 'with import ./config.nix; mkDerivation { name = "foo1"; builder = builtins.toFile "builder" "mkdir $out; echo hello > $out/foo"; }' | nix-build - --no-out-link --auto-optimise-store) -outPath2=$(echo 'with import ./config.nix; mkDerivation { name = "foo2"; builder = builtins.toFile "builder" "mkdir $out; echo hello > $out/foo"; }' | nix-build - --no-out-link --auto-optimise-store) - -inode1="$(stat --format=%i $outPath1/foo)" -inode2="$(stat --format=%i $outPath2/foo)" -if [ "$inode1" != "$inode2" ]; then - echo "inodes do not match" - exit 1 -fi - -nlink="$(stat --format=%h $outPath1/foo)" -if [ "$nlink" != 3 ]; then - echo "link count incorrect" - exit 1 -fi - -outPath3=$(echo 'with import ./config.nix; mkDerivation { name = "foo3"; builder = builtins.toFile "builder" "mkdir $out; echo hello > $out/foo"; }' | nix-build - --no-out-link) - -inode3="$(stat --format=%i $outPath3/foo)" -if [ "$inode1" = "$inode3" ]; then - echo "inodes match unexpectedly" - exit 1 -fi - -# XXX: This should work through the daemon too -NIX_REMOTE="" nix-store --optimise - -inode1="$(stat --format=%i $outPath1/foo)" -inode3="$(stat --format=%i $outPath3/foo)" -if [ "$inode1" != "$inode3" ]; then - echo "inodes do not match" - exit 1 -fi - -nix-store --gc - -if [ -n "$(ls $NIX_STORE_DIR/.links)" ]; then - echo ".links directory not empty after GC" - exit 1 -fi diff --git a/tests/functional2/store/test_optimise_store.py b/tests/functional2/store/test_optimise_store.py new file mode 100644 index 000000000..c284dcf24 --- /dev/null +++ b/tests/functional2/store/test_optimise_store.py @@ -0,0 +1,46 @@ +from pathlib import Path + +from testlib.fixtures.file_helper import with_files +from testlib.fixtures.nix import Nix +from testlib.utils import get_global_asset + + +@with_files({"config.nix": get_global_asset("config.nix")}) +class TestOptimizeStore: + def _test_optimise_store(self, nix: Nix): + def build(name: str, *extra_args: str) -> Path: + expr = f""" + with import ./config.nix; mkDerivation {{ + name = "{name}"; + builder = builtins.toFile "builder" "mkdir $out; echo hello > $out/foo"; + }} + """ + + result = nix.nix_build(["-E", expr, "--no-out-link", *extra_args]).run().ok() + return Path(result.stdout_plain) + + out1 = build("foo1", "--auto-optimise-store") + out2 = build("foo2", "--auto-optimise-store") + + assert (out1 / "foo").samefile(out2 / "foo") + assert (out1 / "foo").stat().st_nlink == 3 + + out3 = build("foo3", "--no-auto-optimise-store") + + assert not (out1 / "foo").samefile(out3 / "foo") + + nix.nix_store(["--optimise"]).run().ok() + + assert (out1 / "foo").samefile(out3 / "foo") + + nix.nix_store(["--gc"]).run().ok() + + assert list((nix.env.dirs.real_store_dir / ".links").glob("*")) == [] + + def test_optimise_store(self, nix: Nix): + self._test_optimise_store(nix) + + def test_optimise_store_daemon(self, nix: Nix): + nix.settings.auto_optimise_store = True + with nix.daemon([], {"trusted-users": "*"}) as inner: + self._test_optimise_store(inner)