Skip to content

Instantly share code, notes, and snippets.

@tomgco
Last active January 2, 2018 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomgco/f11a0d3cae7c900e68958856fb0ceca5 to your computer and use it in GitHub Desktop.
Save tomgco/f11a0d3cae7c900e68958856fb0ceca5 to your computer and use it in GitHub Desktop.
Run the CNCF kubernetes conformance tests.
#!/usr/bin/env bash
###
#
# This suite of tests runs the cncf kubernetes cluster conformance tests.
# These tests can take upto an hour (60mins) to run, so sit back, grab a
# cup of tea and enjoy seeing your build going green! (we hope).
#
###
#
# Prelude - make bash behave sanely
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
#
set -euo pipefail
IFS=$'\n\t'
# Beware of CDPATH gotchas causing cd not to work correctly when a user has
# set this in their environment
# https://bosker.wordpress.com/2012/02/12/bash-scripters-beware-of-the-cdpath/
unset CDPATH
# resolved directory and self
declare -r DIR=$(cd "$(dirname "$0")" && pwd)
declare -r THIS_SCRIPT="${DIR}/$(basename "$0")"
cd "${DIR}"
INTERACTIVE=
CONTEXT=
PREVIOUS_CONTEXT=$(kubectl config current-context)
main() {
get_or_set_smoke_test_context
start_smoke_test
wait_for_pod_running
wait_for_smoke_test_exit
copy_files
print_results
delete_smoke_test
reset_context
}
usage() {
cat << EOF
usage: $0 [option]
Kubernetes conformance test
OPTIONS:
-h Show this message
-c [context] Switch to the specified kubectl context
EOF
}
if [[ -t 0 ]] && [[ -t 1 ]]; then
INTERACTIVE=1
fi
while getopts "hc:?" OPTION
do
case $OPTION in
h)
usage
exit 0
;;
c)
CONTEXT=$OPTARG
;;
?)
usage
exit 404
;;
esac
done
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
RESET="$(tput sgr0)"
get_or_set_smoke_test_context() {
local temp_context
if [ -z "$CONTEXT" ]; then
temp_context=$(kubectl config current-context)
else
kubectl config use-context "$CONTEXT"
temp_context=$CONTEXT
echo "kubectl current context has been changed to ${GREEN}${temp_context}${RESET}"
fi
cat <<EOF
The current cluster-info is $(kubectl cluster-info | head -n1)
Your current context is "${RED}$temp_context${RESET}".
EOF
read -n 1 -rp "Do you wish to continue? [y/n]: " current_env_input
echo -e "\n"
if [ "$current_env_input" = "y" ]; then
echo "${GREEN}[continuing]${RESET} with context: ${YELLOW}$temp_context${RESET}"
else
echo -e "${RED}[exiting]${RESET}"
exit 12
fi
}
start_smoke_test() {
echo "${GREEN}[starting]${RESET} sonobuoy conformance tests${RESET}"
curl -sL https://raw.githubusercontent.com/cncf/k8s-conformance/master/sonobuoy-conformance.yaml | kubectl apply -f -
}
delete_smoke_test() {
echo "${RED}[deleting]${RESET} sonobuoy conformance tests${RESET}"
curl -sL https://raw.githubusercontent.com/cncf/k8s-conformance/master/sonobuoy-conformance.yaml | kubectl delete -f -
}
wait_for_pod_running() {
echo "${BLUE}[waiting]${RESET} for pod: ${YELLOW}sonobuoy${RESET}"
while :
do
if [ "$(kubectl -n sonobuoy get pod sonobuoy -o json | jq .status.phase -r)" = "Running" ]; then
break
fi
sleep 1
done
echo "${GREEN}[done]${RESET}"
}
wait_for_smoke_test_exit() {
echo "${BLUE}[waiting]${RESET} for tests to exit"
while :
do
if kubectl logs -n sonobuoy sonobuoy | grep -q 'no-exit was specified, sonobuoy is now blocking'
then
break
fi
sleep 1
done
echo "${GREEN}[done]${RESET}"
}
copy_files() {
mkdir -p results
kubectl cp sonobuoy/sonobuoy:/tmp/sonobuoy ./results
tar xfz results/*.tar.gz -C results
}
reset_context() {
kubectl config use-context "$PREVIOUS_CONTEXT"
}
print_results() {
tail -n 3 < plugings/e2e/results/e2e.log
}
cleanup() {
delete_smoke_test
reset_context
}
trap cleanup EXIT INT TERM
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment