Skip to content

Run unit tests without connecting to CI clusters

Mitchell Nielsen requested to merge test-without-live-cluster into master

Summary

Uses envtest to remove the requirement for running tests against the live CI cluster.

Job times don't seem to have changed drastically:

Job name Time on master Time in this MR
slow_unit_tests[1] 8:31 8:19
slow_unit_tests[2] 8:04 8:09
unit_tests[1] 5:58 6:02
unit_tests[2] 6:24 5:40

Closes #221 (closed)

References

Testing

Setting KUBECONFIG: "" in CI confirms that we don't have a connection to the CI cluster (#106 (closed) notes that KUBECONFIG needed to be exposed for tests to pass, and #177 (comment 638975999) notes that tests failed when we didn't expose KUBECONFIG).

To further validate, we can test locally in a container that is known to not have a connection to any live cluster. See instructions below.

1. Update Dockerfile

diff --git a/Dockerfile b/Dockerfile
index 9b74b10..7221b36 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -19,7 +19,7 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager
 
 # Use distroless as minimal base image to package the manager binary
 # Refer to https://github.com/GoogleContainerTools/distroless for more details
-FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
+FROM registry.access.redhat.com/ubi8/ubi-minimal:latest AS release
 
 LABEL name=gitlab-operator \
       vendor='GitLab, Inc.' \
@@ -46,3 +46,32 @@ COPY --from=builder /workspace/manager .
 USER ${USER_UID}
 
 ENTRYPOINT ["/manager"]
+
+# ----- Stage: unit tests template -----
+
+FROM registry.gitlab.com/gitlab-org/gitlab-build-images:gitlab-operator-build-base AS unit_tests_template
+WORKDIR /workspace
+
+ENV HELM_CHARTS=/charts
+ENV CHART_VERSION=5.3.0
+ENV GITLAB_OPERATOR_ASSETS=/workspace/hack/assets
+ENV KUBEBUILDER_ASSETS=/usr/local/kubebuilder/bin
+ENV USE_EXISTING_CLUSTER="false"
+ENV KUBECONFIG=""
+
+COPY --from=release /charts /charts
+COPY . .
+
+# ----- Stage: unit tests -----
+
+FROM unit_tests_template AS unit_tests
+RUN mkdir coverage && /go/bin/ginkgo -skip 'controller' -cover -outputdir=coverage ./...
+
+# ----- Stage: slow unit tests -----
+
+FROM unit_tests_template AS slow_unit_tests
+RUN mkdir coverage && /go/bin/ginkgo -focus 'controller' -cover -outputdir=coverage ./...
+
+# ----- Stage: release (final) -----
+
+FROM release

2. Build image

$ ./scripts/retrieve_gitlab_charts.sh
$ mkdir -p .go/pkg/mod
$ docker build -t operator:local .

Ensure the build passes.

Edited by Mitchell Nielsen

Merge request reports