Skip to content
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

[ACM-14841] Added unit-test job for konflux #509

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
97 changes: 97 additions & 0 deletions .tekton/discovery-operator-main-unit-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
kind: Pipeline
apiVersion: tekton.dev/v1
metadata:
name: discovery-operator-main-unit-test
spec:
params:
- description: 'Snapshot of the application'
name: SNAPSHOT
default: '{"components": [{"name":"test-app", "containerImage": "quay.io/example/repo:latest"}]}'
type: string
- description: 'Namespace where the application is running'
name: NAMESPACE
default: "default"
type: string
- description: 'Expected output'
name: EXPECTED_OUTPUT
default: ""
type: string
tasks:
- name: unit-test
params:
- name: SNAPSHOT
value: $(params.SNAPSHOT)
- name: NAMESPACE
value: $(params.NAMESPACE)
- name: EXPECTED_OUTPUT
value: $(params.EXPECTED_OUTPUT)
taskSpec:
params:
- name: SNAPSHOT
- name: NAMESPACE
- name: EXPECTED_OUTPUT
results:
- name: TEST_OUTPUT
steps:
- name: run-tests
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 podman

# 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 PATH=$PATH:/usr/local/bin:/usr/local/go/bin:$HOME/go/bin
export GOPATH=$HOME/go

go install github.com/onsi/ginkgo/ginkgo@latest
go install gotest.tools/gotestsum@latest

# Set Vars
TARGET_COMPONENT_NAME="discovery-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)
Loading