diff --git a/Makefile b/Makefile index 0b7bb631c4..740af90629 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ test: @# Filter out integration. $(GO) list ./... | grep -vw integration | xargs $(GO_TEST) cd cmd && $(GO_TEST) ./... + sh deploy/healthcheck_test.sh test-with-libpfm: GO_FLAGS=-race -tags libpfm test-with-libpfm: test diff --git a/deploy/healthcheck.sh b/deploy/healthcheck.sh index df65e493fc..2c57871cd9 100644 --- a/deploy/healthcheck.sh +++ b/deploy/healthcheck.sh @@ -18,11 +18,20 @@ PORT=8080 # Extract port from the cadvisor process command line -if [ -f /proc/1/cmdline ]; then - CMDLINE=$(tr '\0' ' ' < /proc/1/cmdline) +# Allow tests to supply a fake cmdline file. +CMDLINE_FILE="${CADVISOR_HEALTHCHECK_CMDLINE_FILE:-/proc/1/cmdline}" +if [ -f "${CMDLINE_FILE}" ]; then + CMDLINE=$(tr '\0' ' ' < "${CMDLINE_FILE}") - # Look for -port=XXXX or --port=XXXX + # Look for -port=XXXX, --port=XXXX, -port XXXX, or --port XXXX. + next_arg_is_port=false for arg in $CMDLINE; do + if [ "${next_arg_is_port}" = true ]; then + PORT="${arg}" + next_arg_is_port=false + continue + fi + case "$arg" in -port=*) PORT="${arg#-port=}" @@ -30,6 +39,9 @@ if [ -f /proc/1/cmdline ]; then --port=*) PORT="${arg#--port=}" ;; + -port|--port) + next_arg_is_port=true + ;; esac done fi diff --git a/deploy/healthcheck_test.sh b/deploy/healthcheck_test.sh new file mode 100644 index 0000000000..bad2a84094 --- /dev/null +++ b/deploy/healthcheck_test.sh @@ -0,0 +1,56 @@ +#!/bin/sh + +# Copyright 2026 Google Inc. 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. + +set -eu + +SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) + +TMPDIR=$(mktemp -d) +trap 'rm -rf "${TMPDIR}"' EXIT + +run_case() { + name="$1" + expected_port="$2" + shift 2 + + cmdline_file="${TMPDIR}/${name}.cmdline" + wget_log="${TMPDIR}/${name}.wget" + + : > "${cmdline_file}" + for arg in "$@"; do + printf '%s\0' "${arg}" >> "${cmdline_file}" + done + + cat > "${TMPDIR}/wget" < "${wget_log}" +case "\$*" in + *"http://localhost:${expected_port}/healthz"*) exit 0 ;; + *) exit 1 ;; +esac +EOF + chmod +x "${TMPDIR}/wget" + + PATH="${TMPDIR}:${PATH}" \ + CADVISOR_HEALTHCHECK_CMDLINE_FILE="${cmdline_file}" \ + sh "${SCRIPT_DIR}/healthcheck.sh" +} + +run_case default 8080 cadvisor +run_case single_dash_equals 9090 cadvisor -port=9090 +run_case double_dash_equals 9091 cadvisor --port=9091 +run_case single_dash_space 9092 cadvisor -port 9092 +run_case double_dash_space 9093 cadvisor --port 9093