common.sh 14.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/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.

X
Xinbo Weng 已提交
20
# The script expects the following env variables:
21
# OS_ARCH: The operating system and the architecture separated by a hyphen '-' (e.g. darwin-amd64, linux-amd64, windows-amd64)
S
Sharif Elgamal 已提交
22
# VM_DRIVER: the driver to use for the test
23 24
# EXTRA_START_ARGS: additional flags to pass into minikube start
# EXTRA_ARGS: additional flags to pass into minikube
M
Matt Rickard 已提交
25
# JOB_NAME: the name of the logfile and check name to update on github
26

27 28
readonly TEST_ROOT="${HOME}/minikube-integration"
readonly TEST_HOME="${TEST_ROOT}/${OS_ARCH}-${VM_DRIVER}-${MINIKUBE_LOCATION}-$$-${COMMIT}"
M
Medya Gh 已提交
29
export GOPATH="$HOME/go"
M
Medya Ghazizadeh 已提交
30
export KUBECONFIG="${TEST_HOME}/kubeconfig"
M
Medya Gh 已提交
31
export PATH=$PATH:"/usr/local/bin/:/usr/local/go/bin/:$GOPATH/bin"
M
Medya Gh 已提交
32

33 34 35 36
if [ "$(uname)" != "Darwin" ]; then
  # install lsof for finding none driver procs, psmisc to use pstree in cronjobs
  sudo apt-get -y install lsof psmisc
fi
S
Sharif Elgamal 已提交
37

M
Medya Ghazizadeh 已提交
38
# installing golang so we could do go get for gopogh
P
Predrag Rogic 已提交
39
sudo ./installers/check_install_golang.sh "1.15.2" "/usr/local" || true
M
Medya Ghazizadeh 已提交
40

41 42 43
# install docker and kubectl if not present
sudo ./installers/check_install_docker.sh

44
docker rm -f -v $(docker ps -aq) >/dev/null 2>&1 || true
45 46
docker volume prune -f || true
docker system df || true
M
Medya Ghazizadeh 已提交
47

48 49 50 51 52 53 54 55 56
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)"
57
echo "uptime:    $(uptime)"
M
Ma Xinjian 已提交
58
# Setting KUBECONFIG prevents the version check from erroring out due to permission issues
59 60
echo "kubectl:   $(env KUBECONFIG=${TEST_HOME} kubectl version --client --short=true)"
echo "docker:    $(docker version --format '{{ .Client.Version }}')"
M
Medya Ghazizadeh 已提交
61
echo "podman:    $(sudo podman version --format '{{.Version}}' || true)"
M
Medya Gh 已提交
62
echo "go:        $(go version || true)"
M
Medya Gh 已提交
63

64 65 66 67 68 69 70 71 72 73 74

case "${VM_DRIVER}" in
  kvm2)
    echo "virsh:     $(virsh --version)"
  ;;
  virtualbox)
    echo "vbox:      $(vboxmanage --version)"
  ;;
esac

echo ""
75
mkdir -p out/ testdata/
76 77

# Install gsutil if necessary.
78
if ! type -P gsutil >/dev/null; then
79 80 81 82
  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
83
  PATH="$(pwd)/out/gsutil:$PATH"
84 85 86
fi

# Add the out/ directory to the PATH, for using new drivers.
87 88
PATH="$(pwd)/out/":$PATH
export PATH
89

90 91 92 93 94
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"-* \
95
  "gs://minikube-builds/${MINIKUBE_LOCATION}/e2e-${OS_ARCH}" out
D
dlorenc 已提交
96

