Skip to content
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
38 changes: 37 additions & 1 deletion tests/integration/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ unset IMAGES
# Path to binaries compiled from packages in tests/cmd by "make test-binaries").
TESTBINDIR=${INTEGRATION_ROOT}/../cmd/_bin

# ROOT should not be inherited from the environment.
if [ -v ROOT ]; then
echo "helpers.bash: ignoring ROOT set in the environment ($ROOT)" >&2
unset ROOT
fi

# Some variables may not always be set. Set those to empty value,
# if unset, to avoid "unbound variable" error.
: "${ROOTLESS_FEATURES:=}"
Expand Down Expand Up @@ -788,6 +794,31 @@ function wait_pids_gone() {
return 1
}

# Name of the marker file, used by make_test_root and is_test_root.
ROOT_MARKER=".runc-integration-test-root"

# make_test_root creates a directory to be used as $ROOT (or a similar
# per-test directory), marking it as safe to remove in a teardown.
function make_test_root() {
local dir="$1"

mkdir -p "$dir"
touch "$dir/$ROOT_MARKER"
}

# is_test_root returns 0 if the argument is a directory created by
# make_test_root, meaning it is safe to remove it recursively.
function is_test_root() {
local dir="${1:-}"

[ -d "$dir" ] || return 1
# Must be under the bats-created temporary directory.
[ -n "${BATS_RUN_TMPDIR:-}" ] || return 1
[[ "$dir" == "$BATS_RUN_TMPDIR"/* ]] || return 1
# Must contain the marker file.
[ -f "$dir/$ROOT_MARKER" ]
}

function setup_recvtty() {
[ ! -v ROOT ] && return 1 # must not be called without ROOT set
local dir="$ROOT/tty"
Expand Down Expand Up @@ -852,6 +883,7 @@ function setup_bundle() {

# Root for various container directories (state, tty, bundle).
ROOT=$(mktemp -d "$BATS_RUN_TMPDIR/runc.XXXXXX")
make_test_root "$ROOT"
mkdir -p "$ROOT/state" "$ROOT/bundle/rootfs"

# Directories created by mktemp -d have 0700 permission bits. Tests
Expand Down Expand Up @@ -887,7 +919,11 @@ function teardown_bundle() {
for ct in $(__runc list -q); do
__runc delete -f "$ct"
done
rm -rf "$ROOT"
if is_test_root "$ROOT"; then
rm -rf "$ROOT"
else
echo "teardown_bundle: refusing to remove $ROOT (no $ROOT_MARKER in it)" >&2
fi
remove_parent
check_cgroup_removed
}
Expand Down
1 change: 1 addition & 0 deletions tests/integration/list.bats
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load helpers
function setup() {
setup_busybox
ALT_ROOT="$ROOT/alt"
make_test_root "$ALT_ROOT"
mkdir -p "$ALT_ROOT/state"
}

Expand Down
8 changes: 4 additions & 4 deletions tests/rootless.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ALL_FEATURES=("idmap" "cgroup")
if [ -v RUNC_USE_SYSTEMD ]; then
ALL_FEATURES=("idmap")
fi
ROOT="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")"
SRC_ROOT="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")"

# List of environment variables needed for the tests.
# They are usually exported, but since we use ssh below,
Expand Down Expand Up @@ -182,7 +182,7 @@ features_powerset="$(powerset "${ALL_FEATURES[@]}")"

# Make sure we have container images downloaded, as otherwise
# rootless user won't be able to write to $TESTDATA.
"$ROOT"/tests/integration/get-images.sh >/dev/null
"$SRC_ROOT"/tests/integration/get-images.sh >/dev/null

# Iterate over the powerset of all features.
IFS=:
Expand Down Expand Up @@ -218,10 +218,10 @@ for ROOTLESS_FEATURES in $features_powerset; do
# Operation not permitted". Set the correct value explicitly.
ssh_env+=("XDG_RUNTIME_DIR=/run/user/$(id -u rootless)")
ssh -t -t -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "$HOME/.ssh/rootless.key" \
rootless@localhost -- "${ssh_env[@]}" bats -t "$ROOT/tests/integration$ROOTLESS_TESTPATH"
rootless@localhost -- "${ssh_env[@]}" bats -t "$SRC_ROOT/tests/integration$ROOTLESS_TESTPATH"
else
export "${ENV_LIST[@]}"
sudo -HE -u rootless PATH="$PATH" "$(command -v bats)" -t "$ROOT/tests/integration$ROOTLESS_TESTPATH"
sudo -HE -u rootless PATH="$PATH" "$(command -v bats)" -t "$SRC_ROOT/tests/integration$ROOTLESS_TESTPATH"
fi
cleanup
done