Skip to content

Commit

Permalink
Merge branch 'release-9.9' into appstudio-multiclusterhub-operator
Browse files Browse the repository at this point in the history
Signed-off-by: dislbenn <[email protected]>
  • Loading branch information
dislbenn committed Nov 13, 2024
2 parents 8bde6fb + d6aec08 commit 0f35896
Showing 1 changed file with 66 additions and 62 deletions.
128 changes: 66 additions & 62 deletions .tekton/multiclusterhub-operator-unit-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ spec:
name: EXPECTED_OUTPUT
default: ""
type: string
workspaces:
- name: shared-workspace
tasks:
- name: unit-test
description: Run unit tests, and read results
Expand All @@ -28,71 +30,73 @@ spec:
value: $(params.EXPECTED_OUTPUT)
retries: 3
taskSpec:
workspaces:
- name: shared-workspace
params:
- name: SNAPSHOT
- name: NAMESPACE
- name: EXPECTED_OUTPUT
results:
- name: TEST_OUTPUT
description: Test output
steps:
- image: registry.redhat.io/openshift4/ose-cli:latest
env:
- name: SNAPSHOT
value: $(params.SNAPSHOT)
- name: NAMESPACE
value: $(params.NAMESPACE)
- name: EXPECTED_OUTPUT
value: $(params.EXPECTED_OUTPUT)
script: |
# Install Deps
dnf -y install jq git make
# Install golang with a given version
export GOVERSION=1.22.4
curl -O -J https://dl.google.com/go/go$GOVERSION.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go$GOVERSION.linux-amd64.tar.gz
# Runtime Env Config
export GOPATH=$HOME/.go
export PATH="$PATH:$GOPATH/bin"
go install github.com/onsi/ginkgo/ginkgo@latest
go install gotest.tools/gotestsum@latest
# Set Vars
TARGET_COMPONENT_NAME="multiclusterhub-operator"
REPO_URL=$(echo $SNAPSHOT | jq -r --arg name "$TARGET_COMPONENT_NAME" '.components[] | select(.name == $name) | .source.git.url')
REPO_COMMIT=$(echo $SNAPSHOT | jq -r --arg name "$TARGET_COMPONENT_NAME" '.components[] | select(.name == $name) | .source.git.revision')
test_output_file=${PWD}/test_output.json
# Log Vars for Tracibility
echo "REPO_URL: $REPO_URL"
echo "TARGET_COMPONENT_NAME: $TARGET_COMPONENT_NAME"
echo "REPO_COMMIT: $REPO_COMMIT"
echo "SNAPSHOT: $(echo $SNAPSHOT | jq)"
# Clone Repo and checkout at snapshot commit
git clone $REPO_URL $TARGET_COMPONENT_NAME
cd $TARGET_COMPONENT_NAME
git checkout $REPO_COMMIT
results:
- name: TEST_OUTPUT
description: Test output
steps:
- name: install-deps
image: registry.redhat.io/openshift4/ose-cli:latest
script: |
# Install Deps
dnf -y install jq git make
workspaces:
- name: shared-workspace

# Run unit tests with JSON output
make test unit_test_json_output="$test_output_file"
- name: run-tests
image: golang:1.22.4 # Use Go 1.22.4 directly from the Go image
env:
- name: SNAPSHOT
value: $(params.SNAPSHOT)
- name: NAMESPACE
value: $(params.NAMESPACE)
- name: EXPECTED_OUTPUT
value: $(params.EXPECTED_OUTPUT)
script: |
# Runtime Env Config
go install github.com/onsi/ginkgo/ginkgo@latest
go install gotest.tools/gotestsum@latest
# Set Vars
TARGET_COMPONENT_NAME="multiclusterhub-operator"
REPO_URL=$(echo $SNAPSHOT | jq -r --arg name "$TARGET_COMPONENT_NAME" '.components[] | select(.name == $name) | .source.git.url')
REPO_COMMIT=$(echo $SNAPSHOT | jq -r --arg name "$TARGET_COMPONENT_NAME" '.components[] | select(.name == $name) | .source.git.revision')
test_output_file=${PWD}/test_output.json

# Log Vars for Tracibility
echo "REPO_URL: $REPO_URL"
echo "TARGET_COMPONENT_NAME: $TARGET_COMPONENT_NAME"
echo "REPO_COMMIT: $REPO_COMMIT"
echo "SNAPSHOT: $(echo $SNAPSHOT | jq)"

# Clone Repo and checkout at snapshot commit
git clone $REPO_URL $TARGET_COMPONENT_NAME
cd $TARGET_COMPONENT_NAME
git checkout $REPO_COMMIT

# Run unit tests with JSON output
make test unit_test_json_output="$test_output_file"

# Read, process, and write output in accepted format
# Formatting docs: https://redhat-appstudio.github.io/book/ADR/0030-tekton-results-naming-convention.html#test_output-schema-validation
FAILURES=$(cat $test_output_file | jq 'select(.Action == "fail")' | jq -r --slurp 'length')
SUCCESSES=$(cat $test_output_file | jq 'select(.Action == "pass")' | jq -r --slurp 'length')
# Hard-code warnings
WARNINGS=0
RESULT="$(if [[ $FAILURES == 0 ]]; then echo "SUCCESS"; else echo "FAILURE"; fi)"
TIMESTAMP="$(date +%s)"
TEST_OUTPUT=$(jq -r --null-input \
--arg failures $FAILURES \
--arg successes $SUCCESSES \
--arg warnings $WARNINGS \
--arg result "$RESULT" \
--arg timestamp "$TIMESTAMP" \
'{"result": $result, "successes": $successes|tonumber, "failures": $failures|tonumber, "warnings": $warnings|tonumber, "timestamp": $timestamp}' \
)
echo -n "$TEST_OUTPUT" | tee $(results.TEST_OUTPUT.path)

# Read, process, and write output in accepted format
# Formatting docs: https://redhat-appstudio.github.io/book/ADR/0030-tekton-results-naming-convention.html#test_output-schema-validation
FAILURES=$(cat $test_output_file | jq 'select(.Action == "fail")' | jq -r --slurp 'length')
SUCCESSES=$(cat $test_output_file | jq 'select(.Action == "pass")' | jq -r --slurp 'length')
# Hard-code warnings
WARNINGS=0
RESULT="$(if [[ $FAILURES == 0 ]]; then echo "SUCCESS"; else echo "FAILURE"; fi)"
TIMESTAMP="$(date +%s)"
TEST_OUTPUT=$(jq -r --null-input \
--arg failures $FAILURES \
--arg successes $SUCCESSES \
--arg warnings $WARNINGS \
--arg result "$RESULT" \
--arg timestamp "$TIMESTAMP" \
'{"result": $result, "successes": $successes|tonumber, "failures": $failures|tonumber, "warnings": $warnings|tonumber, "timestamp": $timestamp}' \
)
echo -n "$TEST_OUTPUT" | tee $(results.TEST_OUTPUT.path)

0 comments on commit 0f35896

Please sign in to comment.