Skip to content

Commit 669c451

Browse files
Mikalai Radchukncdc
authored andcommitted
Adds E2E coverage reporting
Signed-off-by: Mikalai Radchuk <[email protected]>
1 parent a90c035 commit 669c451

File tree

9 files changed

+141
-8
lines changed

9 files changed

+141
-8
lines changed

.github/workflows/e2e.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,17 @@ jobs:
2222

2323
- name: Run e2e tests
2424
run: |
25-
make e2e
25+
# By default make stops building on first non-zero exit code which
26+
# in case of E2E tests will mean that code coverage will only be
27+
# collected on successful runs. We want to collect coverage even
28+
# after failing tests.
29+
# With -k flag make will continue the build, but will return non-zero
30+
# exit code in case of any errors.
31+
make -k e2e
32+
33+
- uses: codecov/codecov-action@v3
34+
with:
35+
files: e2e-cover.out
36+
flags: e2e
37+
fail_ci_if_error: true
38+
functionalities: fixes

.github/workflows/unit-test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ jobs:
2525
- uses: codecov/codecov-action@v3
2626
with:
2727
files: cover.out
28+
flags: unit
2829
fail_ci_if_error: true
2930
functionalities: fixes

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ Dockerfile.cross
1414
# Test binary, build with `go test -c`
1515
*.test
1616

17-
# Output of the go coverage tool, specifically when used with LiteIDE
17+
# Output of the go coverage tools
1818
*.out
19+
coverage
1920

