Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shellcheck some test scripts #10797

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ perl/Makefile.config

# /tests/functional/
/tests/functional/test-tmp
/tests/functional/common/vars-and-functions.sh
/tests/functional/common/subst-vars.sh
/tests/functional/result*
/tests/functional/restricted-innocent
/tests/functional/shell
Expand Down
15 changes: 1 addition & 14 deletions maintainers/flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -514,18 +514,6 @@
''^tests/functional/brotli\.sh$''
''^tests/functional/build-delete\.sh$''
''^tests/functional/build-dry\.sh$''
''^tests/functional/build-remote-content-addressed-fixed\.sh$''
''^tests/functional/build-remote-content-addressed-floating\.sh$''
''^tests/functional/build-remote-input-addressed\.sh$''
''^tests/functional/build-remote-trustless-after\.sh$''
''^tests/functional/build-remote-trustless-should-fail-0\.sh$''
''^tests/functional/build-remote-trustless-should-pass-0\.sh$''
''^tests/functional/build-remote-trustless-should-pass-1\.sh$''
''^tests/functional/build-remote-trustless-should-pass-2\.sh$''
''^tests/functional/build-remote-trustless-should-pass-3\.sh$''
''^tests/functional/build-remote-trustless\.sh$''
''^tests/functional/build-remote-with-mounted-ssh-ng\.sh$''
''^tests/functional/build-remote\.sh$''
''^tests/functional/build\.sh$''
''^tests/functional/ca/build-cache\.sh$''
''^tests/functional/ca/build-dry\.sh$''
Expand Down Expand Up @@ -554,8 +542,7 @@
''^tests/functional/check-reqs\.sh$''
''^tests/functional/check\.sh$''
''^tests/functional/chroot-store\.sh$''
''^tests/functional/common\.sh$''
''^tests/functional/common/init\.sh$''
''^tests/functional/common/vars-and-functions\.sh$''
''^tests/functional/completions\.sh$''
''^tests/functional/compression-levels\.sh$''
''^tests/functional/compute-levels\.sh$''
Expand Down
3 changes: 1 addition & 2 deletions mk/common-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# Remove overall test dir (at most one of the two should match) and
# remove file extension.

