interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / test / netns-refuse.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Negative control for test/http80-netns.sh and test/sshd-keepalive.sh. |
| 3 | # A second run must refuse when the namespaces already exist, and must not kill the first |
| 4 | # run's processes or delete its namespaces. The old scripts installed an EXIT trap before |
| 5 | # that check, so the refusal itself did the damage. A missing scratch directory must also |
| 6 | # exit before any trap. |
| 7 | # sudo bash infra/test/netns-refuse.sh [scratch dir] |
| 8 | set -u |
| 9 | [ "$(id -u)" = 0 ] || { echo "run as root (sudo)" >&2; exit 2; } |
| 10 | INFRA=$(cd "$(dirname "$0")/.." && pwd) |
| 11 | SCR=$(cd "${1:-/tmp}" && pwd) || { echo "scratch dir not usable" >&2; exit 2; } |
| 12 | pass=0; fail=0 |
| 13 | ok() { pass=$((pass+1)); echo "PASS $*"; } |
| 14 | bad() { fail=$((fail+1)); echo "FAIL $*"; } |
| 15 | |
| 16 | probe() { # script name name... |
| 17 | local script=$1; shift |
| 18 | local names=("$@") n p scratch out rc alive=1 present=1 |
| 19 | local pids=() |
| 20 | for n in "${names[@]}"; do |
| 21 | ip netns list | grep -qE "^$n( |$)" && { echo "probe: $n already exists; refusing" >&2; exit 2; } |
| 22 | done |
| 23 | for n in "${names[@]}"; do |
| 24 | ip netns add "$n" || { bad "$script: cannot create probe $n"; return; } |
| 25 | ip netns exec "$n" sleep 120 & |
| 26 | pids+=("$!") |
| 27 | done |
| 28 | sleep 0.3 |
| 29 | scratch=$(mktemp -d "$SCR/netns-refuse.XXXXXX") |
| 30 | out=$(bash "$INFRA/test/$script" "$scratch" 2>&1); rc=$? |
| 31 | rm -rf -- "$scratch" |
| 32 | [ "$rc" = 2 ] && printf '%s\n' "$out" | grep -q 'already exist; refusing' \ |
| 33 | && ok "$script refused (rc 2)" || bad "$script rc $rc, out: $(printf '%s' "$out" | tail -n 1)" |
| 34 | for n in "${names[@]}"; do |
| 35 | ip netns list | grep -qE "^$n( |$)" || present=0 |
| 36 | done |
| 37 | [ "$present" = 1 ] && ok "$script left ${names[*]} in place" || bad "$script deleted a probe namespace" |
| 38 | for p in "${pids[@]}"; do |
| 39 | kill -0 "$p" 2>/dev/null || alive=0 |
| 40 | done |
| 41 | [ "$alive" = 1 ] && ok "$script left the probe sleep processes alive" || bad "$script killed a probe sleep" |
| 42 | # missing scratch directory: must exit before the trap, so the probes stay |
| 43 | out=$(bash "$INFRA/test/$script" "$SCR/does-not-exist" 2>&1); rc=$? |
| 44 | [ "$rc" = 2 ] && printf '%s\n' "$out" | grep -q 'scratch dir not usable' \ |
| 45 | && ok "$script missing scratch exits 2" || bad "$script missing scratch rc $rc" |
| 46 | present=1 |
| 47 | for n in "${names[@]}"; do |
| 48 | ip netns list | grep -qE "^$n( |$)" || present=0 |
| 49 | done |
| 50 | [ "$present" = 1 ] && ok "$script missing scratch left ${names[*]}" || bad "$script missing scratch deleted a probe" |
| 51 | for p in "${pids[@]}"; do kill "$p" 2>/dev/null || true; done |
| 52 | sleep 0.2 |
| 53 | for n in "${names[@]}"; do |
| 54 | ip netns pids "$n" 2>/dev/null | xargs -r kill 2>/dev/null || true |
| 55 | ip netns del "$n" 2>/dev/null || true |
| 56 | done |
| 57 | } |
| 58 | |
| 59 | probe sshd-keepalive.sh kasrv kacli |
| 60 | probe http80-netns.sh ipl80-srv ipl80-cli |
| 61 | echo "netns-refuse: $pass passed, $fail failed" |
| 62 | [ "$fail" = 0 ] |