|
| 1 | +#!/bin/sh |
| 2 | +# enforcement/eval/test-executor.sh — conformance test for |
| 3 | +# the local enforcement executor. 10 cases; 10/10 PASS |
| 4 | +# expected. |
| 5 | +# |
| 6 | +# Tests the run-executor.sh script in isolation (the |
| 7 | +# GitHub Actions workflow itself is tested by the workflow |
| 8 | +# YAML schema check, not by this script). |
| 9 | +# |
| 10 | +# Usage: sh enforcement/eval/test-executor.sh |
| 11 | +# (run from the playbook repo root, not the consumer repo) |
| 12 | + |
| 13 | +set -e |
| 14 | + |
| 15 | +# Resolve paths relative to the script location. |
| 16 | +TEST_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 17 | +PLAYBOOK_ROOT="$(cd "$TEST_DIR/../.." && pwd)" |
| 18 | +EXECUTOR="$PLAYBOOK_ROOT/enforcement/executor/run-executor.sh" |
| 19 | +WORKFLOW="$PLAYBOOK_ROOT/.github/workflows/enforcement-executor.yml" |
| 20 | + |
| 21 | +command -v git >/dev/null 2>&1 || \ |
| 22 | + { printf 'test-executor.sh: git not in PATH.\n' >&2; exit 1; } |
| 23 | +[ -x "$EXECUTOR" ] || \ |
| 24 | + { printf 'test-executor.sh: executor not found at %s\n' "$EXECUTOR" >&2; exit 1; } |
| 25 | + |
| 26 | +PASS=0 |
| 27 | +FAIL=0 |
| 28 | +REPO="" |
| 29 | + |
| 30 | +# Helper: create a clean temp repo with a fake verifier |
| 31 | +# script. Each test case gets a fresh repo to avoid |
| 32 | +# state leakage between cases. |
| 33 | +setup_repo() { |
| 34 | + REPO=$(mktemp -d -t enforcement-executor-test-XXXXXX) |
| 35 | + cd "$REPO" |
| 36 | + git init -q |
| 37 | + git config user.email "t@t" |
| 38 | + git config user.name "T" |
| 39 | + mkdir -p scripts |
| 40 | + cat > scripts/run-verifier.sh <<'EOF' |
| 41 | +#!/bin/sh |
| 42 | +# Fake verifier that records a report and exits 0. |
| 43 | +mkdir -p reports |
| 44 | +printf "# Session drift report\n\n## Header\n\n- **Timestamp:** %s\n- **Range:** root..HEAD\n- **Total commits analyzed:** 0\n\n## Summary\n\n- **Drift count:** 0\n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > reports/session-drift.md |
| 45 | +exit 0 |
| 46 | +EOF |
| 47 | + chmod +x scripts/run-verifier.sh |
| 48 | +} |
| 49 | + |
| 50 | +# Helper: cleanup the temp repo. |
| 51 | +cleanup_repo() { |
| 52 | + if [ -n "$REPO" ] && [ -d "$REPO" ]; then |
| 53 | + rm -rf "$REPO" |
| 54 | + fi |
| 55 | + REPO="" |
| 56 | +} |
| 57 | + |
| 58 | +# Track all cleanup needs |
| 59 | +ALL_CLEANUPS="" |
| 60 | +add_cleanup() { ALL_CLEANUPS="$ALL_CLEANUPS $1"; } |
| 61 | +run_cleanups() { |
| 62 | + for d in $ALL_CLEANUPS; do |
| 63 | + [ -n "$d" ] && [ -d "$d" ] && rm -rf "$d" |
| 64 | + done |
| 65 | + ALL_CLEANUPS="" |
| 66 | +} |
| 67 | +trap run_cleanups EXIT |
| 68 | + |
| 69 | +# check <name> <expected: pass|fail> <command> |
| 70 | +check() { |
| 71 | + if eval "$3" 2>/dev/null; then o=pass; else o=fail; fi |
| 72 | + if [ "$o" = "$2" ]; then |
| 73 | + PASS=$((PASS+1)) |
| 74 | + printf ' PASS: %s\n' "$1" |
| 75 | + else |
| 76 | + FAIL=$((FAIL+1)) |
| 77 | + printf ' FAIL: %s\n' "$1" |
| 78 | + fi |
| 79 | +} |
| 80 | + |
| 81 | +# Case 1: basic run — verifier runs, report produced |
| 82 | +setup_repo |
| 83 | +check "basic run" pass \ |
| 84 | + "ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' && [ -f reports/session-drift.md ]" |
| 85 | +cleanup_repo |
| 86 | + |
| 87 | +# Case 2: env-var override — ENFORCEMENT_VERIFIER_SCRIPT |
| 88 | +# points to a non-existent script. The executor should |
| 89 | +# exit non-zero with a "verifier script not found" error. |
| 90 | +setup_repo |
| 91 | +check "env-var override" pass \ |
| 92 | + "! ENFORCEMENT_VERIFIER_SCRIPT=scripts/does-not-exist.sh ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' 2>/dev/null" |
| 93 | +cleanup_repo |
| 94 | + |
| 95 | +# Case 3: idempotency window — second run within 1h is skipped |
| 96 | +setup_repo |
| 97 | +ENFORCEMENT_SKIP_IF_RECENT=0 "$EXECUTOR" >/dev/null 2>&1 |
| 98 | +if ENFORCEMENT_SKIP_IF_RECENT=1 "$EXECUTOR" 2>&1 | grep -q 'skipping'; then |
| 99 | + PASS=$((PASS+1)) |
| 100 | + printf ' PASS: %s\n' "idempotency window" |
| 101 | +else |
| 102 | + FAIL=$((FAIL+1)) |
| 103 | + printf ' FAIL: %s\n' "idempotency window" |
| 104 | +fi |
| 105 | +cleanup_repo |
| 106 | + |
| 107 | +# Case 4: lock file cleanup — the script creates a lock |
| 108 | +# file via mktemp and cleans it up via the EXIT trap. |
| 109 | +# We verify the trap fires by checking that the lock |
| 110 | +# file is gone after the run. |
| 111 | +setup_repo |
| 112 | +ENFORCEMENT_SKIP_IF_RECENT=0 "$EXECUTOR" >/dev/null 2>&1 |
| 113 | +# The lock file is in /tmp with prefix enforcement-executor. |
| 114 | +LOCK_FILES=$(ls /tmp/enforcement-executor.* 2>/dev/null | wc -l) |
| 115 | +if [ "$LOCK_FILES" -eq 0 ]; then |
| 116 | + PASS=$((PASS+1)) |
| 117 | + printf ' PASS: %s\n' "lock file cleanup" |
| 118 | +else |
| 119 | + FAIL=$((FAIL+1)) |
| 120 | + printf ' FAIL: %s\n' "lock file cleanup ($LOCK_FILES leftover)" |
| 121 | +fi |
| 122 | +cleanup_repo |
| 123 | + |
| 124 | +# Case 5: missing verifier script — executor exits non-zero |
| 125 | +setup_repo |
| 126 | +rm scripts/run-verifier.sh |
| 127 | +check "missing verifier script" pass \ |
| 128 | + "! ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' 2>/dev/null" |
| 129 | +cleanup_repo |
| 130 | + |
| 131 | +# Case 6: empty run (0 drift) — produces a report, exits 0 |
| 132 | +setup_repo |
| 133 | +check "empty run produces report" pass \ |
| 134 | + "ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' && [ -s reports/session-drift.md ]" |
| 135 | +cleanup_repo |
| 136 | + |
| 137 | +# Case 7: workflow file present and valid YAML |
| 138 | +if [ -f "$WORKFLOW" ]; then |
| 139 | + if /data/data/com.termux/files/usr/bin/python3 -c " |
| 140 | +import yaml |
| 141 | +class L(yaml.SafeLoader): pass |
| 142 | +def on_constructor(loader, node): |
| 143 | + return loader.construct_scalar(node) |
| 144 | +L.add_constructor('tag:yaml.org,2002:bool', on_constructor) |
| 145 | +d = yaml.load(open('$WORKFLOW'), Loader=L) |
| 146 | +assert 'jobs' in d and 'executor' in d['jobs'], 'jobs.executor missing' |
| 147 | +assert 'timeout-minutes' in d, 'top-level timeout-minutes missing' |
| 148 | +assert d['timeout-minutes'] <= 5, 'timeout too long' |
| 149 | +print('workflow OK') |
| 150 | +" 2>/dev/null; then |
| 151 | + PASS=$((PASS+1)) |
| 152 | + printf ' PASS: %s\n' "workflow file valid YAML" |
| 153 | + else |
| 154 | + FAIL=$((FAIL+1)) |
| 155 | + printf ' FAIL: %s\n' "workflow file valid YAML" |
| 156 | + fi |
| 157 | +else |
| 158 | + FAIL=$((FAIL+1)) |
| 159 | + printf ' FAIL: %s\n' "workflow file not found at $WORKFLOW" |
| 160 | +fi |
| 161 | + |
| 162 | +# Case 8: workflow has the 3 required triggers |
| 163 | +if [ -f "$WORKFLOW" ]; then |
| 164 | + if /data/data/com.termux/files/usr/bin/python3 -c " |
| 165 | +import yaml |
| 166 | +class L(yaml.SafeLoader): pass |
| 167 | +def on_constructor(loader, node): |
| 168 | + return loader.construct_scalar(node) |
| 169 | +L.add_constructor('tag:yaml.org,2002:bool', on_constructor) |
| 170 | +d = yaml.load(open('$WORKFLOW'), Loader=L) |
| 171 | +triggers = list(d['on'].keys()) |
| 172 | +required = ['workflow_run', 'schedule', 'workflow_dispatch'] |
| 173 | +missing = [t for t in required if t not in triggers] |
| 174 | +assert not missing, f'missing triggers: {missing}' |
| 175 | +print('all 3 triggers present') |
| 176 | +" 2>/dev/null; then |
| 177 | + PASS=$((PASS+1)) |
| 178 | + printf ' PASS: %s\n' "workflow has 3 required triggers" |
| 179 | + else |
| 180 | + FAIL=$((FAIL+1)) |
| 181 | + printf ' FAIL: %s\n' "workflow has 3 required triggers" |
| 182 | + fi |
| 183 | +else |
| 184 | + FAIL=$((FAIL+1)) |
| 185 | + printf ' FAIL: %s\n' "workflow file not found at $WORKFLOW" |
| 186 | +fi |
| 187 | + |
| 188 | +# Case 9: --no-llm is the default — the executor runs |
| 189 | +# successfully without ENFORCEMENT_USE_LLM=1 |
| 190 | +setup_repo |
| 191 | +check "--no-llm is the default" pass \ |
| 192 | + "ENFORCEMENT_SKIP_IF_RECENT=0 '$EXECUTOR' 2>&1 | grep -q 'use_llm=0'" |
| 193 | +cleanup_repo |
| 194 | + |
| 195 | +# Case 10: idempotency state file is created/updated |
| 196 | +setup_repo |
| 197 | +rm -f reports/session-drift.log |
| 198 | +ENFORCEMENT_SKIP_IF_RECENT=0 "$EXECUTOR" >/dev/null 2>&1 |
| 199 | +check "idempotency state file created" pass \ |
| 200 | + "[ -f reports/session-drift.log ] && grep -q 'executor starting' reports/session-drift.log" |
| 201 | +cleanup_repo |
| 202 | + |
| 203 | +printf '\nResult: %d PASS, %d FAIL\n' "$PASS" "$FAIL" |
| 204 | +[ "$FAIL" -eq 0 ] |
0 commit comments