Skip to content

Commit

Permalink
fixup! Adds E2E coverage reporting
Browse files Browse the repository at this point in the history
Signed-off-by: Mikalai Radchuk <mradchuk@redhat.com>
  • Loading branch information
m1kola committed Jul 26, 2023
1 parent bfc7b57 commit 6bcb822
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,8 @@ e2e: KIND_CLUSTER_NAME=operator-controller-e2e
e2e: KUSTOMIZE_BUILD_DIR=config/e2e
e2e: GO_BUILD_FLAGS=-cover
e2e: run kind-load-test-artifacts test-e2e ## Run e2e test suite on local kind cluster
# Coverage-instrumented binary produces coverage on termination, so we scale down the manager before gathering the coverage
kubectl -n $(OPERATOR_CONTROLLER_NAMESPACE) scale deployment/operator-controller-controller-manager --replicas=0
kubectl -n $(OPERATOR_CONTROLLER_NAMESPACE) run pvc-copy --image busybox --restart=Never --override-type strategic --overrides='{"spec": {"volumes": [{"name": "data", "persistentVolumeClaim": {"claimName": "e2e-coverage"}}], "containers": [{"name": "pvc-copy", "securityContext": {"runAsUser": 65532, "runAsNonRoot": true, "allowPrivilegeEscalation": false, "seccompProfile": {"type": "RuntimeDefault"}, "capabilities": {"drop": ["ALL"]}}, "volumeMounts": [{"name": "data", "mountPath": "/data"}]}]}}' -- sleep infinity
kubectl -n $(OPERATOR_CONTROLLER_NAMESPACE) wait --for=condition=ready pod pvc-copy
kubectl -n $(OPERATOR_CONTROLLER_NAMESPACE) cp pvc-copy:/data/ ./coverage/
go tool covdata textfmt -i=./coverage/ -o e2e-cover.out
# TODO: Make it run the following even if parent targets fail
COVERAGE_OUTPUT=./e2e-cover.out ./hack/e2e-coverage.sh
$(MAKE) kind-cluster-cleanup KIND_CLUSTER_NAME=$(KIND_CLUSTER_NAME)

kind-load: $(KIND) ## Loads the currently constructed image onto the cluster
Expand Down
1 change: 1 addition & 0 deletions config/e2e/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace: operator-controller-system
resources:
- ../default
- manager_e2e_coverage_pvc.yaml
- manager_e2e_coverage_copy_pod.yaml

patches:
- path: manager_e2e_coverage_patch.yaml
34 changes: 34 additions & 0 deletions config/e2e/manager_e2e_coverage_copy_pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: v1
kind: Pod
metadata:
name: e2e-coverage-copy-pod
labels:
app.kubernetes.io/name: e2e-coverage-copy-pod
app.kubernetes.io/instance: controller-manager
app.kubernetes.io/component: e2e-coverage
app.kubernetes.io/created-by: operator-controller
app.kubernetes.io/part-of: operator-controller
app.kubernetes.io/managed-by: kustomize
spec:
restartPolicy: Never
securityContext:
runAsNonRoot: true
runAsUser: 65532
seccompProfile:
type: RuntimeDefault
containers:
- name: tar
image: busybox:1.36
command: ["sleep", "infinity"]
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- "ALL"
volumeMounts:
- name: e2e-coverage-volume
mountPath: /e2e-coverage
volumes:
- name: e2e-coverage-volume
persistentVolumeClaim:
claimName: e2e-coverage
29 changes: 29 additions & 0 deletions hack/e2e-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

OPERATOR_CONTROLLER_NAMESPACE="${OPERATOR_CONTROLLER_NAMESPACE:-operator-controller-system}"
OPERATOR_CONTROLLER_MANAGER_DEPLOYMENT_NAME="${OPERATOR_CONTROLLER_MANAGER_DEPLOYMENT_NAME:-operator-controller-controller-manager}"
COVERAGE_OUTPUT="${COVERAGE_OUTPUT:-e2e-cover.out}"

# Create a temporary directory for coverage
COVERAGE_DIR=$(mktemp -d)

# Trap to cleanup temporary directory on script exit
function cleanup {
rm -rf "$COVERAGE_DIR"
}
trap cleanup EXIT

# Coverage-instrumented binary produces coverage on termination,
# so we scale down the manager before gathering the coverage
kubectl -n "$OPERATOR_CONTROLLER_NAMESPACE" scale deployment/"$OPERATOR_CONTROLLER_MANAGER_DEPLOYMENT_NAME" --replicas=0

# Wait for the copy pod to be ready
kubectl -n "$OPERATOR_CONTROLLER_NAMESPACE" wait --for=condition=ready pod e2e-coverage-copy-pod

# Copy the coverage data from the temporary pod
kubectl -n "$OPERATOR_CONTROLLER_NAMESPACE" cp e2e-coverage-copy-pod:/e2e-coverage/ "$COVERAGE_DIR"

# Convert binary coverage data files into the textual format
go tool covdata textfmt -i "$COVERAGE_DIR" -o "$COVERAGE_OUTPUT"

echo "Coverage report generated successfully at: $COVERAGE_OUTPUT"

0 comments on commit 6bcb822

Please sign in to comment.