|
| 1 | +#!/bin/sh |
| 2 | +# Verify that regex IP detection neither launches an intermediate shell nor |
| 3 | +# leaves zombie processes when crond is the container's PID 1. |
| 4 | + |
| 5 | +set -eu |
| 6 | + |
| 7 | +if [ "${DDNS_SIGNAL_TEST_IP:-}" = "1" ]; then |
| 8 | + trap ': > /signal/terminated; exit 0' TERM INT |
| 9 | + : > /signal/started |
| 10 | + while :; do |
| 11 | + sleep 1 |
| 12 | + done |
| 13 | +fi |
| 14 | + |
| 15 | +TEST_ROOT=/tmp/ddns-zombie-test |
| 16 | +TEST_BIN="$TEST_ROOT/bin" |
| 17 | +PARENT_LOG="$TEST_ROOT/ip-parents" |
| 18 | +READY_FILE="$TEST_ROOT/ready" |
| 19 | + |
| 20 | +setup_container() { |
| 21 | + mkdir -p "$TEST_BIN" |
| 22 | + cat > "$TEST_BIN/ip" <<'EOF' |
| 23 | +#!/bin/sh |
| 24 | +set -eu |
| 25 | +
|
| 26 | +if [ "$#" -ne 1 ] || [ "$1" != "address" ]; then |
| 27 | + echo "Unexpected ip arguments: $*" >&2 |
| 28 | + exit 64 |
| 29 | +fi |
| 30 | +
|
| 31 | +cat "/proc/$PPID/comm" >> /tmp/ddns-zombie-test/ip-parents |
| 32 | +cat <<'EOF_IP_OUTPUT' |
| 33 | +2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 |
| 34 | + inet6 2001:db8::42/64 scope global |
| 35 | +EOF_IP_OUTPUT |
| 36 | +EOF |
| 37 | + chmod 755 "$TEST_BIN/ip" |
| 38 | + : > "$PARENT_LOG" |
| 39 | + : > "$READY_FILE" |
| 40 | +} |
| 41 | + |
| 42 | +check_process_tree() { |
| 43 | + init_name=$(cat /proc/1/comm) |
| 44 | + if [ "$init_name" != "tini" ]; then |
| 45 | + echo "Expected tini as PID 1, got $init_name" >&2 |
| 46 | + return 1 |
| 47 | + fi |
| 48 | + |
| 49 | + crond_parent=$(ps -o pid,ppid,stat,comm | awk 'NR > 1 && $4 == "crond" { print $2; exit }') |
| 50 | + if [ "$crond_parent" != "1" ]; then |
| 51 | + echo "Expected crond to be a direct child of tini, got PPID ${crond_parent:-missing}" >&2 |
| 52 | + return 1 |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +check_container() { |
| 57 | + expected_runs=$1 |
| 58 | + actual_runs=$(wc -l < "$PARENT_LOG") |
| 59 | + |
| 60 | + check_process_tree |
| 61 | + |
| 62 | + if [ "$actual_runs" -ne "$expected_runs" ]; then |
| 63 | + echo "Expected $expected_runs ip executions, got $actual_runs" >&2 |
| 64 | + cat "$PARENT_LOG" >&2 |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | + |
| 68 | + if grep -Eq "^(sh|ash|busybox)$" "$PARENT_LOG"; then |
| 69 | + echo "Regex IP detection launched ip through an intermediate shell" >&2 |
| 70 | + cat "$PARENT_LOG" >&2 |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + zombies=$(ps -o pid,ppid,stat,comm | awk 'NR > 1 && $3 ~ /^Z/ { print }') |
| 75 | + if [ -n "$zombies" ]; then |
| 76 | + echo "Zombie processes found after regex IP detection:" >&2 |
| 77 | + echo "$zombies" >&2 |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | + |
| 81 | + echo "Regex IP detection completed $actual_runs runs without shell intermediates or zombies" |
| 82 | +} |
| 83 | + |
| 84 | +test_startup_signal_forwarding() { |
| 85 | + docker run --detach \ |
| 86 | + --name "$signal_container" \ |
| 87 | + --platform "$platform" \ |
| 88 | + --env "PATH=/tmp:$base_path" \ |
| 89 | + --env DDNS_DNS=debug \ |
| 90 | + --env DDNS_IPV6=zombie-test.example \ |
| 91 | + --env 'DDNS_INDEX6=regex:2001:db8:.*' \ |
| 92 | + --env DDNS_CACHE=false \ |
| 93 | + --env DDNS_SIGNAL_TEST_IP=1 \ |
| 94 | + --volume "$script_path:/tmp/ip:ro" \ |
| 95 | + --volume "$signal_dir:/signal" \ |
| 96 | + "$image" >/dev/null |
| 97 | + |
| 98 | + attempt=0 |
| 99 | + until [ -f "$signal_dir/started" ]; do |
| 100 | + attempt=$((attempt + 1)) |
| 101 | + if [ "$attempt" -ge 30 ]; then |
| 102 | + echo "Timed out waiting for the initial DDNS update" >&2 |
| 103 | + docker logs "$signal_container" >&2 |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | + sleep 1 |
| 107 | + done |
| 108 | + |
| 109 | + docker stop --time 5 "$signal_container" >/dev/null |
| 110 | + if [ ! -f "$signal_dir/terminated" ]; then |
| 111 | + echo "Tini did not forward SIGTERM to the initial DDNS process group" >&2 |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | + |
| 115 | + exit_code=$(docker inspect --format '{{.State.ExitCode}}' "$signal_container") |
| 116 | + if [ "$exit_code" -eq 137 ]; then |
| 117 | + echo "Initial DDNS shutdown required SIGKILL" >&2 |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + echo "Tini forwarded SIGTERM during the initial DDNS update" |
| 121 | +} |
| 122 | + |
| 123 | +case "${1:-}" in |
| 124 | + --setup) |
| 125 | + setup_container |
| 126 | + exit 0 |
| 127 | + ;; |
| 128 | + --check) |
| 129 | + check_container "$2" |
| 130 | + exit 0 |
| 131 | + ;; |
| 132 | + --ready) |
| 133 | + test -f "$READY_FILE" |
| 134 | + check_process_tree |
| 135 | + exit 0 |
| 136 | + ;; |
| 137 | +esac |
| 138 | + |
| 139 | +image=${1:?Usage: test-docker-zombies.sh IMAGE [PLATFORM]} |
| 140 | +platform=${2:-linux/amd64} |
| 141 | +runs=${DDNS_ZOMBIE_TEST_RUNS:-8} |
| 142 | +script_dir=$(cd "$(dirname "$0")" && pwd) |
| 143 | +script_path="$script_dir/$(basename "$0")" |
| 144 | +container="ddns-zombie-test-$$" |
| 145 | +signal_container="${container}-signal" |
| 146 | +signal_dir="${TMPDIR:-/tmp}/${signal_container}" |
| 147 | +base_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" |
| 148 | +mkdir -p "$signal_dir" |
| 149 | + |
| 150 | +cleanup() { |
| 151 | + docker rm -f "$container" >/dev/null 2>&1 || true |
| 152 | + docker rm -f "$signal_container" >/dev/null 2>&1 || true |
| 153 | + rm -rf "$signal_dir" |
| 154 | +} |
| 155 | +trap cleanup 0 1 2 15 |
| 156 | + |
| 157 | +expected_entrypoint='["/sbin/tini","-g","--","/bin/entrypoint.sh"]' |
| 158 | +actual_entrypoint=$(docker image inspect --format '{{json .Config.Entrypoint}}' "$image") |
| 159 | +if [ "$actual_entrypoint" != "$expected_entrypoint" ]; then |
| 160 | + echo "Unexpected image entrypoint: $actual_entrypoint" >&2 |
| 161 | + exit 1 |
| 162 | +fi |
| 163 | + |
| 164 | +docker run --detach \ |
| 165 | + --name "$container" \ |
| 166 | + --platform "$platform" \ |
| 167 | + --entrypoint /sbin/tini \ |
| 168 | + --volume "$script_dir:/tests:ro" \ |
| 169 | + "$image" \ |
| 170 | + -g -- /bin/sh -c "/bin/sh /tests/test-docker-zombies.sh --setup; exec crond -f" >/dev/null |
| 171 | + |
| 172 | +attempt=0 |
| 173 | +until docker exec "$container" /bin/sh /tests/test-docker-zombies.sh --ready 2>/dev/null; do |
| 174 | + attempt=$((attempt + 1)) |
| 175 | + if [ "$attempt" -ge 30 ]; then |
| 176 | + echo "Timed out waiting for the zombie test container" >&2 |
| 177 | + docker logs "$container" >&2 |
| 178 | + exit 1 |
| 179 | + fi |
| 180 | + sleep 1 |
| 181 | +done |
| 182 | + |
| 183 | +test_path="$TEST_BIN:$base_path" |
| 184 | +run=0 |
| 185 | +while [ "$run" -lt "$runs" ]; do |
| 186 | + output=$(docker exec --env "PATH=$test_path" "$container" \ |
| 187 | + /bin/ddns \ |
| 188 | + --dns=debug \ |
| 189 | + '--index6=regex:2001:db8:.*' \ |
| 190 | + --ipv6=zombie-test.example \ |
| 191 | + --cache=false) |
| 192 | + if ! printf '%s\n' "$output" | grep -q '^\[IPv6\] 2001:db8::42$'; then |
| 193 | + echo "Regex IPv6 result did not reach the debug provider:" >&2 |
| 194 | + echo "$output" >&2 |
| 195 | + exit 1 |
| 196 | + fi |
| 197 | + run=$((run + 1)) |
| 198 | +done |
| 199 | + |
| 200 | +sleep 1 |
| 201 | +docker exec "$container" /bin/sh /tests/test-docker-zombies.sh --check "$runs" |
| 202 | + |
| 203 | +docker stop --time 5 "$container" >/dev/null |
| 204 | +exit_code=$(docker inspect --format '{{.State.ExitCode}}' "$container") |
| 205 | +if [ "$exit_code" -eq 137 ]; then |
| 206 | + echo "Container required SIGKILL instead of a graceful Tini shutdown" >&2 |
| 207 | + exit 1 |
| 208 | +fi |
| 209 | +echo "Tini forwarded shutdown with container exit code $exit_code" |
| 210 | + |
| 211 | +test_startup_signal_forwarding |
0 commit comments