From 85a24530524e5a7410fd00714a4a8f4633b4f758 Mon Sep 17 00:00:00 2001 From: cidkidnix Date: Wed, 17 May 2023 13:48:59 -0500 Subject: [PATCH] Add tests for read-only local store Make sure we don't go down the path of making temproots when doing operations on a read-only store --- src/libstore/gc.cc | 4 ++++ tests/local.mk | 3 ++- tests/read-only-store.sh | 29 +++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/read-only-store.sh diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc index 0038ec802..09f6ddb9e 100644 --- a/src/libstore/gc.cc +++ b/src/libstore/gc.cc @@ -110,6 +110,10 @@ void LocalStore::createTempRootsFile() void LocalStore::addTempRoot(const StorePath & path) { + if (readOnly) { + debug("Read-only store doesn't support creating lock files for temp roots, but nothing can be deleted anyways."); + return; + } createTempRootsFile(); /* Open/create the global GC lock file. */ diff --git a/tests/local.mk b/tests/local.mk index 9e340e2e2..576529567 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -134,7 +134,8 @@ nix_tests = \ flakes/show.sh \ impure-derivations.sh \ path-from-hash-part.sh \ - toString-path.sh + toString-path.sh \ + read-only-store.sh ifeq ($(HAVE_LIBCPUID), 1) nix_tests += compute-levels.sh diff --git a/tests/read-only-store.sh b/tests/read-only-store.sh new file mode 100644 index 000000000..20ded9634 --- /dev/null +++ b/tests/read-only-store.sh @@ -0,0 +1,29 @@ +source common.sh + +clearStore + +## Testing read-only mode without forcing the underlying store to actually be read-only + +# Make sure the command fails when the store doesn't already have a database +expectStderr 1 nix-store --store local?read-only=true --add dummy | grepQuiet "unable to create database while in read-only mode" + +# Make sure the store actually has a current-database +nix-store --add dummy + +# Try again and make sure we fail when adding a item not already in the store +expectStderr 1 nix-store --store local?read-only=true --add eval.nix | grepQuiet "attempt to write a readonly database" + +# Make sure we can get an already-present store-path in the database +nix-store --store local?read-only=true --add dummy + +## Ensure store is actually read-only +chmod -R -w $TEST_ROOT/store +chmod -R -w $TEST_ROOT/var + +# Make sure we fail on add operations on the read-only store +# This is only for adding files that are not *already* in the store +expectStderr 1 nix-store --add eval.nix | grepQuiet "error: opening lock file '$(readlink -e $TEST_ROOT)/var/nix/db/big-lock'" +expectStderr 1 nix-store --store local?read-only=true --add eval.nix | grepQuiet "Permission denied" + +# Should succeed +nix-store --store local?read-only=true --add dummy