From 8923e880eb81354c9a9ba32a6d1a91c8dd44057b Mon Sep 17 00:00:00 2001 From: Ryan Phillips Date: Thu, 19 Dec 2024 13:01:11 -0600 Subject: [PATCH] tekton: add go-unit-tests and golangci lint --- .tekton/instaslice-go-unit-tests.yaml | 112 ++++++++++++++++++++++++++ Makefile | 4 + 2 files changed, 116 insertions(+) create mode 100644 .tekton/instaslice-go-unit-tests.yaml diff --git a/.tekton/instaslice-go-unit-tests.yaml b/.tekton/instaslice-go-unit-tests.yaml new file mode 100644 index 00000000..b7114ab6 --- /dev/null +++ b/.tekton/instaslice-go-unit-tests.yaml @@ -0,0 +1,112 @@ +--- +apiVersion: tekton.dev/v1beta1 +kind: PipelineRun +metadata: + name: go-unit-test-lint + annotations: + pipelinesascode.tekton.dev/task: "[git-clone]" + pipelinesascode.tekton.dev/max-keep-runs: "2" + pipelinesascode.tekton.dev/cancel-in-progress: "true" + pipelinesascode.tekton.dev/on-target-branch: "main" + pipelinesascode.tekton.dev/on-event: "pull_request" + pipelinesascode.tekton.dev/on-path-change: "[***/*.go]" +spec: + params: + - name: repo_url + value: "{{repo_url}}" + - name: revision + value: "{{revision}}" + pipelineSpec: + params: + - name: repo_url + - name: revision + tasks: + - name: go + taskSpec: + workspaces: + - name: source + steps: + - name: fetch-repo + ref: + resolver: http + params: + - name: url + value: https://raw.githubusercontent.com/tektoncd/catalog/main/stepaction/git-clone/0.1/git-clone.yaml + params: + - name: output-path + value: $(workspaces.source.path) + - name: url + value: "$(params.repo_url)" + - name: revision + value: "$(params.revision)" + - name: cache-fetch + ref: + resolver: http + params: + - name: url + value: https://raw.githubusercontent.com/openshift-pipelines/tekton-caches/main/tekton/cache-fetch.yaml + params: + - name: patterns + value: ["**go.mod", "**go.sum"] + - name: source + value: oci://image-registry.openshift-image-registry.svc:5000/$(context.pipelineRun.namespace)/cache-go:{{hash}} + - name: cachePath + value: $(workspaces.source.path)/go-build-cache + - name: workingdir + value: $(workspaces.source.path) + - name: unittest + # we get bumped out when usingh the official image with docker.io + # ratelimit so workaround this. + image: golang:1.22 + env: + - name: GOCACHE + value: $(workspaces.source.path)/go-build-cache/cache + - name: GOMODCACHE + value: $(workspaces.source.path)/go-build-cache/mod + workingDir: $(workspaces.source.path) + script: | + #!/usr/bin/env bash + set -eux + git config --global --add safe.directory $(workspaces.source.path) + make test-unit + + - name: lint + image: golangci/golangci-lint:v1.62.2 + workingDir: $(workspaces.source.path) + env: + - name: GOCACHE + value: $(workspaces.source.path)/go-build-cache/cache + - name: GOMODCACHE + value: $(workspaces.source.path)/go-build-cache/mod + - name: GOLANGCILINT_CACHE + value: $(workspaces.source.path)/go-build-cache/golangci-cache + script: | + #!/usr/bin/env bash + set -eux + make lint + + - name: cache-upload + ref: + resolver: http + params: + - name: url + value: https://raw.githubusercontent.com/openshift-pipelines/tekton-caches/main/tekton/cache-upload.yaml + params: + - name: patterns + value: ["**go.mod", "**go.sum"] + - name: target + value: oci://image-registry.openshift-image-registry.svc:5000/$(context.pipelineRun.namespace)/cache-go:{{hash}} + - name: cachePath + value: $(workspaces.source.path)/go-build-cache + - name: workingdir + value: $(workspaces.source.path) + - name: force-cache-upload + value: "false" + workspaces: + - name: source + workspace: source + workspaces: + - name: source + workspaces: + - name: source + emptyDir: {} diff --git a/Makefile b/Makefile index 69af6dd0..1d022021 100644 --- a/Makefile +++ b/Makefile @@ -138,6 +138,10 @@ vet: ## Run go vet against code. test: manifests generate 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 +.PHONY: test-unit +test-unit: + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out + # Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors. .PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up. test-e2e: