en un clic
writing-bash-functions
// Use when adding or modifying bash utility functions in test/utils.sh. Covers naming conventions, function structure, BATS tests, mock patterns for external tools, TEST_OUTPUT format, and shellcheck compliance.
// Use when adding or modifying bash utility functions in test/utils.sh. Covers naming conventions, function structure, BATS tests, mock patterns for external tools, TEST_OUTPUT format, and shellcheck compliance.
| name | writing-bash-functions |
| description | Use when adding or modifying bash utility functions in test/utils.sh. Covers naming conventions, function structure, BATS tests, mock patterns for external tools, TEST_OUTPUT format, and shellcheck compliance. |
Bash utility functions in test/utils.sh support Tekton task steps by handling image parsing, scanning output parsing, OPM operations, Pyxis API calls, and Tekton result formatting. File contains 100+ functions across 2000+ lines.
| Item | Location/Convention |
|---|---|
| Main functions file | test/utils.sh |
| BATS test files | unittests_bash/test_utils.bats, unittests_bash/test_cosign.bats |
| Test data fixtures | unittests_bash/data/*.json |
| Integration tests | test/conftest.sh (separate BATS file) |
| Prefix | Purpose | Example |
|---|---|---|
get_ | Retrieve/extract a value | get_image_labels(), get_base_image() |
extract_ | Parse complex data | extract_unique_bundles_from_catalog() |
parse_ | Transform format | parse_image_url(), parse_test_output() |
handle_ | Error handling | handle_error(), handle_pyxis_response_pages() |
make_ | Construct output | make_result_json() |
validate_ | Check conditions | validate_ocp_version() |
replace_ | Substitute values | replace_image_pullspec() |
function_name() {
# Parameter validation
if [ -z "$1" ]; then
echo "ERROR: parameter required" >&2
exit 2
fi
local result="$1"
# Use local for all variables
# Return via echo -n (no trailing newline for JSON)
echo -n "$result"
}
Exit codes: 0=success, 1=error, 2=parameter error, 3=format error
Three critical output functions consumed by Tekton:
| Function | Purpose | Output format |
|---|---|---|
make_result_json | Format Tekton task result | JSON with name, value, type |
parse_test_output | Convert conftest/sarif JSON to TEST_OUTPUT | Structured JSON with result, timestamp, note, namespace |
handle_error | Trap and report errors | Tekton error result with structured message |
See test/utils.sh for full implementations.
File: unittests_bash/test_{name}.bats
#!/usr/bin/env bats
setup() {
source test/utils.sh
# Mock external tools as bash functions
skopeo() {
if [[ $1 == "inspect" && $2 == "--no-tags" && $3 == "docker://registry/image@digest" ]]; then
echo '{"Name": "image", "Architecture": "amd64"}'
fi
}
export -f skopeo
}
@test "get_image_labels returns labels" {
run get_image_labels "registry/image@digest"
[ "$status" -eq 0 ]
[ "$output" != "" ]
}
Patterns:
@test "description" { ... } — BATS test formatrun function_name args — Execute and capture result[ "$status" -eq 0 ] — Assert exit codetest_json_eq expected_json output_json — Compare JSON (ignores timestamps)export -f mock_function — Export mock function to subshellsSkopeo is mocked in setup() as a bash function with if-elif branches:
skopeo() {
if [[ $1 == "inspect" && $2 == "--no-tags" && $3 == "--raw" && $4 == "docker://image@digest" ]]; then
echo '{"schemaVersion": 2, ...}'
elif [[ $1 == "inspect" && $2 == "--no-tags" && $3 == "docker://image@digest" ]]; then
echo '{"Name": "image", ...}'
fi
}
Add new elif branches for each new image reference you test.
JSON/YAML files in unittests_bash/data/ used as mock API responses:
conftest_failures.json — conftest output with violationsconftest_successes.json — conftest output with no violationssarif_failures.json — SARIF format vulnerability data@test context or as function argumentsRun before pushing:
shellcheck -s bash test/utils.sh
Common violations:
"${var}" not $var| Mistake | Fix |
|---|---|
Missing local keyword | All function-local variables must use local |
Using echo instead of echo -n | Trailing newline breaks JSON piping to jq |
| Not mocking external tools | setup() must override skopeo, opm, cosign as functions |
| Wrong test data path | File unittests_bash/data/conftest_failures.json sourced in test |
| Shellcheck violation | Run shellcheck -s bash test/utils.sh before pushing |
| Not exporting mock function | Use export -f function_name so subshells see the mock |
Use when adding new tools, binaries, or packages to the konflux-test container image. Covers artifacts.lock.yaml (generic binaries), rpms.in.yaml (system packages), multi-architecture support, and hermetic build constraints.
Use when CI checks fail unexpectedly, when preparing code for CI, or when encountering non-obvious build and pipeline behavior. Covers hermetic builds, Tekton pipelines, multi-arch, GitHub Actions checks, and integration test structure.
Use when preparing a pull request for review or before pushing. Checklist of commit conventions, Rego policy tests at 100% coverage, BATS tests, code quality checks, and CI check requirements.
Use when running OPA policy unit tests, BATS bash tests, shellcheck, hadolint, or conftest integration tests locally. Covers test commands, coverage requirements, test data, and prerequisites.
Use when writing, modifying, or reviewing OPA/conftest Rego policies. Covers package naming, rule prefixes (violation_ and warn_), conftest namespaces, violation object structures, imports, and unit test patterns.