2021
# Release output
2122
dist/**
@@ -35,4 +36,4 @@ install.sh
3536
.\#*
3637

3738
# documentation website asset folder
38-
docs/_site
39+
docs/_site

Makefile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ KIND_CLUSTER_NAME ?= operator-controller
2424

2525
CONTAINER_RUNTIME ?= docker
2626

27+
KUSTOMIZE_BUILD_DIR ?= config/default
28+
2729
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
2830
ifeq (,$(shell go env GOBIN))
2931
GOBIN=$(shell go env GOPATH)/bin
@@ -103,7 +105,13 @@ test-unit: $(SETUP_ENVTEST) ## Run the unit tests
103105
eval $$($(SETUP_ENVTEST) use -p env $(ENVTEST_VERSION)) && go test -tags $(GO_BUILD_TAGS) -count=1 -short $(UNIT_TEST_DIRS) -coverprofile cover.out
104106

105107
e2e: KIND_CLUSTER_NAME=operator-controller-e2e
106-
e2e: run kind-load-test-artifacts test-e2e kind-cluster-cleanup ## Run e2e test suite on local kind cluster
108+
e2e: KUSTOMIZE_BUILD_DIR=config/e2e
109+
e2e: GO_BUILD_FLAGS=-cover
110+
e2e: run kind-load-test-artifacts test-e2e e2e-coverage kind-cluster-cleanup ## Run e2e test suite on local kind cluster
111+
112+
.PHONY: e2e-coverage
113+
e2e-coverage:
114+
COVERAGE_OUTPUT=./e2e-cover.out ./hack/e2e-coverage.sh
107115

108116
kind-load: $(KIND) ## Loads the currently constructed image onto the cluster
109117
$(KIND) load docker-image $(IMG) --name $(KIND_CLUSTER_NAME)
@@ -115,6 +123,7 @@ kind-cluster: $(KIND) kind-cluster-cleanup ## Standup a kind cluster
115123
kind-cluster-cleanup: $(KIND) ## Delete the kind cluster
116124
$(KIND) delete cluster --name ${KIND_CLUSTER_NAME}
117125

126+
.PHONY: kind-load-test-artifacts
118127
kind-load-test-artifacts: $(KIND) ## Load the e2e testdata container images into a kind cluster
119128
$(CONTAINER_RUNTIME) build $(TESTDATA_DIR)/bundles/registry-v1/prometheus-operator.v0.47.0 -t localhost/testdata/bundles/registry-v1/prometheus-operator:v0.47.0
120129
$(CONTAINER_RUNTIME) build $(TESTDATA_DIR)/bundles/plain-v0/plain.v0.1.0 -t localhost/testdata/bundles/plain-v0/plain:v0.1.0
@@ -174,7 +183,7 @@ release: $(GORELEASER) ## Runs goreleaser for the operator-controller. By defaul
174183

175184
quickstart: export MANIFEST="https://github.com/operator-framework/operator-controller/releases/download/$(VERSION)/operator-controller.yaml"
176185
quickstart: $(KUSTOMIZE) generate ## Generate the installation release manifests and scripts
177-
$(KUSTOMIZE) build config/default | sed "s/:devel/:$(VERSION)/g" > operator-controller.yaml
186+
$(KUSTOMIZE) build $(KUSTOMIZE_BUILD_DIR) | sed "s/:devel/:$(VERSION)/g" > operator-controller.yaml
178187
envsubst '$$CATALOGD_VERSION,$$CERT_MGR_VERSION,$$RUKPAK_VERSION,$$MANIFEST' < scripts/install.tpl.sh > install.sh
179188

180189
##@ Deployment
@@ -186,7 +195,7 @@ endif
186195
.PHONY: install
187196
install: export MANIFEST="./operator-controller.yaml"
188197
install: manifests $(KUSTOMIZE) generate ## Install CRDs into the K8s cluster specified in ~/.kube/config.
189-
$(KUSTOMIZE) build config/default > operator-controller.yaml
198+
$(KUSTOMIZE) build $(KUSTOMIZE_BUILD_DIR) > operator-controller.yaml
190199
envsubst '$$CATALOGD_VERSION,$$CERT_MGR_VERSION,$$RUKPAK_VERSION,$$MANIFEST' < scripts/install.tpl.sh | bash -s
191200

192201
.PHONY: uninstall
@@ -196,8 +205,8 @@ uninstall: manifests $(KUSTOMIZE) ## Uninstall CRDs from the K8s cluster specifi
196205
.PHONY: deploy
197206
deploy: manifests $(KUSTOMIZE) ## Deploy controller to the K8s cluster specified in ~/.kube/config.
198207
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
199-
$(KUSTOMIZE) build config/default | kubectl apply -f -
208+
$(KUSTOMIZE) build $(KUSTOMIZE_BUILD_DIR) | kubectl apply -f -
200209

201210
.PHONY: undeploy
202211
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
203-
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
212+
$(KUSTOMIZE) build $(KUSTOMIZE_BUILD_DIR) | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

config/e2e/kustomization.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace: operator-controller-system
2+
3+
resources:
4+
- ../default
5+
- manager_e2e_coverage_pvc.yaml
6+
- manager_e2e_coverage_copy_pod.yaml
7+
8+
patches:
9+
- path: manager_e2e_coverage_patch.yaml
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: e2e-coverage-copy-pod
5+
labels:
6+
app.kubernetes.io/name: e2e-coverage-copy-pod
7+
app.kubernetes.io/instance: controller-manager
8+
app.kubernetes.io/component: e2e-coverage
9+
app.kubernetes.io/created-by: operator-controller
10+
app.kubernetes.io/part-of: operator-controller
11+
app.kubernetes.io/managed-by: kustomize
12+
spec:
13+
restartPolicy: Never
14+
securityContext:
15+
runAsNonRoot: true
16+
runAsUser: 65532
17+
seccompProfile:
18+
type: RuntimeDefault
19+
containers:
20+
- name: tar
21+
image: busybox:1.36
22+
command: ["sleep", "infinity"]
23+
securityContext:
24+
allowPrivilegeEscalation: false
25+
capabilities:
26+
drop:
27+
- "ALL"
28+
volumeMounts:
29+
- name: e2e-coverage-volume
30+
mountPath: /e2e-coverage
31+
readOnly: true
32+
volumes:
33+
- name: e2e-coverage-volume
34+
persistentVolumeClaim:
35+
claimName: e2e-coverage
36+
readOnly: true
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: controller-manager
5+
namespace: system
6+
spec:
7+
template:
8+
spec:
9+
containers:
10+
- name: kube-rbac-proxy
11+
- name: manager
12+
env:
13+
- name: GOCOVERDIR
14+
value: /e2e-coverage
15+
volumeMounts:
16+
- name: e2e-coverage-volume
17+
mountPath: /e2e-coverage
18+
volumes:
19+
- name: e2e-coverage-volume
20+
persistentVolumeClaim:
21+
claimName: e2e-coverage
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: v1
2+
kind: PersistentVolumeClaim
3+
metadata:
4+
name: e2e-coverage
5+
spec:
6+
accessModes:
7+
- ReadWriteOnce
8+
resources:
9+
requests:
10+
storage: 64Mi

hack/e2e-coverage.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
COVERAGE_OUTPUT="${COVERAGE_OUTPUT:-e2e-cover.out}"
6+
7+
OPERATOR_CONTROLLER_NAMESPACE="operator-controller-system"
8+
OPERATOR_CONTROLLER_MANAGER_DEPLOYMENT_NAME="operator-controller-controller-manager"
9+
COPY_POD_NAME="e2e-coverage-copy-pod"
10+
11+
# Create a temporary directory for coverage
12+
COVERAGE_DIR=$(mktemp -d)
13+
14+
# Trap to cleanup temporary directory on script exit
15+
function cleanup {
16+
rm -rf "$COVERAGE_DIR"
17+
}
18+
trap cleanup EXIT
19+
20+
# Coverage-instrumented binary produces coverage on termination,
21+
# so we scale down the manager before gathering the coverage
22+
kubectl -n "$OPERATOR_CONTROLLER_NAMESPACE" scale deployment/"$OPERATOR_CONTROLLER_MANAGER_DEPLOYMENT_NAME" --replicas=0
23+
24+
# Wait for the copy pod to be ready
25+
kubectl -n "$OPERATOR_CONTROLLER_NAMESPACE" wait --for=condition=ready pod "$COPY_POD_NAME"
26+
27+
# Copy the coverage data from the temporary pod
28+
kubectl -n "$OPERATOR_CONTROLLER_NAMESPACE" cp "$COPY_POD_NAME":/e2e-coverage/ "$COVERAGE_DIR"
29+
30+
# Convert binary coverage data files into the textual format
31+
go tool covdata textfmt -i "$COVERAGE_DIR" -o "$COVERAGE_OUTPUT"
32+
33+
echo "Coverage report generated successfully at: $COVERAGE_OUTPUT"

0 commit comments

Comments
 (0)