Repository: redhat-openshift-builds/operator
Pull request #382: Add unit test in a pull request pipeline
Author: login: sayan-biswas
Created: 2025-04-29T18:29:41Z | Merged: 2025-04-30T10:56:43Z

Changes:
- Enable unit test step in the pull request pipeline


Diff:
diff --git a/.tekton/openshift-builds-operator-pull-request.yaml b/.tekton/openshift-builds-operator-pull-request.yaml
index 01a8d5c1d..1e8a22299 100644
--- a/.tekton/openshift-builds-operator-pull-request.yaml
+++ b/.tekton/openshift-builds-operator-pull-request.yaml
@@ -7,7 +7,6 @@ metadata:
     build.appstudio.redhat.com/pull_request_number: '{{pull_request_number}}'
     build.appstudio.redhat.com/target_branch: '{{target_branch}}'
     pipelinesascode.tekton.dev/max-keep-runs: "3"
-    pipelinesascode.tekton.dev/task: "[.tekton/openshift-builds-operator-unit-test.yaml]"
     pipelinesascode.tekton.dev/on-cel-expression: |
       event == "pull_request" &&
       target_branch == "main" &&
@@ -46,6 +45,12 @@ spec:
       - linux/arm64
   - name: prefetch-input
     value: '{"packages": [{"type": "gomod"}]}'
+  - name: run-unit-test
+    value: "true"
+  - name: unit-test-revision
+    value: "6392835dbcdc367e616d448af25f37cce1e403b9"
+  - name: unit-test-command
+    value: "make test"
   pipelineRef:
     resolver: git
     params:
diff --git a/.tekton/openshift-builds-operator-push.yaml b/.tekton/openshift-builds-operator-push.yaml
index a969121ef..5c9dd0029 100644
--- a/.tekton/openshift-builds-operator-push.yaml
+++ b/.tekton/openshift-builds-operator-push.yaml
@@ -6,7 +6,6 @@ metadata:
     build.appstudio.redhat.com/commit_sha: '{{revision}}'
     build.appstudio.redhat.com/target_branch: '{{target_branch}}'
     pipelinesascode.tekton.dev/max-keep-runs: "3"
-    pipelinesascode.tekton.dev/task: "[.tekton/openshift-builds-operator-unit-test.yaml]"
     pipelinesascode.tekton.dev/on-cel-expression: |
       event == "push" &&
       target_branch == "main" &&
diff --git a/.tekton/openshift-builds-operator-unit-test.yaml b/.tekton/openshift-builds-operator-unit-test.yaml
deleted file mode 100644
index 4f0300e9c..000000000
--- a/.tekton/openshift-builds-operator-unit-test.yaml
+++ /dev/null
@@ -1,68 +0,0 @@
-apiVersion: tekton.dev/v1
-kind: Task
-metadata:
-  name: unit-test
-spec:
-  description: Runs unit test for OpenShift Builds Operator.
-  params:
-  - default: -race -cover -v
-    description: Flags to use for the test command
-    name: GO_TEST_FLAGS
-  - default: linux
-    description: Operating system to use for testing
-    name: GOOS
-  - default: amd64
-    description: System architecture to use for testing
-    name: GOARCH
-  - default: auto
-    description: Go module support
-    name: GO111MODULE
-  - default: ""
-    description: Go caching directory path
-    name: GOCACHE
-  - default: ""
-    description: Go mod caching directory path
-    name: GOMODCACHE
-  - description: The Trusted Artifact URI pointing to the artifact with the application source code.
-    name: SOURCE_ARTIFACT
-    type: string
-  stepTemplate:
-    volumeMounts:
-      - mountPath: /var/workdir
-        name: workdir
-        readOnly: false
-    # run as root to be able to do write operations in the run-test task
-    securityContext:
-      runAsUser: 0
-  steps:
-  # step to fetch the Trusted Artifact and make it available to the next steps.
-  - name: use-trusted-artifact
-    image: quay.io/redhat-appstudio/build-trusted-artifacts:latest@sha256:52f1391e6f1c472fd10bb838f64fae2ed3320c636f536014978a5ddbdfc6b3af
-    args:
-      - use
-      - $(params.SOURCE_ARTIFACT)=/var/workdir/source
-  - name: run-test
-    image: registry.access.redhat.com/ubi9/go-toolset@sha256:703937e152d049e62f5aa8ab274a4253468ab70f7b790d92714b37cf0a140555
-    env:
-    - name: GOOS
-      value: $(params.GOOS)
-    - name: GOARCH
-      value: $(params.GOARCH)
-    - name: GO_TEST_FLAGS
-      value: $(params.GO_TEST_FLAGS)
-    - name: GO111MODULE
-      value: $(params.GO111MODULE)
-    - name: GOCACHE
-      value: $(params.GOCACHE)
-    - name: GOMODCACHE
-      value: $(params.GOMODCACHE)
-    script: |
-      #!/usr/bin/env bash
-      set -eux
-      git config --global --add safe.directory "$(pwd)"
-      make test
-    workingDir: /var/workdir/source
-  volumes:
-    # volume to store a copy of the source code accessible only to this Task.
-    - name: workdir
-      emptyDir: {}
diff --git a/Makefile b/Makefile
index 96fd45a99..559fc4f8b 100644
--- a/Makefile
+++ b/Makefile
@@ -128,7 +128,7 @@ vet: ## Run go vet against code.
 	go vet ./...
 
 .PHONY: test
