CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / dx / clean_user_cleanup_test.sh
| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # Host-only orchestration tests. All privilege, network and GPU-entry commands |
| 4 | # are mocked; no real user account, sudo, network request or device is used. |
| 5 | set -euo pipefail |
| 6 | root=$(cd "$(dirname "$0")/../.." && pwd) |
| 7 | script=${1:-$root/tools/runtime/clean-user-hello.sh} |
| 8 | work=$(mktemp -d) |
| 9 | trap 'rm -rf "$work"' EXIT |
| 10 | mkdir "$work/bin" |
| 11 | cat > "$work/bin/sudo" <<'MOCK' |
| 12 | #!/usr/bin/env bash |
| 13 | set -eu |
| 14 | printf '%s\n' "$*" >> "$MOCK_CALLS" |
| 15 | case "$1" in |
| 16 | useradd) exit "$MOCK_USERADD_RC" ;; |
| 17 | -u) exit 0 ;; # Never execute the requested login shell. |
| 18 | test) exit 1 ;; # No real trace was produced. |
| 19 | pkill|userdel|rm) exit 0 ;; # Record only; never execute. |
| 20 | *) echo UNEXPECTED_PRIVILEGE >> "$MOCK_CALLS"; echo "unexpected privileged command: $1" >&2; exit 99 ;; |
| 21 | esac |
| 22 | MOCK |
| 23 | cat > "$work/bin/curl" <<'MOCK' |
| 24 | #!/usr/bin/env bash |
| 25 | echo curl >> "$MOCK_CALLS" |
| 26 | exit 7 # Stop after account creation, before README/GPU execution. |
| 27 | MOCK |
| 28 | cat > "$work/bin/flock" <<'MOCK' |
| 29 | #!/usr/bin/env bash |
| 30 | echo UNEXPECTED_GPU_ENTRY >> "$MOCK_CALLS" |
| 31 | exit 99 |
| 32 | MOCK |
| 33 | cat > "$work/bin/getent" <<'MOCK' |
| 34 | #!/usr/bin/env bash |
| 35 | exit 2 # The mock account is absent after successful mock deletion. |
| 36 | MOCK |
| 37 | cat > "$work/bin/date" <<'MOCK' |
| 38 | #!/usr/bin/env bash |
| 39 | printf '%s\n' 010203 # Make clock-based names collide deterministically. |
| 40 | MOCK |
| 41 | real_mktemp=$(command -v mktemp) |
| 42 | cat > "$work/bin/mktemp" <<'MOCK' |
| 43 | #!/usr/bin/env bash |
| 44 | if [ "${MOCK_MKTEMP_FAIL:-0}" = 1 ]; then exit 1; fi |
| 45 | exec "$REAL_MKTEMP" "$@" |
| 46 | MOCK |
| 47 | chmod +x "$work/bin/"* |
| 48 | pass=0; fail=0 |
| 49 | check() { |
| 50 | local name=$1; shift |
| 51 | if "$@"; then echo "PASS $name"; pass=$((pass + 1)) |
| 52 | else echo "FAIL $name"; fail=$((fail + 1)); fi |
| 53 | } |
| 54 | no_account_cleanup() { ! grep -Eq '^(pkill|userdel) ' "$1"; } |
| 55 | no_privilege() { [ ! -s "$1" ]; } |
| 56 | run_case() { |
| 57 | local name=$1 add_rc=$2 temp_fail=${3:-0} |
| 58 | mkdir -p "$work/$name/tmp" |
| 59 | : > "$work/$name/calls" |
| 60 | set +e |
| 61 | env PATH="$work/bin:/usr/bin:/bin" \ |
| 62 | REAL_MKTEMP="$real_mktemp" MOCK_CALLS="$work/$name/calls" \ |
| 63 | MOCK_USERADD_RC="$add_rc" MOCK_MKTEMP_FAIL="$temp_fail" \ |
| 64 | TMPDIR="$work/$name/tmp" CUDAPP_BOARD= \ |
| 65 | bash "$script" main "$work/$name/run.log" > "$work/$name/output" 2>&1 |
| 66 | rc=$? |
| 67 | set -e |
| 68 | check "$name-exit-3" test "$rc" -eq 3 |
| 69 | check "$name-never-enters-gpu" test "$(grep -c UNEXPECTED_GPU_ENTRY "$work/$name/calls" || true)" -eq 0 |
| 70 | check "$name-only-expected-privilege" test "$(grep -c UNEXPECTED_PRIVILEGE "$work/$name/calls" || true)" -eq 0 |
| 71 | } |
| 72 | |
| 73 | # useradd exit 9 means the candidate account already exists. Its processes and |
| 74 | # home belong to someone else; the cleanup must never issue deletion commands. |
| 75 | run_case collision 9 |
| 76 | check existing-account-untouched no_account_cleanup "$work/collision/calls" |
| 77 | run_case failure 1 |
| 78 | check failed-creation-untouched no_account_cleanup "$work/failure/calls" |
| 79 | |
| 80 | # Successful creation followed by a network/setup failure must still remove |
| 81 | # precisely the owned temporary account, not leak it or use a different name. |
| 82 | run_case created 0 |
| 83 | created=$(sed -n 's/^useradd -m -s \/bin\/bash //p' "$work/created/calls") |
| 84 | check created-name-nonempty test -n "$created" |
| 85 | check owned-process-cleanup grep -Fxq "pkill -u $created" "$work/created/calls" |
| 86 | check owned-account-cleanup grep -Fxq "userdel -r $created" "$work/created/calls" |
| 87 | check one-owned-account-deletion test "$(grep -c '^userdel ' "$work/created/calls" || true)" -eq 1 |
| 88 | |
| 89 | # Even with the same UTC clock value, simultaneous/repeated candidates differ. |
| 90 | run_case created_again 0 |
| 91 | again=$(sed -n 's/^useradd -m -s \/bin\/bash //p' "$work/created_again/calls") |
| 92 | check distinct-candidate-names test "$created" != "$again" |
| 93 | |
| 94 | # A missing temporary directory must abort before creating any account. |
| 95 | run_case temp_failure 0 1 |
| 96 | check temp-failure-no-privilege no_privilege "$work/temp_failure/calls" |
| 97 | |
| 98 | echo "clean-user cleanup tests: $pass passed, $fail failed (mocked commands only)" |
| 99 | [ "$fail" -eq 0 ] |