@@ -82,7 +82,11 @@ vet: ## Run go vet against code.
8282
8383.PHONY : test
8484test : manifests generate fmt vet envtest # # Run tests.
85- KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) -p path) " go test ` go list ./... | grep -v test/e2e` -coverprofile coverage.out
85+ KUBEBUILDER_ASSETS=" $( shell $( ENVTEST) use $( ENVTEST_K8S_VERSION) -p path) " go test $(TESTARGS ) ` go list ./... | grep -v test/e2e`
86+
87+ .PHONY : test-coverage
88+ test-coverage : TESTARGS = -json -cover -covermode=atomic -coverprofile=coverage_unit.out
89+ test-coverage : test
8690
8791GOSEC = $(LOCAL_BIN ) /gosec
8892GOSEC_VERSION = 2.9.6
@@ -159,6 +163,7 @@ HUB_KUBECONFIG ?= $(PWD)/policy-addon-ctrl1.kubeconfig
159163HUB_KUBECONFIG_INTERNAL ?= $(PWD ) /policy-addon-ctrl1.kubeconfig-internal
160164HUB_CLUSTER_NAME ?= policy-addon-ctrl1
161165MANAGED_CLUSTER_NAME ?= cluster1
166+ KIND_VERSION ?= latest
162167ifneq ($(KIND_VERSION ) , latest)
163168 KIND_ARGS = --image kindest/node:$(KIND_VERSION)
164169else
@@ -220,7 +225,7 @@ CONTROLLER_NAMESPACE ?= governance-policy-addon-controller-system
220225
221226.PHONY : kind-run-local
222227kind-run-local : manifests generate fmt vet $(KIND_KUBECONFIG ) # # Run the policy-addon-controller locally against the kind cluster.
223- KUBECONFIG=$(KIND_KUBECONFIG ) kubectl get ns $( CONTROLLER_NAMESPACE ) ; if [ $$ ? -ne 0 ] ; then kubectl create ns $( CONTROLLER_NAMESPACE) ; fi
228+ - KUBECONFIG=$(KIND_KUBECONFIG ) kubectl create ns $(CONTROLLER_NAMESPACE )
224229 go run ./main.go controller --kubeconfig=$(KIND_KUBECONFIG ) --namespace $(CONTROLLER_NAMESPACE )
225230
226231.PHONY : kind-load-image
@@ -235,24 +240,52 @@ kind-regenerate-controller: manifests generate kustomize $(KIND_KUBECONFIG) ## R
235240 mv config/default/kustomization.yaml.tmp config/default/kustomization.yaml
236241 KUBECONFIG=$(KIND_KUBECONFIG ) kubectl delete -n $(CONTROLLER_NAMESPACE ) pods -l=app=governance-policy-addon-controller
237242
238- DEPLOYMENT_TARGETS := kind-deploy-registration-operator-hub kind-deploy-registration-operator-managed \
239- kind-approve-cluster kind-load-image kind-regenerate-controller
243+ OCM_PREP_TARGETS := kind-deploy-registration-operator-hub kind-deploy-registration-operator-managed kind-approve-cluster
244+ .PHONY : kind-prep-ocm
245+ kind-prep-ocm : $(OCM_PREP_TARGETS ) # # Install OCM registration pieces and connect the clusters
246+
240247.PHONY : kind-deploy-controller
241- kind-deploy-controller : $( DEPLOYMENT_TARGETS ) # # Deploy the policy-addon-controller to the kind cluster.
248+ kind-deploy-controller : kind-prep-ocm kind-load-image kind-regenerate-controller # # Deploy the policy-addon-controller to the kind cluster.
242249
243250GINKGO = $(LOCAL_BIN ) /ginkgo
244251.PHONY : e2e-dependencies
245252e2e-dependencies : # # Download ginkgo locally if necessary.
246253 $(call go-get-tool,github.com/onsi/ginkgo/v2/ginkgo@$(shell awk '/github.com\/onsi\/ginkgo\/v2/ {print $$2}' go.mod) )
247254
248255.PHONY : e2e-test
249- e2e-test : e2e-dependencies
250- $(GINKGO ) -v --label-filter=" !hosted-mode" --fail-fast --slow-spec-threshold=10s test/e2e
256+ e2e-test : e2e-dependencies # # Run E2E tests.
257+ $(GINKGO ) -v --label-filter=" !hosted-mode" --fail-fast --slow-spec-threshold=10s $( E2E_TEST_ARGS ) test/e2e
251258
252259.PHONY : e2e-test-hosted-mode
253260e2e-test-hosted-mode : e2e-dependencies
254261 $(GINKGO ) -v --label-filter=" hosted-mode" --fail-fast --slow-spec-threshold=10s test/e2e
255262
263+ .PHONY : e2e-test-coverage
264+ e2e-test-coverage : E2E_TEST_ARGS = --json-report=report_e2e.json --output-dir=.
265+ e2e-test-coverage : e2e-run-instrumented e2e-test e2e-stop-instrumented # # Run E2E tests using instrumented controller.
266+
267+ .PHONY : e2e-build-instrumented
268+ e2e-build-instrumented :
269+ go test -covermode=atomic -coverpkg=$(shell cat go.mod | head -1 | cut -d ' ' -f 2) /... -c -tags e2e ./ -o build/_output/bin/$(IMG ) -instrumented
270+
271+ COVERAGE_E2E_OUT ?= coverage_e2e.out
272+ .PHONY : e2e-run-instrumented
273+ e2e-run-instrumented : e2e-build-instrumented
274+ -KUBECONFIG=$(KIND_KUBECONFIG ) kubectl create ns $(CONTROLLER_NAMESPACE )
275+ CONFIG_POLICY_CONTROLLER_IMAGE=" $( REGISTRY) /config-policy-controller:$( TAG) " \
276+ KUBE_RBAC_PROXY_IMAGE=" registry.redhat.io/openshift4/ose-kube-rbac-proxy:v4.10" \
277+ GOVERNANCE_POLICY_SPEC_SYNC_IMAGE=" $( REGISTRY) /governance-policy-spec-sync:$( TAG) " \
278+ GOVERNANCE_POLICY_STATUS_SYNC_IMAGE=" $( REGISTRY) /governance-policy-status-sync:$( TAG) " \
279+ GOVERNANCE_POLICY_TEMPLATE_SYNC_IMAGE=" $( REGISTRY) /governance-policy-template-sync:$( TAG) " \
280+ ./build/_output/bin/$(IMG ) -instrumented -test.v -test.run=" ^TestRunMain$$ " -test.coverprofile=$(COVERAGE_E2E_OUT ) \
281+ --kubeconfig=" $( KIND_KUBECONFIG) " & > build/_output/controller.log &
282+
283+ .PHONY : e2e-stop-instrumented
284+ e2e-stop-instrumented :
285+ ps -ef | grep ' $(IMG)' | grep -v grep | awk ' {print $$2}' | xargs kill -s SIGUSR1
286+ sleep 5 # wait for tests to gracefully shut down
287+ -ps -ef | grep ' $(IMG)' | grep -v grep | awk ' {print $$2}' | xargs kill
288+
256289.PHONY : e2e-debug
257290e2e-debug : # # Collect debug logs from deployed clusters.
258291 @echo " ##### Gathering information from $( KIND_NAME) #####"
@@ -262,7 +295,9 @@ e2e-debug: ## Collect debug logs from deployed clusters.
262295 -KUBECONFIG=$(KIND_KUBECONFIG ) kubectl -n open-cluster-management-agent-addon get deployments
263296 -KUBECONFIG=$(KIND_KUBECONFIG ) kubectl -n open-cluster-management-agent-addon get pods
264297 -KUBECONFIG=$(KIND_KUBECONFIG ) kubectl get manifestwork --all-namespaces -o yaml
265-
298+
299+ @echo " * Local controller log:" "
300+ -cat build/_output/controller.log
266301 @echo " * Container logs in namespace $( CONTROLLER_NAMESPACE) :"
267302 -@for POD in $(shell KUBECONFIG=$(KIND_KUBECONFIG ) kubectl -n $(CONTROLLER_NAMESPACE ) get pods -o name) ; do \
268303 for CONTAINER in $$ (KUBECONFIG=$( KIND_KUBECONFIG) kubectl -n $( CONTROLLER_NAMESPACE) get $$ {POD} -o jsonpath={.spec.containers[* ].name}); do \
@@ -301,3 +336,19 @@ lint-dependencies:
301336# eg: lint: lint-go lint-yaml
302337.PHONY : lint
303338lint : lint-dependencies lint-all # # Run linting against the code.
339+
340+ # #@ Test Coverage
341+ GOCOVMERGE = $(LOCAL_BIN ) /gocovmerge
342+ .PHONY : coverage-dependencies
343+ coverage-dependencies :
344+ $(call go-get-tool,github.com/wadey/[email protected] ) 345+
346+ COVERAGE_FILE = coverage.out
347+ .PHONY : coverage-merge
348+ coverage-merge : coverage-dependencies # # Merge coverage reports.
349+ @echo Merging the coverage reports into $(COVERAGE_FILE )
350+ $(GOCOVMERGE ) $(PWD ) /coverage_* > $(COVERAGE_FILE )
351+
352+ .PHONY : coverage-verify
353+ coverage-verify : # # Verify coverage percentage meets coverage thresholds.
354+ ./build/common/scripts/coverage_calc.sh
0 commit comments