Test checksumming

This commit is contained in:
Sam Stephenson 2012-11-19 17:53:32 -06:00
parent 86f9cb7d60
commit 275b2f5919
9 changed files with 226 additions and 0 deletions

70
test/checksum.bats Normal file
View file

@ -0,0 +1,70 @@
#!/usr/bin/env bats
load test_helper
export RUBY_BUILD_SKIP_MIRROR=1
export RUBY_BUILD_CACHE_PATH=
@test "package URL without checksum" {
stub md5 true
stub curl "-*S* : cat package-1.0.0.tar.gz"
install_fixture definitions/without-checksum
[ "$status" -eq 0 ]
[ -x "${INSTALL_ROOT}/bin/package" ]
unstub curl
unstub md5
}
@test "package URL with valid checksum" {
stub md5 true "echo 83e6d7725e20166024a1eb74cde80677"
stub curl "-*S* : cat package-1.0.0.tar.gz"
install_fixture definitions/with-checksum
[ "$status" -eq 0 ]
[ -x "${INSTALL_ROOT}/bin/package" ]
unstub curl
unstub md5
}
@test "package URL with invalid checksum" {
stub md5 true "echo 83e6d7725e20166024a1eb74cde80677"
stub curl "-*S* : cat package-1.0.0.tar.gz"
install_fixture definitions/with-invalid-checksum
[ "$status" -eq 1 ]
[ ! -f "${INSTALL_ROOT}/bin/package" ]
unstub curl
unstub md5
}
@test "package URL with checksum but no MD5 support" {
stub md5 false
stub curl "-*S* : cat package-1.0.0.tar.gz"
install_fixture definitions/with-checksum
[ "$status" -eq 0 ]
[ -x "${INSTALL_ROOT}/bin/package" ]
unstub curl
unstub md5
}
@test "package with invalid checksum" {
stub md5 true "echo invalid"
stub curl "-*S* : cat package-1.0.0.tar.gz"
install_fixture definitions/with-checksum
[ "$status" -eq 1 ]
[ ! -f "${INSTALL_ROOT}/bin/package" ]
unstub curl
unstub md5
}

View file

@ -0,0 +1 @@
install_package "package-1.0.0" "http://example.com/packages/package-1.0.0.tar.gz#83e6d7725e20166024a1eb74cde80677" copy

View file

@ -0,0 +1 @@
install_package "package-1.0.0" "http://example.com/packages/package-1.0.0.tar.gz#invalid" copy

View file

@ -0,0 +1 @@
install_package "package-1.0.0" "http://example.com/packages/package-1.0.0.tar.gz" copy

BIN
test/fixtures/package-1.0.0.tar.gz vendored Normal file

Binary file not shown.

1
test/stubs/curl/curl Symbolic link
View file

@ -0,0 +1 @@
../stub

1
test/stubs/md5/md5 Symbolic link
View file

@ -0,0 +1 @@
../stub

105
test/stubs/stub Executable file
View file

@ -0,0 +1,105 @@
#!/usr/bin/env bash
set -e
status=0
program="${0##*/}"
PROGRAM="$(echo "$program" | tr a-z A-Z)"
[ -n "$TMPDIR" ] || TMPDIR="/tmp"
_STUB_PLAN="${PROGRAM}_STUB_PLAN"
_STUB_RUN="${PROGRAM}_STUB_RUN"
_STUB_DIR="${PROGRAM}_STUB_DIR"
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
[ -e "${!_STUB_PLAN}" ] || exit 1
[ -n "${!_STUB_RUN}" ] || eval "${_STUB_RUN}"="${TMPDIR}/${program}-stub-run"
# Initialize or load the stub run information.
eval "${_STUB_INDEX}"=1
eval "${_STUB_RESULT}"=0
[ ! -e "${!_STUB_RUN}" ] || source "${!_STUB_RUN}"
# Loop over each line in the plan.
index=0
while IFS= read -r line; do
index=$(($index + 1))
if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then
# We found the plan line we're interested in.
# Start off by assuming success.
result=0
# Split the line into an array of arguments to
# match and a command to run to produce output.
command=" $line"
if [ "$command" != "${command/ : }" ]; then
patterns="${command%% : *}"
command="${command#* : }"
fi
# Naively split patterns by whitespace for now.
# In the future, use a sed script to split while
# respecting quoting.
patterns=($patterns)
arguments=("$@")
# Match the expected argument patterns to actual
# arguments.
for (( i=0; i<${#patterns[@]}; i++ )); do
pattern="${patterns[$i]}"
argument="${arguments[$i]}"
case "$argument" in
$pattern ) ;;
* ) result=1 ;;
esac
done
# If the arguments matched, evaluate the command
# in a subshell. Otherwise, log the failure.
if [ $result -eq 0 ] ; then
dir="$(pwd)"
[ ! -d "${!_STUB_DIR}" ] || dir="${!_STUB_DIR}"
set +e
( cd "$dir" ; eval "$command" )
status="$?"
set -e
else
eval "${_STUB_RESULT}"=1
fi
fi
done < "${!_STUB_PLAN}"
if [ -n "${!_STUB_END}" ]; then
# Clean up the run file.
rm -f "${!_STUB_RUN}"
# If the number of lines in the plan is larger than
# the requested index, we failed.
if [ $index -ge "${!_STUB_INDEX}" ]; then
eval "${_STUB_RESULT}"=1
fi
# Return the result.
exit "${!_STUB_RESULT}"
else
# If the requested index is larger than the number
# of lines in the plan file, we failed.
if [ "${!_STUB_INDEX}" -gt $index ]; then
eval "${_STUB_RESULT}"=1
fi
# Write out the run information.
{ echo "${_STUB_INDEX}=$((${!_STUB_INDEX} + 1))"
echo "${_STUB_RESULT}=${!_STUB_RESULT}"
} > "${!_STUB_RUN}"
exit "$status"
fi

46
test/test_helper.bash Normal file
View file

@ -0,0 +1,46 @@
export PATH="$BATS_TEST_DIRNAME/../bin:$PATH"
export TMP="$BATS_TEST_DIRNAME/tmp"
export FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures"
export INSTALL_ROOT="$TMP/install"
teardown() {
rm -fr "$TMP"/*
}
stub() {
local program="$1"
local prefix="$(echo "$program" | tr a-z A-Z)"
shift
export "${prefix}_STUB_PLAN"="${TMP}/${program}-stub-plan"
export "${prefix}_STUB_RUN"="${TMP}/${program}-stub-run"
export "${prefix}_STUB_DIR"="$FIXTURE_ROOT"
export "${prefix}_STUB_END"=
export PATH="${BATS_TEST_DIRNAME}/stubs/${program}:$PATH"
rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run"
for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done
}
unstub() {
local program="$1"
local prefix="$(echo "$program" | tr a-z A-Z)"
export "${prefix}_STUB_DIR"=
export "${prefix}_STUB_END"=1
local path="${BATS_TEST_DIRNAME}/stubs/$program"
local escaped_path="${path//\//\\/}"
export PATH="${PATH/${escaped_path}:}"
"${path}/$program"
}
install_fixture() {
local name="$1"
local destination="$2"
[ -n "$destination" ] || destination="$INSTALL_ROOT"
run ruby-build "$FIXTURE_ROOT/$name" "$destination"
}