2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-20 01:12:18 +00:00
Files
xcat-core/xCAT-test/bats/helpers/script_sandbox.bash
T
Vinícius Ferrão 28b22d7825 test(provisioning): exercise disk selection with BATS
Source the unchanged selector with explicit partition and udev fixtures.
Bound awk discovery to the fixture so runner filesystem size cannot delay
selection. Keep rendered-installer coverage and independent CI failures.
2026-10-16 19:06:47 -03:00

43 lines
1.6 KiB
Bash

#!/usr/bin/env bash
load "${BASH_SOURCE[0]%/*}/shell_source.bash"
setup_script_sandbox()
{
[ "$(uname -s)" = Linux ] || skip 'Script filesystem isolation requires Linux'
local bwrap utility executable directory
bwrap=$(PATH=/usr/bin:/bin type -P bwrap) || {
echo 'Install bubblewrap to run the script tests' >&2
return 1
}
fixture="${BATS_TEST_TMPDIR:?bats-core 1.4 or newer is required}/fixture"
mkdir -p "$fixture"/{bin,etc,tmp,var,run}
sandbox=(env -i PATH=/usr/bin:/bin LC_ALL=C "$bwrap"
--unshare-all --die-with-parent --new-session
--ro-bind / / --proc /proc --dev /dev
--bind "$fixture/etc" /etc --bind "$fixture/tmp" /tmp
--bind "$fixture/var" /var --bind "$fixture/run" /run
--bind "$fixture" /run/fixture --chdir /tmp
--setenv PATH /usr/bin:/bin --setenv LC_ALL C)
for directory in /usr/bin /usr/sbin /bin /sbin; do
if [ -d "$directory" ] && [ ! -L "$directory" ]; then
sandbox+=(--ro-bind "$fixture/bin" "$directory")
fi
done
for utility in bash sh timeout "$@"; do
executable=$(PATH=/usr/bin:/bin type -P "$utility") || {
echo "Required utility is unavailable: $utility" >&2
return 1
}
cp -L "$executable" "$fixture/bin/$utility"
done
if PATH=/usr/bin:/bin type -P coreutils >/dev/null; then
cp -L "$(PATH=/usr/bin:/bin type -P coreutils)" "$fixture/bin/coreutils"
fi
run "${sandbox[@]}" /bin/sh -c 'test ! -e /etc/os-release'
if [ "$status" -ne 0 ]; then
echo "Cannot isolate script: $output" >&2
return 1
fi
}