-
Notifications
You must be signed in to change notification settings - Fork 9
refactor(ci): make docker image smoke tests table-driven #1111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a79f18a
refactor(ci): make docker image smoke tests table-driven
sami-alajrami 4e172de
fix(ci): report a single aggregate smoke test attestation
sami-alajrami da22766
restore attestation name to smoke-test
sami-alajrami 19caa20
chore: Apply suggestion from @mbevc1
mbevc1 5a852c2
chore: Apply suggestion from @mbevc1
mbevc1 d630417
fix(ci): harden smoke test reporting against silent failures
sami-alajrami ab99168
fix(ci): don't let a Snyk failure mask smoke test results
sami-alajrami abfc030
fix(ci): stop a partially-executed smoke test run reporting compliant
sami-alajrami 757f900
Merge branch 'main' into docker-smoke-test-refactor
sami-alajrami ddd7df3
fix(ci): restore api-token for attest-artifact-dir smoke test
sami-alajrami 727e0a6
cleanup unnecessary long comment
sami-alajrami 31230af
fix(ci): assert the dry-run banner instead of absence of an error
sami-alajrami 14e7b80
remove redundant test case and cleanup comments
sami-alajrami File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ TODO.md | |
| merkely.yaml | ||
| kosli.yaml | ||
| pipe.json | ||
| smoke-test-results.json | ||
| dist/ | ||
| coverage.out | ||
| cover.out | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| #!/bin/bash | ||
| # Runs smoke tests against the built docker image and records each case's | ||
| # outcome to $RESULTS_FILE for the CI workflow to report as a Kosli | ||
| # attestation. | ||
| # | ||
| # Usage: IMAGE=... TAG=... RESULTS_FILE=... ./scripts/docker-smoke-tests.sh | ||
| set -uo pipefail | ||
|
|
||
| IMAGE="${IMAGE:?IMAGE is required}" | ||
| TAG="${TAG:?TAG is required}" | ||
| RESULTS_FILE="${RESULTS_FILE:?RESULTS_FILE is required}" | ||
|
|
||
| REPO_ROOT="${GITHUB_WORKSPACE:-$(git rev-parse --show-toplevel)}" | ||
| if [ -z "$REPO_ROOT" ]; then | ||
| echo "could not determine repo root" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| EXIT_CODE=0 | ||
| RESULTS="[]" | ||
|
|
||
| # Updates the named entry's outcome in $RESULTS and flushes to $RESULTS_FILE. | ||
| write_result() { | ||
| local name="$1" | ||
| local outcome="$2" | ||
|
|
||
| RESULTS="$(jq --arg name "$name" --arg outcome "$outcome" \ | ||
| 'map(if .name == $name then .outcome = $outcome else . end)' <<< "$RESULTS")" \ | ||
| || { echo "jq failed" >&2; exit 1; } | ||
|
sami-alajrami marked this conversation as resolved.
|
||
| printf '%s\n' "$RESULTS" > "$RESULTS_FILE" || { echo "failed to write $RESULTS_FILE" >&2; exit 1; } | ||
| } | ||
|
|
||
| # Runs a smoke test case and records its outcome, keyed by name. The CI | ||
| # workflow reports a single aggregate attestation, compliant only if every | ||
| # recorded case succeeded, with the results file attached. Every case is | ||
| # seeded as "not-run" before any case executes (see below), so a run that | ||
| # aborts part-way leaves a results file that reads as incomplete rather than | ||
| # as a clean pass. | ||
| # Usage: run_case <case-name> <test-function> | ||
| run_case() { | ||
| local name="$1" | ||
| local test_fn="$2" | ||
|
|
||
| echo "::group::Smoke test: ${name}" | ||
| local outcome="success" | ||
| if ! "$test_fn"; then | ||
| outcome="failure" | ||
| EXIT_CODE=1 | ||
| fi | ||
| echo "::endgroup::" | ||
| echo "Smoke test ${name}: ${outcome}" | ||
|
|
||
| write_result "$name" "$outcome" | ||
| } | ||
|
|
||
| # --- Smoke test cases ------------------------------------------------- | ||
| # Add a new smoke test by writing a test_* function below and adding one | ||
| # entry to the CASES array further down — no CI workflow changes needed. | ||
|
|
||
| test_attest_artifact_dir() { | ||
| local commit_sha | ||
| commit_sha="$(git -C "$REPO_ROOT" rev-parse HEAD)" || return 1 | ||
|
|
||
| #--dry-run turns any error into a warning plus exit 0, | ||
| # so the exit code alone can't be trusted here. | ||
|
sami-alajrami marked this conversation as resolved.
|
||
| local output | ||
| output="$(docker run --rm \ | ||
| -v "${REPO_ROOT}":/workspace:ro \ | ||
| -w /workspace \ | ||
| -e KOSLI_API_TOKEN=any-token-will-do \ | ||
| -e KOSLI_ORG=test-org \ | ||
| "${IMAGE}:${TAG}" \ | ||
| attest artifact /workspace/internal/utils \ | ||
| --artifact-type dir \ | ||
| --flow test-flow \ | ||
| --trail test-trail \ | ||
| --name test-artifact \ | ||
| --build-url https://example.com/build/1 \ | ||
| --commit-url "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/kosli-dev/cli/commit/${commit_sha}" \ | ||
| --repo-root /workspace \ | ||
| --dry-run \ | ||
| --debug 2>&1)" | ||
| local status=$? | ||
| echo "$output" | ||
|
sami-alajrami marked this conversation as resolved.
sami-alajrami marked this conversation as resolved.
|
||
|
|
||
| # The dry-run banner is only printed once the dir is fingerprinted, git info | ||
| # resolved and the payload built (requests.go:254), so asserting it is | ||
| # present proves this case did its work — and fails closed if the command | ||
| # dies earlier or the message is reworded. | ||
|
sami-alajrami marked this conversation as resolved.
|
||
| grep -q "THIS IS A DRY-RUN" <<< "$output" || return 1 | ||
| [ "$status" -eq 0 ] | ||
| } | ||
|
sami-alajrami marked this conversation as resolved.
|
||
|
|
||
| # --- Run all cases ------------------------------------------------------ | ||
| # Add a case by adding one entry here alongside its test_* function above. | ||
|
|
||
| CASES=( | ||
| "attest-artifact-dir:test_attest_artifact_dir" | ||
| ) | ||
|
sami-alajrami marked this conversation as resolved.
|
||
|
|
||
| # Seed every case as not-run and flush before running any of them, so an | ||
| # abort part-way through (crash, timeout, hung docker run) leaves a results | ||
| # file that visibly distinguishes "didn't run" from "passed". | ||
| for entry in "${CASES[@]}"; do | ||
| RESULTS="$(jq --arg name "${entry%%:*}" '. + [{name: $name, outcome: "not-run"}]' <<< "$RESULTS")" \ | ||
| || { echo "jq failed" >&2; exit 1; } | ||
| done | ||
| printf '%s\n' "$RESULTS" > "$RESULTS_FILE" || { echo "failed to write $RESULTS_FILE" >&2; exit 1; } | ||
|
|
||
| for entry in "${CASES[@]}"; do | ||
| run_case "${entry%%:*}" "${entry##*:}" | ||
| done | ||
|
|
||
| exit $EXIT_CODE | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.