tests: abuild-fetch: add test for file locking

make curl block via a fifo, so we can simulate that it is downloading.
when we want fake that curl is done with the download we write to the
fifo.

We then check that a second invocation of abuild-fetch will block til
the first one is done.
This commit is contained in:
Natanael Copa 2021-04-20 12:40:33 +02:00
parent e8cfedc2b3
commit e2882e4371
1 changed files with 38 additions and 3 deletions

View File

@ -7,17 +7,21 @@ setup() {
export PATH="$bindir:$PATH"
# fake curl
cat >> "$bindir"/curl <<-EOF
cat > "$bindir"/curl <<-EOF
#!/bin/sh
touch "$tmpdir"/curl-invoked
touch \${STAMP:-"$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
cat > "$bindir"/wget <<-EOF
#!/bin/sh
PATH=/usr/local/bin:/usr/bin:/bin
@ -58,3 +62,34 @@ teardown() {
run PATH="$bindir" WGET_EXITCODE=1 $ABUILD_FETCH -d "$tmpdir" https://example.com/non-existing
[ $status -ne 0 ]
}
@test "abuild-fetch: test locking" {
fifo="$tmpdir"/fifo
mkfifo $fifo
STAMP="$tmpdir"/stamp1 FIFO="$tmpdir"/fifo $ABUILD_FETCH -d "$tmpdir" https://example.com/foo &
pid1=$!
# make sure to unblock the fake curl in case test failure so we dont block bats
teardown() {
if [ -d /proc/$pid1 ]; then
echo "done fifo" > "$tmpdir"/fifo
fi
rm -rf "$tmpdir"
}
ls -la "$tmpdir"
export STAMP="$tmpdir"/stamp2
$ABUILD_FETCH -d "$tmpdir" https://example.com/foo &
pid2=$!
ls -la "$tmpdir"
# second stamp should not exist til after first abuild-fetch completes
[ ! -f "$tmpdir"/stamp2 ]
echo "done fifo" > "$tmpdir"/fifo
wait $pid1
wait $pid2
ls -la "$tmpdir"
[ -f "$tmpdir"/stamp2 ]
}