Skip to content

Commit 10b9854

Browse files
tests(integration): Move integration tests to tests/
Move tests-related targets in Makefile to a separate section `Test`. Signed-off-by: Bhargav Ravuri <[email protected]>
1 parent 236a5d6 commit 10b9854

20 files changed

+92
-168
lines changed

Makefile

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,55 +70,64 @@ all: build
7070
# http://linuxcommand.org/lc3_adv_awk.php
7171

7272
.PHONY: help
73-
help: ## Display this help.
73+
help: ## Display this help.
7474
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
7575

7676
##@ Development
7777

7878
.PHONY: manifests
79-
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
79+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
8080
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
8181

8282
.PHONY: generate
83-
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
83+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
8484
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
8585

86+
##@ Test
87+
8688
.PHONY: fmt
87-
fmt: ## Run go fmt against code.
89+
fmt: ## Run go fmt against code.
8890
go fmt ./...
8991

9092
.PHONY: vet
91-
vet: ## Run go vet against code.
93+
vet: ## Run go vet against code.
9294
go vet ./...
9395

94-
.PHONY: test
95-
test: fmt vet envtest ## Run tests.
96-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -v -coverprofile=coverage.out -coverpkg ./... ./...
96+
.PHONY: test-unit
97+
test-unit: ## Run unit tests.
98+
go test -v -coverprofile=coverage.out `go list ./controllers/... ./pkg/... | grep -v ./pkg/mocks`
9799

98-
.PHONY: unit-test-coverage
99-
unit-test-coverage: test
100+
.PHONY: test-coverage
101+
test-coverage: test-unit ## Run unit tests and print code coverage.
100102
go tool cover -func coverage.out
101103

104+
.PHONY: test-integration
105+
test-integration: ## Run integration tests.
106+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -v -tags=integration ./...
107+
108+
.PHONY: test
109+
test: fmt vet envtest test-unit test-integration ## Run all tests (fmt, vet, envtest, unit & integration).
110+
102111
.PHONY: test-docker
103-
test-docker:
112+
test-docker: ## Run all tests (fmt, vet, envtest, unit & integration) inside a Docker container.
104113
docker build -t test -f test.Dockerfile . && docker run test
105114

106115
##@ Build
107116

108117
.PHONY: build
109-
build: generate fmt vet ## Build manager binary.
118+
build: generate fmt vet ## Build manager binary.
110119
go build -o bin/manager main.go
111120

112121
.PHONY: run
113-
run: manifests generate fmt vet ## Run a controller from your host.
122+
run: manifests generate fmt vet ## Run a controller from your host.
114123
go run ./main.go
115124

116125
.PHONY: docker-build
117-
docker-build: ## Build docker image with the manager.
126+
docker-build: ## Build docker image with the manager.
118127
docker build -t ${IMG} .
119128

120129
.PHONY: docker-push
121-
docker-push: ## Push docker image with the manager.
130+
docker-push: ## Push docker image with the manager.
122131
docker push ${IMG}
123132

124133
##@ Deployment
@@ -140,36 +149,36 @@ ifndef ignore-not-found
140149
endif
141150

142151
.PHONY: install
143-
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
152+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
144153
$(KUSTOMIZE) build config/crd | kubectl apply -f -
145154

146155
.PHONY: uninstall
147-
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
156+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
148157
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
149158

150159
.PHONY: deploy
151-
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
160+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
152161
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
153162
$(KUSTOMIZE) build config/default | kubectl apply -f -
154163

155164
.PHONY: undeploy
156-
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.
165+
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.
157166
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
158167

159168
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
160169
.PHONY: controller-gen
161-
controller-gen: ## Download controller-gen locally if necessary.
170+
controller-gen: ## Download controller-gen locally if necessary.
162171
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@latest)
163172

164173
KUSTOMIZE = $(shell pwd)/bin/kustomize
165174
.PHONY: kustomize
166-
kustomize: manifests ## Download kustomize locally if necessary.
175+
kustomize: manifests ## Download kustomize locally if necessary.
167176
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
168177
kustomize build config/default > deploy/kubeslice-operator.yaml
169178

170179
ENVTEST = $(shell pwd)/bin/setup-envtest
171180
.PHONY: envtest
172-
envtest: ## Download envtest-setup locally if necessary.
181+
envtest: ## Download envtest-setup locally if necessary.
173182
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
174183

