|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Test that SIGTERM propagates through the full CI pipeline and kills Docker containers. |
| 3 | +# |
| 4 | +# When tests run with ISOLATE=1, they execute inside a Docker container via docker_isolate. |
| 5 | +# If the pipeline receives SIGTERM (e.g., Ctrl-C during `parallelize`), the container must |
| 6 | +# be killed. |
| 7 | +# |
| 8 | +# The full signal flow on Ctrl-C: |
| 9 | +# Terminal sends SIGINT to foreground process group |
| 10 | +# → parallel, run_test_cmd receive signal (same process group) |
| 11 | +# → run_test_cmd sig_handler fires |
| 12 | +# → must send SIGTERM to exec_test's process GROUP (not just exec_test) |
| 13 | +# → docker_isolate receives SIGTERM → cleanup trap → docker rm -f |
| 14 | +# |
| 15 | +# The critical detail: run_test_cmd uses set -m, so exec_test runs in its |
| 16 | +# OWN process group. The sig_handler must use kill -TERM -$pid (negative = group), |
| 17 | +# otherwise docker_isolate is orphaned and the container keeps running. |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +source "$(git rev-parse --show-toplevel)/ci3/source" |
| 21 | + |
| 22 | +RED=$'\033[0;31m' |
| 23 | +GREEN=$'\033[0;32m' |
| 24 | +NC=$'\033[0m' |
| 25 | + |
| 26 | +TESTS_PASSED=0 |
| 27 | +TESTS_FAILED=0 |
| 28 | + |
| 29 | +# Unique prefix to avoid container name collisions. |
| 30 | +PREFIX="ci3sigtest$$" |
| 31 | + |
| 32 | +function pass { echo "${GREEN}✓${NC} $1"; ((TESTS_PASSED++)) || true; } |
| 33 | +function fail { |
| 34 | + echo "${RED}✗${NC} $1: $2" |
| 35 | + ((TESTS_FAILED++)) || true |
| 36 | +} |
| 37 | + |
| 38 | +# Wait for a container to appear (up to N seconds). |
| 39 | +function wait_for_container { |
| 40 | + local name=$1 max=${2:-15} |
| 41 | + for ((i=0; i<max; i++)); do |
| 42 | + if docker ps --filter "name=^${name}$" --format '{{.Names}}' | grep -q "^${name}$"; then |
| 43 | + return 0 |
| 44 | + fi |
| 45 | + sleep 1 |
| 46 | + done |
| 47 | + return 1 |
| 48 | +} |
| 49 | + |
| 50 | +# Check that a container has stopped (within N seconds). |
| 51 | +function wait_for_no_container { |
| 52 | + local name=$1 max=${2:-10} |
| 53 | + for ((i=0; i<max; i++)); do |
| 54 | + if ! docker ps --filter "name=^${name}$" --format '{{.Names}}' | grep -q "^${name}$"; then |
| 55 | + return 0 |
| 56 | + fi |
| 57 | + sleep 1 |
| 58 | + done |
| 59 | + return 1 |
| 60 | +} |
| 61 | + |
| 62 | +# Cleanup any leftover containers on exit. |
| 63 | +function cleanup_all { |
| 64 | + docker ps -a --filter "name=$PREFIX" --format '{{.Names}}' | while read -r name; do |
| 65 | + docker rm -f "$name" &>/dev/null || true |
| 66 | + done |
| 67 | +} |
| 68 | +trap cleanup_all EXIT |
| 69 | + |
| 70 | +# Verify docker is available and the build image exists. |
| 71 | +if ! docker info &>/dev/null; then |
| 72 | + echo "SKIP: Docker not available" |
| 73 | + exit 0 |
| 74 | +fi |
| 75 | + |
| 76 | +if ! docker image inspect aztecprotocol/build:3.0 &>/dev/null; then |
| 77 | + echo "SKIP: aztecprotocol/build:3.0 image not available" |
| 78 | + exit 0 |
| 79 | +fi |
| 80 | + |
| 81 | +echo "=== Signal Propagation Tests ===" |
| 82 | + |
| 83 | +# ─── Test: full pipeline (parallelize) SIGTERM kills container ────── |
| 84 | +# |
| 85 | +# Mirrors the real Ctrl-C scenario: |
| 86 | +# echo "hash:ISOLATE=1 sleep 60" | parallelize |
| 87 | +# User presses Ctrl-C → SIGINT to foreground process group. |
| 88 | +# |
| 89 | +# We use set -m so the backgrounded pipeline gets its own process group, |
| 90 | +# then kill the group (simulating Ctrl-C to the foreground group). |
| 91 | +# This reaches parallel and run_test_cmd, but NOT exec_test/docker_isolate |
| 92 | +# (they're in a separate group due to run_test_cmd's set -m). |
| 93 | +# The test passes only if run_test_cmd's sig_handler propagates to docker_isolate. |
| 94 | +echo "--- Test: echo '...' | parallelize receives SIGTERM ---" |
| 95 | +name="${PREFIX}t1" |
| 96 | + |
| 97 | +# Job control: backgrounded pipeline gets its own process group. |
| 98 | +set -m |
| 99 | +echo "fakehash:ISOLATE=1:CPUS=1:NAME=${name}:TIMEOUT=120s sleep 60" | parallelize 1 &>/dev/null & |
| 100 | +pid=$! |
| 101 | +set +m |
| 102 | + |
| 103 | +if wait_for_container "$name"; then |
| 104 | + # Kill the pipeline's process group (simulates Ctrl-C). |
| 105 | + kill -TERM -- -$pid 2>/dev/null || true |
| 106 | + wait $pid 2>/dev/null || true |
| 107 | + if wait_for_no_container "$name"; then |
| 108 | + pass "parallelize SIGTERM propagation" |
| 109 | + else |
| 110 | + fail "parallelize SIGTERM propagation" "Container still running after SIGTERM" |
| 111 | + fi |
| 112 | +else |
| 113 | + fail "parallelize SIGTERM propagation" "Container never started" |
| 114 | +fi |
| 115 | + |
| 116 | +echo |
| 117 | +if [[ $TESTS_FAILED -eq 0 ]]; then |
| 118 | + echo "${GREEN}All $TESTS_PASSED tests passed.${NC}" |
| 119 | + exit 0 |
| 120 | +else |
| 121 | + echo "${RED}$TESTS_FAILED of $((TESTS_PASSED + TESTS_FAILED)) tests failed.${NC}" |
| 122 | + exit 1 |
| 123 | +fi |
0 commit comments