minikube/hack/jenkins/common.sh

365 lines
11 KiB
Bash
Raw Normal View History

#!/bin/bash
# Copyright 2016 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script downloads the test files from the build bucket and makes some executable.
# The script expects the following env variables:
# OS_ARCH: The operating system and the architecture separated by a hyphen '-' (e.g. darwin-amd64, linux-amd64, windows-amd64)
2017-01-09 23:17:17 +00:00
# VM_DRIVER: the vm-driver to use for the test
# EXTRA_START_ARGS: additional flags to pass into minikube start
# EXTRA_ARGS: additional flags to pass into minikube
2017-01-09 23:17:17 +00:00
# JOB_NAME: the name of the logfile and check name to update on github
readonly TEST_ROOT="${HOME}/minikube-integration"
readonly TEST_HOME="${TEST_ROOT}/${OS_ARCH}-${VM_DRIVER}-${MINIKUBE_LOCATION}-$$-${COMMIT}"
2019-12-13 08:20:56 +00:00
readonly TEST_OUT="${TEST_HOME}/test.out"
readonly JSON_OUT="${TEST_HOME}/test.json"
readonly HTML_OUT="${TEST_HOME}/test.html"
echo ">> Starting at $(date)"
echo ""
echo "arch: ${OS_ARCH}"
echo "build: ${MINIKUBE_LOCATION}"
echo "driver: ${VM_DRIVER}"
echo "job: ${JOB_NAME}"
echo "test home: ${TEST_HOME}"
echo "sudo: ${SUDO_PREFIX}"
echo "kernel: $(uname -v)"
echo "uptime: $(uptime)"
# Setting KUBECONFIG prevents the version ceck from erroring out due to permission issues
echo "kubectl: $(env KUBECONFIG=${TEST_HOME} kubectl version --client --short=true)"
echo "docker: $(docker version --format '{{ .Client.Version }}')"
2019-12-13 08:20:56 +00:00
# we need golang for test html binary
# this script is copied by jenkins job config
2019-12-13 10:19:09 +00:00
sudo chmod +x ./check_install_golang.sh
sudo ./check_install_golang.sh 1.13.4 /usr/local
2019-12-13 09:03:41 +00:00
case "${VM_DRIVER}" in
kvm2)
echo "virsh: $(virsh --version)"
;;
virtualbox)
echo "vbox: $(vboxmanage --version)"
;;
esac
echo ""
mkdir -p out/ testdata/
# Install gsutil if necessary.
if ! type -P gsutil >/dev/null; then
if [[ ! -x "out/gsutil/gsutil" ]]; then
echo "Installing gsutil to $(pwd)/out ..."
curl -s https://storage.googleapis.com/pub/gsutil.tar.gz | tar -C out/ -zxf -
fi
PATH="$(pwd)/out/gsutil:$PATH"
fi
# Add the out/ directory to the PATH, for using new drivers.
PATH="$(pwd)/out/":$PATH
export PATH
echo ""
echo ">> Downloading test inputs from ${MINIKUBE_LOCATION} ..."
gsutil -qm cp \
"gs://minikube-builds/${MINIKUBE_LOCATION}/minikube-${OS_ARCH}" \
"gs://minikube-builds/${MINIKUBE_LOCATION}/docker-machine-driver"-* \
"gs://minikube-builds/${MINIKUBE_LOCATION}/e2e-${OS_ARCH}" out
gsutil -qm cp -r "gs://minikube-builds/${MINIKUBE_LOCATION}/testdata"/* testdata/
gsutil -qm cp "gs://minikube-builds/${MINIKUBE_LOCATION}/gvisor-addon" testdata/
# Set the executable bit on the e2e binary and out binary
export MINIKUBE_BIN="out/minikube-${OS_ARCH}"
export E2E_BIN="out/e2e-${OS_ARCH}"
chmod +x "${MINIKUBE_BIN}" "${E2E_BIN}" out/docker-machine-driver-*
2019-10-14 19:12:48 +00:00
"${MINIKUBE_BIN}" version
2018-12-05 21:48:31 +00:00
procs=$(pgrep "minikube-${OS_ARCH}|e2e-${OS_ARCH}" || true)
if [[ "${procs}" != "" ]]; then
echo "Warning: found stale test processes to kill:"
ps -f -p ${procs} || true
kill ${procs} || true
kill -9 ${procs} || true
fi
2019-10-09 22:37:12 +00:00
# Quickly notice misconfigured test roots
mkdir -p "${TEST_ROOT}"
# Cleanup stale test outputs.
echo ""
echo ">> Cleaning up after previous test runs ..."
2019-10-09 22:37:12 +00:00
for entry in $(ls ${TEST_ROOT}); do
2019-10-17 18:37:14 +00:00
test_path="${TEST_ROOT}/${entry}"
ls -lad "${test_path}" || continue
echo "* Cleaning stale test path: ${test_path}"
for tunnel in $(find ${test_path} -name tunnels.json -type f); do
env MINIKUBE_HOME="$(dirname ${tunnel})" ${MINIKUBE_BIN} tunnel --cleanup || true
done
2017-08-15 04:02:55 +00:00
2019-10-17 18:37:14 +00:00
for home in $(find ${test_path} -name .minikube -type d); do
env MINIKUBE_HOME="$(dirname ${home})" ${MINIKUBE_BIN} delete --all || true
2019-10-09 22:37:12 +00:00
sudo rm -Rf "${home}"
done
2019-10-17 18:37:14 +00:00
for kconfig in $(find ${test_path} -name kubeconfig -type f); do
sudo rm -f "${kconfig}"
done
2019-10-09 22:37:12 +00:00
# Be very specific to avoid accidentally deleting other items, like wildcards or devices
2019-10-17 18:37:14 +00:00
if [[ -d "${test_path}" ]]; then
rm -Rf "${test_path}" || true
elif [[ -f "${test_path}" ]]; then
rm -f "${test_path}" || true
2019-10-09 22:37:12 +00:00
fi
done
# sometimes tests left over zombie procs that won't exit
# for example:
# jenkins 20041 0.0 0.0 0 0 ? Z Aug19 0:00 [minikube-linux-] <defunct>
2019-08-20 01:36:19 +00:00
zombie_defuncts=$(ps -A -ostat,ppid | awk '/[zZ]/ && !a[$2]++ {print $2}')
if [[ "${zombie_defuncts}" != "" ]]; then
echo "Found zombie defunct procs to kill..."
2019-08-20 00:43:12 +00:00
ps -f -p ${zombie_defuncts} || true
kill ${zombie_defuncts} || true
fi
if type -P virsh; then
2019-09-17 04:02:31 +00:00
virsh -c qemu:///system list --all --uuid \
2018-11-30 05:44:20 +00:00
| xargs -I {} sh -c "virsh -c qemu:///system destroy {}; virsh -c qemu:///system undefine {}" \
|| true
echo ">> virsh VM list after clean up (should be empty):"
2019-07-24 20:32:32 +00:00
virsh -c qemu:///system list --all || true
fi
if type -P vboxmanage; then
killall VBoxHeadless || true
sleep 1
killall -9 VBoxHeadless || true
for guid in $(vboxmanage list vms | grep -Eo '\{[a-zA-Z0-9-]+\}'); do
echo "- Removing stale VirtualBox VM: $guid"
vboxmanage startvm "${guid}" --type emergencystop || true
vboxmanage unregistervm "${guid}" || true
done
2019-10-09 22:55:30 +00:00
ifaces=$(vboxmanage list hostonlyifs | grep -E "^Name:" | awk '{ print $2 }')
for if in $ifaces; do
vboxmanage hostonlyif remove "${if}" || true
done
2019-10-09 22:55:30 +00:00
echo ">> VirtualBox VM list after clean up (should be empty):"
2019-07-24 20:32:32 +00:00
vboxmanage list vms || true
echo ">> VirtualBox interface list after clean up (should be empty):"
vboxmanage list hostonlyifs || true
fi
2019-08-20 02:18:52 +00:00
if type -P hdiutil; then
2018-11-30 05:44:20 +00:00
hdiutil info | grep -E "/dev/disk[1-9][^s]" || true
hdiutil info \
| grep -E "/dev/disk[1-9][^s]" \
| awk '{print $1}' \
| xargs -I {} sh -c "hdiutil detach {}" \
|| true
fi
2019-07-27 22:16:23 +00:00
# cleaning up stale hyperkits
if type -P hyperkit; then
for pid in $(pgrep hyperkit); do
echo "Killing stale hyperkit $pid"
ps -f -p $pid || true
kill $pid || true
kill -9 $pid || true
done
2019-07-27 22:16:23 +00:00
fi
if [[ "${VM_DRIVER}" == "hyperkit" ]]; then
if [[ -e out/docker-machine-driver-hyperkit ]]; then
sudo chown root:wheel out/docker-machine-driver-hyperkit || true
sudo chmod u+s out/docker-machine-driver-hyperkit || true
fi
fi
2017-08-15 04:02:55 +00:00
2018-12-05 21:48:31 +00:00
kprocs=$(pgrep kubectl || true)
if [[ "${kprocs}" != "" ]]; then
echo "error: killing hung kubectl processes ..."
2018-12-05 21:48:31 +00:00
ps -f -p ${kprocs} || true
sudo -E kill ${kprocs} || true
2017-08-15 04:02:55 +00:00
fi
2017-01-09 23:17:17 +00:00
# clean up none drivers binding on 8443
2019-07-27 22:16:23 +00:00
none_procs=$(sudo lsof -i :8443 | tail -n +2 | awk '{print $2}' || true)
if [[ "${none_procs}" != "" ]]; then
echo "Found stale api servers listening on 8443 processes to kill: "
for p in $none_procs
do
echo "Kiling stale none driver: $p"
2019-07-27 22:46:10 +00:00
sudo -E ps -f -p $p || true
sudo -E kill $p || true
sudo -E kill -9 $p || true
2019-07-27 22:16:23 +00:00
done
fi
function cleanup_stale_routes() {
local show="netstat -rn -f inet"
local del="sudo route -n delete"
if [[ "$(uname)" == "Linux" ]]; then
show="ip route show"
del="sudo ip route delete"
fi
local troutes=$($show | awk '{ print $1 }' | grep 10.96.0.0 || true)
for route in ${troutes}; do
echo "WARNING: deleting stale tunnel route: ${route}"
$del "${route}" || true
done
}
cleanup_stale_routes || true
mkdir -p "${TEST_HOME}"
export MINIKUBE_HOME="${TEST_HOME}/.minikube"
export KUBECONFIG="${TEST_HOME}/kubeconfig"
2019-10-25 22:08:46 +00:00
# Build the gvisor image so that we can integration test changes to pkg/gvisor
chmod +x ./testdata/gvisor-addon
# skipping gvisor mac because ofg https://github.com/kubernetes/minikube/issues/5137
if [ "$(uname)" != "Darwin" ]; then
2019-10-25 22:08:46 +00:00
# Should match GVISOR_IMAGE_VERSION in Makefile
docker build -t gcr.io/k8s-minikube/gvisor-addon:2 -f testdata/gvisor-addon-Dockerfile ./testdata
fi
2019-10-30 13:37:48 +00:00
readonly LOAD=$(uptime | egrep -o "load average.*: [0-9]+" | cut -d" " -f3)
if [[ "${LOAD}" -gt 2 ]]; then
echo ""
echo "********************** LOAD WARNING ********************************"
echo "Load average is very high (${LOAD}), which may cause failures. Top:"
if [[ "$(uname)" == "Darwin" ]]; then
# Two samples, macOS does not calculate CPU usage on the first one
top -l 2 -o cpu -n 5 | tail -n 15
else
top -b -n1 | head -n 15
fi
echo "********************** LOAD WARNING ********************************"
echo "Sleeping 30s to see if load goes down ...."
sleep 30
uptime
fi
e2e_start_time="$(date -u +%s)"
echo ""
echo ">> Starting ${E2E_BIN} at $(date)"
set -x
${SUDO_PREFIX}${E2E_BIN} \
-minikube-start-args="--vm-driver=${VM_DRIVER} ${EXTRA_START_ARGS}" \
-expected-default-driver="${EXPECTED_DEFAULT_DRIVER}" \
2019-10-30 05:12:23 +00:00
-test.timeout=70m \
${EXTRA_TEST_ARGS} \
2019-12-13 08:20:56 +00:00
-binary="${MINIKUBE_BIN}" | tee "${TEST_OUT}" && result=$? || result=$?
set +x
echo ">> ${E2E_BIN} exited with ${result} at $(date)"
echo ""
2017-01-09 23:17:17 +00:00
if [[ $result -eq 0 ]]; then
status="success"
echo "minikube: SUCCESS"
2017-01-09 23:17:17 +00:00
else
status="failure"
echo "minikube: FAIL"
2017-01-09 23:17:17 +00:00
fi
2019-12-10 23:43:43 +00:00
## caclucate the time took to finish running e2e binary test.
2019-12-10 22:51:19 +00:00
e2e_end_time="$(date -u +%s)"
elapsed=$(($e2e_end_time-$e2e_start_time))
2019-12-10 23:43:43 +00:00
min=$(($elapsed/60))
sec=$(tail -c 3 <<< $((${elapsed}00/60)))
elapsed=$min.$sec
2019-12-10 22:51:19 +00:00
description="completed with ${status} in ${elapsed} minute(s)."
echo $description
2019-12-13 08:20:56 +00:00
# Generate well-formed test output
2019-12-13 08:38:57 +00:00
go tool test2json < "${TEST_OUT}" > "${JSON_OUT}"
2019-12-13 08:20:56 +00:00
go get -u github.com/medyagh/goprettyorgohome
goprettyorgohome -in "${JSON_OUT}" -out "${HTML_OUT}"
gsutil -qm cp "${JSON_OUT}" "gs://minikube-builds/${MINIKUBE_LOCATION}/${JOB_NAME}.json"
gsutil -qm cp "${HTML_OUT}" "gs://minikube-builds/${MINIKUBE_LOCATION}/${JOB_NAME}.html"
echo ">> Cleaning up after ourselves ..."
${SUDO_PREFIX}${MINIKUBE_BIN} tunnel --cleanup || true
${SUDO_PREFIX}${MINIKUBE_BIN} delete >/dev/null 2>/dev/null || true
cleanup_stale_routes || true
2018-11-30 05:44:20 +00:00
${SUDO_PREFIX} rm -Rf "${MINIKUBE_HOME}" || true
${SUDO_PREFIX} rm -f "${KUBECONFIG}" || true
rmdir "${TEST_HOME}"
echo ">> ${TEST_HOME} completed at $(date)"
2019-11-12 00:58:33 +00:00
if [[ "${MINIKUBE_LOCATION}" == "master" ]]; then
exit $result
fi
2019-11-12 00:58:33 +00:00
# retry_github_status provides reliable github status updates
function retry_github_status() {
local commit=$1
2019-11-12 00:58:33 +00:00
local context=$2
2019-11-12 01:58:11 +00:00
local state=$3
local token=$4
local target=$5
local desc=$6
2019-11-12 00:58:33 +00:00
# Retry in case we hit our GitHub API quota or fail other ways.
local attempt=0
local timeout=2
local code=-1
while [[ "${attempt}" -lt 8 ]]; do
local out=$(mktemp)
code=$(curl -o "${out}" -s --write-out "%{http_code}" -L \
"https://api.github.com/repos/kubernetes/minikube/statuses/${commit}?access_token=${token}" \
2019-11-12 00:58:33 +00:00
-H "Content-Type: application/json" \
-X POST \
-d "{\"state\": \"${state}\", \"description\": \"Jenkins: ${desc}\", \"target_url\": \"${target}\", \"context\": \"${context}\"}" || echo 999)
2019-11-12 00:58:33 +00:00
# 2xx HTTP codes
if [[ "${code}" =~ ^2 ]]; then
break
fi
cat "${out}" && rm -f "${out}"
2019-11-12 00:58:33 +00:00
echo "HTTP code ${code}! Retrying in ${timeout} .."
sleep "${timeout}"
attempt=$(( attempt + 1 ))
timeout=$(( timeout * 2 ))
done
}
retry_github_status "${COMMIT}" "${JOB_NAME}" "${status}" "${access_token}" "https://storage.googleapis.com/minikube-builds/logs/${MINIKUBE_LOCATION}/${JOB_NAME}.txt" "${description}"
2018-11-29 23:51:41 +00:00
exit $result