From 7a0da9ed31bb9ec452fd25904ff40388aad618ee Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Tue, 17 Sep 2024 11:50:32 +0200 Subject: [PATCH] Add tests for add-gc-root --- src/nix/store-add-gc-root.cc | 10 ++++- tests/functional/local.mk | 1 + tests/functional/meson.build | 1 + tests/functional/store-add-gc-root.sh | 53 +++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100755 tests/functional/store-add-gc-root.sh diff --git a/src/nix/store-add-gc-root.cc b/src/nix/store-add-gc-root.cc index cecab256413..a29cd2c0ca9 100644 --- a/src/nix/store-add-gc-root.cc +++ b/src/nix/store-add-gc-root.cc @@ -19,6 +19,12 @@ struct CmdAddGCRoot : StoreCommand .handler = {&links}, .completer = completePath, }); + + addFlag({ + .longName = "no-check", + .description = "Do not test the validity of created roots.", + .handler = {&checkResults, false}, + }); } std::string description() override @@ -51,7 +57,9 @@ struct CmdAddGCRoot : StoreCommand if (checkResults) { auto path = indirectRootStore.followLinksToStorePath(indirectPath); indirectRootStore.addTempRoot(path); - // TODO: ensure `path` is safe from concurrent GC or fail. + if (!indirectRootStore.isValidPath(path)) { + throw Error("Indirect root '%1%' is no a symbolic link to a valid store path", link); + } } indirectRootStore.addIndirectRoot(indirectPath); diff --git a/tests/functional/local.mk b/tests/functional/local.mk index 3f796291a56..344c10edd82 100644 --- a/tests/functional/local.mk +++ b/tests/functional/local.mk @@ -103,6 +103,7 @@ nix_tests = \ nix-profile.sh \ suggestions.sh \ store-info.sh \ + store-add-gc-root.sh \ fetchClosure.sh \ completions.sh \ impure-derivations.sh \ diff --git a/tests/functional/meson.build b/tests/functional/meson.build index 69b6d31949e..50562f3af37 100644 --- a/tests/functional/meson.build +++ b/tests/functional/meson.build @@ -184,6 +184,7 @@ suites = [ 'debugger.sh', 'extra-sandbox-profile.sh', 'help.sh', + 'store-add-gc-root.sh', ], 'workdir': meson.current_build_dir(), }, diff --git a/tests/functional/store-add-gc-root.sh b/tests/functional/store-add-gc-root.sh new file mode 100755 index 00000000000..076300f43f2 --- /dev/null +++ b/tests/functional/store-add-gc-root.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +source common.sh + +clearStoreIfPossible + +function delete() { nix-store --delete "$storePath"; } +function build() { nix build --no-link --print-out-paths -f simple.nix; } + +# Check that path can be deleted +storePath=$(build) +delete + +# Check that add-gc-root prevents deletion, +# and removing the root make it deletable again. +storePath=$(build) +ln -sn "$storePath" myroot +nix store add-gc-root myroot +if delete; then false; fi +rm myroot +delete + +# Create several roots at once +storePath=$(build) +ln -sn "$storePath" myroot1 +ln -sn "$storePath" myroot2 +ln -sn "$storePath" myroot3 +nix store add-gc-root myroot1 myroot2 myroot3 +if delete; then false; fi +rm myroot3 myroot2 +if delete; then false; fi +rm myroot1 +delete + +# Test detection of invalid roots +# 1. path deleted before root creation +storePath=$(build) +delete +ln -sn "$storePath" myroot +if nix store add-gc-root myroot; then false; fi +nix store add-gc-root --no-check myroot +rm myroot + +# 2. invalid path +ln -sn /invalid-target myroot +if nix store add-gc-root myroot; then false; fi +nix store add-gc-root --no-check myroot +rm myroot + +# Fail when trying to setup a direct root +storePath=$(build) +if nix store add-gc-root "$storePath"; then false; fi +