common.sh 9.2 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
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)"
# 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 ""
54
mkdir -p out/ testdata/
55 56

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

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

69 70 71 72 73 74
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
D
dlorenc 已提交
75

76
gsutil -qm cp "gs://minikube-builds/${MINIKUBE_LOCATION}/testdata"/* testdata/
M
Medya Gh 已提交
77 78 79 80 81

# to be used by TestVersionUpgrade
gsutil -qm cp gs://minikube/releases/latest/minikube-${OS_ARCH} testdata/minikube-${OS_ARCH}-latest-stable
chmod +x "testdata/minikube-${OS_ARCH}-latest-stable"

D
dlorenc 已提交
82

83 84 85 86 87
# 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
Thomas Stromberg 已提交
88
procs=$(pgrep "minikube-${OS_ARCH}|e2e-${OS_ARCH}" || true)
89
if [[ "${procs}" != "" ]]; then
M
Medya Gh 已提交
90
  echo "Warning: found stale test processes to kill:"
91 92 93
  ps -f -p ${procs} || true
  kill ${procs} || true
  kill -9 ${procs} || true
94
fi
95 96

# Cleanup stale test outputs.
97 98
echo ""
echo ">> Cleaning up after previous test runs ..."
99

100
for stale_dir in ${TEST_ROOT}/*; do
101 102 103 104 105
  echo "* Cleaning stale test: ${stale_dir}"
  export MINIKUBE_HOME="${stale_dir}/.minikube"
  export KUBECONFIG="${stale_dir}/kubeconfig"

  if [[ -d "${MINIKUBE_HOME}" ]]; then
106
    if [[ -r "${MINIKUBE_HOME}/tunnels.json" ]]; then
107
      cat "${MINIKUBE_HOME}/tunnels.json"
108
      ${MINIKUBE_BIN} tunnel --cleanup || true
109
    fi
110 111 112 113 114 115 116 117 118
    echo "Shutting down stale minikube instance ..."
    if [[ -w "${MINIKUBE_HOME}" ]]; then
        "${MINIKUBE_BIN}" delete || true
        rm -Rf "${MINIKUBE_HOME}"
    else
      sudo -E "${MINIKUBE_BIN}" delete || true
      sudo rm -Rf "${MINIKUBE_HOME}"
    fi
  fi
M
Matt Rickard 已提交
119

120 121 122 123 124 125 126
  if [[ -f "${KUBECONFIG}" ]]; then
    if [[ -w "${KUBECONFIG}" ]]; then
      rm -f "${KUBECONFIG}"
    else
      sudo rm -f "${KUBECONFIG}" || true
    fi
  fi
D
dlorenc 已提交
127

128 129
  rmdir "${stale_dir}" || true
done
130

131 132 133 134 135
if type -P virsh; then
  virsh -c qemu:///system list --all
  virsh -c qemu:///system list --all \
    | grep minikube \
    | awk '{ print $2 }' \
T
Thomas Stromberg 已提交
136 137
    | xargs -I {} sh -c "virsh -c qemu:///system destroy {}; virsh -c qemu:///system undefine {}" \
    || true
M
Medya Gh 已提交
138 139 140 141 142 143
  virsh -c qemu:///system list --all \
    | grep Test \
    | awk '{ print $2 }' \
    | xargs -I {} sh -c "virsh -c qemu:///system destroy {}; virsh -c qemu:///system undefine {}" \
    || true

M
Medya Gh 已提交
144
  # list again after clean up
M
Medya Gh 已提交
145 146 147
  virsh -c qemu:///system list --all || true


M
Medya Gh 已提交
148 149
  echo ">> Virsh VM list after clean up (should be empty) :"
  virsh -c qemu:///system list --all || true
150
fi
151

152
if type -P vboxmanage; then
T
Thomas Stromberg 已提交
153
  vboxmanage list vms || true
154
  vboxmanage list vms \
T
Thomas Stromberg 已提交
155
    | grep minikube \
156 157 158
    | cut -d'"' -f2 \
    | xargs -I {} sh -c "vboxmanage startvm {} --type emergencystop; vboxmanage unregistervm {} --delete" \
    || true
M
Medya Gh 已提交
159 160 161 162 163 164 165

  # remove inaccessible stale VMs https://github.com/kubernetes/minikube/issues/4872
  vboxmanage list vms \
    | grep inaccessible \
    | cut -d'"' -f3 \
    | xargs -I {} sh -c "vboxmanage startvm {} --type emergencystop; vboxmanage unregistervm {} --delete" \
    || true
M
Medya Gh 已提交
166
  # list them again after clean up
M
Medya Gh 已提交
167 168 169 170 171 172
  vboxmanage list vms \
    | grep Test \
    | cut -d'"' -f2 \
    | xargs -I {} sh -c "vboxmanage startvm {} --type emergencystop; vboxmanage unregistervm {} --delete" \
    || true

M
Medya Gh 已提交
173
  vboxmanage list vms || true
M
Medya Gh 已提交
174 175


M
Medya Gh 已提交
176 177
  echo ">> Vbox VM list after clean up (should be empty) :"
  vboxmanage list vms || true
178 179 180
fi

if type -P hdiutil; then
T
Thomas Stromberg 已提交
181
  hdiutil info | grep -E "/dev/disk[1-9][^s]" || true
182 183
  hdiutil info \
      | grep -E "/dev/disk[1-9][^s]" \
184
      | awk '{print $1}' \
185 186
      | xargs -I {} sh -c "hdiutil detach {}" \
      || true
187
fi
188

M
Medya Gh 已提交
189 190 191
# cleaning up stale hyperkits
if type -P hyperkit; then
  # find all hyperkits excluding com.docker
192
  hyper_procs=$(ps aux | grep hyperkit | grep -v com.docker | grep -v grep | grep -v osx_integration_tests_hyperkit.sh | awk '{print $2}' || true)
M
Medya Gh 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205
  if [[ "${hyper_procs}" != "" ]]; then
    echo "Found stale hyperkits test processes to kill : "
    for p in $hyper_procs
    do
    echo "Killing stale hyperkit $p"
    ps -f -p $p || true
    kill $p || true
    kill -9 $p || true
    done
  fi
fi


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

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

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

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

  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
247 248 249 250
}

cleanup_stale_routes || true

251
mkdir -p "${TEST_HOME}"
252 253 254
export MINIKUBE_HOME="${TEST_HOME}/.minikube"
export MINIKUBE_WANTREPORTERRORPROMPT=False
export KUBECONFIG="${TEST_HOME}/kubeconfig"
255

256 257 258
# Display the default image URL
echo ""
echo ">> ISO URL"
T
Thomas Stromberg 已提交
259
"${MINIKUBE_BIN}" start -h | grep iso-url || true
M
Matt Rickard 已提交
260

261 262 263
echo ""
echo ">> Starting ${E2E_BIN} at $(date)"
${SUDO_PREFIX}${E2E_BIN} \
264 265
  -minikube-start-args="--vm-driver=${VM_DRIVER} ${EXTRA_START_ARGS}" \
  -minikube-args="--v=10 --logtostderr ${EXTRA_ARGS}" \
M
Medya Gh 已提交
266
  -test.v -test.timeout=100m -binary="${MINIKUBE_BIN}" && result=$? || result=$?
267 268
echo ">> ${E2E_BIN} exited with ${result} at $(date)"
echo ""
269

M
Matt Rickard 已提交
270 271
if [[ $result -eq 0 ]]; then
  status="success"
272
  echo "minikube: SUCCESS"
M
Matt Rickard 已提交
273 274
else
  status="failure"
275
  echo "minikube: FAIL"
276
  source print-debug-info.sh
M
Matt Rickard 已提交
277 278
fi

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

${SUDO_PREFIX} rm -Rf "${MINIKUBE_HOME}" || true
${SUDO_PREFIX} rm -f "${KUBECONFIG}" || true
286 287 288
rmdir "${TEST_HOME}"
echo ">> ${TEST_HOME} completed at $(date)"

T
Thomas Stromberg 已提交
289
if [[ "${MINIKUBE_LOCATION}" != "master" ]]; then
290
  readonly target_url="https://storage.googleapis.com/minikube-builds/logs/${MINIKUBE_LOCATION}/${JOB_NAME}.txt"
T
Thomas Stromberg 已提交
291
  curl -s "https://api.github.com/repos/kubernetes/minikube/statuses/${COMMIT}?access_token=$access_token" \
M
Matt Rickard 已提交
292 293 294
  -H "Content-Type: application/json" \
  -X POST \
  -d "{\"state\": \"$status\", \"description\": \"Jenkins\", \"target_url\": \"$target_url\", \"context\": \"${JOB_NAME}\"}"
295
fi
T
Thomas Stromberg 已提交
296
exit $result