Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions deploy/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,30 @@
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=}"
;;
--port=*)
PORT="${arg#--port=}"
;;
-port|--port)
next_arg_is_port=true
;;
esac
done
fi
Expand Down
56 changes: 56 additions & 0 deletions deploy/healthcheck_test.sh
Original file line number Diff line number Diff line change
@@ -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" <<EOF
#!/bin/sh
printf '%s\n' "\$*" > "${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