tests: use BATS_TEST_TMPDIR, don't dump temp files in repo

This commit is contained in:
Alex Xu (Hello71) 2021-12-24 15:52:32 -05:00 committed by Natanael Copa
parent 074a335464
commit b69e0230d0
6 changed files with 78 additions and 108 deletions

View File

@ -1,8 +1,7 @@
setup() {
export ABUILD_FETCH="$PWD/../abuild-fetch"
tmpdir="$BATS_TMPDIR"/abuild-fetch
bindir="$tmpdir"/bin
bindir="$BATS_TEST_TMPDIR"/bin
mkdir -p "$bindir"
export PATH="$bindir:$PATH"
@ -10,7 +9,7 @@ setup() {
cat > "$bindir"/curl <<-EOF
#!/bin/sh
touch \${STAMP:-"$tmpdir"/curl-invoked}
touch \${STAMP:-"$BATS_TEST_TMPDIR"/curl-invoked}
echo "[\$\$] Fake curl invoked with: \$@"
if [ -n "\$FIFO" ]; then
echo "[\$\$] waiting for fifo \$FIFO"
@ -25,7 +24,7 @@ setup() {
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/bin
touch "$tmpdir"/wget-invoked
touch "$BATS_TEST_TMPDIR"/wget-invoked
echo "Fake wget invoked with: \$@"
exit \${WGET_EXITCODE:-0}
EOF
@ -33,108 +32,104 @@ setup() {
}
teardown() {
rm -rf "$tmpdir"
}
@test "abuild-fetch: help text" {
$ABUILD_FETCH -h
}
@test "abuild-fetch: test curl invocation" {
$ABUILD_FETCH -d "$tmpdir" https://example.com/non-existing
[ -f "$tmpdir"/curl-invoked ]
$ABUILD_FETCH -d "$BATS_TEST_TMPDIR" https://example.com/non-existing
[ -f "$BATS_TEST_TMPDIR"/curl-invoked ]
}
@test "abuild-fetch: test curl failure" {
run CURL_EXITCODE=1 $ABUILD_FETCH -d "$tmpdir" https://example.com/non-existing
run CURL_EXITCODE=1 $ABUILD_FETCH -d "$BATS_TEST_TMPDIR" https://example.com/non-existing
[ $status -ne 0 ]
}
@test "abuild-fetch: that --insecure is passed for http://" {
$ABUILD_FETCH -d "$tmpdir" http://example.com/non-existing | grep insecure
$ABUILD_FETCH -d "$BATS_TEST_TMPDIR" http://example.com/non-existing | grep insecure
}
@test "abuild-fetch: test wget fallback" {
rm "$bindir"/curl
PATH="$bindir" $ABUILD_FETCH -d "$tmpdir" https://example.com/non-existing
[ -f "$tmpdir"/wget-invoked ]
PATH="$bindir" $ABUILD_FETCH -d "$BATS_TEST_TMPDIR" https://example.com/non-existing
[ -f "$BATS_TEST_TMPDIR"/wget-invoked ]
}
@test "abuild-fetch: test wget fallback failure" {
rm "$bindir"/curl
run PATH="$bindir" WGET_EXITCODE=1 $ABUILD_FETCH -d "$tmpdir" https://example.com/non-existing
run PATH="$bindir" WGET_EXITCODE=1 $ABUILD_FETCH -d "$BATS_TEST_TMPDIR" https://example.com/non-existing
[ $status -ne 0 ]
}
@test "abuild-fetch: test that --no-check-certificate is passed to wget fallback with http://" {
rm "$bindir"/curl
PATH="$bindir" $ABUILD_FETCH -d "$tmpdir" http://example.com/non-existing | grep no-check-certificate
PATH="$bindir" $ABUILD_FETCH -d "$BATS_TEST_TMPDIR" http://example.com/non-existing | grep no-check-certificate
}
@test "abuild-fetch: test locking" {
fifo1="$tmpdir"/fifo1
fifo2="$tmpdir"/fifo2
fifo1="$BATS_TEST_TMPDIR"/fifo1
fifo2="$BATS_TEST_TMPDIR"/fifo2
mkfifo $fifo1 $fifo2
# make sure to unblock the fake curl in case test failure so we dont block bats
teardown() {
if [ -d /proc/$pid1 ]; then
echo "done fifo1" > "$tmpdir"/fifo1
echo "done fifo1" > "$BATS_TEST_TMPDIR"/fifo1
fi
if [ -d /proc/$pid2 ]; then
echo "done fifo2" > "$tmpdir"/fifo2
echo "done fifo2" > "$BATS_TEST_TMPDIR"/fifo2
fi
rm -rf "$tmpdir"
rm -rf "$BATS_TEST_TMPDIR"
}
CURL_EXITCODE=1 STAMP="$tmpdir"/stamp1 FIFO="$tmpdir"/fifo1 $ABUILD_FETCH -d "$tmpdir" https://example.com/foo &
CURL_EXITCODE=1 STAMP="$BATS_TEST_TMPDIR"/stamp1 FIFO="$BATS_TEST_TMPDIR"/fifo1 $ABUILD_FETCH -d "$BATS_TEST_TMPDIR" https://example.com/foo &
pid1=$!
# wait til curl is called
while ! [ -f "$tmpdir"/stamp1 ]; do
while ! [ -f "$BATS_TEST_TMPDIR"/stamp1 ]; do
sleep 0.1
done
# try a second fetch, while the first one is still running
STAMP="$tmpdir"/stamp2 FIFO="$tmpdir"/fifo2 $ABUILD_FETCH -d "$tmpdir" https://example.com/foo &
STAMP="$BATS_TEST_TMPDIR"/stamp2 FIFO="$BATS_TEST_TMPDIR"/fifo2 $ABUILD_FETCH -d "$BATS_TEST_TMPDIR" https://example.com/foo &
pid2=$!
ls -la "$tmpdir"
ls -la "$BATS_TEST_TMPDIR"
# second stamp should not exist til after first abuild-fetch completes
[ ! -f "$tmpdir"/stamp2 ]
[ ! -f "$BATS_TEST_TMPDIR"/stamp2 ]
# tell curl to similuate download complete of first
echo "done fifo1" > "$tmpdir"/fifo1
echo "done fifo1" > "$BATS_TEST_TMPDIR"/fifo1
run wait $pid1
[ $status -ne 0 ]
# wait til second instance gets lock to simulate download start
while ! [ -f "$tmpdir"/stamp2 ]; do
while ! [ -f "$BATS_TEST_TMPDIR"/stamp2 ]; do
sleep 0.1
done
# first instance is done. lets retry download. second instance should block us
rm "$tmpdir"/stamp1
STAMP="$tmpdir"/stamp1 FIFO="$tmpdir"/fifo1 $ABUILD_FETCH -d "$tmpdir" https://example.com/foo &
rm "$BATS_TEST_TMPDIR"/stamp1
STAMP="$BATS_TEST_TMPDIR"/stamp1 FIFO="$BATS_TEST_TMPDIR"/fifo1 $ABUILD_FETCH -d "$BATS_TEST_TMPDIR" https://example.com/foo &
pid1=$!
sleep 0.2
# the first stamp should not exist, second instance should block the retry
# skip this test on s390x, due to sleep(0) not working there
if [ "$(uname -m)" != "s390x" ]; then
[ ! -f "$tmpdir"/stamp1 ]
[ ! -f "$BATS_TEST_TMPDIR"/stamp1 ]
fi
# simulate second download finished
echo "done fifo2" > "$tmpdir"/fifo2
echo "done fifo2" > "$BATS_TEST_TMPDIR"/fifo2
wait $pid2
# first should get unblocked
echo "done fifo1" > "$tmpdir"/fifo1
echo "done fifo1" > "$BATS_TEST_TMPDIR"/fifo1
wait $pid1
# verify that first actually called curl
[ -f "$tmpdir"/stamp1 ]
[ -f "$BATS_TEST_TMPDIR"/stamp1 ]
ls -la "$tmpdir"
ls -la "$BATS_TEST_TMPDIR"
}

View File

@ -1,22 +1,10 @@
setup() {
startdir=$PWD
export ABUILD_GZSPLIT="$PWD/.."/abuild-gzsplit
outdir="$PWD/tmp"
mkdir -p $outdir
export ABUILD_GZSPLIT="$PWD/../abuild-gzsplit"
datadir="$PWD/testdata"
cd "$outdir"
}
teardown() {
rm -f "$outdir"/control.tar.gz \
"$outdir"/data.tar.gz \
"$outdir"/signatures.tar.gz
rmdir "$outdir"
cd "$startdir"
cd "$BATS_TEST_TMPDIR"
}
@test "abuild-gzsplit: 3.11 package" {
run $ABUILD_GZSPLIT < "$datadir"/alpine-base-3.11.6-r0.apk
[ "$status" -eq 0 ]
}

View File

@ -1,23 +1,18 @@
setup() {
export ABUILD_KEYGEN="$PWD/../abuild-keygen"
export ABUILD_SHAREDIR="$PWD/.."
tmpdir="$BATS_TMPDIR"/abuild-keygen
export ABUILD_USERDIR="$tmpdir"/user
export ABUILD_USERDIR="$BATS_TEST_TMPDIR"/user
mkdir -p "$ABUILD_USERDIR"
export abuild_keygen_install_root=${ABUILD_USERDIR}
# provide a fake git
mkdir -p "$tmpdir"/bin
cat >"$tmpdir"/bin/git <<-EOF
mkdir -p "$BATS_TEST_TMPDIR"/bin
cat >"$BATS_TEST_TMPDIR"/bin/git <<-EOF
#!/bin/sh
exit 1
EOF
chmod +x "$tmpdir"/bin/git
export PATH="$tmpdir/bin:$PATH"
}
teardown() {
rm -rf "$tmpdir"
chmod +x "$BATS_TEST_TMPDIR"/bin/git
export PATH="$BATS_TEST_TMPDIR/bin:$PATH"
}
@test "abuild-keygen: help text" {
@ -39,7 +34,7 @@ teardown() {
}
@test "abuild-keygen: --install option (interactive)" {
yes | SUDO= $ABUILD_KEYGEN --install
echo | SUDO= $ABUILD_KEYGEN --install
}
@test "abuild-keygen: --install -n (non-interacive)" {

View File

@ -4,10 +4,9 @@ setup() {
export ABUILD="$PWD/../abuild"
export ABUILD_SHAREDIR="$PWD/.."
export ABUILD_CONF=/dev/null
tmpdir="$BATS_TMPDIR"/abuild
export REPODEST="$tmpdir"/packages
export REPODEST="$BATS_TEST_TMPDIR"/packages
export CLEANUP="srcdir bldroot pkgdir deps"
export WORKDIR="$tmpdir"/work
export WORKDIR="$BATS_TEST_TMPDIR"/work
export APORTSDIR="$PWD"/testrepo
export PATH="$PWD/../:$PATH"
export ARCH=$(apk --print-arch)
@ -16,11 +15,7 @@ setup() {
abuild-keygen -ain >/dev/null 2>&1
fi
mkdir -p "$tmpdir" "$WORKDIR"
}
teardown() {
rm -rf "$tmpdir"
mkdir -p "$BATS_TEST_TMPDIR" "$WORKDIR"
}
@test 'abuild-sign: do not record user name/id in index' {

View File

@ -4,11 +4,10 @@ setup() {
export ABUILD="$PWD/../abuild"
export ABUILD_SHAREDIR="$PWD/.."
export ABUILD_CONF=/dev/null
tmpdir="$BATS_TMPDIR"/abuild
export REPODEST="$tmpdir"/packages
export REPODEST="$BATS_TEST_TMPDIR"/packages
export CLEANUP="srcdir bldroot pkgdir deps"
export WORKDIR="$tmpdir"/work
export GIT_CONFIG_GLOBAL="$tmpdir"/gitconfig
export WORKDIR="$BATS_TEST_TMPDIR"/work
export GIT_CONFIG_GLOBAL="$BATS_TEST_TMPDIR"/gitconfig
export APORTSDIR="$PWD"/testrepo
export PATH="$PWD/../:$PATH"
@ -16,14 +15,10 @@ setup() {
abuild-keygen -ain >/dev/null 2>&1
fi
mkdir -p "$tmpdir" "$WORKDIR"
mkdir -p "$WORKDIR"
printf "[color]\n\tui = always\n" > "$GIT_CONFIG_GLOBAL"
}
teardown() {
rm -rf "$tmpdir"
}
@test "abuild: help text" {
$ABUILD -h
}
@ -67,8 +62,8 @@ teardown() {
arch=$($ABUILD -A)
$ABUILD
mkdir "$tmpdir/dbgpkg-1.0-r0"
cd "$tmpdir/dbgpkg-1.0-r0"
mkdir "$BATS_TEST_TMPDIR/dbgpkg-1.0-r0"
cd "$BATS_TEST_TMPDIR/dbgpkg-1.0-r0"
tar -xf "$REPODEST"/testrepo/$arch/dbgpkg-1.0-r0.apk
! [ -e usr/lib/debug ]
debuginfo=$(readelf -wk usr/bin/hello | grep '^ Separate debug info file: [^/]*\.debug$')
@ -76,8 +71,8 @@ teardown() {
[ "$(nm usr/bin/hello 2>&1)" = "nm: usr/bin/hello: no symbols" ]
[ usr/bin/hello -ef usr/bin/hello-hard ]
mkdir "$tmpdir/dbgpkg-dbg-1.0-r0"
cd "$tmpdir/dbgpkg-dbg-1.0-r0"
mkdir "$BATS_TEST_TMPDIR/dbgpkg-dbg-1.0-r0"
cd "$BATS_TEST_TMPDIR/dbgpkg-dbg-1.0-r0"
tar -xf "$REPODEST"/testrepo/$arch/dbgpkg-dbg-1.0-r0.apk
! [ -e usr/bin ]
[ -n "$(nm usr/lib/debug/usr/bin/$debuginfo_file)" ]
@ -113,14 +108,14 @@ teardown() {
}
@test "abuild: test checksum generation" {
mkdir -p "$tmpdir"/foo
cat > "$tmpdir"/foo/APKBUILD <<-EOF
mkdir -p "$BATS_TEST_TMPDIR"/foo
cat > "$BATS_TEST_TMPDIR"/foo/APKBUILD <<-EOF
pkgname="foo"
pkgver="1.0"
source="test.txt"
EOF
echo "foo" > "$tmpdir"/foo/test.txt
cd "$tmpdir"/foo
echo "foo" > "$BATS_TEST_TMPDIR"/foo/test.txt
cd "$BATS_TEST_TMPDIR"/foo
$ABUILD checksum
. ./APKBUILD && echo "$sha512sums" | sed '/^$/d' > sums
cat sums
@ -128,22 +123,22 @@ teardown() {
}
@test "abuild: test duplicates in checksum generation" {
mkdir -p "$tmpdir"/foo "$tmpdir"/foo/dir1 "$tmpdir"/foo/dir2
cat > "$tmpdir"/foo/APKBUILD <<-EOF
mkdir -p "$BATS_TEST_TMPDIR"/foo "$BATS_TEST_TMPDIR"/foo/dir1 "$BATS_TEST_TMPDIR"/foo/dir2
cat > "$BATS_TEST_TMPDIR"/foo/APKBUILD <<-EOF
pkgname="foo"
pkgver="1.0"
source="dir1/testfile dir2/testfile"
EOF
echo "first" > "$tmpdir"/foo/dir1/testfile
echo "second" > "$tmpdir"/foo/dir2/testfile
cd "$tmpdir"/foo
echo "first" > "$BATS_TEST_TMPDIR"/foo/dir1/testfile
echo "second" > "$BATS_TEST_TMPDIR"/foo/dir2/testfile
cd "$BATS_TEST_TMPDIR"/foo
run $ABUILD checksum
[ $status -ne 0 ]
}
@test "abuild: verify main package does not inherit subpackage dependencies" {
mkdir -p "$tmpdir"/testrepo/subpkg-dep-leak
cd "$tmpdir"/testrepo/subpkg-dep-leak
mkdir -p "$BATS_TEST_TMPDIR"/testrepo/subpkg-dep-leak
cd "$BATS_TEST_TMPDIR"/testrepo/subpkg-dep-leak
cat > APKBUILD <<-EOF
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname="subpkg-dep-leak"
@ -186,7 +181,8 @@ teardown() {
}
@test "abuild: reject initd script with improper shebang" {
cd testrepo/invalid-initd/
cp -r testrepo/invalid-initd "$BATS_TEST_TMPDIR"
cd "$BATS_TEST_TMPDIR/invalid-initd"
sed 's#@source@#test.initd#' APKBUILD.in >APKBUILD
run $ABUILD unpack
@ -197,7 +193,8 @@ teardown() {
@test "abuild: reject remote initd script with improper shebang" {
skip 'flaky'
cd testrepo/invalid-initd/
cp -r testrepo/invalid-initd "$BATS_TEST_TMPDIR"
cd "$BATS_TEST_TMPDIR/invalid-initd"
sed 's#@source@#test.initd::https://tpaste.us/ovyL?.initd#' APKBUILD.in >APKBUILD
$ABUILD fetch
@ -209,7 +206,8 @@ teardown() {
@test "abuild: reject remote initd without initd extension with improper shebang" {
skip 'Not handled yet'
cd testrepo/invalid-initd/
cp -r testrepo/invalid-initd "$BATS_TEST_TMPDIR"
cd "$BATS_TEST_TMPDIR/invalid-initd"
sed 's#@source@#test.initd::https://tpaste.us/ovyL#' APKBUILD.in >APKBUILD
run $ABUILD fetch unpack

View File

@ -4,27 +4,26 @@ setup() {
export ABUILD_KEYGEN="$PWD/../abuild-keygen"
export ABUILD_SHAREDIR="$PWD/.."
export ABUILD_CONF=/dev/null
tmpdir="$BATS_TMPDIR"/abump
export ABUILD_USERDIR="$tmpdir"/.config
export ABUILD_USERDIR="$BATS_TEST_TMPDIR"/.config
export PACKAGER="Test User <user@example.com>"
export REPODEST="$tmpdir"/packages
mkdir -p $tmpdir
export REPODEST="$BATS_TEST_TMPDIR"/packages
mkdir -p $BATS_TEST_TMPDIR
export CLEANUP="srcdir bldroot pkgdir deps"
export APORTSDIR="$tmpdir"
export APORTSDIR="$BATS_TEST_TMPDIR"
export ABUILD_OPTS=""
export ABUILD_APK_INDEX_OPTS="--keys-dir=$ABUILD_USERDIR"
export PATH="$PWD/../:$PATH"
$ABUILD_KEYGEN --append -n
cd "$tmpdir"
cd "$BATS_TEST_TMPDIR"
git init --quiet
git config user.email "user@example.com"
git config user.name "Test User"
}
teardown() {
rm -rf "$tmpdir"
rm -rf "$BATS_TEST_TMPDIR"
}
@test "abump: help text" {
@ -32,8 +31,8 @@ teardown() {
}
@test "abump: simple bump" {
mkdir -p "$tmpdir"/main/foo
cd "$tmpdir"/main/foo
mkdir -p "$BATS_TEST_TMPDIR"/main/foo
cd "$BATS_TEST_TMPDIR"/main/foo
echo "first" > foo-1.0.txt
echo "second" > foo-1.1.txt
cat > APKBUILD <<-EOF
@ -60,8 +59,8 @@ teardown() {
}
@test "abump: test bumping same version" {
mkdir -p "$tmpdir"/main/foo
cd "$tmpdir"/main/foo
mkdir -p "$BATS_TEST_TMPDIR"/main/foo
cd "$BATS_TEST_TMPDIR"/main/foo
echo "first" > foo-1.0.txt
echo "second" > foo-1.1.txt
cat > APKBUILD <<-EOF
@ -89,8 +88,8 @@ teardown() {
}
@test "abump: test bumping same version which is not in git" {
mkdir -p "$tmpdir"/main/foo
cd "$tmpdir"/main/foo
mkdir -p "$BATS_TEST_TMPDIR"/main/foo
cd "$BATS_TEST_TMPDIR"/main/foo
echo "first" > foo-1.0.txt
echo "second" > foo-1.1.txt
cat > APKBUILD <<-EOF