Skip to content

Commit

Permalink
tests(integration): Move integration tests to tests/
Browse files Browse the repository at this point in the history
Signed-off-by: Bhargav Ravuri <[email protected]>
  • Loading branch information
Bhargav-InfraCloud committed Mar 6, 2024
1 parent 76c298a commit 3fcd049
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 149 deletions.
25 changes: 17 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,33 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

##@ Test

.PHONY: fmt
fmt: ## Run go fmt against code.
fmt: ## Run go fmt against code.
go fmt ./...

.PHONY: vet
vet: ## Run go vet against code.
vet: ## Run go vet against code.
go vet ./...

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

.PHONY: unit-test-coverage
unit-test-coverage: test
.PHONY: test-coverage
test-coverage: test-unit ## Run unit tests and print code coverage.
go tool cover -func coverage.out

.PHONY: test-integration
test-integration: ## Run integration tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -v -tags=integration ./...

.PHONY: test
test: fmt vet envtest test-unit test-integration ## Run all tests (fmt, vet, envtest, unit & integration).

.PHONY: test-docker
test-docker:
test-docker: ## Run all tests (fmt, vet, envtest, unit & integration) inside a Docker container.
docker build -t test -f test.Dockerfile . && docker run test

##@ Build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package cluster
import (
"context"
"errors"
"reflect"
"runtime/debug"
"testing"

hubv1alpha1 "github.com/kubeslice/apis/pkg/controller/v1alpha1"
"github.com/kubeslice/kubeslice-monitoring/pkg/metrics"
kubeslicev1beta1 "github.com/kubeslice/worker-operator/api/v1beta1"
utilmock "github.com/kubeslice/worker-operator/pkg/mocks"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -87,9 +86,7 @@ func TestGetOperatorClusterRole(t *testing.T) {
mock.IsType(&rbacv1.ClusterRole{}),
).Return(nil)
_, err := reconciler.getOperatorClusterRole(ctx)
if expected.err != err {
t.Error("Expected error:", expected.err, " but got ", err)
}
assert.ErrorIs(t, expected.err, err)
}

func TestCreateDeregisterJobPositiveScenarios(t *testing.T) {
Expand Down Expand Up @@ -194,9 +191,7 @@ func TestCreateDeregisterJobPositiveScenarios(t *testing.T) {
mock.IsType([]k8sclient.CreateOption(nil)),
).Return(nil)
err := reconciler.createDeregisterJob(ctx, testClusterObj)
if expected.err != err {
t.Error("Expected error:", expected.err, " but got ", err)
}
assert.ErrorIs(t, expected.err, err)
}

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

err := reconciler.createDeregisterJob(ctx, testClusterObj)
if expected.errMsg != err.Error() {
t.Error("Expected error:", expected.errMsg, " but got ", err)
}
assert.Equal(t, expected.errMsg, err.Error())
}

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

err := reconciler.createDeregisterJob(ctx, testClusterObj)
if expected.errMsg != err.Error() {
t.Error("Expected error:", expected.errMsg, " but got ", err)
}
assert.Equal(t, expected.errMsg, err.Error())
}

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

err := reconciler.createDeregisterJob(ctx, testClusterObj)
if expected.errMsg != err.Error() {
t.Error("Expected error:", expected.errMsg, " but got ", err)
}
assert.Equal(t, expected.errMsg, err.Error())
}

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

err := reconciler.createDeregisterJob(ctx, testClusterObj)
if expected.errMsg != err.Error() {
t.Error("Expected error:", expected.errMsg, " but got ", err)
}
assert.Equal(t, expected.errMsg, err.Error())
}

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

err := reconciler.createDeregisterJob(ctx, testClusterObj)
if expected.errMsg != err.Error() {
t.Error("Expected error:", expected.errMsg, " but got ", err)
}
assert.Equal(t, expected.errMsg, err.Error())
}

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

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

err := reconciler.createDeregisterJob(ctx, testClusterObj)
if expected.errMsg != err.Error() {
t.Error("Expected error:", expected.errMsg, " but got ", err)
}
assert.Equal(t, expected.errMsg, err.Error())
}

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

err := reconciler.createDeregisterJob(ctx, testClusterObj)
if expected.errMsg != err.Error() {
t.Error("Expected error:", expected.errMsg, " but got ", err)
}
assert.Equal(t, expected.errMsg, err.Error())
}

func TestGetConfigmapScriptData(t *testing.T) {
data, err := getCleanupScript()
AssertNoError(t, err)
if len(data) == 0 {
t.Fatalf("unable to get configmap data")
}
assert.NoError(t, err)
assert.NotZero(t, len(data), "unable to get configmap data")
}

