2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-12-04 08:07:04 +00:00

test(postscripts): reuse the restricted script sandbox

Share filesystem setup, command staging and executable restrictions. Keep each adapter responsible for its script mounts and working directory.

Route and package cases now use the common restricted tool set and per-test writable directories. Preserve their command doubles and network namespace isolation.
This commit is contained in:
Vinícius Ferrão
2026-11-21 02:59:57 -03:00
parent 85ffaffca5
commit a1b5224dc7
3 changed files with 38 additions and 61 deletions
+30 -7
View File
@@ -2,38 +2,61 @@
load "${BASH_SOURCE[0]%/*}/shell_source.bash"
setup_script_sandbox()
setup_filesystem_sandbox()
{
[ "$(uname -s)" = Linux ] || skip 'Script filesystem isolation requires Linux'
local bwrap utility executable directory
local bwrap
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}
mkdir -p "$fixture"/{bin,etc,tmp,var,run} || return
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
--bind "$fixture" /run/fixture
--setenv PATH /usr/bin:/bin --setenv LC_ALL C)
}
restrict_sandbox_tools()
{
local directory
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
}
stage_sandbox_tools()
{
local utility executable
for utility in "$@"; do
executable=$(PATH=/usr/bin:/bin type -P "$utility") || {
echo "Required utility is unavailable: $utility" >&2
return 1
}
cp -L "$executable" "$fixture/bin/$utility"
cp -L "$executable" "$fixture/bin/$utility" || return
done
if PATH=/usr/bin:/bin type -P coreutils >/dev/null; then
cp -L "$(PATH=/usr/bin:/bin type -P coreutils)" "$fixture/bin/coreutils"
cp -L "$(PATH=/usr/bin:/bin type -P coreutils)" "$fixture/bin/coreutils" || return
fi
}
setup_script_sandbox()
{
local directory=/tmp
if [ "${1:-}" = --no-chdir ]; then
directory=
shift
fi
setup_filesystem_sandbox || return
[ -z "$directory" ] || sandbox+=(--chdir "$directory")
stage_sandbox_tools bash sh timeout "$@" || return
restrict_sandbox_tools
run "${sandbox[@]}" /bin/sh -c 'test ! -e /etc/os-release'
if [ "$status" -ne 0 ]; then
echo "Cannot isolate script: $output" >&2
+3 -34
View File
@@ -1,39 +1,12 @@
#!/usr/bin/env bats
load 'helpers/shell_source'
load 'helpers/script_sandbox'
setup()
{
[ "$(uname -s)" = Linux ] || skip 'otherpkgs filesystem isolation requires Linux'
local utility executable
local utilities=(bash sh basename dirname cat cp expr grep ls mkdir rm uname wc)
local bwrap
bwrap=$(PATH=/usr/bin:/bin type -P bwrap) || {
echo 'Install bubblewrap to run the otherpkgs test' >&2
return 1
}
setup_script_sandbox --no-chdir basename dirname cat cp expr grep ls mkdir rm uname wc
postscripts=$(repo_path xCAT/postscripts)
fixture="$BATS_TEST_TMPDIR/fixture"
mkdir -p "$fixture/bin"
sandbox=(env -i PATH=/usr/bin:/bin LC_ALL=C "$bwrap"
--unshare-all --die-with-parent --new-session
--ro-bind / / --tmpfs /etc --tmpfs /usr/bin --tmpfs /tmp
--proc /proc --dev /dev --setenv PATH /usr/bin --setenv LC_ALL C)
if PATH=/usr/bin:/bin type -P coreutils >/dev/null; then
utilities+=(coreutils)
fi
for utility in "${utilities[@]}"; do
executable=$(PATH=/usr/bin:/bin type -P "$utility") || {
echo "Required utility is unavailable: $utility" >&2
return 1
}
sandbox+=(--ro-bind "$executable" "/usr/bin/$utility")
done
run "${sandbox[@]}" /usr/bin/sh -c 'test ! -e /etc/os-release'
if [ "$status" -ne 0 ]; then
echo "Cannot isolate otherpkgs: $output" >&2
return 1
fi
sandbox+=(--setenv PATH /usr/bin)
}
run_case()
@@ -83,10 +56,6 @@ SH
local command=("${sandbox[@]}"
--bind "$fixture" /tmp/fixture
--ro-bind "$postscripts" /tmp/postscripts --chdir /tmp/fixture)
local tool
for tool in logger dpkg rpm mount "$manager"; do
command+=(--ro-bind "$fixture/bin/$tool" "/usr/bin/$tool")
done
command+=(
--setenv OSVER rhel9 --setenv ARCH x86_64 --setenv UPDATENODE 1
--setenv NFSSERVER package-server --setenv HTTPPORT 80
+5 -20
View File
@@ -1,21 +1,11 @@
#!/usr/bin/env bats
load 'helpers/shell_source'
load 'helpers/script_sandbox'
setup()
{
[ "$(uname -s)" = Linux ] || skip 'routeop filesystem isolation requires Linux'
local bwrap utility
bwrap=$(PATH=/usr/bin:/bin type -P bwrap) || {
echo 'Install bubblewrap to run the routeop test' >&2
return 1
}
for utility in bash cat dirname grep sed tr uname; do
PATH=/usr/bin:/bin type -P "$utility" >/dev/null || {
echo "Required utility is unavailable: $utility" >&2
return 1
}
done
setup_script_sandbox --no-chdir cat dirname grep sed tr uname
local utility
postscripts=$(repo_path xCAT/postscripts)
for utility in routeop xcatlib.sh; do
[ -r "$postscripts/$utility" ] || {
@@ -23,7 +13,6 @@ setup()
return 1
}
done
fixture="$BATS_TEST_TMPDIR/fixture"
mkdir -p "$fixture/bin" "$fixture/etc/sysconfig/network"
printf '%s\n' SLE VERSION=15 >"$fixture/etc/SUSE-brand"
saved_routes="$fixture/etc/sysconfig/network/routes"
@@ -65,13 +54,9 @@ printf '%s\n' "unexpected nmcli $*" >>/tmp/fixture/commands
exit 97
SH
chmod +x "$fixture/bin/ip" "$fixture/bin/logger" "$fixture/bin/nmcli"
sandbox=(env -i PATH=/usr/bin:/bin LC_ALL=C "$bwrap"
--unshare-all --die-with-parent --new-session
--ro-bind / / --tmpfs /tmp --tmpfs /run --proc /proc --dev /dev
--bind "$fixture" /tmp/fixture --bind "$fixture/etc" /etc
sandbox+=(--bind "$fixture" /tmp/fixture
--ro-bind "$postscripts" /tmp/postscripts --chdir /tmp/fixture
--setenv PATH /tmp/fixture/bin:/usr/bin:/bin --setenv LC_ALL C
--setenv OSVER sles15)
--setenv PATH /tmp/fixture/bin:/usr/bin:/bin --setenv OSVER sles15)
run "${sandbox[@]}" /bin/sh -c 'test -f /etc/SUSE-brand && test ! -e /etc/os-release'
if [ "$status" -ne 0 ]; then
echo "Cannot isolate routeop: $output" >&2