gem/tests/run_when_host_clean.sh
2026-08-30 18:24:55 -06:00

64 lines
2.1 KiB
Bash
Executable file

#!/bin/sh
set -eu
[ "$#" -eq 1 ] || { echo "usage: $0 RUNNER" >&2; exit 2; }
runner=$1
fixture=$(mktemp -d /private/tmp/gem-clean-host.XXXXXX)
trap 'rm -rf "$fixture"' EXIT INT TERM HUP
sample()
{
busy=$1
ac=$2
thermal=$3
jq -cn --argjson busy "$busy" --argjson ac "$ac" --argjson thermal "$thermal" \
'{load_average: "{ 4.0 4.0 4.0 }", load_1m: 4, logical_cpus: 16,
short_window_busy_cpu_percent: $busy, power_state: "fixture",
ac_power: $ac, thermal_state: "fixture", no_thermal_warning: $thermal,
top_processes: "fixture"}'
}
{
sample 20 true true
sample 5 true true
sample 5 true false
sample 5 true true
sample 5 true true
sample 5 true true
} > "$fixture/samples.ndjson"
GEM_CLEAN_HOST_FIXTURE_FILE="$fixture/samples.ndjson" \
GEM_CLEAN_HOST_INTERVAL_SECONDS=0 \
GEM_CLEAN_HOST_CONSECUTIVE_SAMPLES=3 \
"$runner" "$fixture/receipt.ndjson" -- /usr/bin/printf '%s\n' 'argument preserved' \
> "$fixture/target-output.txt"
[ "$(wc -l < "$fixture/receipt.ndjson" | tr -d ' ')" -eq 6 ]
[ "$(cat "$fixture/target-output.txt")" = 'argument preserved' ]
jq -se '
length == 6 and
(map(.schema_version == 1) | all) and
(map(.status) == ["fail","pass","fail","pass","pass","pass"]) and
(map(.consecutive_clean_samples) == [0,1,0,1,2,3]) and
(.[5].required_consecutive_clean_samples == 3)
' "$fixture/receipt.ndjson" >/dev/null
{
sample 20 true true
sample 5 false true
} > "$fixture/fail-samples.ndjson"
set +e
GEM_CLEAN_HOST_FIXTURE_FILE="$fixture/fail-samples.ndjson" \
GEM_CLEAN_HOST_INTERVAL_SECONDS=0 \
"$runner" "$fixture/fail-receipt.ndjson" -- /usr/bin/touch "$fixture/must-not-run" \
> "$fixture/fail-stdout.txt" 2> "$fixture/fail-stderr.txt"
status=$?
set -e
[ "$status" -eq 75 ]
[ ! -e "$fixture/must-not-run" ]
grep -q 'fixture exhausted before a clean window' "$fixture/fail-stderr.txt"
if GEM_CLEAN_HOST_FIXTURE_FILE="$fixture/samples.ndjson" \
"$runner" "$fixture/receipt.ndjson" -- /usr/bin/true >/dev/null 2>&1; then
echo "clean-host runner overwrote an existing receipt" >&2
exit 1
fi