97
gsutil -qm cp -r "gs://minikube-builds/${MINIKUBE_LOCATION}/testdata"/* testdata/
D
dlorenc 已提交
98

99 100
gsutil -qm cp "gs://minikube-builds/${MINIKUBE_LOCATION}/gvisor-addon" testdata/

D
dlorenc 已提交
101

102 103 104 105
# 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-*
T
tstromberg 已提交
106
"${MINIKUBE_BIN}" version
107

T
Thomas Stromberg 已提交
108
procs=$(pgrep "minikube-${OS_ARCH}|e2e-${OS_ARCH}" || true)
109
if [[ "${procs}" != "" ]]; then
M
Medya Gh 已提交
110
  echo "Warning: found stale test processes to kill:"
111 112 113
  ps -f -p ${procs} || true
  kill ${procs} || true
  kill -9 ${procs} || true
114
fi
115

T
tstromberg 已提交
116 117 118
# Quickly notice misconfigured test roots
mkdir -p "${TEST_ROOT}"

119
# Cleanup stale test outputs.
120 121
echo ""
echo ">> Cleaning up after previous test runs ..."
T
tstromberg 已提交
122
for entry in $(ls ${TEST_ROOT}); do
123 124 125 126 127
  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
128 129
    env MINIKUBE_HOME="$(dirname ${tunnel})" ${MINIKUBE_BIN} tunnel --cleanup || true
  done
D
dlorenc 已提交
130

131
  for home in $(find ${test_path} -name .minikube -type d); do
132
    env MINIKUBE_HOME="$(dirname ${home})" ${MINIKUBE_BIN} delete --all || true
T
tstromberg 已提交
133
    sudo rm -Rf "${home}"
134
  done
135

136
  for kconfig in $(find ${test_path} -name kubeconfig -type f); do
137 138 139
    sudo rm -f "${kconfig}"
  done

T
tstromberg 已提交
140
  # Be very specific to avoid accidentally deleting other items, like wildcards or devices
141 142 143 144
  if [[ -d "${test_path}" ]]; then
    rm -Rf "${test_path}" || true
  elif [[ -f "${test_path}" ]]; then
    rm -f "${test_path}" || true
T
tstromberg 已提交
145
  fi
146
done
147 148 149 150

# 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>
M
Medya Gh 已提交
151
zombie_defuncts=$(ps -A -ostat,ppid | awk '/[zZ]/ && !a[$2]++ {print $2}')
152 153
if [[ "${zombie_defuncts}" != "" ]]; then
  echo "Found zombie defunct procs to kill..."
M
Medya Gh 已提交
154
  ps -f -p ${zombie_defuncts} || true
155
  kill ${zombie_defuncts} || true
156 157
fi

158
if type -P virsh; then
T
Thomas Stromberg 已提交
159
  virsh -c qemu:///system list --all --uuid \
T
Thomas Stromberg 已提交
160 161
    | xargs -I {} sh -c "virsh -c qemu:///system destroy {}; virsh -c qemu:///system undefine {}" \
    || true
162
  echo ">> virsh VM list after clean up (should be empty):"
M
Medya Gh 已提交
163
  virsh -c qemu:///system list --all || true
164
fi
165

166
if type -P vboxmanage; then
167 168 169 170
  killall VBoxHeadless || true
  sleep 1
  killall -9 VBoxHeadless || true

171
  for guid in $(vboxmanage list vms | grep -Eo '\{[a-zA-Z0-9-]+\}'); do
172
    echo "- Removing stale VirtualBox VM: $guid"
173 174
    vboxmanage startvm "${guid}" --type emergencystop || true
    vboxmanage unregistervm "${guid}" || true
175
  done
T
tstromberg 已提交
176

177
  ifaces=$(vboxmanage list hostonlyifs | grep -E "^Name:" | awk '{ print $2 }')
178 179 180
  for if in $ifaces; do
    vboxmanage hostonlyif remove "${if}" || true
  done
T
tstromberg 已提交
181

182
  echo ">> VirtualBox VM list after clean up (should be empty):"
M
Medya Gh 已提交
183
  vboxmanage list vms || true
184 185
  echo ">> VirtualBox interface list after clean up (should be empty):"
  vboxmanage list hostonlyifs || true
186 187
fi

M
Medya Gh 已提交
188

189
if type -P hdiutil; then
T
Thomas Stromberg 已提交
190
  hdiutil info | grep -E "/dev/disk[1-9][^s]" || true
191 192
  hdiutil info \
      | grep -E "/dev/disk[1-9][^s]" \
193
      | awk '{print $1}' \
194 195
      | xargs -I {} sh -c "hdiutil detach {}" \
      || true
196
fi
197

M
Medya Gh 已提交
198 199
# cleaning up stale hyperkits
if type -P hyperkit; then
200 201 202 203 204 205
  for pid in $(pgrep hyperkit); do
    echo "Killing stale hyperkit $pid"
    ps -f -p $pid || true
    kill $pid || true
    kill -9 $pid || true
  done
M
Medya Gh 已提交
206 207
fi

208
if [[ "${VM_DRIVER}" == "hyperkit" ]]; then
209 210
  if [[ -e out/docker-machine-driver-hyperkit ]]; then
    sudo chown root:wheel out/docker-machine-driver-hyperkit || true
211
    sudo chmod u+s out/docker-machine-driver-hyperkit || true
212 213
  fi
fi
D
dlorenc 已提交
214

T
Thomas Stromberg 已提交
215
kprocs=$(pgrep kubectl || true)
216 217
if [[ "${kprocs}" != "" ]]; then
  echo "error: killing hung kubectl processes ..."
T
Thomas Stromberg 已提交
218
  ps -f -p ${kprocs} || true
219
  sudo -E kill ${kprocs} || true
D
dlorenc 已提交
220
fi
M
Matt Rickard 已提交
221

S
Sharif Elgamal 已提交
222

223
# clean up none drivers binding on 8443
M
Medya Gh 已提交
224 225 226 227 228 229
  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"
M
Medya Gh 已提交
230 231 232
    sudo -E ps -f -p $p || true
    sudo -E kill $p || true
    sudo -E kill -9 $p || true
M
Medya Gh 已提交
233 234
    done
  fi
235

236
function cleanup_stale_routes() {
237 238 239 240 241 242
  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"
243
  fi
244 245 246 247 248 249

  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
250 251 252 253
}

cleanup_stale_routes || true

254
mkdir -p "${TEST_HOME}"
255
export MINIKUBE_HOME="${TEST_HOME}/.minikube"
256

T
tstromberg 已提交
257 258

# Build the gvisor image so that we can integration test changes to pkg/gvisor
259 260 261
chmod +x ./testdata/gvisor-addon
# skipping gvisor mac because ofg https://github.com/kubernetes/minikube/issues/5137
if [ "$(uname)" != "Darwin" ]; then
T
tstromberg 已提交
262 263
  # Should match GVISOR_IMAGE_VERSION in Makefile
  docker build -t gcr.io/k8s-minikube/gvisor-addon:2 -f testdata/gvisor-addon-Dockerfile ./testdata
264
fi
265

T
Merge  
Thomas Stromberg 已提交
266
readonly LOAD=$(uptime | egrep -o "load average.*: [0-9]+" | cut -d" " -f3)
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
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

M
Medya Gh 已提交
283 284 285 286
readonly TEST_OUT="${TEST_HOME}/testout.txt"
readonly JSON_OUT="${TEST_HOME}/test.json"
readonly HTML_OUT="${TEST_HOME}/test.html"

287
e2e_start_time="$(date -u +%s)"
288 289
echo ""
echo ">> Starting ${E2E_BIN} at $(date)"
290
set -x
M
Medya Gh 已提交
291 292 293 294

if test -f "${TEST_OUT}"; then
  rm "${TEST_OUT}" || true # clean up previous runs of same build
fi
M
Medya Gh 已提交
295
touch "${TEST_OUT}"
296
${SUDO_PREFIX}${E2E_BIN} \
S
Sharif Elgamal 已提交
297
  -minikube-start-args="--driver=${VM_DRIVER} ${EXTRA_START_ARGS}" \
M
Medya Gh 已提交
298
  -test.timeout=70m -test.v \
299
  ${EXTRA_TEST_ARGS} \
M
Medya Gh 已提交
300
  -binary="${MINIKUBE_BIN}" 2>&1 | tee "${TEST_OUT}"
M
Medya Gh 已提交
301 302

result=${PIPESTATUS[0]} # capture the exit code of the first cmd in pipe.
303
set +x
304 305
echo ">> ${E2E_BIN} exited with ${result} at $(date)"
echo ""
306

M
Matt Rickard 已提交
307 308
if [[ $result -eq 0 ]]; then
  status="success"
309
  echo "minikube: SUCCESS"
M
Matt Rickard 已提交
310 311
else
  status="failure"
312
  echo "minikube: FAIL"
M
Matt Rickard 已提交
313 314
fi

M
Medya Gh 已提交
315
## caclucate the time took to finish running e2e binary test.
M
Medya Gh 已提交
316 317
e2e_end_time="$(date -u +%s)"
elapsed=$(($e2e_end_time-$e2e_start_time))
M
Medya Gh 已提交
318 319 320
min=$(($elapsed/60))
sec=$(tail -c 3 <<< $((${elapsed}00/60)))
elapsed=$min.$sec
M
Medya Gh 已提交
321

322 323
SHORT_COMMIT=${COMMIT:0:7}
JOB_GCS_BUCKET="minikube-builds/logs/${MINIKUBE_LOCATION}/${SHORT_COMMIT}/${JOB_NAME}"
M
Medya Gh 已提交
324 325
echo ">> Copying ${TEST_OUT} to gs://${JOB_GCS_BUCKET}out.txt"
gsutil -qm cp "${TEST_OUT}" "gs://${JOB_GCS_BUCKET}out.txt"
M
Medya Gh 已提交
326 327 328


echo ">> Attmpting to convert test logs to json"
M
Medya Gh 已提交
329 330 331 332
if test -f "${JSON_OUT}"; then
  rm "${JSON_OUT}" || true # clean up previous runs of same build
fi

M
Medya Gh 已提交
333 334
touch "${JSON_OUT}"

M
Medya Gh 已提交
335
# Generate JSON output
M
Medya Gh 已提交
336
echo ">> Running go test2json"
M
Medya Gh 已提交
337
go tool test2json -t < "${TEST_OUT}" > "${JSON_OUT}" || true
M
Medya Gh 已提交
338

M
typo  
Medya Gh 已提交
339
if ! type "jq" > /dev/null; then
M
Medya Gh 已提交
340 341 342 343 344 345 346 347
echo ">> Installing jq"
    if [ "$(uname)" != "Darwin" ]; then
      curl -LO https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 && sudo install jq-linux64 /usr/local/bin/jq
    else
      curl -LO https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64 && sudo install jq-osx-amd64 /usr/local/bin/jq
    fi
fi

M
Medya Gh 已提交
348
echo ">> Installing gopogh"
349
if [ "$(uname)" != "Darwin" ]; then
M
Medya Gh 已提交
350
  curl -LO https://github.com/medyagh/gopogh/releases/download/v0.2.4/gopogh-linux-amd64 && sudo install gopogh-linux-amd64 /usr/local/bin/gopogh
351
else
M
Medya Gh 已提交
352
  curl -LO https://github.com/medyagh/gopogh/releases/download/v0.2.4/gopogh-darwin-amd64 && sudo install gopogh-darwin-amd64 /usr/local/bin/gopogh
353 354
fi

M
Medya Gh 已提交
355
echo ">> Running gopogh"
M
Medya Gh 已提交
356 357 358 359
if test -f "${HTML_OUT}"; then
    rm "${HTML_OUT}" || true # clean up previous runs of same build
fi

M
Medya Gh 已提交
360
touch "${HTML_OUT}"
M
Medya Gh 已提交
361 362 363
gopogh_status=$(gopogh -in "${JSON_OUT}" -out "${HTML_OUT}" -name "${JOB_NAME}" -pr "${MINIKUBE_LOCATION}" -repo github.com/kubernetes/minikube/  -details "${COMMIT}") || true
fail_num=$(echo $gopogh_status | jq '.NumberOfFail')
test_num=$(echo $gopogh_status | jq '.NumberOfTests')       
364
pessimistic_status="${fail_num} / ${test_num} failures"
M
Medya Gh 已提交
365
description="completed with ${status} in ${elapsed} minute(s)."
M
typo  
Medya Ghazizadeh 已提交
366
if [ "$status" = "failure" ]; then
M
Medya Gh 已提交
367 368 369 370
  description="completed with ${pessimistic_status} in ${elapsed} minute(s)."
fi
echo $description

M
Medya Gh 已提交
371
echo ">> uploading ${JSON_OUT}"
M
Medya Gh 已提交
372
gsutil -qm cp "${JSON_OUT}" "gs://${JOB_GCS_BUCKET}.json" || true
M
Medya Gh 已提交
373
echo ">> uploading ${HTML_OUT}"
M
Medya Gh 已提交
374
gsutil -qm cp "${HTML_OUT}" "gs://${JOB_GCS_BUCKET}.html" || true
M
Medya Gh 已提交
375

376

M
Medya Gh 已提交
377 378 379 380
public_log_url="https://storage.googleapis.com/${JOB_GCS_BUCKET}.txt"
if grep -q html "$HTML_OUT"; then
  public_log_url="https://storage.googleapis.com/${JOB_GCS_BUCKET}.html"
fi
M
Medya Gh 已提交
381

382
echo ">> Cleaning up after ourselves ..."
383
${SUDO_PREFIX}${MINIKUBE_BIN} tunnel --cleanup || true
M
Medya Ghazizadeh 已提交
384
${SUDO_PREFIX}${MINIKUBE_BIN} delete --all --purge >/dev/null 2>/dev/null || true
385
cleanup_stale_routes || true
T
Thomas Stromberg 已提交
386 387 388

${SUDO_PREFIX} rm -Rf "${MINIKUBE_HOME}" || true
${SUDO_PREFIX} rm -f "${KUBECONFIG}" || true
M
Medya Gh 已提交
389 390 391 392
${SUDO_PREFIX} rm -f "${TEST_OUT}" || true
${SUDO_PREFIX} rm -f "${JSON_OUT}" || true
${SUDO_PREFIX} rm -f "${HTML_OUT}" || true
rmdir "${TEST_HOME}" || true
393 394
echo ">> ${TEST_HOME} completed at $(date)"

T
Thomas Stromberg 已提交
395 396
if [[ "${MINIKUBE_LOCATION}" == "master" ]]; then
  exit $result
397
fi
T
Thomas Stromberg 已提交
398 399 400

# retry_github_status provides reliable github status updates
function retry_github_status() {
401
  local commit=$1
T
Thomas Stromberg 已提交
402
  local context=$2
T
Thomas Stromberg 已提交
403 404 405
  local state=$3
  local token=$4
  local target=$5
406
  local desc=$6
T
Thomas Stromberg 已提交
407 408 409 410 411 412

   # Retry in case we hit our GitHub API quota or fail other ways.
  local attempt=0
  local timeout=2
  local code=-1

413
  while [[ "${attempt}" -lt 8 ]]; do
414 415
    local out=$(mktemp)
    code=$(curl -o "${out}" -s --write-out "%{http_code}" -L \
416
      "https://api.github.com/repos/kubernetes/minikube/statuses/${commit}?access_token=${token}" \
T
Thomas Stromberg 已提交
417 418
      -H "Content-Type: application/json" \
      -X POST \
419
      -d "{\"state\": \"${state}\", \"description\": \"Jenkins: ${desc}\", \"target_url\": \"${target}\", \"context\": \"${context}\"}" || echo 999)
T
Thomas Stromberg 已提交
420 421 422 423 424 425

    # 2xx HTTP codes
    if [[ "${code}" =~ ^2 ]]; then
      break
    fi

426
    cat "${out}" && rm -f "${out}"
T
Thomas Stromberg 已提交
427 428 429
    echo "HTTP code ${code}! Retrying in ${timeout} .."
    sleep "${timeout}"
    attempt=$(( attempt + 1 ))
430
    timeout=$(( timeout * 5 )) 
T
Thomas Stromberg 已提交
431 432 433
  done
}

434

M
Medya Gh 已提交
435
retry_github_status "${COMMIT}" "${JOB_NAME}" "${status}" "${access_token}" "${public_log_url}" "${description}"
T
Thomas Stromberg 已提交
436
exit $result