-
Notifications
You must be signed in to change notification settings - Fork 199
/
kafkatopic_validator_test.go
345 lines (306 loc) · 11.6 KB
/
kafkatopic_validator_test.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright © 2022 Cisco Systems, Inc. and/or its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package webhooks
import (
"context"
"strings"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
//nolint:staticcheck
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/go-logr/logr"
"github.com/banzaicloud/koperator/api/v1alpha1"
"github.com/banzaicloud/koperator/api/v1beta1"
"github.com/banzaicloud/koperator/pkg/kafkaclient"
"github.com/banzaicloud/koperator/pkg/util"
)
func newMockCluster() *v1beta1.KafkaCluster {
return &v1beta1.KafkaCluster{
TypeMeta: metav1.TypeMeta{
Kind: "KafkaCluster",
APIVersion: "v1beta1",
},
ObjectMeta: metav1.ObjectMeta{Name: "test-cluster", Namespace: "test-namespace"},
Spec: v1beta1.KafkaClusterSpec{},
}
}
func newMockTopic() *v1alpha1.KafkaTopic {
return &v1alpha1.KafkaTopic{
ObjectMeta: metav1.ObjectMeta{Name: "test-topic", Namespace: "test-namespace"},
Spec: v1alpha1.KafkaTopicSpec{
Name: "test-topic",
Partitions: 0,
ReplicationFactor: 0,
ClusterRef: v1alpha1.ClusterReference{
Name: "test-cluster",
Namespace: "test-namespace",
},
},
}
}
func newMockClients(cluster *v1beta1.KafkaCluster) (runtimeClient.WithWatch, kafkaclient.KafkaClient, func(client runtimeClient.Client, cluster *v1beta1.KafkaCluster) (kafkaclient.KafkaClient, func(), error)) {
scheme := runtime.NewScheme()
_ = v1beta1.AddToScheme(scheme)
_ = v1alpha1.AddToScheme(scheme)
client := fake.NewClientBuilder().WithScheme(scheme).Build()
kafkaClient, _, _ := kafkaclient.NewMockFromCluster(client, cluster)
returnMockedKafkaClient := func(client runtimeClient.Client, cluster *v1beta1.KafkaCluster) (kafkaclient.KafkaClient, func(), error) {
return kafkaClient, func() { kafkaClient.Close() }, nil
}
return client, kafkaClient, returnMockedKafkaClient
}
func TestCheckKafkaTopicExist(t *testing.T) {
cluster := newMockCluster()
client, kafkaClient, returnMockedKafkaClient := newMockClients(cluster)
kafkaTopicValidator := KafkaTopicValidator{
Client: client,
NewKafkaFromCluster: returnMockedKafkaClient,
}
err := kafkaClient.CreateTopic(&kafkaclient.CreateTopicOptions{Name: "test-topic", ReplicationFactor: 1, Partitions: 2, Config: util.MapStringStringPointer(map[string]string{"testConfKey": "testConfVal"})})
if err != nil {
t.Error("creation of topic should have been successful")
}
testCases := []struct {
testName string
kafkaTopic v1alpha1.KafkaTopic
expectedErrors []string
}{
{
testName: "topic configuration is same and managedBy koperator",
kafkaTopic: v1alpha1.KafkaTopic{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{TopicManagedByAnnotationKey: TopicManagedByKoperatorAnnotationValue},
},
Spec: v1alpha1.KafkaTopicSpec{
Name: "test-topic",
Partitions: 2,
ReplicationFactor: 1,
Config: map[string]string{"testConfKey": "testConfVal"},
ClusterRef: v1alpha1.ClusterReference{},
},
},
expectedErrors: []string{},
},
{
testName: "topic configuration is same and not managedBy koperator",
kafkaTopic: v1alpha1.KafkaTopic{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
Spec: v1alpha1.KafkaTopicSpec{
Name: "test-topic",
Partitions: 2,
ReplicationFactor: 1,
Config: map[string]string{"testConfKey": "testConfVal"},
ClusterRef: v1alpha1.ClusterReference{},
},
},
expectedErrors: []string{TopicManagedByAnnotationKey},
},
{
testName: "topic replication factor is different and managedBy koperator",
kafkaTopic: v1alpha1.KafkaTopic{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{TopicManagedByAnnotationKey: TopicManagedByKoperatorAnnotationValue},
},
Spec: v1alpha1.KafkaTopicSpec{
Name: "test-topic",
Partitions: 2,
ReplicationFactor: 5,
Config: map[string]string{"testConfKey": "testConfVal"},
ClusterRef: v1alpha1.ClusterReference{},
},
},
expectedErrors: []string{"replication"},
},
{
testName: "topic partition is different and managedBy koperator",
kafkaTopic: v1alpha1.KafkaTopic{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{TopicManagedByAnnotationKey: TopicManagedByKoperatorAnnotationValue},
},
Spec: v1alpha1.KafkaTopicSpec{
Name: "test-topic",
Partitions: 5,
ReplicationFactor: 1,
Config: map[string]string{"testConfKey": "testConfVal"},
ClusterRef: v1alpha1.ClusterReference{},
},
},
expectedErrors: []string{"partition"},
},
{
testName: "topic partition and replication is different and not managedBy koperator",
kafkaTopic: v1alpha1.KafkaTopic{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{TopicManagedByAnnotationKey: "other"},
},
Spec: v1alpha1.KafkaTopicSpec{
Name: "test-topic",
Partitions: 5,
ReplicationFactor: 5,
Config: map[string]string{"testConfKey": "testConfVal"},
ClusterRef: v1alpha1.ClusterReference{},
},
},
expectedErrors: []string{"replication", "partition", TopicManagedByAnnotationKey},
},
{
testName: "topic configuration is different and managedBy koperator",
kafkaTopic: v1alpha1.KafkaTopic{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{TopicManagedByAnnotationKey: TopicManagedByKoperatorAnnotationValue},
},
Spec: v1alpha1.KafkaTopicSpec{
Name: "test-topic",
Partitions: 2,
ReplicationFactor: 1,
Config: map[string]string{"testConfKey": "testConfVal1"},
ClusterRef: v1alpha1.ClusterReference{},
},
},
expectedErrors: []string{"its configuration must be"},
},
}
for _, testCase := range testCases {
testCase := testCase
t.Run(testCase.testName, func(t *testing.T) {
t.Parallel()
fieldErrorList, err := kafkaTopicValidator.checkKafka(context.Background(), &testCase.kafkaTopic, cluster)
if err != nil {
t.Errorf("err should be nil, got: %s", err)
}
for _, err := range testCase.expectedErrors {
if !strings.Contains(fieldErrorList.ToAggregate().Error(), err) {
t.Errorf("missing error: %s from: %s", err, fieldErrorList.ToAggregate().Error())
}
}
})
}
}
func TestValidateTopic(t *testing.T) {
topic := newMockTopic()
cluster := newMockCluster()
client, kafkaClient, returnMockedKafkaClient := newMockClients(cluster)
kafkaTopicValidator := KafkaTopicValidator{
Client: client,
NewKafkaFromCluster: returnMockedKafkaClient,
}
// Test non-existent kafka cluster
fieldErrorList, err := kafkaTopicValidator.validateKafkaTopic(context.Background(), logr.Discard(), topic)
if err != nil {
t.Errorf("err should be nil, got: %s", err)
}
// Test kafka topic with invalid partitions, and replicas, and not found cluster
if len(fieldErrorList) != 3 {
t.Errorf("there should be 3 invalid field, got %d", len(fieldErrorList))
}
if !strings.Contains(fieldErrorList.ToAggregate().Error(), "not exist") {
t.Error("Expected not found cluster")
}
if err := kafkaTopicValidator.Client.Create(context.TODO(), cluster); err != nil {
t.Error("Expected no error, got:", err)
}
// set a valid partitions
topic.Spec.Partitions = 2
// Test kafka topic with invalid replication factor
fieldErrorList, err = kafkaTopicValidator.validateKafkaTopic(context.Background(), logr.Discard(), topic)
if err != nil {
t.Errorf("err should be nil, got: %s", err)
}
if len(fieldErrorList) != 1 {
t.Error("Expected not allowed due to invalid replication factor, got allowed")
}
// set a valid replication factor
topic.Spec.ReplicationFactor = 1
// test topic marked for deletion
now := metav1.Now()
topic.SetDeletionTimestamp(&now)
fieldErrorList, err = kafkaTopicValidator.validateKafkaTopic(context.Background(), logr.Discard(), topic)
if err != nil {
t.Errorf("err should be nil, got: %s", err)
}
if len(fieldErrorList) != 0 {
t.Error("Expected allowed due to topic marked for deletion")
}
// remove deletion timestamp
topic.SetDeletionTimestamp(nil)
// test cluster marked for deletion
cluster.SetDeletionTimestamp(&now)
fieldErrorList, err = kafkaTopicValidator.validateKafkaTopic(context.Background(), logr.Discard(), topic)
if err != nil {
t.Errorf("err should be nil, got: %s", err)
}
if len(fieldErrorList) != 0 {
t.Error("Expected allowed due to cluster marked for deletion")
}
// remove deletion timestamp from cluster
cluster.SetDeletionTimestamp(nil)
if err := kafkaTopicValidator.Client.Update(context.TODO(), cluster); err != nil {
t.Error("Expected no error, got:", err)
}
// test no rejection reasons
fieldErrorList, err = kafkaTopicValidator.validateKafkaTopic(context.Background(), logr.Discard(), topic)
if err != nil {
t.Errorf("err should be nil, got: %s", err)
}
if len(fieldErrorList) != 0 {
t.Error("Expected allowed due to no issues")
}
// Rejection reasons
// Replication factor larger than num brokers
topic.Spec.ReplicationFactor = 2
fieldErrorList, err = kafkaTopicValidator.validateKafkaTopic(context.Background(), logr.Discard(), topic)
if err != nil {
t.Errorf("err should be nil, got: %s", err)
}
if len(fieldErrorList) != 1 {
t.Error("Expected not allowed due to replication factor larger than num brokers, got allowed")
}
topic.Spec.ReplicationFactor = 1
err = kafkaClient.CreateTopic(&kafkaclient.CreateTopicOptions{Name: "test-topic", ReplicationFactor: 1, Partitions: 2})
if err != nil {
t.Error("creation of topic should have been successful")
}
topic.Name = "test-topic"
// Add topic and test existing topic reason
if err := kafkaTopicValidator.Client.Create(context.TODO(), topic); err != nil {
t.Error("Expected no error, got:", err)
}
// partition decrease attempt
topic.Spec.Partitions = 1
fieldErrorList, err = kafkaTopicValidator.validateKafkaTopic(context.Background(), logr.Discard(), topic)
if err != nil {
t.Errorf("err should be nil, got: %s", err)
}
if len(fieldErrorList) != 1 {
t.Error("Expected not allowed due to partition decrease, got allowed")
} else if !strings.Contains(fieldErrorList.ToAggregate().Error(), "kafka does not support decreasing partition count on an existing") {
t.Error("Expected not allowed for reason: kafka does not support decreasing partition count")
}
// replication factor change attempt
topic.Spec.Partitions = 2
topic.Spec.ReplicationFactor = 2
fieldErrorList, err = kafkaTopicValidator.validateKafkaTopic(context.Background(), logr.Discard(), topic)
if err != nil {
t.Errorf("err should be nil, got: %s", err)
}
if len(fieldErrorList) != 1 {
t.Error("Expected not allowed due to replication factor change, got allowed")
} else if !strings.Contains(fieldErrorList.ToAggregate().Error(), "kafka does not support changing the replication factor") {
t.Error("Expected not allowed for reason: kafka does not support changing the replication factor")
}
}