From 6509549535ef006868664ed55a7eac6ada1e81d8 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Fri, 16 Feb 2024 01:06:44 +0100 Subject: [PATCH 1/4] code coverage data for all packages for integration tests --- .github/codecov.yaml | 5 ++++- Makefile | 5 +++-- controllers/suite_test.go | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/codecov.yaml b/.github/codecov.yaml index feb13927f..982e9f459 100644 --- a/.github/codecov.yaml +++ b/.github/codecov.yaml @@ -45,6 +45,10 @@ component_management: name: api/v1beta1 (u) paths: - api/v1beta1 + - component_id: api-v1beta2 + name: api/v1beta2 (u) + paths: + - api/v1beta2 - component_id: common name: pkg/common (u) paths: @@ -69,4 +73,3 @@ component_management: name: controllers (i) paths: - controllers - diff --git a/Makefile b/Makefile index 1690d9527..4998ba59f 100644 --- a/Makefile +++ b/Makefile @@ -86,6 +86,7 @@ ENVTEST_K8S_VERSION = 1.22 # Directories containing unit & integration test packages UNIT_DIRS := ./pkg/... ./api/... ./controllers/... INTEGRATION_DIRS := ./controllers... +INTEGRATION_COVER_PKGS := ./pkg/...,./controllers/...,./api/... # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) @@ -289,7 +290,7 @@ test: test-unit test-integration ## Run all tests test-integration: clean-cov generate fmt vet envtest ## Run Integration tests. mkdir -p coverage/integration - KUBEBUILDER_ASSETS="$(shell $(ENVTEST) $(ARCH_PARAM) use $(ENVTEST_K8S_VERSION) -p path)" USE_EXISTING_CLUSTER=true go test $(INTEGRATION_DIRS) -coverprofile $(PROJECT_PATH)/coverage/integration/cover.out -tags integration -ginkgo.v -ginkgo.progress -v -timeout 0 + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) $(ARCH_PARAM) use $(ENVTEST_K8S_VERSION) -p path)" go test $(INTEGRATION_DIRS) -coverpkg=$(INTEGRATION_COVER_PKGS) -coverprofile $(PROJECT_PATH)/coverage/integration/cover.out -tags integration -ginkgo.v -ginkgo.progress -v -timeout 0 ifdef TEST_NAME test-unit: TEST_PATTERN := --run $(TEST_NAME) @@ -415,7 +416,7 @@ install-metallb: kustomize yq ## Installs the metallb load balancer allowing use ./utils/docker-network-ipaddresspool.sh kind $(YQ) | kubectl apply -n metallb-system -f - .PHONY: uninstall-metallb -uninstall-metallb: $(KUSTOMIZE) +uninstall-metallb: $(KUSTOMIZE) $(KUSTOMIZE) build config/metallb | kubectl delete -f - .PHONY: install-olm diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 9f8ac9f30..0f1e99f1a 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -72,6 +72,7 @@ var _ = BeforeSuite(func() { testEnv = &envtest.Environment{ CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, ErrorIfCRDPathMissing: true, + UseExistingCluster: &[]bool{true}[0], } cfg, err := testEnv.Start() From 94be38b4820c290bf13ac273977f4b5c271d57fe Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Fri, 16 Feb 2024 09:44:45 +0100 Subject: [PATCH 2/4] integration tests cover many paths --- .github/codecov.yaml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/.github/codecov.yaml b/.github/codecov.yaml index 982e9f459..977980fce 100644 --- a/.github/codecov.yaml +++ b/.github/codecov.yaml @@ -27,18 +27,6 @@ ignore: - api/external/** # ignoring external vendor code - "**/*.deepcopy.go" # ignore controller-gen generated code -flag_management: - individual_flags: - - name: unit - paths: - - pkg/** - - api/** - carryforward: true - - name: integration - paths: - - controllers/** - carryforward: true - component_management: individual_components: - component_id: api-v1beta1 From 61d26cdab8be5ac31b5c7efa31d54eb0e93c9694 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Fri, 16 Feb 2024 09:45:03 +0100 Subject: [PATCH 3/4] .github/workflows/test.yaml: codecov/codecov-action@v4 --- .github/workflows/test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f97fee460..4f393183d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -36,7 +36,7 @@ jobs: run: | make test-unit - name: Upload unit-test coverage reports to CodeCov # more at https://github.com/codecov/codecov-action - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} flags: unit @@ -92,7 +92,7 @@ jobs: run: | make test-integration - name: Upload integration-test coverage reports to CodeCov # more at https://github.com/codecov/codecov-action - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} flags: integration From f3553fa525d3e1b84c39f8533eecbdf22b5716a6 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Fri, 16 Feb 2024 12:29:33 +0100 Subject: [PATCH 4/4] use ginkgo CLI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cleanup controller-runtime’s envtest environment to run e2e tests as it is not being used --- .github/workflows/test.yaml | 1 - Makefile | 38 +++++++++++------ controllers/helper_test.go | 42 +++++++++++++++++-- .../ratelimitpolicy_controller_test.go | 33 --------------- 4 files changed, 64 insertions(+), 50 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4f393183d..397bd80a2 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -53,7 +53,6 @@ jobs: exclude: - istio-type: sail pr-event: true - fail-fast: false runs-on: ubuntu-latest env: KIND_CLUSTER_NAME: kuadrant-test diff --git a/Makefile b/Makefile index 4998ba59f..c66415ec4 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,4 @@ # Setting SHELL to bash allows bash commands to be executed by recipes. -# This is a requirement for 'setup-envtest.sh' in the test target. # Options are set to exit when a recipe line exits non-zero or a piped command fails. SHELL = /usr/bin/env bash -o pipefail .SHELLFLAGS = -ec @@ -80,12 +79,10 @@ endif # Image URL to use all building/pushing image targets IMG ?= $(IMAGE_TAG_BASE):$(IMAGE_TAG) -# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. -ENVTEST_K8S_VERSION = 1.22 # Directories containing unit & integration test packages UNIT_DIRS := ./pkg/... ./api/... ./controllers/... -INTEGRATION_DIRS := ./controllers... +INTEGRATION_TEST_SUITE_PATHS := ./controllers/... INTEGRATION_COVER_PKGS := ./pkg/...,./controllers/...,./api/... # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) @@ -239,6 +236,17 @@ $(ACT): .PHONY: act act: $(ACT) ## Download act locally if necessary. +GINKGO = $(PROJECT_PATH)/bin/ginkgo +$(GINKGO): + # In order to make sure the version of the ginkgo cli installed + # is the same as the version of go.mod, + # instead of calling go-install-tool, + # running go install from the current module will pick version from current go.mod file. + GOBIN=$(PROJECT_DIR)/bin go install github.com/onsi/ginkgo/v2/ginkgo + +.PHONY: ginkgo +ginkgo: $(GINKGO) ## Download ginkgo locally if necessary. + ##@ Development define patch-config envsubst \ @@ -283,20 +291,28 @@ vet: ## Run go vet against code. .PHONY: clean-cov clean-cov: ## Remove coverage reports - rm -rf coverage + rm -rf $(PROJECT_PATH)/coverage .PHONY: test test: test-unit test-integration ## Run all tests -test-integration: clean-cov generate fmt vet envtest ## Run Integration tests. - mkdir -p coverage/integration - KUBEBUILDER_ASSETS="$(shell $(ENVTEST) $(ARCH_PARAM) use $(ENVTEST_K8S_VERSION) -p path)" go test $(INTEGRATION_DIRS) -coverpkg=$(INTEGRATION_COVER_PKGS) -coverprofile $(PROJECT_PATH)/coverage/integration/cover.out -tags integration -ginkgo.v -ginkgo.progress -v -timeout 0 +test-integration: clean-cov generate fmt vet ginkgo ## Run Integration tests. + mkdir -p $(PROJECT_PATH)/coverage/integration +# Check `ginkgo help run` for command line options. For example to filtering tests. + $(GINKGO) \ + --coverpkg $(INTEGRATION_COVER_PKGS) \ + --output-dir $(PROJECT_PATH)/coverage/integration \ + --coverprofile cover.out \ + -tags integration \ + --fail-fast \ + -v \ + $(INTEGRATION_TEST_SUITE_PATHS) ifdef TEST_NAME test-unit: TEST_PATTERN := --run $(TEST_NAME) endif test-unit: clean-cov generate fmt vet ## Run Unit tests. - mkdir -p coverage/unit + mkdir -p $(PROJECT_PATH)/coverage/unit go test $(UNIT_DIRS) -coverprofile $(PROJECT_PATH)/coverage/unit/cover.out -tags unit -v -timeout 0 $(TEST_PATTERN) .PHONY: namespace @@ -435,10 +451,6 @@ undeploy-catalog: $(KUSTOMIZE) ## Undeploy controller from the K8s cluster speci $(KUSTOMIZE) build config/deploy/olm | kubectl delete -f - -ENVTEST = $(shell pwd)/bin/setup-envtest -envtest: ## Download envtest-setup locally if necessary. - $(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest) - # go-install-tool will 'go install' any package $2 and install it to $1. PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) define go-install-tool diff --git a/controllers/helper_test.go b/controllers/helper_test.go index 910a5596f..44edfb266 100644 --- a/controllers/helper_test.go +++ b/controllers/helper_test.go @@ -11,11 +11,9 @@ import ( "path/filepath" "time" - kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" - "github.com/kuadrant/kuadrant-operator/pkg/common" - "github.com/google/uuid" . "github.com/onsi/gomega" + istioclientgoextensionv1alpha1 "istio.io/client-go/pkg/apis/extensions/v1alpha1" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -30,6 +28,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" logf "sigs.k8s.io/controller-runtime/pkg/log" gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" + gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" + + kuadrantv1beta1 "github.com/kuadrant/kuadrant-operator/api/v1beta1" + kuadrantv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" + "github.com/kuadrant/kuadrant-operator/pkg/common" ) func ApplyKuadrantCR(namespace string) { @@ -293,3 +296,36 @@ func testGatewayIsReady(gateway *gatewayapiv1.Gateway) func() bool { return err == nil && meta.IsStatusConditionTrue(existingGateway.Status.Conditions, common.GatewayProgrammedConditionType) } } + +func testRLPIsAccepted(rlpKey client.ObjectKey) func() bool { + return func() bool { + existingRLP := &kuadrantv1beta2.RateLimitPolicy{} + err := k8sClient.Get(context.Background(), rlpKey, existingRLP) + if err != nil { + return false + } + if !meta.IsStatusConditionTrue(existingRLP.Status.Conditions, string(gatewayapiv1alpha2.PolicyConditionAccepted)) { + return false + } + + return true + } +} + +func testWasmPluginIsAvailable(key client.ObjectKey) func() bool { + return func() bool { + wp := &istioclientgoextensionv1alpha1.WasmPlugin{} + err := k8sClient.Get(context.Background(), key, wp) + if err != nil { + return false + } + + // Unfortunately, WasmPlugin does not have status yet + // Leaving this here for future use + //if !meta.IsStatusConditionTrue(wp.Status.Conditions, "Available") { + // return false + //} + + return true + } +} diff --git a/controllers/ratelimitpolicy_controller_test.go b/controllers/ratelimitpolicy_controller_test.go index b6ba8793a..a8b04c8d4 100644 --- a/controllers/ratelimitpolicy_controller_test.go +++ b/controllers/ratelimitpolicy_controller_test.go @@ -752,36 +752,3 @@ var _ = Describe("RateLimitPolicy CEL Validations", func() { }) }) }) - -func testRLPIsAccepted(rlpKey client.ObjectKey) func() bool { - return func() bool { - existingRLP := &kuadrantv1beta2.RateLimitPolicy{} - err := k8sClient.Get(context.Background(), rlpKey, existingRLP) - if err != nil { - return false - } - if !meta.IsStatusConditionTrue(existingRLP.Status.Conditions, string(gatewayapiv1alpha2.PolicyConditionAccepted)) { - return false - } - - return true - } -} - -func testWasmPluginIsAvailable(key client.ObjectKey) func() bool { - return func() bool { - wp := &istioclientgoextensionv1alpha1.WasmPlugin{} - err := k8sClient.Get(context.Background(), key, wp) - if err != nil { - return false - } - - // Unfortunately, WasmPlugin does not have status yet - // Leaving this here for future use - //if !meta.IsStatusConditionTrue(wp.Status.Conditions, "Available") { - // return false - //} - - return true - } -}