abuild/tests/abuild-fetch.bats

136 lines
3.8 KiB
Bash

setup() {
export ABUILD_FETCH="$PWD/../abuild-fetch"
bindir="$BATS_TEST_TMPDIR"/bin
mkdir -p "$bindir"
export PATH="$bindir:$PATH"
# fake curl
cat > "$bindir"/curl <<-EOF
#!/bin/sh
touch \${STAMP:-"$BATS_TEST_TMPDIR"/curl-invoked}
echo "[\$\$] Fake curl invoked with: \$@"
if [ -n "\$FIFO" ]; then
echo "[\$\$] waiting for fifo \$FIFO"
cat "\$FIFO"
fi
exit \${CURL_EXITCODE:-0}
EOF
chmod +x "$bindir"/curl
# fake wget
cat > "$bindir"/wget <<-EOF
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/bin
touch "$BATS_TEST_TMPDIR"/wget-invoked
echo "Fake wget invoked with: \$@"
exit \${WGET_EXITCODE:-0}
EOF
chmod +x "$bindir"/wget
}
@test "abuild-fetch: help text" {
$ABUILD_FETCH -h
}
@test "abuild-fetch: test curl invocation" {
$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 "$BATS_TEST_TMPDIR" https://example.com/non-existing
[ $status -ne 0 ]
}
@test "abuild-fetch: that --insecure is passed for http://" {
$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 "$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 "$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 "$BATS_TEST_TMPDIR" http://example.com/non-existing | grep no-check-certificate
}
@test "abuild-fetch: test locking" {
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" > "$BATS_TEST_TMPDIR"/fifo1
fi
if [ -d /proc/$pid2 ]; then
echo "done fifo2" > "$BATS_TEST_TMPDIR"/fifo2
fi
rm -rf "$BATS_TEST_TMPDIR"
}
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 "$BATS_TEST_TMPDIR"/stamp1 ]; do
sleep 0.1
done
# try a second fetch, while the first one is still running
STAMP="$BATS_TEST_TMPDIR"/stamp2 FIFO="$BATS_TEST_TMPDIR"/fifo2 $ABUILD_FETCH -d "$BATS_TEST_TMPDIR" https://example.com/foo &
pid2=$!
ls -la "$BATS_TEST_TMPDIR"
# second stamp should not exist til after first abuild-fetch completes
[ ! -f "$BATS_TEST_TMPDIR"/stamp2 ]
# tell curl to similuate download complete of first
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 "$BATS_TEST_TMPDIR"/stamp2 ]; do
sleep 0.1
done
# first instance is done. lets retry download. second instance should block us
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 "$BATS_TEST_TMPDIR"/stamp1 ]
fi
# simulate second download finished
echo "done fifo2" > "$BATS_TEST_TMPDIR"/fifo2
wait $pid2
# first should get unblocked
echo "done fifo1" > "$BATS_TEST_TMPDIR"/fifo1
wait $pid1
# verify that first actually called curl
[ -f "$BATS_TEST_TMPDIR"/stamp1 ]
ls -la "$BATS_TEST_TMPDIR"
}