Skip to content

Commit

Permalink
Add tests for add-gc-root
Browse files Browse the repository at this point in the history
  • Loading branch information
layus committed Oct 1, 2024
1 parent 59c6f2b commit 7a0da9e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/nix/store-add-gc-root.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions tests/functional/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions tests/functional/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ suites = [
'debugger.sh',
'extra-sandbox-profile.sh',
'help.sh',
'store-add-gc-root.sh',
],
'workdir': meson.current_build_dir(),
},
Expand Down
53 changes: 53 additions & 0 deletions tests/functional/store-add-gc-root.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 7a0da9e

Please sign in to comment.