Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests-extension/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ lint: $(GOLANGCI_LINT) #HELP Run golangci linter.
fix-lint: $(GOLANGCI_LINT) #HELP Fix lint issues
$(GOLANGCI_LINT) run --fix

.PHONY: verify-images-json
verify-images-json: #HELP Verify that 'images' subcommand outputs valid JSON (no log pollution)
@./hack/verify-images-json.sh $(TOOLS_BIN_DIR)/olmv0-tests-ext

# Bindata generation
.PHONY: bindata
bindata: $(GO_BINDATA)
Expand Down
74 changes: 74 additions & 0 deletions tests-extension/hack/verify-images-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash

# verify-images-json.sh
#
# Verifies that the 'images' subcommand outputs valid JSON without log pollution.
#
# Usage:
# ./hack/verify-images-json.sh [path-to-binary]
#
# Example:
# ./hack/verify-images-json.sh ./bin/olmv0-tests-ext

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

# Default binary path
BINARY="${1:-${PROJECT_ROOT}/bin/olmv0-tests-ext}"

echo "Verifying olmv0-tests-ext images output is valid JSON..."

# Check if binary exists
if [[ ! -f "${BINARY}" ]]; then
echo -e "ERROR: Binary not found at: ${BINARY}"
echo "Please run 'make build' first."
exit 1
fi

# Run the images command and capture all output (stdout + stderr)
output=$("${BINARY}" images 2>&1)

# Create temporary directory for Go validation program
tmpdir=$(mktemp -d)
trap 'rm -rf "${tmpdir}"' EXIT

# Write Go validation program to temporary file
cat > "${tmpdir}/validate.go" <<'GO_CODE'
package main

import (
"encoding/json"
"io"
"os"
)

func main() {
data, err := io.ReadAll(os.Stdin)
if err != nil {
os.Exit(1)
}

if !json.Valid(data) {
os.Exit(1)
}
}
GO_CODE

# Validate JSON using Go's json.Valid function
# This matches exactly what binary.go:548 does (json.Unmarshal)
if ! echo "${output}" | go run "${tmpdir}/validate.go" 2>/dev/null; then
echo -e "ERROR: 'olmv0-tests-ext images' output is not valid JSON!"
echo "This usually means log statements are polluting the JSON output."
echo ""
echo "Output was:"
echo "----------------------------------------"
echo "${output}"
echo "----------------------------------------"
echo ""
exit 1
fi

echo -e "olmv0-tests-ext images output is valid JSON"
exit 0