-test: manifests generate fmt vet envtest ## Run tests.
+test: fmt vet envtest ## Run tests.
 	KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out -v -ginkgo.v
 
 # e2e tests must run against an OpenShift Cluster.


Reviews:
user: login: avinal
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-04-30T10:44:01Z
user: login: avinal
body: One doubt. Looks goods to me.
state: APPROVED
author_association: MEMBER
submitted_at: 2025-04-30T10:44:23Z
user: login: sayan-biswas
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-04-30T10:54:34Z
user: login: avinal
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-04-30T10:56:27Z

Review comments:
diff_hunk: @@ -46,6 +45,12 @@ spec:
       - linux/arm64
   - name: prefetch-input
     value: '{"packages": [{"type": "gomod"}]}'
+  - name: run-unit-test
+    value: "true"
+  - name: unit-test-revision
+    value: "6392835dbcdc367e616d448af25f37cce1e403b9"
path: .tekton/openshift-builds-operator-pull-request.yaml
user: login: avinal
body: This value is a just a placeholder or corresponds to some image/commit?
created_at: 2025-04-30T10:44:01Z
author_association: MEMBER
diff_hunk: @@ -46,6 +45,12 @@ spec:
       - linux/arm64
   - name: prefetch-input
     value: '{"packages": [{"type": "gomod"}]}'
+  - name: run-unit-test
+    value: "true"
+  - name: unit-test-revision
+    value: "6392835dbcdc367e616d448af25f37cce1e403b9"
path: .tekton/openshift-builds-operator-pull-request.yaml
user: login: sayan-biswas
body: This value is an actual commit sha from the release repo (https://github.com/redhat-openshift-builds/release/commit/6392835dbcdc367e616d448af25f37cce1e403b9)

Although the default value in the pipeline is `main` (https://github.com/redhat-openshift-builds/release/blob/6392835dbcdc367e616d448af25f37cce1e403b9/pipelines/konflux-build-multi-platform.yaml#L99),
we still need to explicitly mention a SHA due to the below Conforma error.

```
[Violation] tasks.pinned_task_refs
  ImageRef: quay.io/redhat-user-workloads/rh-openshift-builds-tenant/openshift-builds-operator@sha256:7c86f82bba45abc62408a36798e311c4f6e393cc7670ad085ea315b26090b071
  Reason: Task unit-test is used by pipeline task unit-test via an unpinned reference.
  Title: Pinned Task references
  Description: Ensure that all Tasks in the SLSA Provenance attestation use an immuntable reference to the Task definition. To
  exclude this rule add "tasks.pinned_task_refs:unit-test" to the `exclude` section of the policy configuration.
  Solution: Make sure the build pipeline uses Tasks via pinned references. For example, if the git resolver is used, use a commit
  ID instead of a branch name.
```
created_at: 2025-04-30T10:54:34Z
author_association: CONTRIBUTOR
diff_hunk: @@ -46,6 +45,12 @@ spec:
       - linux/arm64
   - name: prefetch-input
     value: '{"packages": [{"type": "gomod"}]}'
+  - name: run-unit-test
+    value: "true"
+  - name: unit-test-revision
+    value: "6392835dbcdc367e616d448af25f37cce1e403b9"
path: .tekton/openshift-builds-operator-pull-request.yaml
user: login: avinal
body: Ok, TY
created_at: 2025-04-30T10:56:27Z
author_association: MEMBER

PR comments: