Skip to content

Commit dca4739

Browse files
add unit tests for core and worker controllers
Signed-off-by: andoriyaprashant <[email protected]>
1 parent 41c35b3 commit dca4739

7 files changed

+865
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
6+
"github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
v1 "k8s.io/api/core/v1"
10+
rbacv1 "k8s.io/api/rbac/v1"
11+
"k8s.io/apimachinery/pkg/api/errors"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/apimachinery/pkg/types"
14+
)
15+
16+
const (
17+
clusterName1 = "cluster-avesha"
18+
clusterNamespace1 = "kubeslice-" + clusterName1
19+
clusterName2 = "cluster-demo"
20+
clusterNamespace2 = "kubeslice-" + clusterName2
21+
)
22+
23+
var _ = Describe("Cluster controller", func() {
24+
When("Creating Cluster CR", func() {
25+
It("should create Cluster CR and related resources without errors", func() {
26+
By("Creating a new Cluster CR")
27+
ctx := context.Background()
28+
29+
cluster := &v1alpha1.Cluster{
30+
ObjectMeta: metav1.ObjectMeta{
31+
Name: clusterName1,
32+
Namespace: controlPlaneNamespace,
33+
},
34+
Spec: v1alpha1.ClusterSpec{
35+
ClusterProperty: v1alpha1.ClusterProperty{
36+
Telemetry: v1alpha1.Telemetry{
37+
TelemetryProvider: "test-property",
38+
},
39+
},
40+
},
41+
}
42+
Expect(k8sClient.Create(ctx, cluster)).Should(Succeed())
43+
44+
By("Looking up the created Cluster CR")
45+
clusterLookupKey := types.NamespacedName{
46+
Name: clusterName1,
47+
Namespace: controlPlaneNamespace,
48+
}
49+
createdCluster := &v1alpha1.Cluster{}
50+
Eventually(func() bool {
51+
err := k8sClient.Get(ctx, clusterLookupKey, createdCluster)
52+
return err == nil
53+
}, timeout, interval).Should(BeTrue())
54+
55+
By("Looking up the created Cluster Namespace")
56+
nsLookupKey := types.NamespacedName{
57+
Name: clusterNamespace1,
58+
}
59+
createdNS := &v1.Namespace{}
60+
Eventually(func() bool {
61+
err := k8sClient.Get(ctx, nsLookupKey, createdNS)
62+
return err == nil
63+
}, timeout, interval).Should(BeTrue())
64+
65+
By("Looking up the created Cluster Role")
66+
roleLookupKey := types.NamespacedName{
67+
Name: "kubeslice-cluster-role",
68+
Namespace: clusterNamespace1,
69+
}
70+
createdRole := &rbacv1.Role{}
71+
Eventually(func() bool {
72+
err := k8sClient.Get(ctx, roleLookupKey, createdRole)
73+
return err == nil
74+
}, timeout, interval).Should(BeTrue())
75+
76+
By("Looking up the created Cluster Role Binding")
77+
rbLookupKey := types.NamespacedName{
78+
Name: "kubeslice-cluster-rolebinding",
79+
Namespace: clusterNamespace1,
80+
}
81+
createdRB := &rbacv1.RoleBinding{}
82+
Eventually(func() bool {
83+
err := k8sClient.Get(ctx, rbLookupKey, createdRB)
84+
return err == nil
85+
}, timeout, interval).Should(BeTrue())
86+
87+
By("Looking up the created Cluster Service Account")
88+
saLookupKey := types.NamespacedName{
89+
Name: "kubeslice-cluster-sa",
90+
Namespace: clusterNamespace1,
91+
}
92+
createdSA := &v1.ServiceAccount{}
93+
Eventually(func() bool {
94+
err := k8sClient.Get(ctx, saLookupKey, createdSA)
95+
return err == nil
96+
}, timeout, interval).Should(BeTrue())
97+
98+
By("Deleting the created Cluster CR")
99+
Expect(k8sClient.Delete(ctx, createdCluster)).Should(Succeed())
100+
Eventually(func() bool {
101+
err := k8sClient.Get(ctx, clusterLookupKey, createdCluster)
102+
return errors.IsNotFound(err)
103+
}, timeout, interval).Should(BeTrue())
104+
})
105+
106+
It("should handle deletion of a Cluster CR gracefully", func() {
107+
By("Creating a new Cluster CR")
108+
ctx := context.Background()
109+
110+
cluster := &v1alpha1.Cluster{
111+
ObjectMeta: metav1.ObjectMeta{
112+
Name: clusterName2,
113+
Namespace: controlPlaneNamespace,
114+
},
115+
Spec: v1alpha1.ClusterSpec{
116+
ClusterProperty: v1alpha1.ClusterProperty{
117+
Telemetry: v1alpha1.Telemetry{
118+
TelemetryProvider: "test-property-2",
119+
},
120+
},
121+
},
122+
}
123+
Expect(k8sClient.Create(ctx, cluster)).Should(Succeed())
124+
125+
clusterLookupKey := types.NamespacedName{
126+
Name: clusterName2,
127+
Namespace: controlPlaneNamespace,
128+
}
129+
130+
createdCluster := &v1alpha1.Cluster{}
131+
132+
By("Deleting the created Cluster CR")
133+
Expect(k8sClient.Delete(ctx, cluster)).Should(Succeed())
134+
135+
By("Looking up the deleted Cluster CR")
136+
Eventually(func() bool {
137+
err := k8sClient.Get(ctx, clusterLookupKey, createdCluster)
138+
return err != nil
139+
}, timeout, interval).Should(BeTrue())
140+
})
141+
})
142+
})
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
6+
controllerv1alpha1 "github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
"k8s.io/apimachinery/pkg/api/errors"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/types"
12+
)
13+
14+
const (
15+
serviceExportConfigName1 = "sec-test-1"
16+
serviceExportConfigName2 = "sec-test-2"
17+
)
18+
19+
var _ = Describe("ServiceExportConfig controller", func() {
20+
When("Creating ServiceExportConfig CR", func() {
21+
It("should create ServiceExportConfig CR without errors", func() {
22+
By("Creating a new ServiceExportConfig CR")
23+
ctx := context.Background()
24+
25+
sec := &controllerv1alpha1.ServiceExportConfig{
26+
ObjectMeta: metav1.ObjectMeta{
27+
Name: serviceExportConfigName1,
28+
Namespace: controlPlaneNamespace,
29+
},
30+
Spec: controllerv1alpha1.ServiceExportConfigSpec{
31+
ServiceName: "test-service",
32+
ServiceNamespace: "default",
33+
SourceCluster: "test-cluster",
34+
SliceName: "test-slice",
35+
ServiceDiscoveryPorts: []controllerv1alpha1.ServiceDiscoveryPort{
36+
{
37+
Name: "http",
38+
Protocol: "TCP",
39+
Port: 80,
40+
ServicePort: 8080,
41+
ServiceProtocol: "TCP",
42+
},
43+
},
44+
},
45+
}
46+
Expect(k8sClient.Create(ctx, sec)).Should(Succeed())
47+
48+
By("Looking up the created ServiceExportConfig CR")
49+
secLookupKey := types.NamespacedName{
50+
Name: serviceExportConfigName1,
51+
Namespace: controlPlaneNamespace,
52+
}
53+
createdSec := &controllerv1alpha1.ServiceExportConfig{}
54+
Eventually(func() bool {
55+
err := k8sClient.Get(ctx, secLookupKey, createdSec)
56+
return err == nil
57+
}, timeout, interval).Should(BeTrue())
58+
59+
By("Deleting the created ServiceExportConfig CR")
60+
Expect(k8sClient.Delete(ctx, createdSec)).Should(Succeed())
61+
Eventually(func() bool {
62+
err := k8sClient.Get(ctx, secLookupKey, createdSec)
63+
return errors.IsNotFound(err)
64+
}, timeout, interval).Should(BeTrue())
65+
})
66+
67+
It("should handle deletion of a ServiceExportConfig CR gracefully", func() {
68+
By("Creating a new ServiceExportConfig CR")
69+
ctx := context.Background()
70+
71+
sec := &controllerv1alpha1.ServiceExportConfig{
72+
ObjectMeta: metav1.ObjectMeta{
73+
Name: serviceExportConfigName2,
74+
Namespace: controlPlaneNamespace,
75+
},
76+
Spec: controllerv1alpha1.ServiceExportConfigSpec{
77+
ServiceName: "another-service",
78+
ServiceNamespace: "default",
79+
SourceCluster: "test-cluster-2",
80+
SliceName: "another-slice",
81+
ServiceDiscoveryPorts: []controllerv1alpha1.ServiceDiscoveryPort{
82+
{
83+
Name: "https",
84+
Protocol: "TCP",
85+
Port: 443,
86+
ServicePort: 8443,
87+
ServiceProtocol: "TCP",
88+
},
89+
},
90+
},
91+
}
92+
Expect(k8sClient.Create(ctx, sec)).Should(Succeed())
93+
94+
secLookupKey := types.NamespacedName{
95+
Name: serviceExportConfigName2,
96+
Namespace: controlPlaneNamespace,
97+
}
98+
99+
createdSec := &controllerv1alpha1.ServiceExportConfig{}
100+
101+
By("Deleting the created ServiceExportConfig CR")
102+
Expect(k8sClient.Delete(ctx, sec)).Should(Succeed())
103+
104+
By("Looking up the deleted ServiceExportConfig CR")
105+
Eventually(func() bool {
106+
err := k8sClient.Get(ctx, secLookupKey, createdSec)
107+
return errors.IsNotFound(err)
108+
}, timeout, interval).Should(BeTrue())
109+
})
110+
})
111+
})
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package controller
2+
3+
import (
4+
"context"
5+
6+
controllerv1alpha1 "github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
"k8s.io/apimachinery/pkg/api/errors"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/apimachinery/pkg/types"
12+
)
13+
14+
const (
15+
sliceQoSConfigName1 = "sliceqos-test-1"
16+
sliceQoSConfigName2 = "sliceqos-test-2"
17+
)
18+
19+
var _ = Describe("SliceQoSConfig controller", func() {
20+
When("Creating SliceQoSConfig CR", func() {
21+
It("should create and delete SliceQoSConfig CR without errors", func() {
22+
By("Creating a new SliceQoSConfig CR")
23+
ctx := context.Background()
24+
25+
expectedSpec := controllerv1alpha1.SliceQoSConfigSpec{
26+
QueueType: "HTB",
27+
Priority: 1,
28+
TcType: "BANDWIDTH_CONTROL",
29+
BandwidthCeilingKbps: 100000,
30+
BandwidthGuaranteedKbps: 50000,
31+
DscpClass: "AF11",
32+
}
33+
34+
sliceQoS := &controllerv1alpha1.SliceQoSConfig{
35+
ObjectMeta: metav1.ObjectMeta{
36+
Name: sliceQoSConfigName1,
37+
Namespace: controlPlaneNamespace,
38+
},
39+
Spec: expectedSpec,
40+
}
41+
Expect(k8sClient.Create(ctx, sliceQoS)).Should(Succeed())
42+
43+
By("Looking up the created SliceQoSConfig CR")
44+
sliceQoSLookupKey := types.NamespacedName{
45+
Name: sliceQoSConfigName1,
46+
Namespace: controlPlaneNamespace,
47+
}
48+
createdSliceQoS := &controllerv1alpha1.SliceQoSConfig{}
49+
Eventually(func() bool {
50+
err := k8sClient.Get(ctx, sliceQoSLookupKey, createdSliceQoS)
51+
return err == nil
52+
}, timeout, interval).Should(BeTrue())
53+
54+
By("Verifying the created CR has the expected spec values")
55+
Expect(createdSliceQoS.Spec).To(Equal(expectedSpec))
56+
57+
By("Deleting the created SliceQoSConfig CR")
58+
Expect(k8sClient.Delete(ctx, createdSliceQoS)).Should(Succeed())
59+
Eventually(func() bool {
60+
err := k8sClient.Get(ctx, sliceQoSLookupKey, createdSliceQoS)
61+
return errors.IsNotFound(err)
62+
}, timeout, interval).Should(BeTrue())
63+
})
64+
65+
It("should handle deletion of a SliceQoSConfig CR gracefully", func() {
66+
By("Creating a new SliceQoSConfig CR")
67+
ctx := context.Background()
68+
69+
expectedSpec := controllerv1alpha1.SliceQoSConfigSpec{
70+
QueueType: "HTB",
71+
Priority: 2,
72+
TcType: "BANDWIDTH_CONTROL",
73+
BandwidthCeilingKbps: 200000,
74+
BandwidthGuaranteedKbps: 100000,
75+
DscpClass: "AF21",
76+
}
77+
78+
sliceQoS := &controllerv1alpha1.SliceQoSConfig{
79+
ObjectMeta: metav1.ObjectMeta{
80+
Name: sliceQoSConfigName2,
81+
Namespace: controlPlaneNamespace,
82+
},
83+
Spec: expectedSpec,
84+
}
85+
Expect(k8sClient.Create(ctx, sliceQoS)).Should(Succeed())
86+
87+
sliceQoSLookupKey := types.NamespacedName{
88+
Name: sliceQoSConfigName2,
89+
Namespace: controlPlaneNamespace,
90+
}
91+
92+
createdSliceQoS := &controllerv1alpha1.SliceQoSConfig{}
93+
94+
By("Looking up the created CR and verifying spec values")
95+
Eventually(func() bool {
96+
err := k8sClient.Get(ctx, sliceQoSLookupKey, createdSliceQoS)
97+
return err == nil
98+
}, timeout, interval).Should(BeTrue())
99+
Expect(createdSliceQoS.Spec).To(Equal(expectedSpec))
100+
101+
By("Deleting the created SliceQoSConfig CR")
102+
Expect(k8sClient.Delete(ctx, sliceQoS)).Should(Succeed())
103+
104+
By("Ensuring the CR is actually deleted")
105+
Eventually(func() bool {
106+
err := k8sClient.Get(ctx, sliceQoSLookupKey, createdSliceQoS)
107+
return errors.IsNotFound(err)
108+
}, timeout, interval).Should(BeTrue())
109+
})
110+
})
111+
})

0 commit comments

Comments
 (0)