Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions controllers/controller/cluster_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package controller

import (
"context"

"github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

const (
clusterName1 = "cluster-avesha"
clusterNamespace1 = "kubeslice-" + clusterName1
clusterName2 = "cluster-demo"
clusterNamespace2 = "kubeslice-" + clusterName2
)

var _ = Describe("Cluster controller", func() {
When("Creating Cluster CR", func() {
It("should create Cluster CR and related resources without errors", func() {
By("Creating a new Cluster CR")
ctx := context.Background()

cluster := &v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName1,
Namespace: controlPlaneNamespace,
},
Spec: v1alpha1.ClusterSpec{
ClusterProperty: v1alpha1.ClusterProperty{
Telemetry: v1alpha1.Telemetry{
TelemetryProvider: "test-property",
},
},
},
}
Expect(k8sClient.Create(ctx, cluster)).Should(Succeed())

By("Looking up the created Cluster CR")
clusterLookupKey := types.NamespacedName{
Name: clusterName1,
Namespace: controlPlaneNamespace,
}
createdCluster := &v1alpha1.Cluster{}
Eventually(func() bool {
err := k8sClient.Get(ctx, clusterLookupKey, createdCluster)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Looking up the created Cluster Namespace")
nsLookupKey := types.NamespacedName{
Name: clusterNamespace1,
}
createdNS := &v1.Namespace{}
Eventually(func() bool {
err := k8sClient.Get(ctx, nsLookupKey, createdNS)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Looking up the created Cluster Role")
roleLookupKey := types.NamespacedName{
Name: "kubeslice-cluster-role",
Namespace: clusterNamespace1,
}
createdRole := &rbacv1.Role{}
Eventually(func() bool {
err := k8sClient.Get(ctx, roleLookupKey, createdRole)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Looking up the created Cluster Role Binding")
rbLookupKey := types.NamespacedName{
Name: "kubeslice-cluster-rolebinding",
Namespace: clusterNamespace1,
}
createdRB := &rbacv1.RoleBinding{}
Eventually(func() bool {
err := k8sClient.Get(ctx, rbLookupKey, createdRB)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Looking up the created Cluster Service Account")
saLookupKey := types.NamespacedName{
Name: "kubeslice-cluster-sa",
Namespace: clusterNamespace1,
}
createdSA := &v1.ServiceAccount{}
Eventually(func() bool {
err := k8sClient.Get(ctx, saLookupKey, createdSA)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Deleting the created Cluster CR")
Expect(k8sClient.Delete(ctx, createdCluster)).Should(Succeed())
Eventually(func() bool {
err := k8sClient.Get(ctx, clusterLookupKey, createdCluster)
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue())
})

It("should handle deletion of a Cluster CR gracefully", func() {
By("Creating a new Cluster CR")
ctx := context.Background()

cluster := &v1alpha1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName2,
Namespace: controlPlaneNamespace,
},
Spec: v1alpha1.ClusterSpec{
ClusterProperty: v1alpha1.ClusterProperty{
Telemetry: v1alpha1.Telemetry{
TelemetryProvider: "test-property-2",
},
},
},
}
Expect(k8sClient.Create(ctx, cluster)).Should(Succeed())

clusterLookupKey := types.NamespacedName{
Name: clusterName2,
Namespace: controlPlaneNamespace,
}

createdCluster := &v1alpha1.Cluster{}

By("Deleting the created Cluster CR")
Expect(k8sClient.Delete(ctx, cluster)).Should(Succeed())

By("Looking up the deleted Cluster CR")
Eventually(func() bool {
err := k8sClient.Get(ctx, clusterLookupKey, createdCluster)
return err != nil
}, timeout, interval).Should(BeTrue())
})
})
})
111 changes: 111 additions & 0 deletions controllers/controller/serviceexportconfig_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package controller

import (
"context"

controllerv1alpha1 "github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

const (
serviceExportConfigName1 = "sec-test-1"
serviceExportConfigName2 = "sec-test-2"
)

var _ = Describe("ServiceExportConfig controller", func() {
When("Creating ServiceExportConfig CR", func() {
It("should create ServiceExportConfig CR without errors", func() {
By("Creating a new ServiceExportConfig CR")
ctx := context.Background()

sec := &controllerv1alpha1.ServiceExportConfig{
ObjectMeta: metav1.ObjectMeta{
Name: serviceExportConfigName1,
Namespace: controlPlaneNamespace,
},
Spec: controllerv1alpha1.ServiceExportConfigSpec{
ServiceName: "test-service",
ServiceNamespace: "default",
SourceCluster: "test-cluster",
SliceName: "test-slice",
ServiceDiscoveryPorts: []controllerv1alpha1.ServiceDiscoveryPort{
{
Name: "http",
Protocol: "TCP",
Port: 80,
ServicePort: 8080,
ServiceProtocol: "TCP",
},
},
},
}
Expect(k8sClient.Create(ctx, sec)).Should(Succeed())

By("Looking up the created ServiceExportConfig CR")
secLookupKey := types.NamespacedName{
Name: serviceExportConfigName1,
Namespace: controlPlaneNamespace,
}
createdSec := &controllerv1alpha1.ServiceExportConfig{}
Eventually(func() bool {
err := k8sClient.Get(ctx, secLookupKey, createdSec)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Deleting the created ServiceExportConfig CR")
Expect(k8sClient.Delete(ctx, createdSec)).Should(Succeed())
Eventually(func() bool {
err := k8sClient.Get(ctx, secLookupKey, createdSec)
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue())
})

It("should handle deletion of a ServiceExportConfig CR gracefully", func() {
By("Creating a new ServiceExportConfig CR")
ctx := context.Background()

sec := &controllerv1alpha1.ServiceExportConfig{
ObjectMeta: metav1.ObjectMeta{
Name: serviceExportConfigName2,
Namespace: controlPlaneNamespace,
},
Spec: controllerv1alpha1.ServiceExportConfigSpec{
ServiceName: "another-service",
ServiceNamespace: "default",
SourceCluster: "test-cluster-2",
SliceName: "another-slice",
ServiceDiscoveryPorts: []controllerv1alpha1.ServiceDiscoveryPort{
{
Name: "https",
Protocol: "TCP",
Port: 443,
ServicePort: 8443,
ServiceProtocol: "TCP",
},
},
},
}
Expect(k8sClient.Create(ctx, sec)).Should(Succeed())

secLookupKey := types.NamespacedName{
Name: serviceExportConfigName2,
Namespace: controlPlaneNamespace,
}

createdSec := &controllerv1alpha1.ServiceExportConfig{}

By("Deleting the created ServiceExportConfig CR")
Expect(k8sClient.Delete(ctx, sec)).Should(Succeed())

By("Looking up the deleted ServiceExportConfig CR")
Eventually(func() bool {
err := k8sClient.Get(ctx, secLookupKey, createdSec)
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue())
})
})
})
111 changes: 111 additions & 0 deletions controllers/controller/sliceqosconfig_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package controller

import (
"context"

controllerv1alpha1 "github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

const (
sliceQoSConfigName1 = "sliceqos-test-1"
sliceQoSConfigName2 = "sliceqos-test-2"
)

var _ = Describe("SliceQoSConfig controller", func() {
When("Creating SliceQoSConfig CR", func() {
It("should create and delete SliceQoSConfig CR without errors", func() {
By("Creating a new SliceQoSConfig CR")
ctx := context.Background()

expectedSpec := controllerv1alpha1.SliceQoSConfigSpec{
QueueType: "HTB",
Priority: 1,
TcType: "BANDWIDTH_CONTROL",
BandwidthCeilingKbps: 100000,
BandwidthGuaranteedKbps: 50000,
DscpClass: "AF11",
}

sliceQoS := &controllerv1alpha1.SliceQoSConfig{
ObjectMeta: metav1.ObjectMeta{
Name: sliceQoSConfigName1,
Namespace: controlPlaneNamespace,
},
Spec: expectedSpec,
}
Expect(k8sClient.Create(ctx, sliceQoS)).Should(Succeed())

By("Looking up the created SliceQoSConfig CR")
sliceQoSLookupKey := types.NamespacedName{
Name: sliceQoSConfigName1,
Namespace: controlPlaneNamespace,
}
createdSliceQoS := &controllerv1alpha1.SliceQoSConfig{}
Eventually(func() bool {
err := k8sClient.Get(ctx, sliceQoSLookupKey, createdSliceQoS)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Verifying the created CR has the expected spec values")
Expect(createdSliceQoS.Spec).To(Equal(expectedSpec))

By("Deleting the created SliceQoSConfig CR")
Expect(k8sClient.Delete(ctx, createdSliceQoS)).Should(Succeed())
Eventually(func() bool {
err := k8sClient.Get(ctx, sliceQoSLookupKey, createdSliceQoS)
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue())
})

It("should handle deletion of a SliceQoSConfig CR gracefully", func() {
By("Creating a new SliceQoSConfig CR")
ctx := context.Background()

expectedSpec := controllerv1alpha1.SliceQoSConfigSpec{
QueueType: "HTB",
Priority: 2,
TcType: "BANDWIDTH_CONTROL",
BandwidthCeilingKbps: 200000,
BandwidthGuaranteedKbps: 100000,
DscpClass: "AF21",
}

sliceQoS := &controllerv1alpha1.SliceQoSConfig{
ObjectMeta: metav1.ObjectMeta{
Name: sliceQoSConfigName2,
Namespace: controlPlaneNamespace,
},
Spec: expectedSpec,
}
Expect(k8sClient.Create(ctx, sliceQoS)).Should(Succeed())

sliceQoSLookupKey := types.NamespacedName{
Name: sliceQoSConfigName2,
Namespace: controlPlaneNamespace,
}

createdSliceQoS := &controllerv1alpha1.SliceQoSConfig{}

By("Looking up the created CR and verifying spec values")
Eventually(func() bool {
err := k8sClient.Get(ctx, sliceQoSLookupKey, createdSliceQoS)
return err == nil
}, timeout, interval).Should(BeTrue())
Expect(createdSliceQoS.Spec).To(Equal(expectedSpec))

By("Deleting the created SliceQoSConfig CR")
Expect(k8sClient.Delete(ctx, sliceQoS)).Should(Succeed())

By("Ensuring the CR is actually deleted")
Eventually(func() bool {
err := k8sClient.Get(ctx, sliceQoSLookupKey, createdSliceQoS)
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue())
})
})
})
Loading
Loading