Browse Source

new: added ``cached_wget`` utility function to compose intended for charms to use.

test
Valentin Lab 6 years ago
parent
commit
376c915b10
  1. 18
      bin/compose
  2. 109
      test/wget

18
bin/compose

@ -2601,6 +2601,24 @@ EOF
echo "}" echo "}"
} }
cached_wget() {
local cache_file="$CACHEDIR/$FUNCNAME.cache.$(echo "$*" | md5_compat)" \
url="$1"
if [ -e "$cache_file" ]; then
cat "$cache_file"
touch "$cache_file"
return 0
fi
wget -O- "${url}" |
tee "$cache_file"
if [ "${PIPESTATUS[0]}" != 0 ]; then
rm "$cache_file"
die "Unable to fetch '$url'."
return 1
fi
}
export -f cached_wget
[ "$SOURCED" ] && return 0 [ "$SOURCED" ] && return 0

109
test/wget

@ -0,0 +1,109 @@
#!/usr/bin/env bash-shlib
# -*- mode: shell-script -*-
include shunit
depends sed grep git mkdir readlink
export -f matches
export grep
tmp=/tmp
tprog="../bin/compose"
tprog=$(readlink -f $tprog)
export PATH=".:$PATH"
short_tprog=$(basename "$tprog")
##
## Convenience function
##
init_test() {
test_tmpdir=$(mktemp -d -t tmp.XXXXXXXXXX)
cd "$test_tmpdir"
export CACHEDIR="$test_tmpdir/.cache"
mkdir -p "$CACHEDIR"
}
tear_test() {
rm -rf "$test_tmpdir"
}
##
## Tests
##
function test_wget {
init_test
PORT=45467
assert_list <<EOF
### cached wget
## -- should be able to fetch URL
. "$tprog"
echo -e "HTTP/1.1 200 OK\n\ntoto" | nc -l localhost $PORT &
out=\$(cached_wget http://localhost:$PORT) || exit 1
expected="toto"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- same URL should not try again to join server
. "$tprog"
out=\$(cached_wget http://localhost:$PORT) || exit 1
expected="toto"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
## -- wrong URL should complain
. "$tprog"
out=\$(cached_wget http://localhost:$PORT/foo) || exit 0
echo "Was expected to fail."
exit 1
## -- and be retested
. "$tprog"
echo -e "HTTP/1.1 200 OK\n\ntiti" | nc -l localhost $PORT &
out=\$(cached_wget http://localhost:$PORT/foo) || exit 1
expected="titi"
[ "\$out" == "\$expected" ] || {
echo -e "DIFF:\n\$(diff <(echo "\$out") <(echo "\$expected"))"
exit 1
}
EOF
tear_test
}
continue_on_error="0" testbench $*
Loading…
Cancel
Save