common.sh 10.3 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)
M
Matt Rickard 已提交
22
# VM_DRIVER: the vm-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 29 30 31 32 33 34 35 36 37
readonly TEST_ROOT="${HOME}/minikube-integration"
readonly TEST_HOME="${TEST_ROOT}/${OS_ARCH}-${VM_DRIVER}-${MINIKUBE_LOCATION}-$$-${COMMIT}"
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)"
38
echo "uptime:    $(uptime)"
39 40 41 42 43 44 45 46 47 48 49 50 51 52
# 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 }}')"

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

echo ""
53
mkdir -p out/ testdata/
54 55

# Install gsutil if necessary.
56
if ! type -P gsutil >/dev/null; then
57 58 59 60
  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
61
  PATH="$(pwd)/out/gsutil:$PATH"
62 63 64
fi

# Add the out/ directory to the PATH, for using new drivers.
65 66
PATH="$(pwd)/out/":$PATH
export PATH
67

68 69 70 71 72
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"-* \
73
  "gs://minikube-builds/${MINIKUBE_LOCATION}/e2e-${OS_ARCH}" out
D
dlorenc 已提交
74

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

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

D
dlorenc 已提交
79

80 81 82 83
# 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 已提交
84
"${MINIKUBE_BIN}" version
85

T
Thomas Stromberg 已提交
86
procs=$(pgrep "minikube-${OS_ARCH}|e2e-${OS_ARCH}" || true)
87
if [[ "${procs}" != "" ]]; then
M
Medya Gh 已提交
88
  echo "Warning: found stale test processes to kill:"
89 90 91
  ps -f -p ${procs} || true
  kill ${procs} || true
  kill -9 ${procs} || true
92
fi
93

T
tstromberg 已提交
94 95 96
# Quickly notice misconfigured test roots
mkdir -p "${TEST_ROOT}"

97
# Cleanup stale test outputs.
98 99
echo ""
echo ">> Cleaning up after previous test runs ..."
T
tstromberg 已提交
100
for entry in $(ls ${TEST_ROOT}); do
101 102 103 104 105
  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
106 107
    env MINIKUBE_HOME="$(dirname ${tunnel})" ${MINIKUBE_BIN} tunnel --cleanup || true
  done
D
dlorenc 已提交
108

109
  for home in $(find ${test_path} -name .minikube -type d); do
110
    env MINIKUBE_HOME="$(dirname ${home})" ${MINIKUBE_BIN} delete --all || true
T
tstromberg 已提交
111
    sudo rm -Rf "${home}"
112
  done
113

114
  for kconfig in $(find ${test_path} -name kubeconfig -type f); do
115 116 117
    sudo rm -f "${kconfig}"
  done

T
tstromberg 已提交
118
  # Be very specific to avoid accidentally deleting other items, like wildcards or devices
119 120 121 122
  if [[ -d "${test_path}" ]]; then
    rm -Rf "${test_path}" || true
  elif [[ -f "${test_path}" ]]; then
    rm -f "${test_path}" || true
T
tstromberg 已提交
123
  fi
124
done
125 126 127 128

# 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 已提交
129
zombie_defuncts=$(ps -A -ostat,ppid | awk '/[zZ]/ && !a[$2]++ {print $2}')
130 131
if [[ "${zombie_defuncts}" != "" ]]; then
  echo "Found zombie defunct procs to kill..."
M
Medya Gh 已提交
132
  ps -f -p ${zombie_defuncts} || true
133
  kill ${zombie_defuncts} || true
134 135
fi

136
if type -P virsh; then
T
Thomas Stromberg 已提交
137
  virsh -c qemu:///system list --all --uuid \
T
Thomas Stromberg 已提交
138 139
    | xargs -I {} sh -c "virsh -c qemu:///system destroy {}; virsh -c qemu:///system undefine {}" \
    || true
140
  echo ">> virsh VM list after clean up (should be empty):"
M
Medya Gh 已提交
141
  virsh -c qemu:///system list --all || true
142
fi
143

144
if type -P vboxmanage; then
145 146 147 148
  killall VBoxHeadless || true
  sleep 1
  killall -9 VBoxHeadless || true

149
  for guid in $(vboxmanage list vms | grep -Eo '\{[a-zA-Z0-9-]+\}'); do
150
    echo "- Removing stale VirtualBox VM: $guid"
151 152
    vboxmanage startvm "${guid}" --type emergencystop || true
    vboxmanage unregistervm "${guid}" || true
153
  done
T
tstromberg 已提交
154

155
  ifaces=$(vboxmanage list hostonlyifs | grep -E "^Name:" | awk '{ print $2 }')
156 157 158
  for if in $ifaces; do
    vboxmanage hostonlyif remove "${if}" || true
  done
T
tstromberg 已提交
159

160
  echo ">> VirtualBox VM list after clean up (should be empty):"
M
Medya Gh 已提交
161
  vboxmanage list vms || true
162 163
  echo ">> VirtualBox interface list after clean up (should be empty):"
  vboxmanage list hostonlyifs || true
164 165
fi

M
Medya Gh 已提交
166

167
if type -P hdiutil; then
T
Thomas Stromberg 已提交
168
  hdiutil info | grep -E "/dev/disk[1-9][^s]" || true
