Skip to content
Open
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
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ jobs:
- name: Build
if: steps.cache-build.outputs.cache-hit != 'true'
run: make
- name: Run Unit Tests
run: make go-unit-test
- name: Run Integration Tests
run: make test-parallel
- name: Generate code coverage artifacts
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: code-coverage
path: cover.out
path: cover-*.out
- name: Upload code coverage information to codecov.io
if: ${{ !cancelled() }}
uses: codecov/[email protected]
with:
files: cover.out
files: cover.out,cover-go-unit.out
fail_ci_if_error: false
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
Expand Down
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CURRENT_DIR=$(shell pwd)
# Image URL to use all building/pushing image targets
IMG ?= quay.io/argoprojlabs/gitops-promoter:latest
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
Expand Down Expand Up @@ -79,8 +80,12 @@ vet: ## Run go vet against code.
go vet ./...

.PHONY: test
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
test: test-deps ## Run Ginkgo tests using go test, we prefer to use test-parallel but this target is nice because it dose not require ginkgo cli.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -run TestControllersGinkgo ./... -coverprofile cover.out

.PHONY: go-unit-test
go-unit-test: ## Run go unit tests
NO_GINKGO="true" KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover-go-unit.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.
Expand Down Expand Up @@ -211,7 +216,7 @@ GORELEASER ?= $(LOCALBIN)/goreleaser-$(GORELEASER_VERSION)
KUSTOMIZE_VERSION ?= v5.3.0
CONTROLLER_TOOLS_VERSION ?= v0.16.3
ENVTEST_VERSION ?= release-0.19
GOLANGCI_LINT_VERSION ?= v1.62.0
GOLANGCI_LINT_VERSION ?= v1.62.2
MOCKERY_VERSION ?= v2.42.2
NILAWAY_VERSION ?= latest
GINKGO_VERSION=$(shell go list -m all | grep github.com/onsi/ginkgo/v2 | awk '{print $$2}')
Expand Down
5 changes: 4 additions & 1 deletion internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ const (
WebhookReceiverPort = 3333
)

func TestControllers(t *testing.T) {
func TestControllersGinkgo(t *testing.T) {
if os.Getenv("NO_GINKGO") == "true" {
t.Skip("Skipping testing in CI environment")
}
t.Parallel()

RegisterFailHandler(Fail)
Expand Down
117 changes: 117 additions & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package utils

import (
"testing"

promoterv1alpha1 "github.com/argoproj-labs/gitops-promoter/api/v1alpha1"
"github.com/stretchr/testify/assert"
)

func TestTruncateString(t *testing.T) {
t.Parallel() // Enable parallel execution for the top-level test
tests := []struct {
name string
input string
length int
expected string
}{
{"Empty string", "", 5, ""},
{"Short string", "abc", 5, "abc"},
{"Exact length", "abcde", 5, "abcde"},
{"Truncated string", "abcdef", 5, "abcde"},
{"Negative length", "abcdef", -1, ""},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
result := TruncateString(test.input, test.length)
assert.Equal(t, test.expected, result)
})
}
}

func TestUpsertEnvironmentStatus(t *testing.T) {
t.Parallel() // Enable parallel execution for the top-level test
tests := []struct {
name string
initial []promoterv1alpha1.EnvironmentStatus
insert promoterv1alpha1.EnvironmentStatus
expected []promoterv1alpha1.EnvironmentStatus
}{
{
name: "Upsert on empty slice",
initial: []promoterv1alpha1.EnvironmentStatus{},
insert: promoterv1alpha1.EnvironmentStatus{Branch: "main"},
expected: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}},
},
{
name: "Append new element",
initial: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}},
insert: promoterv1alpha1.EnvironmentStatus{Branch: "dev"},
expected: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}, {Branch: "dev"}},
},
{
name: "Edge case with one element",
initial: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}},
insert: promoterv1alpha1.EnvironmentStatus{Branch: "dev"},
expected: []promoterv1alpha1.EnvironmentStatus{{Branch: "main"}, {Branch: "dev"}},
},
{
name: "Update existing element",
initial: []promoterv1alpha1.EnvironmentStatus{{
Branch: "main",
Active: promoterv1alpha1.PromotionStrategyBranchStateStatus{
Dry: promoterv1alpha1.CommitShaState{
Sha: "old",
},
Hydrated: promoterv1alpha1.CommitShaState{
Sha: "old",
},
CommitStatus: promoterv1alpha1.PromotionStrategyCommitStatus{
Sha: "old",
Phase: "pending",
},
},
}},
insert: promoterv1alpha1.EnvironmentStatus{
Branch: "main",
Active: promoterv1alpha1.PromotionStrategyBranchStateStatus{
Dry: promoterv1alpha1.CommitShaState{
Sha: "new",
},
Hydrated: promoterv1alpha1.CommitShaState{
Sha: "new",
},
CommitStatus: promoterv1alpha1.PromotionStrategyCommitStatus{
Sha: "new",
Phase: "success",
},
},
},
expected: []promoterv1alpha1.EnvironmentStatus{{
Branch: "main",
Active: promoterv1alpha1.PromotionStrategyBranchStateStatus{
Dry: promoterv1alpha1.CommitShaState{
Sha: "new",
},
Hydrated: promoterv1alpha1.CommitShaState{
Sha: "new",
},
CommitStatus: promoterv1alpha1.PromotionStrategyCommitStatus{
Sha: "new",
Phase: "success",
},
},
}},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
result := UpsertEnvironmentStatus(test.initial, test.insert)
assert.Equal(t, test.expected, result)
})
}
}
4 changes: 4 additions & 0 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package e2e

import (
"fmt"
"os"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -26,6 +27,9 @@ import (

// Run e2e tests using the Ginkgo runner.
func TestE2E(t *testing.T) {
if os.Getenv("NO_GINKGO") == "true" {
t.Skip("Skipping testing in CI environment")
}
t.Parallel()
RegisterFailHandler(Fail)

Expand Down