-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcluster_controller_test.go
More file actions
142 lines (125 loc) · 4.08 KB
/
cluster_controller_test.go
File metadata and controls
142 lines (125 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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())
})
})
})