# shellcheck disable=SC2154
test_name=$(echo -n "$test" | sed \
test_name=$(echo -n "${test?must be defined by caller (test runner)}" | sed \
-e "s|^tests/unit/[^/]*/data/||" \
-e "s|^tests/functional/||" \
-e "s|\.sh$||" \
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/add.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

path1=$(nix-store --add ./dummy)
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/bash-profile.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

sed -e "s|@localstatedir@|$TEST_ROOT/profile-var|g" -e "s|@coreutils@|$coreutils|g" < ../../scripts/nix-profile.sh.in > $TEST_ROOT/nix-profile.sh
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/binary-cache-build-remote.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

clearStore
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/binary-cache.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

needLocalStore "'--no-require-sigs' can’t be used with the daemon"
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/brotli.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

clearStore
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/build-delete.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

clearStore
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/build-dry.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

###################################################
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/build-remote-content-addressed-fixed.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

file=build-hook-ca-fixed.nix
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/build-remote-content-addressed-floating.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

file=build-hook-ca-floating.nix
Expand Down
12 changes: 7 additions & 5 deletions tests/functional/build-remote-input-addressed.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

file=build-hook.nix
Expand All @@ -11,17 +13,17 @@ registerBuildHook () {
# Dummy post-build-hook just to ensure that it's executed correctly.
# (we can't reuse the one from `$PWD/push-to-store.sh` because of
# https://github.com/NixOS/nix/issues/4341)
cat <<EOF > $TEST_ROOT/post-build-hook.sh
cat <<EOF > "$TEST_ROOT/post-build-hook.sh"
#!/bin/sh

echo "Post hook ran successfully"
# Add an empty line to a counter file, just to check that this hook ran properly
echo "" >> $TEST_ROOT/post-hook-counter
EOF
chmod +x $TEST_ROOT/post-build-hook.sh
rm -f $TEST_ROOT/post-hook-counter
chmod +x "$TEST_ROOT/post-build-hook.sh"
rm -f "$TEST_ROOT/post-hook-counter"

echo "post-build-hook = $TEST_ROOT/post-build-hook.sh" >> $NIX_CONF_DIR/nix.conf
echo "post-build-hook = $TEST_ROOT/post-build-hook.sh" >> "$NIX_CONF_DIR/nix.conf"
}

registerBuildHook
Expand All @@ -30,4 +32,4 @@ source build-remote.sh
# `build-hook.nix` has four derivations to build, and the hook runs twice for
# each derivation (once on the builder and once on the host), so the counter
# should contain eight lines now
[[ $(cat $TEST_ROOT/post-hook-counter | wc -l) -eq 8 ]]
[[ $(wc -l < "$TEST_ROOT/post-hook-counter") -eq 8 ]]
9 changes: 7 additions & 2 deletions tests/functional/build-remote-trustless-after.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
outPath=$(readlink -f $TEST_ROOT/result)
grep 'FOO BAR BAZ' ${remoteDir}/${outPath}
# shellcheck shell=bash

# Variables must be defined by caller, so
# shellcheck disable=SC2154

outPath=$(readlink -f "$TEST_ROOT/result")
grep 'FOO BAR BAZ' "${remoteDir}/${outPath}"
6 changes: 6 additions & 0 deletions tests/functional/build-remote-trustless-should-fail-0.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

enableFeatures "daemon-trust-override"
Expand All @@ -22,8 +24,12 @@ nix-build build-hook.nix -A passthru.input2 \
# copy our already-build `input2` to the remote store. That store object
# is input-addressed, so this will fail.

# For script below
# shellcheck disable=SC2034
file=build-hook.nix
# shellcheck disable=SC2034
prog=$(readlink -e ./nix-daemon-untrusting.sh)
# shellcheck disable=SC2034
proto=ssh-ng

expectStderr 1 source build-remote-trustless.sh \
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/build-remote-trustless-should-pass-0.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

# Remote trusts us
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/build-remote-trustless-should-pass-1.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

# Remote trusts us
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/build-remote-trustless-should-pass-2.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

enableFeatures "daemon-trust-override"
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/build-remote-trustless-should-pass-3.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

enableFeatures "daemon-trust-override"
Expand Down
14 changes: 10 additions & 4 deletions tests/functional/build-remote-trustless.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# shellcheck shell=bash

# All variables should be defined externally by the scripts that source
# this, `set -u` will catch any that are forgotten.
# shellcheck disable=SC2154

requireSandboxSupport
[[ $busybox =~ busybox ]] || skipTest "no busybox"
[[ "$busybox" =~ busybox ]] || skipTest "no busybox"

unset NIX_STORE_DIR
unset NIX_STATE_DIR
Expand All @@ -8,7 +14,7 @@ remoteDir=$TEST_ROOT/remote

# Note: ssh{-ng}://localhost bypasses ssh. See tests/functional/build-remote.sh for
# more details.
nix-build $file -o $TEST_ROOT/result --max-jobs 0 \
--arg busybox $busybox \
--store $TEST_ROOT/local \
nix-build "$file" -o "$TEST_ROOT/result" --max-jobs 0 \
--arg busybox "$busybox" \
--store "$TEST_ROOT/local" \
--builders "$proto://localhost?remote-program=$prog&remote-store=${remoteDir}%3Fsystem-features=foo%20bar%20baz - - 1 1 foo,bar,baz"
14 changes: 8 additions & 6 deletions tests/functional/build-remote-with-mounted-ssh-ng.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

requireSandboxSupport
Expand All @@ -6,17 +8,17 @@ requireSandboxSupport
enableFeatures mounted-ssh-store

nix build -Lvf simple.nix \
--arg busybox $busybox \
--out-link $TEST_ROOT/result-from-remote \
--arg busybox "$busybox" \
--out-link "$TEST_ROOT/result-from-remote" \
--store mounted-ssh-ng://localhost

nix build -Lvf simple.nix \
--arg busybox $busybox \
--out-link $TEST_ROOT/result-from-remote-new-cli \
--arg busybox "$busybox" \
--out-link "$TEST_ROOT/result-from-remote-new-cli" \
--store 'mounted-ssh-ng://localhost?remote-program=nix daemon'

# This verifies that the out link was actually created and valid. The ability
# to create out links (permanent gc roots) is the distinguishing feature of
# the mounted-ssh-ng store.
cat $TEST_ROOT/result-from-remote/hello | grepQuiet 'Hello World!'
cat $TEST_ROOT/result-from-remote-new-cli/hello | grepQuiet 'Hello World!'
grepQuiet 'Hello World!' < "$TEST_ROOT/result-from-remote/hello"
grepQuiet 'Hello World!' < "$TEST_ROOT/result-from-remote-new-cli/hello"
44 changes: 24 additions & 20 deletions tests/functional/build-remote.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# shellcheck shell=bash

: "${file?must be defined by caller (remote building test case using this)}"

requireSandboxSupport
[[ $busybox =~ busybox ]] || skipTest "no busybox"
[[ "${busybox-}" =~ busybox ]] || skipTest "no busybox"

# Avoid store dir being inside sandbox build-dir
unset NIX_STORE_DIR
Expand All @@ -15,68 +19,68 @@ fi
builders=(
# system-features will automatically be added to the outer URL, but not inner
# remote-store URL.
"ssh://localhost?remote-store=$TEST_ROOT/machine1?system-features=$(join_by "%20" foo ${EXTRA_SYSTEM_FEATURES[@]}) - - 1 1 $(join_by "," foo ${EXTRA_SYSTEM_FEATURES[@]})"
"$TEST_ROOT/machine2 - - 1 1 $(join_by "," bar ${EXTRA_SYSTEM_FEATURES[@]})"
"ssh-ng://localhost?remote-store=$TEST_ROOT/machine3?system-features=$(join_by "%20" baz ${EXTRA_SYSTEM_FEATURES[@]}) - - 1 1 $(join_by "," baz ${EXTRA_SYSTEM_FEATURES[@]})"
"ssh://localhost?remote-store=$TEST_ROOT/machine1?system-features=$(join_by "%20" foo "${EXTRA_SYSTEM_FEATURES[@]}") - - 1 1 $(join_by "," foo "${EXTRA_SYSTEM_FEATURES[@]}")"
"$TEST_ROOT/machine2 - - 1 1 $(join_by "," bar "${EXTRA_SYSTEM_FEATURES[@]}")"
"ssh-ng://localhost?remote-store=$TEST_ROOT/machine3?system-features=$(join_by "%20" baz "${EXTRA_SYSTEM_FEATURES[@]}") - - 1 1 $(join_by "," baz "${EXTRA_SYSTEM_FEATURES[@]}")"
)

chmod -R +w $TEST_ROOT/machine* || true
rm -rf $TEST_ROOT/machine* || true
chmod -R +w "$TEST_ROOT/machine"* || true
rm -rf "$TEST_ROOT/machine"* || true

# Note: ssh://localhost bypasses ssh, directly invoking nix-store as a
# child process. This allows us to test LegacySSHStore::buildDerivation().
# ssh-ng://... likewise allows us to test RemoteStore::buildDerivation().
nix build -L -v -f $file -o $TEST_ROOT/result --max-jobs 0 \
--arg busybox $busybox \
--store $TEST_ROOT/machine0 \
nix build -L -v -f "$file" -o "$TEST_ROOT/result" --max-jobs 0 \
--arg busybox "$busybox" \
--store "$TEST_ROOT/machine0" \
--builders "$(join_by '; ' "${builders[@]}")"

outPath=$(readlink -f $TEST_ROOT/result)
outPath=$(readlink -f "$TEST_ROOT/result")

grep 'FOO BAR BAZ' $TEST_ROOT/machine0/$outPath
grep 'FOO BAR BAZ' "$TEST_ROOT/machine0/$outPath"

testPrintOutPath=$(nix build -L -v -f $file --no-link --print-out-paths --max-jobs 0 \
--arg busybox $busybox \
--store $TEST_ROOT/machine0 \
testPrintOutPath=$(nix build -L -v -f "$file" --no-link --print-out-paths --max-jobs 0 \
--arg busybox "$busybox" \
--store "$TEST_ROOT/machine0" \
--builders "$(join_by '; ' "${builders[@]}")"
)

[[ $testPrintOutPath =~ store.*build-remote ]]

# Ensure that input1 was built on store1 due to the required feature.
output=$(nix path-info --store $TEST_ROOT/machine1 --all)
output=$(nix path-info --store "$TEST_ROOT/machine1" --all)
echo "$output" | grepQuiet builder-build-remote-input-1.sh
echo "$output" | grepQuietInverse builder-build-remote-input-2.sh
echo "$output" | grepQuietInverse builder-build-remote-input-3.sh
unset output

# Ensure that input2 was built on store2 due to the required feature.
output=$(nix path-info --store $TEST_ROOT/machine2 --all)
output=$(nix path-info --store "$TEST_ROOT/machine2" --all)
echo "$output" | grepQuietInverse builder-build-remote-input-1.sh
echo "$output" | grepQuiet builder-build-remote-input-2.sh
echo "$output" | grepQuietInverse builder-build-remote-input-3.sh
unset output

# Ensure that input3 was built on store3 due to the required feature.
output=$(nix path-info --store $TEST_ROOT/machine3 --all)
output=$(nix path-info --store "$TEST_ROOT/machine3" --all)
echo "$output" | grepQuietInverse builder-build-remote-input-1.sh
echo "$output" | grepQuietInverse builder-build-remote-input-2.sh
echo "$output" | grepQuiet builder-build-remote-input-3.sh
unset output


for i in input1 input3; do
nix log --store $TEST_ROOT/machine0 --file "$file" --arg busybox $busybox passthru."$i" | grep hi-$i
nix log --store "$TEST_ROOT/machine0" --file "$file" --arg busybox "$busybox" "passthru.$i" | grep hi-$i
done

# Behavior of keep-failed
out="$(nix-build 2>&1 failing.nix \
--no-out-link \
--builders "$(join_by '; ' "${builders[@]}")" \
--keep-failed \
--store $TEST_ROOT/machine0 \
--store "$TEST_ROOT/machine0" \
-j0 \
--arg busybox $busybox)" || true
--arg busybox "$busybox")" || true

[[ "$out" =~ .*"note: keeping build directory".* ]]

Expand Down
2 changes: 2 additions & 0 deletions tests/functional/build.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

clearStore
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/case-hack.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

clearStore
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/check-refs.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

clearStore
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/check-reqs.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

clearStore
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/check.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

# XXX: This shouldn’t be, but #4813 cause this test to fail
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/chroot-store.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

source common.sh

echo example > $TEST_ROOT/example.txt
Expand Down
8 changes: 5 additions & 3 deletions tests/functional/common.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# shellcheck shell=bash

set -eu -o pipefail

if [[ -z "${COMMON_SH_SOURCED-}" ]]; then

COMMON_SH_SOURCED=1

dir="$(readlink -f "$(dirname "${BASH_SOURCE[0]-$0}")")"
functionalTestsDir="$(readlink -f "$(dirname "${BASH_SOURCE[0]-$0}")")"

source "$dir"/common/vars-and-functions.sh
source "$dir"/common/init.sh
source "$functionalTestsDir/common/vars-and-functions.sh"
source "$functionalTestsDir/common/init.sh"

if [[ -n "${NIX_DAEMON_PACKAGE:-}" ]]; then
startDaemon
Expand Down
Loading
Loading