func TestConstructJobForClusterDeregister(t *testing.T) {
job := constructJobForClusterDeregister()
AssertEqual(t, job.Name, deregisterJobName)
AssertEqual(t, job.Namespace, ControlPlaneNamespace)
assert.Equal(t, job.Name, deregisterJobName)
assert.Equal(t, job.Namespace, ControlPlaneNamespace)
}

func TestConstructServiceAccount(t *testing.T) {
sa := constructServiceAccount()
AssertEqual(t, sa.Name, serviceAccountName)
AssertEqual(t, sa.Namespace, ControlPlaneNamespace)
assert.Equal(t, sa.Name, serviceAccountName)
assert.Equal(t, sa.Namespace, ControlPlaneNamespace)
}

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

func TestConstructClusterRoleBinding(t *testing.T) {
crb := constructClusterRoleBinding("random-uid")
AssertEqual(t, crb.Name, clusterRoleBindingName)
AssertEqual(t, crb.RoleRef, testClusterRoleRef)
AssertEqual(t, len(crb.Subjects), 1)
AssertEqual(t, crb.Subjects[0], testClusterRoleBindingSubject[0])
assert.Equal(t, crb.Name, clusterRoleBindingName)
assert.Equal(t, crb.RoleRef, testClusterRoleRef)
assert.Len(t, crb.Subjects, 1)
assert.Equal(t, crb.Subjects[0], testClusterRoleBindingSubject[0])
}

func TestConstructConfigMap(t *testing.T) {
data := "this is the data."
cm := constructConfigMap(data)
AssertEqual(t, cm.Data["kubeslice-cleanup.sh"], data)
}

func AssertEqual(t *testing.T, actual interface{}, expected interface{}) {
t.Helper()
if actual != expected {
t.Log("expected --", expected, "actual --", actual)
t.Fail()
}
}

func AssertNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("Expected No Error but got %s, Stack:\n%s", err, string(debug.Stack()))
}
assert.Equal(t, cm.Data["kubeslice-cleanup.sh"], data)
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestReconcilerHandleExternalDependency(t *testing.T) {
client.StatusMock.On("Update",
mock.IsType(ctx),
mock.IsType(&hubv1alpha1.Cluster{}),
mock.IsType([]k8sclient.UpdateOption(nil)),
mock.IsType([]k8sclient.SubResourceUpdateOption(nil)),
).Return(nil)
client.On("List",
mock.IsType(ctx),
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestReconcilerToFailWhileCallingCreateDeregisterJob(t *testing.T) {
client.StatusMock.On("Update",
mock.IsType(ctx),
mock.IsType(&hubv1alpha1.Cluster{}),
mock.IsType([]k8sclient.UpdateOption(nil)),
mock.IsType([]k8sclient.SubResourceUpdateOption(nil)),
).Return(errors.New("error updating status of deregistration on the controller"))
client.On("Create",
mock.IsType(ctx),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var (
testWorkerNamespace = "kubeslice-system"
)

var k8sClient k8sclient.Client

var testVPNKeyRotationObject = &hubv1alpha1.VpnKeyRotation{
ObjectMeta: metav1.ObjectMeta{
Name: testVPNKeyRotationName,
Expand Down
31 changes: 0 additions & 31 deletions pkg/manifest/manifest_suite_test.go

This file was deleted.

31 changes: 0 additions & 31 deletions pkg/webhook/pod/pod_suite_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion test.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.20 as builder
FROM golang:1.21 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
kubeslicev1beta1 "github.com/kubeslice/worker-operator/api/v1beta1"
ossEvents "github.com/kubeslice/worker-operator/events"
"github.com/kubeslice/worker-operator/pkg/hub/controllers"
"github.com/kubeslice/worker-operator/pkg/hub/controllers/cluster"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -54,7 +55,7 @@ var _ = BeforeSuite(func() {
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("../../../..", "config", "crd", "bases"),
filepath.Join("../../../", "config", "crd", "bases"),
filepath.Join("./crds"),
},
ErrorIfCRDPathMissing: true,
Expand Down Expand Up @@ -127,7 +128,7 @@ var _ = BeforeSuite(func() {
Component: "worker-operator",
Namespace: CONTROL_PLANE_NS,
})
clusterReconciler := NewReconciler(
clusterReconciler := cluster.NewReconciler(
k8sClient,
k8sClient,
&spokeClusterEventRecorder,
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 3fcd049

Please sign in to comment.