mirror of
https://github.com/jmcorgan/fips.git
synced 2026-07-30 19:46:15 +00:00
Two runs on one host destroyed each other's containers, producing mid-test "No such container" failures that look like real defects. The automated builder runs a full local CI on the same box every few minutes, so the machine is contended almost always and this has red-ed both a hand run and an automated gate. Two independent causes are fixed here. Container names were hardcoded, and docker names are global rather than scoped by compose project, so two runs collided on the same name; every name now takes an optional suffix that the harness sets from the run id. And the cleanup sweep matched a label shared by every run, so one run's teardown force-removed another's containers; resources now also carry a per-run label and the sweep can be narrowed to it. A third hazard turned up that was not in the original report: the sidecar suite passes explicit compose project names, which override the run-scoped project and put it outside the shared prefix entirely. Its project names, network, and derived container references are now scoped too. Both are default-off. With the suffix unset, names render exactly as they do today and a bare compose invocation is unchanged, which is what keeps the hosted CI and the documentation correct. A cleanup run with no run id still reaps everything, which is what a manual "clear the box" wants. The literal-name sweep was not sufficient: six scripts build container names dynamically from node labels, and two suites create their own containers outside compose. Those are handled at their construction sites. Verified: syntax check on all modified scripts; compose validation on every modified file with the suffix both set and unset; and a synthetic two-run reproduction that shows the old cleanup destroying a bystander run and the new one leaving it alone. Known gap: subnets are still hardcoded, so two concurrent full runs will still collide on address-pool overlap. That fix reaches into topology configs, chaos scenarios, diagrams and production source, so it is left for its own change rather than half-done here.
246 lines
6.4 KiB
Bash
Executable File
246 lines
6.4 KiB
Bash
Executable File
#!/bin/bash
|
|
# Network impairment simulation using tc/netem on FIPS Docker containers.
|
|
#
|
|
# Usage: ./netem.sh <mesh|chain> <apply|remove|status> [options]
|
|
#
|
|
# Actions:
|
|
# apply - Apply netem rules to all containers in the profile
|
|
# remove - Remove netem rules from all containers
|
|
# status - Show current tc qdisc state on each container
|
|
#
|
|
# Options (for apply):
|
|
# --delay <ms> Fixed delay in milliseconds
|
|
# --jitter <ms> Delay variation (requires --delay)
|
|
# --loss <percent> Packet loss percentage
|
|
# --loss-corr <percent> Loss correlation for bursty loss
|
|
# --duplicate <percent> Packet duplication percentage
|
|
# --reorder <percent> Packet reordering probability
|
|
# --corrupt <percent> Bit-level corruption percentage
|
|
#
|
|
# Presets (shorthand for common combinations):
|
|
# --preset lossy 5% loss, 25% correlation
|
|
# --preset congested 50ms delay, 20ms jitter, 2% loss
|
|
# --preset terrible 100ms delay, 40ms jitter, 10% loss, 1% dup, 5% reorder
|
|
#
|
|
# Examples:
|
|
# ./netem.sh mesh apply --delay 50 --loss 5
|
|
# ./netem.sh chain apply --preset congested
|
|
# ./netem.sh mesh status
|
|
# ./netem.sh mesh remove
|
|
set -e
|
|
|
|
trap 'echo ""; echo "Interrupted"; exit 130' INT
|
|
|
|
NODES="a b c d e"
|
|
IFACE="eth0"
|
|
|
|
# Defaults
|
|
DELAY=0
|
|
JITTER=0
|
|
LOSS=0
|
|
LOSS_CORR=0
|
|
DUPLICATE=0
|
|
REORDER=0
|
|
CORRUPT=0
|
|
|
|
usage() {
|
|
echo "Usage: $0 <mesh|chain> <apply|remove|status> [options]"
|
|
echo ""
|
|
echo "Actions:"
|
|
echo " apply - Apply netem rules to all containers"
|
|
echo " remove - Remove netem rules from all containers"
|
|
echo " status - Show current tc qdisc on each container"
|
|
echo ""
|
|
echo "Options (for apply):"
|
|
echo " --delay <ms> Fixed delay"
|
|
echo " --jitter <ms> Delay variation (requires --delay)"
|
|
echo " --loss <percent> Packet loss"
|
|
echo " --loss-corr <percent> Loss correlation"
|
|
echo " --duplicate <percent> Packet duplication"
|
|
echo " --reorder <percent> Packet reordering"
|
|
echo " --corrupt <percent> Bit-level corruption"
|
|
echo " --preset <name> Use a named preset (lossy, congested, terrible)"
|
|
exit 1
|
|
}
|
|
|
|
apply_preset() {
|
|
case "$1" in
|
|
lossy)
|
|
LOSS=5
|
|
LOSS_CORR=25
|
|
;;
|
|
congested)
|
|
DELAY=50
|
|
JITTER=20
|
|
LOSS=2
|
|
;;
|
|
terrible)
|
|
DELAY=100
|
|
JITTER=40
|
|
LOSS=10
|
|
DUPLICATE=1
|
|
REORDER=5
|
|
;;
|
|
*)
|
|
echo "Error: Unknown preset '$1'" >&2
|
|
echo "Available presets: lossy, congested, terrible" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
# Parse arguments
|
|
[ $# -lt 2 ] && usage
|
|
|
|
PROFILE="$1"
|
|
ACTION="$2"
|
|
shift 2
|
|
|
|
case "$PROFILE" in
|
|
mesh|chain) ;;
|
|
*) echo "Error: Profile must be 'mesh' or 'chain'" >&2; exit 1 ;;
|
|
esac
|
|
|
|
case "$ACTION" in
|
|
apply|remove|status) ;;
|
|
*) echo "Error: Action must be 'apply', 'remove', or 'status'" >&2; exit 1 ;;
|
|
esac
|
|
|
|
# Parse options
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--delay) DELAY="$2"; shift 2 ;;
|
|
--jitter) JITTER="$2"; shift 2 ;;
|
|
--loss) LOSS="$2"; shift 2 ;;
|
|
--loss-corr) LOSS_CORR="$2"; shift 2 ;;
|
|
--duplicate) DUPLICATE="$2"; shift 2 ;;
|
|
--reorder) REORDER="$2"; shift 2 ;;
|
|
--corrupt) CORRUPT="$2"; shift 2 ;;
|
|
--preset) apply_preset "$2"; shift 2 ;;
|
|
*)
|
|
echo "Error: Unknown option '$1'" >&2
|
|
usage
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# Build netem parameter string from non-zero values
|
|
build_netem_params() {
|
|
local params=""
|
|
|
|
if [ "$DELAY" != "0" ]; then
|
|
params="delay ${DELAY}ms"
|
|
if [ "$JITTER" != "0" ]; then
|
|
params="$params ${JITTER}ms"
|
|
fi
|
|
fi
|
|
|
|
if [ "$LOSS" != "0" ]; then
|
|
params="$params loss ${LOSS}%"
|
|
if [ "$LOSS_CORR" != "0" ]; then
|
|
params="$params ${LOSS_CORR}%"
|
|
fi
|
|
fi
|
|
|
|
if [ "$DUPLICATE" != "0" ]; then
|
|
params="$params duplicate ${DUPLICATE}%"
|
|
fi
|
|
|
|
if [ "$REORDER" != "0" ]; then
|
|
if [ "$DELAY" = "0" ]; then
|
|
echo "Error: --reorder requires --delay (reordering needs a delay queue)" >&2
|
|
exit 1
|
|
fi
|
|
params="$params reorder ${REORDER}%"
|
|
fi
|
|
|
|
if [ "$CORRUPT" != "0" ]; then
|
|
params="$params corrupt ${CORRUPT}%"
|
|
fi
|
|
|
|
echo "$params"
|
|
}
|
|
|
|
# Check if a container is running
|
|
container_running() {
|
|
docker inspect -f '{{.State.Running}}' "$1" 2>/dev/null | grep -q true
|
|
}
|
|
|
|
do_apply() {
|
|
local params
|
|
params=$(build_netem_params)
|
|
|
|
if [ -z "$params" ]; then
|
|
echo "Error: No impairment parameters specified" >&2
|
|
echo "Use --delay, --loss, --duplicate, --reorder, --corrupt, or --preset" >&2
|
|
exit 1
|
|
fi
|
|
|
|
echo "=== Applying netem: $params ==="
|
|
echo ""
|
|
|
|
for node in $NODES; do
|
|
local container="fips-node-${node}${FIPS_CI_NAME_SUFFIX:-}"
|
|
echo -n " $container ... "
|
|
if ! container_running "$container"; then
|
|
echo "SKIP (not running)"
|
|
continue
|
|
fi
|
|
if docker exec "$container" tc qdisc replace dev "$IFACE" root netem $params 2>&1; then
|
|
echo "OK"
|
|
else
|
|
echo "FAIL"
|
|
fi
|
|
done
|
|
}
|
|
|
|
do_remove() {
|
|
echo "=== Removing netem rules ==="
|
|
echo ""
|
|
|
|
for node in $NODES; do
|
|
local container="fips-node-${node}${FIPS_CI_NAME_SUFFIX:-}"
|
|
echo -n " $container ... "
|
|
if ! container_running "$container"; then
|
|
echo "SKIP (not running)"
|
|
continue
|
|
fi
|
|
# Suppress error if no qdisc exists
|
|
if docker exec "$container" tc qdisc del dev "$IFACE" root 2>/dev/null; then
|
|
echo "OK"
|
|
else
|
|
echo "OK (no rules)"
|
|
fi
|
|
done
|
|
}
|
|
|
|
do_status() {
|
|
echo "=== netem status ==="
|
|
echo ""
|
|
|
|
for node in $NODES; do
|
|
local container="fips-node-${node}${FIPS_CI_NAME_SUFFIX:-}"
|
|
echo " $container:"
|
|
if ! container_running "$container"; then
|
|
echo " (not running)"
|
|
continue
|
|
fi
|
|
local output
|
|
output=$(docker exec "$container" tc qdisc show dev "$IFACE" 2>&1)
|
|
if echo "$output" | grep -q "netem"; then
|
|
echo " $output"
|
|
else
|
|
echo " (no netem rules)"
|
|
fi
|
|
done
|
|
}
|
|
|
|
case "$ACTION" in
|
|
apply) do_apply ;;
|
|
remove) do_remove ;;
|
|
status) do_status ;;
|
|
esac
|
|
|
|
echo ""
|
|
echo "Done."
|