Skip to content

Commit f47a7ae

Browse files
committed
Unit test enhancements
1 parent b365088 commit f47a7ae

File tree

3 files changed

+691
-104
lines changed

3 files changed

+691
-104
lines changed

Makefile

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ endif
9393
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
9494
CRD_OPTIONS ?= "crd:crdVersions=v1,generateEmbeddedObjectMeta=true"
9595

96+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
97+
ENVTEST_K8S_VERSION = 1.24
98+
9699
# Produce files under internal/deploy/kustomize/daily with runtime-component namespace
97100
KUSTOMIZE_NAMESPACE = runtime-component
98101
KUSTOMIZE_IMG = icr.io/appcafe/runtime-component-operator:daily
@@ -141,22 +144,31 @@ LOCALBIN ?= $(shell pwd)/bin
141144
$(LOCALBIN):
142145
mkdir -p $(LOCALBIN)
143146

147+
## Tool Binaries
148+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
144149
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
145-
# find or download controller-gen
146-
# download controller-gen if necessary
147-
.PHONY: controller-gen
148-
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
149-
$(CONTROLLER_GEN): $(LOCALBIN)
150-
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/[email protected]
150+
ENVTEST ?= $(LOCALBIN)/setup-envtest
151151

152-
KUSTOMIZE ?= $(LOCALBIN)/kustomize
152+
## Tool Versions
153153
KUSTOMIZE_VERSION ?= 4.5.5
154+
CONTROLLER_TOOLS_VERSION ?= 0.9.2
155+
154156
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/v${KUSTOMIZE_VERSION}/hack/install_kustomize.sh"
155157
.PHONY: kustomize
156158
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
157159
$(KUSTOMIZE): $(LOCALBIN)
158160
test -s $(LOCALBIN)/kustomize || curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s $(KUSTOMIZE_VERSION) $(LOCALBIN)
159161

162+
.PHONY: controller-gen
163+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
164+
$(CONTROLLER_GEN): $(LOCALBIN)
165+
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@v$(CONTROLLER_TOOLS_VERSION)
166+
167+
.PHONY: envtest
168+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
169+
$(ENVTEST): $(LOCALBIN)
170+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
171+
160172
.PHONY: setup
161173
setup: ## Ensure Operator SDK is installed.
162174
./scripts/installers/install-operator-sdk.sh ${OPERATOR_SDK_RELEASE_VERSION}
@@ -219,12 +231,13 @@ fmt: ## Run go fmt against code.
219231
vet: ## Run go vet against code.
220232
go vet ./...
221233

222-
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
223234
.PHONY: test
224-
test: manifests generate fmt vet ## Run tests.
225-
mkdir -p ${ENVTEST_ASSETS_DIR}
226-
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.7.2/hack/setup-envtest.sh
227-
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
235+
test: manifests generate fmt vet envtest ## Run tests.
236+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
237+
238+
.PHONY: test-cover
239+
test-cover: test
240+
go tool cover -html=cover.out
228241

229242
.PHONY: unit-test
230243
unit-test: ## Run unit tests

utils/reconciler_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package utils
33
import (
44
"context"
55
"fmt"
6-
appstacksv1 "github.com/application-stacks/runtime-component-operator/api/v1"
76
"reflect"
87
"strings"
98
"testing"
109

10+
appstacksv1 "github.com/application-stacks/runtime-component-operator/api/v1"
11+
1112
"github.com/application-stacks/runtime-component-operator/common"
1213

1314
routev1 "github.com/openshift/api/route/v1"
@@ -166,7 +167,7 @@ func TestGetDiscoveryClient(t *testing.T) {
166167
logger := zap.New()
167168
logf.SetLogger(logger)
168169

169-
runtimecomponent := createRuntimeComponent(name, namespace, spec)
170+
runtimecomponent := createRuntimeComponent(objMeta, spec)
170171
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
171172
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
172173
cl := fakeclient.NewFakeClient(objs...)
@@ -186,7 +187,7 @@ func TestCreateOrUpdate(t *testing.T) {
186187
logf.SetLogger(logger)
187188
serviceAccount := &corev1.ServiceAccount{ObjectMeta: defaultMeta}
188189

189-
runtimecomponent := createRuntimeComponent(name, namespace, spec)
190+
runtimecomponent := createRuntimeComponent(objMeta, spec)
190191
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
191192
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
192193
cl := fakeclient.NewFakeClient(objs...)
@@ -207,7 +208,7 @@ func TestDeleteResources(t *testing.T) {
207208
logger := zap.New()
208209
logf.SetLogger(logger)
209210

210-
runtimecomponent := createRuntimeComponent(name, namespace, spec)
211+
runtimecomponent := createRuntimeComponent(objMeta, spec)
211212
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
212213
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
213214
cl := fakeclient.NewFakeClient(objs...)
@@ -260,7 +261,7 @@ func TestGetOpConfigMap(t *testing.T) {
260261
},
261262
}
262263

263-
runtimecomponent := createRuntimeComponent(name, namespace, spec)
264+
runtimecomponent := createRuntimeComponent(objMeta, spec)
264265
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
265266
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
266267
cl := fakeclient.NewFakeClient(objs...)
@@ -286,7 +287,7 @@ func TestManageError(t *testing.T) {
286287
logf.SetLogger(logger)
287288
err := fmt.Errorf("test-error")
288289

289-
runtimecomponent := createRuntimeComponent(name, namespace, spec)
290+
runtimecomponent := createRuntimeComponent(objMeta, spec)
290291
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
291292
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
292293
cl := fakeclient.NewFakeClient(objs...)
@@ -307,7 +308,7 @@ func TestManageSuccess(t *testing.T) {
307308
logger := zap.New()
308309
logf.SetLogger(logger)
309310

310-
runtimecomponent := createRuntimeComponent(name, namespace, spec)
311+
runtimecomponent := createRuntimeComponent(objMeta, spec)
311312
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
312313
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
313314
cl := fakeclient.NewFakeClient(objs...)
@@ -326,7 +327,7 @@ func TestIsGroupVersionSupported(t *testing.T) {
326327
logger := zap.New()
327328
logf.SetLogger(logger)
328329

329-
runtimecomponent := createRuntimeComponent(name, namespace, spec)
330+
runtimecomponent := createRuntimeComponent(objMeta, spec)
330331
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
331332
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
332333
cl := fakeclient.NewFakeClient(objs...)
@@ -363,7 +364,7 @@ func TestIsGroupVersionSupported(t *testing.T) {
363364
func testGetSvcTLSValues(t *testing.T) {
364365
// Configure the runtime component
365366

366-
runtimecomponent := createRuntimeComponent(name, namespace, spec)
367+
runtimecomponent := createRuntimeComponent(objMeta, spec)
367368
expose := true
368369
runtimecomponent.Spec.Expose = &expose
369370
runtimecomponent.Spec.Service = &appstacksv1.RuntimeComponentService{
@@ -404,7 +405,7 @@ func testGetSvcTLSValues(t *testing.T) {
404405
// testGetRouteTLSValues test the function GetRouteTLSValues in reconciler.go.
405406
func testGetRouteTLSValues(t *testing.T) {
406407
// Configure the rumtime component
407-
runtimecomponent := createRuntimeComponent(name, namespace, spec)
408+
runtimecomponent := createRuntimeComponent(objMeta, spec)
408409
terminationPolicy := routev1.TLSTerminationReencrypt
409410
secretRefName := "my-app-route-tls"
410411
runtimecomponent.Spec.Expose = &expose

0 commit comments

Comments
 (0)