175184
# go-get-tool will 'go get' any package $2 and install it to $1.
@@ -187,23 +196,23 @@ rm -rf $$TMP_DIR ;\
187196
endef
188197

189198
.PHONY: bundle
190-
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
199+
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
191200
operator-sdk generate kustomize manifests -q
192201
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
193202
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
194203
operator-sdk bundle validate ./bundle
195204

196205
.PHONY: bundle-build
197-
bundle-build: ## Build the bundle image.
206+
bundle-build: ## Build the bundle image.
198207
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
199208

200209
.PHONY: bundle-push
201-
bundle-push: ## Push the bundle image.
210+
bundle-push: ## Push the bundle image.
202211
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
203212

204213
.PHONY: opm
205214
OPM = ./bin/opm
206-
opm: ## Download opm locally if necessary.
215+
opm: ## Download opm locally if necessary.
207216
ifeq (,$(wildcard $(OPM)))
208217
ifeq (,$(shell which opm 2>/dev/null))
209218
@{ \
@@ -234,12 +243,12 @@ endif
234243
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
235244
# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator
236245
.PHONY: catalog-build
237-
catalog-build: opm ## Build a catalog image.
246+
catalog-build: opm ## Build a catalog image.
238247
$(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
239248

240249
# Push the catalog image.
241250
.PHONY: catalog-push
242-
catalog-push: ## Push a catalog image.
251+
catalog-push: ## Push a catalog image.
243252
$(MAKE) docker-push IMG=$(CATALOG_IMG)
244253

245254
# Generate events schema map

pkg/hub/controllers/cluster/deregister_unit_test.go renamed to pkg/hub/controllers/cluster/deregister_test.go

Lines changed: 23 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package cluster
33
import (
44
"context"
55
"errors"
6-
"reflect"
7-
"runtime/debug"
86
"testing"
97

108
hubv1alpha1 "github.com/kubeslice/apis/pkg/controller/v1alpha1"
119
"github.com/kubeslice/kubeslice-monitoring/pkg/metrics"
1210
kubeslicev1beta1 "github.com/kubeslice/worker-operator/api/v1beta1"
1311
utilmock "github.com/kubeslice/worker-operator/pkg/mocks"
1412
"github.com/prometheus/client_golang/prometheus"
13+
"github.com/stretchr/testify/assert"
1514
"github.com/stretchr/testify/mock"
1615
batchv1 "k8s.io/api/batch/v1"
1716
corev1 "k8s.io/api/core/v1"
@@ -87,9 +86,7 @@ func TestGetOperatorClusterRole(t *testing.T) {
8786
mock.IsType(&rbacv1.ClusterRole{}),
8887
).Return(nil)
8988
_, err := reconciler.getOperatorClusterRole(ctx)
90-
if expected.err != err {
91-
t.Error("Expected error:", expected.err, " but got ", err)
92-
}
89+
assert.ErrorIs(t, expected.err, err)
9390
}
9491

9592
func TestCreateDeregisterJobPositiveScenarios(t *testing.T) {
@@ -194,9 +191,7 @@ func TestCreateDeregisterJobPositiveScenarios(t *testing.T) {
194191
mock.IsType([]k8sclient.CreateOption(nil)),
195192
).Return(nil)
196193
err := reconciler.createDeregisterJob(ctx, testClusterObj)
197-
if expected.err != err {
198-
t.Error("Expected error:", expected.err, " but got ", err)
199-
}
194+
assert.ErrorIs(t, expected.err, err)
200195
}
201196

202197
func TestReconcilerFailToUpdateClusterRegistrationStatus(t *testing.T) {
@@ -237,9 +232,7 @@ func TestReconcilerFailToUpdateClusterRegistrationStatus(t *testing.T) {
237232
).Return(errors.New("error updating status of deregistration on the controller"))
238233

239234
err := reconciler.createDeregisterJob(ctx, testClusterObj)
240-
if expected.errMsg != err.Error() {
241-
t.Error("Expected error:", expected.errMsg, " but got ", err)
242-
}
235+
assert.Equal(t, expected.errMsg, err.Error())
243236
}
244237

245238
func TestReconcilerFailToCreateServiceAccount(t *testing.T) {
@@ -295,9 +288,7 @@ func TestReconcilerFailToCreateServiceAccount(t *testing.T) {
295288
).Return(errors.New("unable to create service account"))
296289

297290
err := reconciler.createDeregisterJob(ctx, testClusterObj)
298-
if expected.errMsg != err.Error() {
299-
t.Error("Expected error:", expected.errMsg, " but got ", err)
300-
}
291+
assert.Equal(t, expected.errMsg, err.Error())
301292
}
302293

303294
func TestReconcilerFailToFetchOperatorClusterRole(t *testing.T) {
@@ -363,9 +354,7 @@ func TestReconcilerFailToFetchOperatorClusterRole(t *testing.T) {
363354
).Return(errors.New("unable to fetch operator clusterrole"))
364355

365356
err := reconciler.createDeregisterJob(ctx, testClusterObj)
366-
if expected.errMsg != err.Error() {
367-
t.Error("Expected error:", expected.errMsg, " but got ", err)
368-
}
357+
assert.Equal(t, expected.errMsg, err.Error())
369358
}
370359

371360
func TestReconcilerFailToCreateClusterRole(t *testing.T) {
@@ -441,9 +430,7 @@ func TestReconcilerFailToCreateClusterRole(t *testing.T) {
441430
).Return(errors.New("unable to create cluster role"))
442431

443432
err := reconciler.createDeregisterJob(ctx, testClusterObj)
444-
if expected.errMsg != err.Error() {
445-
t.Error("Expected error:", expected.errMsg, " but got ", err)
446-
}
433+
assert.Equal(t, expected.errMsg, err.Error())
447434
}
448435

449436
func TestReconcilerFailToCreateClusterRoleBinding(t *testing.T) {
@@ -524,9 +511,7 @@ func TestReconcilerFailToCreateClusterRoleBinding(t *testing.T) {
524511
).Return(errors.New("unable to create cluster rolebinding"))
525512

526513
err := reconciler.createDeregisterJob(ctx, testClusterObj)
527-
if expected.errMsg != err.Error() {
528-
t.Error("Expected error:", expected.errMsg, " but got ", err)
529-
}
514+
assert.Equal(t, expected.errMsg, err.Error())
530515
}
531516

532517
func TestReconcilerFailToCreateConfigmap(t *testing.T) {
@@ -621,9 +606,7 @@ func TestReconcilerFailToCreateConfigmap(t *testing.T) {
621606
mock.IsType([]k8sclient.CreateOption(nil)),
622607
).Return(errors.New("Unable to create configmap"))
623608
err := reconciler.createDeregisterJob(ctx, testClusterObj)
624-
if expected.errMsg != err.Error() {
625-
t.Error("Expected error:", expected.errMsg, " but got ", err)
626-
}
609+
assert.Equal(t, expected.errMsg, err.Error())
627610
}
628611

629612
func TestReconcilerFailToDeleteJob(t *testing.T) {
@@ -729,9 +712,7 @@ func TestReconcilerFailToDeleteJob(t *testing.T) {
729712
).Return(errors.New("Unable to delete deregister job"))
730713

731714
err := reconciler.createDeregisterJob(ctx, testClusterObj)
732-
if expected.errMsg != err.Error() {
733-
t.Error("Expected error:", expected.errMsg, " but got ", err)
734-
}
715+
assert.Equal(t, expected.errMsg, err.Error())
735716
}
736717

737718
func TestReconcilerFailToCreateDeregisterJob(t *testing.T) {
@@ -842,64 +823,42 @@ func TestReconcilerFailToCreateDeregisterJob(t *testing.T) {
842823
).Return(errors.New("Unable to create deregister job"))
843824

844825
err := reconciler.createDeregisterJob(ctx, testClusterObj)
845-
if expected.errMsg != err.Error() {
846-
t.Error("Expected error:", expected.errMsg, " but got ", err)
847-
}
826+
assert.Equal(t, expected.errMsg, err.Error())
848827
}
849828

850829
func TestGetConfigmapScriptData(t *testing.T) {
851830
data, err := getCleanupScript()
852-
AssertNoError(t, err)
853-
if len(data) == 0 {
854-
t.Fatalf("unable to get configmap data")
855-
}
831+
assert.NoError(t, err)
832+
assert.NotZero(t, len(data), "unable to get configmap data")
856833
}
857834

858835
func TestConstructJobForClusterDeregister(t *testing.T) {
859836
job := constructJobForClusterDeregister()
860-
AssertEqual(t, job.Name, deregisterJobName)
861-
AssertEqual(t, job.Namespace, ControlPlaneNamespace)
837+
assert.Equal(t, job.Name, deregisterJobName)
838+
assert.Equal(t, job.Namespace, ControlPlaneNamespace)
862839
}
863840

864841
func TestConstructServiceAccount(t *testing.T) {
865842
sa := constructServiceAccount()
866-
AssertEqual(t, sa.Name, serviceAccountName)
867-
AssertEqual(t, sa.Namespace, ControlPlaneNamespace)
843+
assert.Equal(t, sa.Name, serviceAccountName)
844+
assert.Equal(t, sa.Namespace, ControlPlaneNamespace)
868845
}
869846

870847
func TestConstructClusterRole(t *testing.T) {
871848
cr := constructClusterRole(testOperatorClusterRole, "random-uid")
872-
isEqual := reflect.DeepEqual(cr.Rules, testOperatorClusterRole.Rules)
873-
if !isEqual {
874-
t.Fatalf("got invalid data in clusterrole Rules: got -- %q want -- %q", &cr.Rules[0], &testOperatorClusterRole.Rules[0])
875-
}
849+
assert.Equal(t, cr.Rules, testOperatorClusterRole.Rules)
876850
}
877851

878852
func TestConstructClusterRoleBinding(t *testing.T) {
879853
crb := constructClusterRoleBinding("random-uid")
880-
AssertEqual(t, crb.Name, clusterRoleBindingName)
881-
AssertEqual(t, crb.RoleRef, testClusterRoleRef)
882-
AssertEqual(t, len(crb.Subjects), 1)
883-
AssertEqual(t, crb.Subjects[0], testClusterRoleBindingSubject[0])
854+
assert.Equal(t, crb.Name, clusterRoleBindingName)
855+
assert.Equal(t, crb.RoleRef, testClusterRoleRef)
856+
assert.Len(t, crb.Subjects, 1)
857+
assert.Equal(t, crb.Subjects[0], testClusterRoleBindingSubject[0])
884858
}
885859

886860
func TestConstructConfigMap(t *testing.T) {
887861
data := "this is the data."
888862
cm := constructConfigMap(data)
889-
AssertEqual(t, cm.Data["kubeslice-cleanup.sh"], data)
890-
}
891-
892-
func AssertEqual(t *testing.T, actual interface{}, expected interface{}) {
893-
t.Helper()
894-
if actual != expected {
895-
t.Log("expected --", expected, "actual --", actual)
896-
t.Fail()
897-
}
898-
}
899-
900-
func AssertNoError(t *testing.T, err error) {
901-
t.Helper()
902-
if err != nil {
903-
t.Errorf("Expected No Error but got %s, Stack:\n%s", err, string(debug.Stack()))
904-
}
863+
assert.Equal(t, cm.Data["kubeslice-cleanup.sh"], data)
905864
}

pkg/hub/controllers/cluster/reconciler_unit_test.go renamed to pkg/hub/controllers/cluster/reconciler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func TestReconcilerHandleExternalDependency(t *testing.T) {
178178
client.StatusMock.On("Update",
179179
mock.IsType(ctx),
180180
mock.IsType(&hubv1alpha1.Cluster{}),
181-
mock.IsType([]k8sclient.UpdateOption(nil)),
181+
mock.IsType([]k8sclient.SubResourceUpdateOption(nil)),
182182
).Return(nil)
183183
client.On("List",
184184
mock.IsType(ctx),
@@ -295,7 +295,7 @@ func TestReconcilerToFailWhileCallingCreateDeregisterJob(t *testing.T) {
295295
client.StatusMock.On("Update",
296296
mock.IsType(ctx),
297297
mock.IsType(&hubv1alpha1.Cluster{}),
298-
mock.IsType([]k8sclient.UpdateOption(nil)),
298+
mock.IsType([]k8sclient.SubResourceUpdateOption(nil)),
299299
).Return(errors.New("error updating status of deregistration on the controller"))
300300
client.On("Create",
301301
mock.IsType(ctx),

pkg/hub/controllers/vpnkeyrotation/reconciler_unit_test.go renamed to pkg/hub/controllers/vpnkeyrotation/reconciler_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ var (
4040
testWorkerNamespace = "kubeslice-system"
4141
)
4242

43+
var k8sClient k8sclient.Client
44+
4345
var testVPNKeyRotationObject = &hubv1alpha1.VpnKeyRotation{
4446
ObjectMeta: metav1.ObjectMeta{
4547
Name: testVPNKeyRotationName,

0 commit comments

Comments
 (0)