github.com/google/cadvisor@v0.49.1/build/integration.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2016 Google Inc. All rights reserved. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 set -e 18 # When running this script locally, you may need to run cadvisor with sudo 19 # permissions if you cadvisor can't find containers. 20 # USE_SUDO=true make test-integration 21 USE_SUDO=${USE_SUDO:-false} 22 cadvisor_bin=${CADVISOR_BIN:-"./_output/cadvisor"} 23 24 if ! [ -f "$cadvisor_bin" ]; then 25 echo Failed to find cadvisor binary for integration test at path $cadvisor_bin 26 exit 1 27 fi 28 29 log_file="cadvisor.log" 30 if [ "$#" -gt 0 ]; then 31 log_file="$1" 32 fi 33 34 TEST_PID=$$ 35 printf "" # Refresh sudo credentials if necessary. 36 function start { 37 set +e # We want to handle errors if cAdvisor crashes. 38 echo ">> starting cAdvisor locally" 39 cadvisor_prereqs="" 40 if [ $USE_SUDO = true ]; then 41 cadvisor_prereqs=sudo 42 fi 43 # cpu, cpuset, percpu, memory, disk, diskIO, network, perf_event metrics should be enabled. 44 GORACE="halt_on_error=1" $cadvisor_prereqs $cadvisor_bin --enable_metrics="cpu,cpuset,percpu,memory,disk,diskIO,network,perf_event" --env_metadata_whitelist=TEST_VAR --v=6 --logtostderr $CADVISOR_ARGS &> "$log_file" 45 exit_code=$? 46 if [ $exit_code != 0 ]; then 47 echo "!! cAdvisor exited unexpectedly with Exit $exit_code" 48 cat $log_file 49 kill $TEST_PID # cAdvisor crashed: abort testing. 50 fi 51 } 52 start & 53 RUNNER_PID=$! 54 55 function cleanup { 56 if pgrep cadvisor > /dev/null; then 57 echo ">> stopping cAdvisor" 58 pkill -SIGINT cadvisor 59 wait $RUNNER_PID 60 fi 61 } 62 trap cleanup EXIT SIGINT TERM 63 64 readonly TIMEOUT=30 # Timeout to wait for cAdvisor, in seconds. 65 START=$(date +%s) 66 while [ "$(curl -Gs http://localhost:8080/healthz)" != "ok" ]; do 67 if (( $(date +%s) - $START > $TIMEOUT )); then 68 echo "Timed out waiting for cAdvisor to start" 69 exit 1 70 fi 71 echo "Waiting for cAdvisor to start ..." 72 sleep 1 73 done 74 75 if [[ "${DOCKER_IN_DOCKER_ENABLED:-}" == "true" ]]; then 76 # see https://github.com/moby/moby/blob/master/hack/dind 77 # cgroup v2: enable nesting 78 if [ -f /sys/fs/cgroup/cgroup.controllers ]; then 79 echo ">> configuring cgroupsv2 for docker in docker..." 80 # move the processes from the root group to the /init group, 81 # otherwise writing subtree_control fails with EBUSY. 82 # An error during moving non-existent process (i.e., "cat") is ignored. 83 mkdir -p /sys/fs/cgroup/init 84 xargs -rn1 < /sys/fs/cgroup/cgroup.procs > /sys/fs/cgroup/init/cgroup.procs || : 85 # enable controllers 86 sed -e 's/ / +/g' -e 's/^/+/' < /sys/fs/cgroup/cgroup.controllers \ 87 > /sys/fs/cgroup/cgroup.subtree_control 88 fi 89 fi 90 91 echo ">> running integration tests against local cAdvisor" 92 if ! [ -f ./api.test ] || ! [ -f ./healthz.test ]; then 93 echo You must compile the ./api.test binary and ./healthz.test binary before 94 echo running the integration tests. 95 exit 1 96 fi 97 ./api.test --vmodule=*=2 -test.v 98 ./healthz.test --vmodule=*=2 -test.v