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