169 170
  hdiutil info \
      | grep -E "/dev/disk[1-9][^s]" \
171
      | awk '{print $1}' \
172 173
      | xargs -I {} sh -c "hdiutil detach {}" \
      || true
174
fi
175

M
Medya Gh 已提交
176 177
# cleaning up stale hyperkits
if type -P hyperkit; then
178 179 180 181 182 183
  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 已提交
184 185
fi

186
if [[ "${VM_DRIVER}" == "hyperkit" ]]; then
187 188
  if [[ -e out/docker-machine-driver-hyperkit ]]; then
    sudo chown root:wheel out/docker-machine-driver-hyperkit || true
189
    sudo chmod u+s out/docker-machine-driver-hyperkit || true
190 191
  fi
fi
D
dlorenc 已提交
192

T
Thomas Stromberg 已提交
193
kprocs=$(pgrep kubectl || true)
194 195
if [[ "${kprocs}" != "" ]]; then
  echo "error: killing hung kubectl processes ..."
T
Thomas Stromberg 已提交
196
  ps -f -p ${kprocs} || true
197
  sudo -E kill ${kprocs} || true
D
dlorenc 已提交
198
fi
M
Matt Rickard 已提交
199

200
# clean up none drivers binding on 8443
M
Medya Gh 已提交
201 202 203 204 205 206
  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 已提交
207 208 209
    sudo -E ps -f -p $p || true
    sudo -E kill $p || true
    sudo -E kill -9 $p || true
M
Medya Gh 已提交
210 211
    done
  fi
212

213
function cleanup_stale_routes() {
214 215 216 217 218 219
  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"
220
  fi
221 222 223 224 225 226

  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
227 228 229 230
}

cleanup_stale_routes || true

231
mkdir -p "${TEST_HOME}"
232 233
export MINIKUBE_HOME="${TEST_HOME}/.minikube"
export KUBECONFIG="${TEST_HOME}/kubeconfig"
234

T
tstromberg 已提交
235 236

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

T
Merge  
Thomas Stromberg 已提交
244
readonly LOAD=$(uptime | egrep -o "load average.*: [0-9]+" | cut -d" " -f3)
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
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

261 262
echo ""
echo ">> Starting ${E2E_BIN} at $(date)"
263
set -x
264
${SUDO_PREFIX}${E2E_BIN} \
265
  -minikube-start-args="--vm-driver=${VM_DRIVER} ${EXTRA_START_ARGS}" \
266
  -expected-default-driver="${EXPECTED_DEFAULT_DRIVER}" \
T
Thomas Stromberg 已提交
267
  -test.timeout=70m \
268
  ${EXTRA_TEST_ARGS} \
269
  -binary="${MINIKUBE_BIN}" && result=$? || result=$?
270
set +x
271 272
echo ">> ${E2E_BIN} exited with ${result} at $(date)"
echo ""
273

M
Matt Rickard 已提交
274 275
if [[ $result -eq 0 ]]; then
  status="success"
276
  echo "minikube: SUCCESS"
M
Matt Rickard 已提交
277 278
else
  status="failure"
279
  echo "minikube: FAIL"
M
Matt Rickard 已提交
280 281
fi

282
echo ">> Cleaning up after ourselves ..."
283
${SUDO_PREFIX}${MINIKUBE_BIN} tunnel --cleanup || true
284
${SUDO_PREFIX}${MINIKUBE_BIN} delete >/dev/null 2>/dev/null || true
285
cleanup_stale_routes || true
T
Thomas Stromberg 已提交
286 287 288

${SUDO_PREFIX} rm -Rf "${MINIKUBE_HOME}" || true
${SUDO_PREFIX} rm -f "${KUBECONFIG}" || true
289
rmdir "${TEST_HOME}"
290 291
echo ">> ${TEST_HOME} completed at $(date)"

T
Thomas Stromberg 已提交
292 293
if [[ "${MINIKUBE_LOCATION}" == "master" ]]; then
  exit $result
294
fi
T
Thomas Stromberg 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308

# retry_github_status provides reliable github status updates
function retry_github_status() {
  local pr=$1
  local context=$2
  local state=$4
  local token=$5
  local target=$6

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

309
  while [[ "${attempt}" -lt 8 ]]; do
T
Thomas Stromberg 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    code=$(curl -s -i --write-out "%{http_code}" -L \
      "https://api.github.com/repos/kubernetes/minikube/statuses/${pr}?access_token=$token" \
      -H "Content-Type: application/json" \
      -X POST \
      -d "{\"state\": \"$state\", \"description\": \"Jenkins\", \"target_url\": \"$target\", \"context\": \"${context}\"}" || echo 999)

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

    echo "HTTP code ${code}! Retrying in ${timeout} .."
    sleep "${timeout}"
    attempt=$(( attempt + 1 ))
    timeout=$(( timeout * 2 ))
  done
}

retry_github_status "${MINIKUBE_LOCATION}" "${JOB_NAME}" "${status}" "${access_token}" "https://storage.googleapis.com/minikube-builds/logs/${MINIKUBE_LOCATION}/${JOB_NAME}.txt"
T
Thomas Stromberg 已提交
329
exit $result