Cloud CLI argument contracts
Cloud and cluster CLIs are external programs with their own argument, authentication and output contracts. Select an explicit profile, region, account, Kubernetes context and namespace as applicable. A profile name alone is not proof of account identity; verify the actual identity and permissions before authorized operations. Use least privilege and consider cost, quotas and API limits. The mock values here are deliberately synthetic; never substitute an actual account or cluster during this lab.
Build arguments as data
Use a Bash array for a fixed command and its options. Quote "${args[@]}" to preserve element boundaries. Use jq --arg to serialize strings to JSON; JSON is accepted by Kubernetes for resource manifests, while arbitrary YAML requires a structured serializer rather than interpolating values into indentation. Validate response structure and status before consuming data. Do not use eval or concatenate untrusted CLI source. The AWS and kubectl mocks below are executables created in the private exercise directory and called by explicit paths. They only recognize the shown contracts; they do not implement real CLIs. Dedicated AWS/cloud and Kubernetes courses are appropriate next steps for identity, permissions and service semantics. The mock response must be exactly one JSON object with the required field and type. Check creation and permissions before running the mock. A successful last JSON value alone is insufficient.
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.
Verify explicit mock AWS arguments
mkdir mock || exit 1cat > mock/aws <<'MOCK' || exit 1#!/bin/bashexpected=(--profile lab --region eu-west-1 --output json sts get-caller-identity)[[ $# == ${#expected[@]} ]] || exit 2for x in "${expected[@]}"; do [[ $1 == "$x" ]] || exit 2; shift; doneprintf '{"Account":"000000000000"}\n'MOCKchmod 700 mock/aws || exit 1args=(--profile lab --region eu-west-1 --output json sts get-caller-identity)result=$(./mock/aws "${args[@]}") || exit 1jq -es 'length == 1 and (.[0] | type == "object" and .Account == "000000000000")' <<< "$result"Expected stdout:
trueThe mock validates every argument and the caller checks the synthetic account. No AWS binary is invoked.
Pin a mock Kubernetes context
mkdir mock || exit 1cat > mock/kubectl <<'MOCK' || exit 1#!/bin/bash[[ $# == 8 && $1 == --context && $2 == lab && $3 == --namespace && $4 == demo && $5 == get && $6 == pods && $7 == -o && $8 == json ]] || exit 2printf '{"items":[]}\n'MOCKchmod 700 mock/kubectl || exit 1args=(--context lab --namespace demo get pods -o json)json=$(./mock/kubectl "${args[@]}") || exit 1jq -es 'length == 1 and (.[0] | type == "object" and (.items | type == "array"))' <<< "$json"Expected stdout:
trueContext and namespace are explicit, and the mock response is checked before further use.
Serialize a label safely
label='daily "report"'jq -cn --arg label "$label" '{metadata:{annotations:{description:$label}}}' > plan.json || exit 1jq -r '.metadata.annotations.description' plan.jsonExpected stdout:
daily "report"jq escapes the value as a string; this is a JSON fragment, not a complete Kubernetes manifest.
Common mistakes
Do not fall back to a real executable if a mock is missing. Do not assume a zero exit status proves the selected account or response schema.
Practice
Change the mock namespace to an unsupported value and verify rejection. Add a name with quotes to the JSON example and inspect the parsed value.
Next: Dry-run and plan operations