Repository: hashicorp/nomad
File: enos/modules/test_cluster_health/scripts/allocs.sh (Shell)

Code before:
#!/usr/bin/env bash
# Copyright IBM Corp. 2015, 2025
# SPDX-License-Identifier: BUSL-1.1

set -euo pipefail

error_exit() {
    printf 'Error: %s\n' "${1}"

    ALL_ALLOCS=$(nomad alloc status -json)
    mkdir -p /tmp/artifacts
    echo "$ALL_ALLOCS" > /tmp/artifacts/logs/allocs.json
    cat /tmp/allocs.json | jq -r '
        ["ID", "Node", "ClientStatus", "DesiredStatus", "JobID"],
        ["--------", "--------", "------------", "-------------", "---------------"],
        (.[] | [.ID[:8], .NodeID[:8], .ClientStatus, .DesiredStatus, .JobID])
        | @tsv' | column -ts $'\t'

    echo "full allocation status for debugging written to: /tmp/allocs.json"
    exit 1
}

MAX_WAIT_TIME=120
POLL_INTERVAL=2

elapsed_time=0

# Quality: nomad_allocs_status: A GET call to /v1/allocs returns the correct number of allocations and they are all running

running_allocs=
allocs_length=
last_error=

checkAllocsCount() {
    local allocs
    allocs=$(nomad alloc status -json) || {
        last_error="Failed to check alloc status"
        return 1
    }

    running_allocs=$(echo "$allocs" | jq '[.[] | select(.ClientStatus == "running")]')
    allocs_length=$(echo "$running_allocs" | jq 'length') \
        || error_exit "Invalid alloc status -json output"


Review comment (bug):
some inconsistencies here and in its twin over yonder.

```suggestion
    mkdir -p /tmp/artifacts
    echo "$ALL_ALLOCS" > /tmp/artifacts/allocs.json
    cat /tmp/artifacts/allocs.json | jq -r '
        ["ID", "Node", "ClientStatus", "DesiredStatus", "JobID"],
        ["--------", "--------", "------------", "-------------", "---------------"],
        (.[] | [.ID[:8], .NodeID[:8], .ClientStatus, .DesiredStatus, .JobID])
        | @tsv' | column -ts $'\t'

    echo "full allocation status for debugging written to: /tmp/artifacts/allocs.json"
```

I might set a variable to avoid the possibility of subtle typos. 🤷 

Code after:
#!/usr/bin/env bash
# Copyright IBM Corp. 2015, 2025
# SPDX-License-Identifier: BUSL-1.1

set -euo pipefail

error_exit() {
    printf 'Error: %s\n' "${1}"
    ALL_ALLOCS=$(nomad alloc status -json)
    mkdir -p /tmp/artifacts
    OUT="/tmp/artifacts/allocs.json"
    echo "$ALL_ALLOCS" > "$OUT"

    cat "$OUT" | jq -r '
        ["ID", "Node", "ClientStatus", "DesiredStatus", "JobID"],
        ["--------", "--------", "------------", "-------------", "---------------"],
        (.[] | [.ID[:8], .NodeID[:8], .ClientStatus, .DesiredStatus, .JobID])
        | @tsv' | column -ts $'\t'

    echo "full allocation status for debugging written to: $OUT"
    exit 1
}

MAX_WAIT_TIME=120
POLL_INTERVAL=2

elapsed_time=0

# Quality: nomad_allocs_status: A GET call to /v1/allocs returns the correct number of allocations and they are all running

running_allocs=
allocs_length=
last_error=

checkAllocsCount() {
    local allocs
    allocs=$(nomad alloc status -json) || {
        last_error="Failed to check alloc status"
        return 1
    }

    running_allocs=$(echo "$allocs" | jq '[.[] | select(.ClientStatus == "running")]')
    allocs_length=$(echo "$running_allocs" | jq 'length') \
        || error_exit "Invalid alloc status -json output"