-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_test.go
318 lines (271 loc) · 9.82 KB
/
controller_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
package main
import (
"github.com/mattfanto/kafkaops-controller/pkg/resources/kafkaops"
fake2 "github.com/mattfanto/kafkaops-controller/pkg/resources/kafkaops/fake"
"reflect"
"testing"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/diff"
kubeinformers "k8s.io/client-go/informers"
k8sfake "k8s.io/client-go/kubernetes/fake"
core "k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
kafkaopscontroller "github.com/mattfanto/kafkaops-controller/pkg/apis/kafkaopscontroller/v1alpha1"
"github.com/mattfanto/kafkaops-controller/pkg/generated/clientset/versioned/fake"
informers "github.com/mattfanto/kafkaops-controller/pkg/generated/informers/externalversions"
)
var (
alwaysReady = func() bool { return true }
noResyncPeriodFunc = func() time.Duration { return 0 }
)
type fixture struct {
t *testing.T
client *fake.Clientset
kubeclient *k8sfake.Clientset
// Objects to put in the store.
kafkaTopicsLister []*kafkaopscontroller.KafkaTopic
// Actions expected to happen on the client.
kubeactions []core.Action
actions []core.Action
kafkaActions []core.Action
// Objects from here preloaded into NewSimpleFake.
kubeobjects []runtime.Object
objects []runtime.Object
kafkaSdk kafkaops.Interface
}
func newFixture(t *testing.T) *fixture {
f := &fixture{}
f.t = t
f.objects = []runtime.Object{}
f.kubeobjects = []runtime.Object{}
f.kafkaSdk = &fake2.FakeKafkaSdk{}
return f
}
func newKafkaTopicResource(name string, replicas *int32, partitions *int32) *kafkaopscontroller.KafkaTopic {
return &kafkaopscontroller.KafkaTopic{
TypeMeta: metav1.TypeMeta{
APIVersion: kafkaopscontroller.SchemeGroupVersion.String()},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: metav1.NamespaceDefault,
},
Spec: kafkaopscontroller.KafkaTopicSpec{
TopicName: name,
Replicas: replicas,
Partitions: partitions,
},
Status: kafkaopscontroller.KafkaTopicStatus{
StatusCode: kafkaopscontroller.EXISTS,
Replicas: int(*replicas),
Partitions: int(*partitions),
// not tested
Conditions: []metav1.Condition{},
},
}
}
func (f *fixture) newController() (*Controller, informers.SharedInformerFactory, kubeinformers.SharedInformerFactory) {
f.client = fake.NewSimpleClientset(f.objects...)
f.kubeclient = k8sfake.NewSimpleClientset(f.kubeobjects...)
i := informers.NewSharedInformerFactory(f.client, noResyncPeriodFunc())
k8sI := kubeinformers.NewSharedInformerFactory(f.kubeclient, noResyncPeriodFunc())
c := NewController(
f.kubeclient,
f.client,
i.Kafkaopscontroller().V1alpha1().KafkaTopics(),
f.kafkaSdk)
c.informerSynced = alwaysReady
c.recorder = &record.FakeRecorder{}
for _, f := range f.kafkaTopicsLister {
err := i.Kafkaopscontroller().V1alpha1().KafkaTopics().Informer().GetIndexer().Add(f)
if err != nil {
return nil, nil, nil
}
}
return c, i, k8sI
}
func (f *fixture) run(kafkaTopicName string) {
f.runController(kafkaTopicName, true, false)
}
func (f *fixture) runExpectError(kafkaTopicName string) {
f.runController(kafkaTopicName, true, true)
}
func (f *fixture) runController(kafkaTopicName string, startInformers bool, expectError bool) {
c, i, k8sI := f.newController()
if startInformers {
stopCh := make(chan struct{})
defer close(stopCh)
i.Start(stopCh)
k8sI.Start(stopCh)
}
err := c.syncHandler(kafkaTopicName)
if !expectError && err != nil {
f.t.Errorf("error syncing kafkatopic: %v", err)
} else if expectError && err == nil {
f.t.Error("expected error syncing kafkatopic, got nil")
}
actions := filterInformerActions(f.client.Actions())
for i, action := range actions {
if len(f.actions) < i+1 {
f.t.Errorf("%d unexpected actions: %+v", len(actions)-len(f.actions), actions[i:])
break
}
expectedAction := f.actions[i]
checkAction(expectedAction, action, f.t)
}
if len(f.actions) > len(actions) {
f.t.Errorf("%d additional expected actions:%+v", len(f.actions)-len(actions), f.actions[len(actions):])
}
k8sActions := filterInformerActions(f.kubeclient.Actions())
for i, action := range k8sActions {
if len(f.kubeactions) < i+1 {
f.t.Errorf("%d unexpected actions: %+v", len(k8sActions)-len(f.kubeactions), k8sActions[i:])
break
}
expectedAction := f.kubeactions[i]
checkAction(expectedAction, action, f.t)
}
if len(f.kubeactions) > len(k8sActions) {
f.t.Errorf("%d additional expected actions:%+v", len(f.kubeactions)-len(k8sActions), f.kubeactions[len(k8sActions):])
}
// Check that the topic has been created
for _, obj := range f.objects {
switch v := obj.(type) {
case *kafkaopscontroller.KafkaTopic:
topicStatus, err := f.kafkaSdk.CheckKafkaTopicStatus(v)
if err != nil {
f.t.Errorf("Error while checking topic status: %s", err)
}
if topicStatus.StatusCode == kafkaopscontroller.NOT_EXISTS {
f.t.Errorf("Expected topic '%s' does not exist", v.Spec.TopicName)
}
}
}
}
// checkAction verifies that expected and actual actions are equal and both have
// same attached resources
func checkAction(expected, actual core.Action, t *testing.T) {
if !(expected.Matches(actual.GetVerb(), actual.GetResource().Resource) && actual.GetSubresource() == expected.GetSubresource()) {
t.Errorf("Expected\n\t%#v\ngot\n\t%#v", expected, actual)
return
}
if reflect.TypeOf(actual) != reflect.TypeOf(expected) {
t.Errorf("Action has wrong type. Expected: %t. Got: %t", expected, actual)
return
}
switch a := actual.(type) {
case core.CreateActionImpl:
e, _ := expected.(core.CreateActionImpl)
expObject := e.GetObject()
object := a.GetObject()
if !reflect.DeepEqual(expObject, object) {
t.Errorf("Action %s %s has wrong object\nDiff:\n %s",
a.GetVerb(), a.GetResource().Resource, diff.ObjectGoPrintSideBySide(expObject, object))
}
case core.UpdateActionImpl:
e, _ := expected.(core.UpdateActionImpl)
expObject := e.GetObject()
object := a.GetObject()
if !objectAlmostEquals(object, expObject) {
t.Errorf("Action %s %s has wrong object\nDiff:\n %s",
a.GetVerb(), a.GetResource().Resource, diff.ObjectGoPrintSideBySide(expObject, object))
}
case core.PatchActionImpl:
e, _ := expected.(core.PatchActionImpl)
expPatch := e.GetPatch()
patch := a.GetPatch()
if !reflect.DeepEqual(expPatch, patch) {
t.Errorf("Action %s %s has wrong patch\nDiff:\n %s",
a.GetVerb(), a.GetResource().Resource, diff.ObjectGoPrintSideBySide(expPatch, patch))
}
default:
t.Errorf("Uncaptured Action %s %s, you should explicitly add a case to capture it",
actual.GetVerb(), actual.GetResource().Resource)
}
}
// objectAlmostEquals implements custom logic to compare two runtime.Object
// based on the object type
func objectAlmostEquals(x, y runtime.Object) bool {
if reflect.TypeOf(x) != reflect.TypeOf(y) {
return false
}
switch x := x.(type) {
case *kafkaopscontroller.KafkaTopic:
// Avoid comparing the condition timestamp
y, _ := y.(*kafkaopscontroller.KafkaTopic)
y = y.DeepCopy()
y.Status.Conditions = []metav1.Condition{}
x = x.DeepCopy()
x.Status.Conditions = []metav1.Condition{}
return reflect.DeepEqual(x, y)
default:
return reflect.DeepEqual(x, y)
}
}
// filterInformerActions filters list and watch actions for testing resources.
// Since list and watch don't change resource state we can filter it to lower
// nose level in our tests.
func filterInformerActions(actions []core.Action) []core.Action {
ret := []core.Action{}
for _, action := range actions {
if len(action.GetNamespace()) == 0 &&
(action.Matches("list", "kafkatopics") ||
action.Matches("watch", "kafkatopics")) {
continue
}
ret = append(ret, action)
}
return ret
}
func (f *fixture) expectUpdateKafkaTopicStatusAction(kafkaTopic *kafkaopscontroller.KafkaTopic) {
action := core.NewUpdateAction(schema.GroupVersionResource{Resource: "kafkatopics"}, kafkaTopic.Namespace, kafkaTopic)
// TODO: Until #38113 is merged, we can't use Subresource
//action.Subresource = "status"
f.actions = append(f.actions, action)
}
func getKey(kafkaTopic *kafkaopscontroller.KafkaTopic, t *testing.T) string {
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(kafkaTopic)
if err != nil {
t.Errorf("Unexpected but I'll definitely update the number of shards! error getting key for kafkaTopic %v: %v", kafkaTopic.Name, err)
return ""
}
return key
}
func TestCreatesTopic(t *testing.T) {
f := newFixture(t)
kafkaTopic := newKafkaTopicResource("test", int32Ptr(1), int32Ptr(3))
f.kafkaTopicsLister = append(f.kafkaTopicsLister, kafkaTopic)
f.objects = append(f.objects, kafkaTopic)
f.expectUpdateKafkaTopicStatusAction(kafkaTopic)
f.run(getKey(kafkaTopic, t))
}
func TestDoNothing(t *testing.T) {
f := newFixture(t)
kafkaTopic := newKafkaTopicResource("test", int32Ptr(1), int32Ptr(3))
f.kafkaTopicsLister = append(f.kafkaTopicsLister, kafkaTopic)
f.objects = append(f.objects, kafkaTopic)
// We create the kafka topic so that the pipeline doesn't have to do anything
//goland:noinspection ALL
f.kafkaSdk.CreateKafkaTopic(kafkaTopic)
f.expectUpdateKafkaTopicStatusAction(kafkaTopic)
f.run(getKey(kafkaTopic, t))
}
func TestKafkaTopicDeviation(t *testing.T) {
f := newFixture(t)
kafkaTopic := newKafkaTopicResource("test", int32Ptr(1), int32Ptr(3))
existingKafkaTopic := newKafkaTopicResource("test", int32Ptr(1), int32Ptr(1))
f.kafkaTopicsLister = append(f.kafkaTopicsLister, kafkaTopic)
f.objects = append(f.objects, kafkaTopic)
//goland:noinspection ALL
f.kafkaSdk.CreateKafkaTopic(existingKafkaTopic)
kafkaTopic = kafkaTopic.DeepCopy()
kafkaTopic.Status.StatusCode = kafkaopscontroller.DEVIATED
kafkaTopic.Status.Replicas = existingKafkaTopic.Status.Replicas
kafkaTopic.Status.Partitions = existingKafkaTopic.Status.Partitions
f.expectUpdateKafkaTopicStatusAction(kafkaTopic)
f.run(getKey(kafkaTopic, t))
}
func int32Ptr(i int32) *int32 { return &i }