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 b307957 commit ef37b7c
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 114 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 @@ -4,14 +4,14 @@ 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 @@ -849,22 +849,20 @@ func TestReconcilerFailToCreateDeregisterJob(t *testing.T) {

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) {
Expand All @@ -877,29 +875,14 @@ func TestConstructClusterRole(t *testing.T) {

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.Equal(t, len(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.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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/vpnkeyrotation"
nsmv1 "github.com/networkservicemesh/sdk-k8s/pkg/tools/k8s/apis/networkservicemesh.io/v1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -75,7 +76,7 @@ var _ = Describe("Hub VPN Key Rotation", func() {
ClusterGatewayMapping: map[string][]string{
CLUSTER_NAME: {gws[0]},
},
Clusters: []string{ClusterName},
Clusters: []string{vpnkeyrotation.ClusterName},
CertificateCreationTime: &metav1.Time{Time: time.Now()},
},
}
Expand Down Expand Up @@ -178,7 +179,7 @@ var _ = Describe("Hub VPN Key Rotation", func() {
ClusterGatewayMapping: map[string][]string{
CLUSTER_NAME: gws,
},
Clusters: []string{ClusterName},
Clusters: []string{vpnkeyrotation.ClusterName},
CertificateCreationTime: &metav1.Time{Time: time.Now()},
CertificateExpiryTime: &metav1.Time{Time: time.Now().AddDate(0, 0, 30)},
RotationInterval: 30,
Expand Down Expand Up @@ -517,7 +518,7 @@ var _ = Describe("Hub VPN Key Rotation", func() {
ClusterGatewayMapping: map[string][]string{
CLUSTER_NAME: {gws[0]},
},
Clusters: []string{ClusterName},
Clusters: []string{vpnkeyrotation.ClusterName},
CertificateCreationTime: &metav1.Time{Time: time.Now()},
CertificateExpiryTime: &metav1.Time{Time: time.Now().AddDate(0, 0, 30)},
RotationInterval: 30,
Expand Down Expand Up @@ -745,7 +746,7 @@ var _ = Describe("Hub VPN Key Rotation", func() {
ClusterGatewayMapping: map[string][]string{
"worker-1": gws,
},
Clusters: []string{ClusterName},
Clusters: []string{vpnkeyrotation.ClusterName},
CertificateCreationTime: &metav1.Time{Time: time.Now()},
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/kubeslice/worker-operator/controllers/slicegateway"
ossEvents "github.com/kubeslice/worker-operator/events"
"github.com/kubeslice/worker-operator/pkg/hub/controllers"
"github.com/kubeslice/worker-operator/pkg/hub/controllers/vpnkeyrotation"
hub "github.com/kubeslice/worker-operator/pkg/hub/hubclient"
hce "github.com/kubeslice/worker-operator/tests/emulator/hubclient"
workernetop "github.com/kubeslice/worker-operator/tests/emulator/workerclient/netop"
Expand Down Expand Up @@ -152,7 +153,7 @@ var _ = BeforeSuite(func() {
if err != nil {
os.Exit(1)
}
rotationReconciler := NewReconciler(
rotationReconciler := vpnkeyrotation.NewReconciler(
k8sClient,
&hub.HubClientConfig{
Client: k8sClient,
Expand Down Expand Up @@ -222,7 +223,7 @@ var _ = AfterSuite(func() {
func shouldProcessVpnKeyRotation(object client.Object) bool {
vpn := object.(*hubv1alpha1.VpnKeyRotation)
for _, v := range vpn.Spec.Clusters {
if v == ClusterName {
if v == vpnkeyrotation.ClusterName {
return true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@
package manifest_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/kubeslice/worker-operator/pkg/manifest"
appsv1 "k8s.io/api/apps/v1"
)

func TestManifest(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Manifest Suite")
}

var _ = Describe("Manifest File", func() {

Context("With k8s deployment manifest", func() {
Expand Down
14 changes: 10 additions & 4 deletions pkg/webhook/pod/webhook_test.go → tests/webhook/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ package pod_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/client"
"testing"

"github.com/kubeslice/worker-operator/controllers"
"github.com/kubeslice/worker-operator/pkg/webhook/pod"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func TestDeploy(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Deploy Webhook Suite")
}

type fakeWebhookClient struct{}

func (f fakeWebhookClient) UpdateSliceApplicationNamespaces(ctx context.Context, slice string, namespace string) error {
Expand Down

0 comments on commit ef37b7c

Please sign in to comment.