@@ -14,7 +14,6 @@ import (
14
14
corev1 "k8s.io/api/core/v1"
15
15
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
16
17
- tarantooliov1alpha1 "github.com/tarantool/tarantool-operator/api/v1alpha1"
18
17
"sigs.k8s.io/controller-runtime/pkg/client"
19
18
)
20
19
@@ -30,107 +29,78 @@ func RandStringRunes(n int) string {
30
29
31
30
var _ = Describe ("cluster_controller unit testing" , func () {
32
31
var (
33
- namespace = "default"
34
- ctx = context .TODO ()
35
-
36
- roleName = "" // setup for every spec in hook
37
- rsTemplateName = ""
38
-
32
+ ctx = context .TODO ()
33
+ namespace = "test"
39
34
clusterName = "test"
40
- clusterId = clusterName
41
-
42
- defaultRolesToAssign = "[\" A\" ,\" B\" ]"
35
+ clusterId = "test"
36
+ ns = & corev1.Namespace {
37
+ ObjectMeta : metav1.ObjectMeta {
38
+ Name : namespace ,
39
+ },
40
+ }
41
+ cartridge = helpers .NewCartridge (helpers.CartridgeParams {
42
+ Namespace : namespace ,
43
+ ClusterName : clusterName ,
44
+ ClusterID : clusterId ,
45
+ })
43
46
)
44
47
45
48
Describe ("cluster_controller manage cluster resources" , func () {
46
49
BeforeEach (func () {
47
- // setup variables for each spec
48
- roleName = fmt .Sprintf ("test-role-%s" , RandStringRunes (4 ))
49
- rsTemplateName = fmt .Sprintf ("test-rs-%s" , RandStringRunes (4 ))
50
-
51
- By ("create new Role " + roleName )
52
- role := helpers .NewRole (helpers.RoleParams {
53
- Name : roleName ,
54
- Namespace : namespace ,
55
- RolesToAssign : defaultRolesToAssign ,
56
- RsNum : int32 (1 ),
57
- RsTemplateName : rsTemplateName ,
58
- ClusterId : clusterId ,
59
- })
60
- // mock owner reference
61
- role .SetOwnerReferences ([]metav1.OwnerReference {
62
- {
63
- APIVersion : "v0" ,
64
- Kind : "mockRef" ,
65
- Name : "mockRef" ,
66
- UID : "-" ,
67
- },
68
- })
69
- Expect (k8sClient .Create (ctx , & role )).NotTo (HaveOccurred (), "failed to create Role" )
70
-
71
- By ("create new Cluster " + clusterName )
72
- cluster := helpers .NewCluster (helpers.ClusterParams {
73
- Name : clusterName ,
74
- Namespace : namespace ,
75
- Id : clusterId ,
76
- })
77
- Expect (k8sClient .Create (ctx , & cluster )).NotTo (HaveOccurred (), "failed to create Cluster" )
50
+ Expect (k8sClient .Create (ctx , ns )).
51
+ NotTo (
52
+ HaveOccurred (),
53
+ fmt .Sprintf ("failed to create Namespace %s" , ns .GetName ()),
54
+ )
55
+
56
+ Expect (k8sClient .Create (ctx , cartridge .Cluster )).
57
+ NotTo (
58
+ HaveOccurred (),
59
+ fmt .Sprintf ("failed to create Cluster %s" , cartridge .Cluster .GetName ()),
60
+ )
61
+
62
+ for _ , role := range cartridge .Roles {
63
+ Expect (k8sClient .Create (ctx , role )).
64
+ NotTo (
65
+ HaveOccurred (),
66
+ fmt .Sprintf ("failed to create Role %s" , role .GetName ()),
67
+ )
68
+ }
69
+
70
+ for _ , rs := range cartridge .ReplicasetTemplates {
71
+ Expect (k8sClient .Create (ctx , rs )).
72
+ NotTo (
73
+ HaveOccurred (),
74
+ fmt .Sprintf ("failed to create ReplicasetTemplate %s" , rs .GetName ()),
75
+ )
76
+ }
77
+
78
+ for _ , svc := range cartridge .Services {
79
+ Expect (k8sClient .Create (ctx , svc )).
80
+ NotTo (
81
+ HaveOccurred (),
82
+ fmt .Sprintf ("failed to create Service %s" , svc .GetName ()),
83
+ )
84
+ }
78
85
})
79
86
80
87
AfterEach (func () {
81
- By ("remove role object " + roleName )
82
- role := & tarantooliov1alpha1.Role {}
83
- Expect (
84
- k8sClient .Get (ctx , client.ObjectKey {Name : roleName , Namespace : namespace }, role ),
85
- ).NotTo (HaveOccurred (), "failed to get Role" )
86
-
87
- Expect (k8sClient .Delete (ctx , role )).NotTo (HaveOccurred (), "failed to delete Role" )
88
-
89
- By ("remove Cluster object " + clusterName )
90
- cluster := & tarantooliov1alpha1.Cluster {}
88
+ By ("remove Namespace object " + namespace )
89
+ ns := & corev1.Namespace {}
91
90
Expect (
92
- k8sClient .Get (ctx , client.ObjectKey {Name : clusterName , Namespace : namespace }, cluster ),
93
- ).NotTo (HaveOccurred (), "failed to get Cluster " )
91
+ k8sClient .Get (ctx , client.ObjectKey {Name : namespace }, ns ),
92
+ ).NotTo (HaveOccurred (), "failed to get Namespace " )
94
93
95
- Expect (k8sClient .Delete (ctx , cluster )).NotTo (HaveOccurred (), "failed to delete Cluster " )
94
+ Expect (k8sClient .Delete (ctx , ns )).NotTo (HaveOccurred (), "failed to delete Namespace " )
96
95
})
97
96
98
97
Context ("manage cluster leader: tarantool instance accepting admin requests" , func () {
99
- BeforeEach (func () {
100
- By ("create cluster endpoints" )
101
- ep := corev1.Endpoints {
102
- ObjectMeta : metav1.ObjectMeta {
103
- Name : clusterId ,
104
- Namespace : namespace ,
105
- },
106
- Subsets : []corev1.EndpointSubset {
107
- {
108
- Addresses : []corev1.EndpointAddress {
109
- {IP : "1.1.1.1" },
110
- {IP : "2.2.2.2" },
111
- {IP : "3.3.3.3" },
112
- },
113
- },
114
- },
115
- }
116
- Expect (k8sClient .Create (ctx , & ep )).NotTo (HaveOccurred (), "failed to create cluster endpoints" )
117
- })
118
-
119
- AfterEach (func () {
120
- ep := corev1.Endpoints {}
121
- Expect (
122
- k8sClient .Get (ctx , client.ObjectKey {Name : clusterId , Namespace : namespace }, & ep ),
123
- ).NotTo (HaveOccurred (), "failed to get cluster endpoints" )
124
-
125
- Expect (k8sClient .Delete (ctx , & ep )).NotTo (HaveOccurred (), "failed to delete endpoints" )
126
- })
127
-
128
98
It ("change the leader if the previous one does not exist" , func () {
129
99
By ("get the chosen leader" )
130
100
ep := corev1.Endpoints {}
131
101
Eventually (
132
102
func () bool {
133
- err := k8sClient .Get (ctx , client.ObjectKey {Name : clusterId , Namespace : namespace }, & ep )
103
+ err := k8sClient .Get (ctx , client.ObjectKey {Name : clusterName , Namespace : namespace }, & ep )
134
104
if err != nil {
135
105
return false
136
106
}
@@ -141,7 +111,8 @@ var _ = Describe("cluster_controller unit testing", func() {
141
111
142
112
return false
143
113
},
144
- time .Second * 100 , time .Millisecond * 500 ,
114
+ 2 * time .Minute ,
115
+ 500 * time .Millisecond ,
145
116
).Should (BeTrue ())
146
117
147
118
By ("save old leader" )
@@ -172,13 +143,14 @@ var _ = Describe("cluster_controller unit testing", func() {
172
143
}
173
144
return false
174
145
},
175
- time .Second * 10 , time .Millisecond * 500 ,
146
+ 2 * time .Minute ,
147
+ 500 * time .Millisecond ,
176
148
).Should (BeTrue ())
177
149
})
178
150
})
179
151
})
180
152
181
- Describe ("cluster_contriller unit testing functions" , func () {
153
+ Describe ("cluster_controller unit testing functions" , func () {
182
154
Describe ("function IsLeaderExists must check for existence of leader in annotation of cluster Endpoints" , func () {
183
155
Context ("positive cases (leader exist)" , func () {
184
156
It ("should return True if leader assigned and exist" , func () {
0 commit comments