2015-07-08 16:30:14 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
IFS=$'\n\t'
|
2015-01-30 08:59:48 -05:00
|
|
|
|
2015-07-08 16:30:14 -04:00
|
|
|
#
|
|
|
|
|
# Test harness for the atomic CLI.
|
|
|
|
|
#
|
|
|
|
|
export PYTHONPATH=${PYTHONPATH:-$(pwd)}
|
|
|
|
|
export WORK_DIR=$(mktemp -p $(pwd) -d -t .tmp.XXXXXXXXXX)
|
|
|
|
|
export DOCKER=${DOCKER:-"/usr/bin/docker"}
|
|
|
|
|
export SECRET=`dd if=/dev/urandom bs=4096 count=1 2> /dev/null | sha256sum`
|
2015-11-25 11:24:18 -05:00
|
|
|
export ATOMIC_LIBEXEC="$(pwd)"
|
2016-10-26 08:36:41 -04:00
|
|
|
export ATOMIC_CLIENT="$(pwd)/atomic_dbus_client.py"
|
2015-07-08 16:30:14 -04:00
|
|
|
|
2016-05-20 20:23:13 +02:00
|
|
|
mkdir $WORK_DIR/ostree-repo
|
2016-05-23 09:00:08 +02:00
|
|
|
ostree --repo=$WORK_DIR/ostree-repo init --mode=bare-user
|
2016-05-20 20:23:13 +02:00
|
|
|
export ATOMIC_OSTREE_REPO=$WORK_DIR/ostree-repo
|
|
|
|
|
|
2015-07-08 16:30:14 -04:00
|
|
|
# This image contains the secret, so it needs to be rebuilt each time.
|
|
|
|
|
cat > tests/test-images/Dockerfile.secret <<EOF
|
|
|
|
|
FROM scratch
|
|
|
|
|
LABEL "Name"="atomic-test-secret"
|
|
|
|
|
LABEL "Secret"="${SECRET}"
|
|
|
|
|
ADD secret /secret
|
2015-01-29 16:26:44 -05:00
|
|
|
EOF
|
2015-07-08 16:30:14 -04:00
|
|
|
|
|
|
|
|
LOG=${LOG:-"$(pwd)/tests.log"}
|
|
|
|
|
|
|
|
|
|
echo -n '' > ${LOG}
|
|
|
|
|
|
|
|
|
|
cleanup () {
|
|
|
|
|
rm -rf ${WORK_DIR}
|
|
|
|
|
}
|
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
2015-07-21 16:11:08 -04:00
|
|
|
_checksum () {
|
|
|
|
|
if [[ -d "${1}" ]]; then
|
|
|
|
|
CHK=`find "${1}" -type f -exec sha256sum {} \; | sha256sum`
|
|
|
|
|
echo "${CHK}"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
if [[ -e "${1}" ]]; then
|
|
|
|
|
echo "$(sha256sum ${1})"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-08 16:30:14 -04:00
|
|
|
# Ensure the test-environment has a standard set of images.
|
|
|
|
|
# This function will not rebuild images if the dockerfile
|
|
|
|
|
# is the same as its last build.
|
|
|
|
|
make_docker_images () {
|
|
|
|
|
echo "${SECRET}" > ${WORK_DIR}/secret
|
|
|
|
|
echo "Pulling standard images from Docker Hub..." | tee -a ${LOG}
|
2016-11-10 15:39:44 -06:00
|
|
|
${DOCKER} pull centos>> ${LOG}
|
2015-07-08 16:30:14 -04:00
|
|
|
echo "Building images from tests/test-images..." | tee -a ${LOG}
|
2017-04-03 19:13:18 +02:00
|
|
|
for df in `find ./tests/test-images/ -name 'Dockerfile.*' | sort`; do
|
2015-07-21 16:11:08 -04:00
|
|
|
# Don't include directories for dockerfile data
|
|
|
|
|
if [[ -d "${df}" ]]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
BASE_NAME="$(basename ${df})"
|
|
|
|
|
|
|
|
|
|
chksum=$(_checksum ${df})
|
|
|
|
|
IFS=$'.' read -a split <<< "${BASE_NAME}"
|
2015-07-08 16:30:14 -04:00
|
|
|
iname="atomic-test-${split[1]}"
|
2015-07-21 16:11:08 -04:00
|
|
|
# If there is a matching Dockerfile.X.d, then include its contents
|
|
|
|
|
# in the checksum data.
|
|
|
|
|
chksum="${chksum}$(_checksum ${df}.d)"
|
|
|
|
|
|
2015-07-08 16:30:14 -04:00
|
|
|
set +e
|
|
|
|
|
i_chksum=`${DOCKER} inspect -f '{{ .Config.Labels.Checksum }}' \
|
2015-07-21 16:11:08 -04:00
|
|
|
${iname} 2> /dev/null`
|
2015-07-08 16:30:14 -04:00
|
|
|
if [[ ${i_chksum} = "<no-value>" ]] || \
|
|
|
|
|
[[ "${i_chksum}" = "${chksum}" ]]; then
|
|
|
|
|
printf "\tSkipped : ${iname}\n"
|
|
|
|
|
continue
|
|
|
|
|
fi
|
|
|
|
|
set -e
|
|
|
|
|
|
2015-07-21 16:11:08 -04:00
|
|
|
# Copy the dockerfile into the build directory, then label the image
|
|
|
|
|
# with the original Dockerfile's checksum. This allows us to prevent
|
|
|
|
|
# rebuilding images
|
|
|
|
|
df_cp=${WORK_DIR}/${BASE_NAME}
|
2015-07-08 16:30:14 -04:00
|
|
|
cp ${df} ${df_cp}
|
|
|
|
|
printf "\nLABEL \"Checksum\"=\"${chksum}" >> ${df_cp}
|
2015-07-21 16:11:08 -04:00
|
|
|
|
2016-01-09 09:39:14 -06:00
|
|
|
# Copy help.1 into atomic-test-1
|
|
|
|
|
if [[ ${iname} = "atomic-test-1" ]]; then
|
|
|
|
|
cp ./tests/test-images/help.1 ${WORK_DIR}
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Copy help.sh into atomic-test-3
|
2016-11-09 08:45:44 -05:00
|
|
|
if [ ${iname} = "atomic-test-3" -o ${iname} = "atomic-test-4" ]; then
|
2016-01-09 09:39:14 -06:00
|
|
|
cp ./tests/test-images/help.sh ${WORK_DIR}
|
|
|
|
|
fi
|
|
|
|
|
|
2016-04-25 11:11:54 -04:00
|
|
|
# Copy install.sh into atomic-test-6
|
|
|
|
|
if [[ ${iname} = "atomic-test-6" ]]; then
|
|
|
|
|
cp ./tests/test-images/install.sh ${WORK_DIR}
|
2016-08-18 10:34:08 +02:00
|
|
|
cp ./tests/test-images/show-hostname.sh ${WORK_DIR}
|
2016-04-25 11:11:54 -04:00
|
|
|
fi
|
|
|
|
|
|
2017-03-23 16:16:13 +01:00
|
|
|
# Copy needed files into atomic-test-system
|
2016-06-01 13:12:37 +02:00
|
|
|
if [[ ${iname} = "atomic-test-system" ]]; then
|
|
|
|
|
cp ./tests/test-images/system-container-files/* ${WORK_DIR}
|
|
|
|
|
fi
|
|
|
|
|
|
2017-04-26 19:02:44 +00:00
|
|
|
# Copy needed files into atomic-test-system-update
|
|
|
|
|
if [[ ${iname} = "atomic-test-system-update" ]]; then
|
|
|
|
|
cp ./tests/test-images/system-container-update-files/* ${WORK_DIR}
|
|
|
|
|
fi
|
|
|
|
|
|
2017-03-23 16:16:13 +01:00
|
|
|
# Copy needed files atomic-test-system-hostfs
|
|
|
|
|
if [[ ${iname} = "atomic-test-system-hostfs" ]]; then
|
|
|
|
|
cp ./tests/test-images/system-container-files-hostfs/* ${WORK_DIR}
|
|
|
|
|
fi
|
|
|
|
|
|
2017-03-28 15:57:45 -04:00
|
|
|
# Copy runonce files into into atomic-test-runonce-system
|
|
|
|
|
if [[ ${iname} = "atomic-test-runonce" ]]; then
|
|
|
|
|
cp ./tests/test-images/system-container-runonce-files/* ${WORK_DIR}
|
|
|
|
|
fi
|
|
|
|
|
|
2015-07-21 16:11:08 -04:00
|
|
|
# Remove the old image... Though there may not be one.
|
2015-07-08 16:30:14 -04:00
|
|
|
set +e
|
|
|
|
|
${DOCKER} rmi ${iname} &>> ${LOG}
|
|
|
|
|
set -e
|
2015-07-21 16:11:08 -04:00
|
|
|
|
|
|
|
|
if [[ -d "${df}.d" ]]; then
|
|
|
|
|
cp -r "${df}.d" "${WORK_DIR}/${BASE_NAME}.d"
|
|
|
|
|
fi
|
2017-01-26 13:30:10 -06:00
|
|
|
SECONDS=0
|
2015-07-08 16:30:14 -04:00
|
|
|
${DOCKER} build -t ${iname} -f ${df_cp} ${WORK_DIR} >> ${LOG}
|
2017-01-26 13:30:10 -06:00
|
|
|
DURATION=$SECONDS
|
2015-07-21 16:11:08 -04:00
|
|
|
|
|
|
|
|
# Clean up build files.
|
|
|
|
|
rm "${df_cp}"
|
|
|
|
|
if [[ -d "${WORK_DIR}/${BASE_NAME}.d" ]]; then
|
|
|
|
|
rm -r "${WORK_DIR}/${BASE_NAME}.d"
|
|
|
|
|
fi
|
2017-01-26 13:30:10 -06:00
|
|
|
printf "\tBuilt : ${iname} in $DURATION seconds\n"
|
2015-07-08 16:30:14 -04:00
|
|
|
done
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
make_docker_images
|
|
|
|
|
|
2018-06-27 12:53:01 +02:00
|
|
|
|
|
|
|
|
if ! skopeo copy docker-daemon://atomic-test-system:latest ostree:test@$ATOMIC_OSTREE_REPO; then
|
|
|
|
|
export SKOPEO_NO_OSTREE=1
|
|
|
|
|
fi
|
|
|
|
|
|
2017-02-17 10:43:45 -06:00
|
|
|
if [ ! -n "${PYTHON+ }" ]; then
|
|
|
|
|
if hash python3 > /dev/null 2>&1 /dev/null; then
|
|
|
|
|
PYTHON=$(hash -t python3)
|
|
|
|
|
elif type python3 > /dev/null 2>&1; then
|
|
|
|
|
PYTHON=$(type python3 | awk '{print $3}')
|
|
|
|
|
elif hash python2 > /dev/null 2>&1; then
|
|
|
|
|
PYTHON=$(hash -t python2)
|
|
|
|
|
elif type python2 > /dev/null 2>&1; then
|
|
|
|
|
PYTHON=$(type python2 | awk '{print $3}')
|
|
|
|
|
else
|
|
|
|
|
PYTHON='/usr/bin/python'
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2017-08-06 14:47:00 -05:00
|
|
|
export PYTHON=$PYTHON
|
2017-02-17 10:43:45 -06:00
|
|
|
|
2017-03-21 09:03:06 -05:00
|
|
|
# Add images with INSTALL labels to /var/lib/atomic/install.json
|
|
|
|
|
INSTALL_DATA_FILE="$(pwd)/install.json"
|
|
|
|
|
INSTALL_DATA=`docker images --no-trunc | awk '/atomic-test-/ {printf "\"%s\": {\"install_id\": \"%s\"},\n", $1, $3}' | sed 's/sha256://g' | sed '$ s/,$//'`
|
|
|
|
|
echo "{$INSTALL_DATA}" > ${INSTALL_DATA_FILE}
|
|
|
|
|
export ATOMIC_INSTALL_JSON=$INSTALL_DATA_FILE
|
2017-02-17 10:43:45 -06:00
|
|
|
|
2015-07-08 16:30:14 -04:00
|
|
|
echo "UNIT TESTS:"
|
|
|
|
|
|
2016-07-28 17:28:43 +02:00
|
|
|
COVERAGE_BIN=${COVERAGE_BIN-"/usr/bin/coverage"}
|
2016-01-18 11:17:14 -05:00
|
|
|
|
|
|
|
|
if [[ ! -x "${COVERAGE_BIN}" ]]; then
|
|
|
|
|
# Check to see if it is in local instead.
|
|
|
|
|
COVERAGE_BIN="/usr/local/bin/coverage"
|
|
|
|
|
fi
|
|
|
|
|
|
2015-07-20 13:49:28 -04:00
|
|
|
if [[ ! -x "${COVERAGE_BIN}" ]]; then
|
2016-01-18 11:17:14 -05:00
|
|
|
# The executable is "coverage2" on systems with default python3 and no
|
|
|
|
|
# python3 install.
|
|
|
|
|
COVERAGE_BIN="/usr/bin/coverage2"
|
2015-07-08 16:30:14 -04:00
|
|
|
fi
|
|
|
|
|
|
2016-10-27 14:11:37 -04:00
|
|
|
if [[ ! -x "${COVERAGE_BIN}" ]]; then
|
|
|
|
|
COVERAGE_BIN="/usr/bin/coverage3"
|
|
|
|
|
fi
|
|
|
|
|
|
2016-10-12 16:43:34 -04:00
|
|
|
if [[ -x "${COVERAGE_BIN}" ]]; then
|
|
|
|
|
COVERAGE="${COVERAGE_BIN}
|
2015-07-20 13:49:28 -04:00
|
|
|
run
|
|
|
|
|
--source=./Atomic/
|
|
|
|
|
--branch"
|
2016-10-12 16:43:34 -04:00
|
|
|
else
|
|
|
|
|
COVERAGE="${PYTHON:-/usr/bin/python}"
|
|
|
|
|
fi
|
2015-07-08 16:30:14 -04:00
|
|
|
|
2016-10-25 10:44:13 -05:00
|
|
|
if [ ! -n "${TEST_INTEGRATION+ }" ]; then
|
|
|
|
|
set +e
|
|
|
|
|
# Was a single test requested?
|
|
|
|
|
if [ -n "${TEST_UNIT+ }" ]; then
|
|
|
|
|
UTEST=tests/unit/test_${TEST_UNIT}.py
|
|
|
|
|
# Make sure the test actually exists
|
|
|
|
|
if [ ! -e ${UTEST} ]; then
|
|
|
|
|
echo "Failed to find the unittest ${UTEST}"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
${COVERAGE} ${UTEST} | tee -a ${LOG}
|
|
|
|
|
else
|
|
|
|
|
${COVERAGE} -m unittest discover ./tests/unit/ | tee -a ${LOG}
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
_UNIT_FAIL="$?"
|
|
|
|
|
set -e
|
|
|
|
|
else
|
|
|
|
|
echo " SKIPPING UNIT TESTS..."
|
|
|
|
|
_UNIT_FAIL=0
|
|
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
2015-07-08 16:30:14 -04:00
|
|
|
|
|
|
|
|
# CLI integration tests.
|
|
|
|
|
let failures=0 || true
|
|
|
|
|
printf "\nINTEGRATION TESTS:\n" | tee -a ${LOG}
|
|
|
|
|
|
2017-06-12 14:15:59 +02:00
|
|
|
export ATOMIC_NO_DEBUG="atomic"
|
|
|
|
|
|
2016-10-12 16:43:34 -04:00
|
|
|
export ATOMIC="atomic
|
2016-08-10 12:31:49 +02:00
|
|
|
--debug"
|
2016-10-12 16:43:34 -04:00
|
|
|
if [[ -x "${COVERAGE_BIN}" ]]; then
|
|
|
|
|
export ATOMIC="${COVERAGE}
|
|
|
|
|
--append
|
|
|
|
|
$ATOMIC"
|
|
|
|
|
fi
|
2015-07-20 13:49:28 -04:00
|
|
|
|
2015-12-18 11:50:33 -05:00
|
|
|
|
2016-10-25 10:44:13 -05:00
|
|
|
if [ ! -n "${TEST_UNIT+ }" ]; then
|
2017-08-23 17:21:07 +00:00
|
|
|
for tf in `find ./tests/integration/ -name 'test_*.sh'`; do
|
2017-02-17 10:43:45 -06:00
|
|
|
bn=$(basename "$tf")
|
|
|
|
|
extension="${bn##*.}"
|
2016-10-25 10:44:13 -05:00
|
|
|
|
|
|
|
|
if [ -n "${TEST_INTEGRATION+ }" ]; then
|
2017-02-17 10:43:45 -06:00
|
|
|
tfbn="${bn%.*}"
|
|
|
|
|
|
2016-10-25 10:44:13 -05:00
|
|
|
tfbn="${tfbn#test_}"
|
|
|
|
|
if [[ " $TEST_INTEGRATION " != *" $tfbn "* ]]; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
2015-12-18 11:50:33 -05:00
|
|
|
fi
|
|
|
|
|
|
2016-10-25 10:44:13 -05:00
|
|
|
# If it is not running with ENABLE_DESTRUCTIVE and the test has
|
|
|
|
|
# not the :test-destructive tag, skip it silently.
|
|
|
|
|
if test -z "${ENABLE_DESTRUCTIVE-}" && grep ":destructive-test" ${tf} &> /dev/null; then
|
|
|
|
|
continue
|
|
|
|
|
fi
|
2016-05-27 16:44:45 +02:00
|
|
|
|
2016-10-25 10:44:13 -05:00
|
|
|
printf "Running %-40.40s" "$(basename ${tf})...."
|
|
|
|
|
printf "\n==== ${tf} ====\n" >> ${LOG}
|
2017-02-17 10:43:45 -06:00
|
|
|
if [ "${extension}" == "py" ]; then
|
|
|
|
|
tf="$PYTHON ${tf}"
|
|
|
|
|
fi
|
2016-10-25 10:44:13 -05:00
|
|
|
if ${tf} &>> ${LOG}; then
|
|
|
|
|
printf "PASS\n";
|
2016-05-27 16:44:45 +02:00
|
|
|
else
|
2016-10-25 10:44:13 -05:00
|
|
|
if test $? = 77; then
|
|
|
|
|
printf "SKIP\n";
|
|
|
|
|
else
|
|
|
|
|
printf "FAIL\n";
|
|
|
|
|
let "failures += 1"
|
|
|
|
|
fi
|
2016-05-27 16:44:45 +02:00
|
|
|
fi
|
2016-10-25 10:44:13 -05:00
|
|
|
done
|
|
|
|
|
else
|
|
|
|
|
echo " SKIPPING INTEGRATION TESTS..."
|
|
|
|
|
fi
|
2015-07-08 16:30:14 -04:00
|
|
|
|
2016-10-12 16:43:34 -04:00
|
|
|
if [[ -x "${COVERAGE_BIN}" ]]; then
|
|
|
|
|
echo "Coverage report:" | tee -a ${LOG}
|
|
|
|
|
${COVERAGE_BIN} report | tee -a ${LOG}
|
|
|
|
|
fi
|
2015-07-20 13:49:28 -04:00
|
|
|
|
2015-07-08 16:30:14 -04:00
|
|
|
if [[ "${failures}" -eq "0" ]]; then
|
|
|
|
|
if [[ $_UNIT_FAIL -eq 0 ]]; then
|
|
|
|
|
echo "ALL TESTS PASSED"
|
|
|
|
|
exit 0
|
|
|
|
|
else
|
|
|
|
|
echo "Unit tests failed."
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
if [[ $_UNIT_FAIL -ne 0 ]]; then
|
|
|
|
|
echo "Unit tests failed."
|
|
|
|
|
fi
|
|
|
|
|
echo "Integration test failures: ${failures}"
|
|
|
|
|
echo "See ${LOG} for more information."
|
|
|
|
|
fi
|
|
|
|
|
exit 1
|