Mock external CLI testing
A mock external command is a controlled executable that replaces a dependency during a test. It can verify the exact argument vector, return known output and emulate a selected failure. A mock is not a container emulator or a cloud service. Use explicit paths to reviewed fixtures in a private workspace and fail if they are absent. Never fall back to a real cloud binary. Do not rely on mocks for actual API schemas, authorization or runtime semantics.
Test the wrapper contract
Assert exit status, stdout, stderr and file effects separately. Use NUL-delimited argument logs if exact arguments must be compared (including empty values and embedded newlines); newline-delimited logs are ambiguous. Validate output before publishing it. Test the missing dependency, unexpected output and dependency nonzero paths. A dependency returning zero with invalid JSON must not be reported as success. For production use approved integration environments, least privilege and resource budgets; no real Docker socket, cluster or account is used here. Dedicated Docker, Kubernetes and cloud tracks cover provider-specific workflows. Check the full recorded argument vector, not only selected positions. Validate exactly one object before publishing mock data. The negative drills check the specific expected status, not merely any failure.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. jq and GNU coreutils are external dependencies for selected examples. Use only the explicit local mock paths; do not use real accounts, clusters or sockets.
Record exact Docker mock arguments
mkdir mock || exit 1cat > mock/docker <<'MOCK' || exit 1#!/bin/bashprintf '%s\0' "$@" > argv.bin || exit 1printf 'mock inventory\n'MOCKchmod 700 mock/docker || exit 1args=(ps --filter 'label=team=daily report' --format '{{.Names}}')./mock/docker "${args[@]}" || exit 1mapfile -d '' -t observed < argv.bin || exit 1[[ ${#observed[@]} == ${#args[@]} ]] || exit 1for i in "${!args[@]}"; do [[ ${observed[i]} == "${args[i]}" ]] || exit 1doneprintf 'arguments verified\n' Expected stdout:
mock inventoryarguments verifiedThe NUL log preserves exact arguments; no actual Docker daemon is involved.
Fail closed on missing mock
inventory() { [[ -x ./mock/docker ]] || { printf 'mock missing\n' >&2; return 127; } ./mock/docker ps}if inventory; then exit 1; else rc=$?; fi[[ $rc == 127 ]] || exit 1printf 'status=%s\n' "$rc"Expected stdout:
status=127Expected stderr:
mock missingThe caller never falls back to docker on PATH.
Reject malformed mock output
mkdir mock || exit 1cat > mock/aws <<'MOCK' || exit 1#!/bin/bashprintf 'not JSON\n'MOCKchmod 700 mock/aws || exit 1inventory() { local json json=$(./mock/aws ec2 describe-instances) || return "$?" jq -es 'length == 1 and (.[0] | type == "object" and (.Reservations|type == "array"))' <<< "$json" >/dev/null 2>&1 || return 2 printf '%s\n' "$json"}if inventory > result.json; then exit 1; else rc=$?; fi[[ $rc == 2 ]] || exit 1printf 'invalid=%s\n' "$rc" || exit 1[[ -f result.json && ! -s result.json ]] || exit 1Expected stdout:
invalid=2The mock succeeds at execution but the wrapper rejects invalid data. The demonstration result file remains empty.
Common mistakes
Never replace a system CLI or modify global PATH to install a mock. A passing mock test is evidence only for the wrapper contract.
Practice
Modify the mock to return invalid JSON with status zero, then return status 9. Confirm neither is treated as a valid inventory.
Next: Production reliability and performance