-
Notifications
You must be signed in to change notification settings - Fork 104
/
instance_controller_test.go
572 lines (526 loc) · 15.4 KB
/
instance_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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// +build integration
package instance
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/discovery/cached/memory"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/manager"
"github.com/kudobuilder/kudo/pkg/apis"
"github.com/kudobuilder/kudo/pkg/apis/kudo/v1beta1"
"github.com/kudobuilder/kudo/pkg/engine"
"github.com/kudobuilder/kudo/pkg/engine/task"
"github.com/kudobuilder/kudo/pkg/kubernetes"
"github.com/kudobuilder/kudo/pkg/util/convert"
"github.com/kudobuilder/kudo/pkg/util/kudo"
)
const timeout = time.Second * 5
const tick = time.Millisecond * 500
var cfg *rest.Config
func TestMain(m *testing.M) {
t := &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds")},
}
if err := apis.AddToScheme(scheme.Scheme); err != nil {
log.Fatal(err)
}
var err error
if cfg, err = t.Start(); err != nil {
log.Fatal(err)
}
code := m.Run()
// t.Stop() returns an error, but since we exit in the next line anyway, it is suppressed
t.Stop() //nolint:errcheck
os.Exit(code)
}
func TestRestartController(t *testing.T) {
stopMgr, mgrStopped, c := startTestManager(t)
log.Printf("Given an existing instance 'foo-instance' and operator 'foo-operator'")
in := &v1beta1.Instance{
ObjectMeta: metav1.ObjectMeta{Name: "foo-instance", Namespace: "default", Labels: map[string]string{kudo.OperatorLabel: "foo-operator"}},
Spec: v1beta1.InstanceSpec{
OperatorVersion: v1.ObjectReference{
Name: "foo-operator",
Namespace: "default",
},
Parameters: map[string]string{"param": "value"},
},
}
instanceKey, _ := client.ObjectKeyFromObject(in)
assert.NoError(t, c.Create(context.TODO(), in))
ov := &v1beta1.OperatorVersion{
ObjectMeta: metav1.ObjectMeta{Name: "foo-operator", Namespace: "default"},
TypeMeta: metav1.TypeMeta{Kind: "OperatorVersion", APIVersion: "kudo.dev/v1beta1"},
Spec: v1beta1.OperatorVersionSpec{
Plans: map[string]v1beta1.Plan{"deploy": {Phases: []v1beta1.Phase{}}, "update": {Phases: []v1beta1.Phase{}}},
Parameters: []v1beta1.Parameter{
{
Name: "param",
Default: convert.StringPtr("default"),
},
},
},
}
assert.NoError(t, c.Create(context.TODO(), ov))
log.Print("And a deploy plan that was already run")
assert.Eventually(t, func() bool { return instancePlanFinished(instanceKey, "deploy", c) }, timeout, tick)
log.Print("When we stop the manager")
close(stopMgr)
mgrStopped.Wait()
log.Print("And update the instance parameter value")
err := c.Get(context.TODO(), instanceKey, in) // we need to pull here again because the state of instance was modified in between
assert.NoError(t, err)
in.Spec.Parameters = map[string]string{"param": "newvalue"}
assert.NoError(t, c.Update(context.TODO(), in))
log.Print("And restart the manager again")
stopMgr, mgrStopped, c = startTestManager(t)
log.Print("Then an update plan should be triggered instead of deploy plan")
assert.Eventually(t, func() bool { return instancePlanFinished(instanceKey, "update", c) }, timeout, tick)
close(stopMgr)
mgrStopped.Wait()
}
func instancePlanFinished(key client.ObjectKey, planName string, c client.Client) bool {
i := &v1beta1.Instance{}
err := c.Get(context.TODO(), key, i)
if err != nil {
fmt.Printf("%v", err)
return false
}
return i.Status.PlanStatus[planName].Status.IsFinished()
}
func startTestManager(t *testing.T) (chan struct{}, *sync.WaitGroup, client.Client) {
mgr, err := manager.New(cfg, manager.Options{})
assert.Nil(t, err, "Error when creating manager")
discoveryClient, err := kubernetes.GetDiscoveryClient(mgr)
assert.NoError(t, err, "Error when creating discovery client")
cachedDiscoveryClient := memory.NewMemCacheClient(discoveryClient)
err = (&Reconciler{
Client: mgr.GetClient(),
Discovery: cachedDiscoveryClient,
Config: mgr.GetConfig(),
Recorder: mgr.GetEventRecorderFor("instance-controller"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr)
assert.NoError(t, err, "Error when setting up manager")
stop := make(chan struct{})
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
_ = mgr.Start(stop)
wg.Done()
}()
return stop, wg, mgr.GetClient()
}
func Test_makePipes(t *testing.T) {
meta := &engine.Metadata{
InstanceName: "first-operator-instance",
InstanceNamespace: "default",
OperatorName: "first-operator",
OperatorVersionName: "first-operator-1.0",
OperatorVersion: "1.0",
}
tests := []struct {
name string
planName string
plan *v1beta1.Plan
tasks []v1beta1.Task
emeta *engine.Metadata
want map[string]string
wantErr bool
}{
{
name: "no tasks, no pipes",
planName: "deploy",
plan: &v1beta1.Plan{Strategy: "serial", Phases: []v1beta1.Phase{
{
Name: "phase", Strategy: "serial", Steps: []v1beta1.Step{
{
Name: "step", Tasks: []string{}},
}},
}},
tasks: []v1beta1.Task{},
emeta: meta,
want: map[string]string{},
},
{
name: "no pipe tasks, no pipes",
planName: "deploy",
plan: &v1beta1.Plan{Strategy: "serial", Phases: []v1beta1.Phase{
{
Name: "phase", Strategy: "serial", Steps: []v1beta1.Step{
{
Name: "step", Tasks: []string{"task"}},
}},
}},
tasks: []v1beta1.Task{
{
Name: "task",
Kind: "Dummy",
Spec: v1beta1.TaskSpec{
DummyTaskSpec: v1beta1.DummyTaskSpec{Done: false},
},
},
},
emeta: meta,
want: map[string]string{},
},
{
name: "one pipe task, one pipes element",
planName: "deploy",
plan: &v1beta1.Plan{Strategy: "serial", Phases: []v1beta1.Phase{
{
Name: "phase", Strategy: "serial", Steps: []v1beta1.Step{
{
Name: "step", Tasks: []string{"task"}},
}},
}},
tasks: []v1beta1.Task{
{
Name: "task",
Kind: "Pipe",
Spec: v1beta1.TaskSpec{
PipeTaskSpec: v1beta1.PipeTaskSpec{
Pod: "pipe-pod.yaml",
Pipe: []v1beta1.PipeSpec{
{
File: "foo.txt",
Kind: "Secret",
Key: "Foo",
},
},
},
},
},
},
emeta: meta,
want: map[string]string{"Foo": "firstoperatorinstance.deploy.phase.step.task.foo"},
},
{
name: "two pipe tasks, two pipes element",
planName: "deploy",
plan: &v1beta1.Plan{Strategy: "serial", Phases: []v1beta1.Phase{
{
Name: "phase", Strategy: "serial", Steps: []v1beta1.Step{
{Name: "stepOne", Tasks: []string{"task-one"}},
{Name: "stepTwo", Tasks: []string{"task-two"}},
}},
}},
tasks: []v1beta1.Task{
{
Name: "task-one",
Kind: "Pipe",
Spec: v1beta1.TaskSpec{
PipeTaskSpec: v1beta1.PipeTaskSpec{
Pod: "pipe-pod.yaml",
Pipe: []v1beta1.PipeSpec{
{
File: "foo.txt",
Kind: "Secret",
Key: "Foo",
},
},
},
},
},
{
Name: "task-two",
Kind: "Pipe",
Spec: v1beta1.TaskSpec{
PipeTaskSpec: v1beta1.PipeTaskSpec{
Pod: "pipe-pod.yaml",
Pipe: []v1beta1.PipeSpec{
{
File: "bar.txt",
Kind: "ConfigMap",
Key: "Bar",
},
},
},
},
},
},
emeta: meta,
want: map[string]string{
"Foo": "firstoperatorinstance.deploy.phase.stepone.taskone.foo",
"Bar": "firstoperatorinstance.deploy.phase.steptwo.tasktwo.bar",
},
},
{
name: "one pipe task, duplicated pipe keys",
planName: "deploy",
plan: &v1beta1.Plan{Strategy: "serial", Phases: []v1beta1.Phase{
{
Name: "phase", Strategy: "serial", Steps: []v1beta1.Step{
{
Name: "step", Tasks: []string{"task"}},
}},
}},
tasks: []v1beta1.Task{
{
Name: "task",
Kind: "Pipe",
Spec: v1beta1.TaskSpec{
PipeTaskSpec: v1beta1.PipeTaskSpec{
Pod: "pipe-pod.yaml",
Pipe: []v1beta1.PipeSpec{
{
File: "foo.txt",
Kind: "Secret",
Key: "Foo",
},
{
File: "bar.txt",
Kind: "ConfigMap",
Key: "Foo",
},
},
},
},
},
},
emeta: meta,
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got, err := PipesMap(tt.planName, tt.plan, tt.tasks, tt.emeta)
if err != nil {
if !tt.wantErr {
t.Fatalf("PipesMap() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
assert.Equal(t, tt.want, got)
})
}
}
func TestParameterDiff(t *testing.T) {
var (
tests = []struct {
name string
new map[string]string
diff map[string]string
}{
{name: "update one value", new: map[string]string{"one": "11", "two": "2"}, diff: map[string]string{"one": "11"}},
{name: "update multiple values", new: map[string]string{"one": "11", "two": "22"}, diff: map[string]string{"one": "11", "two": "22"}},
{name: "add new value", new: map[string]string{"one": "1", "two": "2", "three": "3"}, diff: map[string]string{"three": "3"}},
{name: "remove one value", new: map[string]string{"one": "1"}, diff: map[string]string{"two": "2"}},
{name: "no difference", new: map[string]string{"one": "1", "two": "2"}, diff: map[string]string{}},
{name: "empty new map", new: map[string]string{}, diff: map[string]string{"one": "1", "two": "2"}},
}
)
var old = map[string]string{"one": "1", "two": "2"}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
diff := v1beta1.ParameterDiff(old, tt.new)
assert.Equal(t, tt.diff, diff)
})
}
}
func TestRichParameterDiff(t *testing.T) {
var empty = map[string]string{}
var old = map[string]string{"one": "1", "two": "2"}
var tests = []struct {
name string
new map[string]string
changed map[string]string
removed map[string]string
}{
{name: "update one value", new: map[string]string{"one": "11", "two": "2"}, changed: map[string]string{"one": "11"}, removed: empty},
{name: "update multiple values", new: map[string]string{"one": "11", "two": "22"}, changed: map[string]string{"one": "11", "two": "22"}, removed: empty},
{name: "add new value", new: map[string]string{"one": "1", "two": "2", "three": "3"}, changed: map[string]string{"three": "3"}, removed: empty},
{name: "remove one value", new: map[string]string{"one": "1"}, changed: empty, removed: map[string]string{"two": "2"}},
{name: "no difference", new: map[string]string{"one": "1", "two": "2"}, changed: empty, removed: empty},
{name: "empty new map", new: empty, changed: empty, removed: map[string]string{"one": "1", "two": "2"}},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
changed, removed := v1beta1.RichParameterDiff(old, tt.new)
assert.Equal(t, tt.changed, changed, "unexpected difference in changed parameters")
assert.Equal(t, tt.removed, removed, "unexpected difference in removed parameters")
})
}
}
func TestEventFilterForDelete(t *testing.T) {
var testParams = []struct {
name string
allowed bool
e event.DeleteEvent
}{
{"A Pod without annotations", true, event.DeleteEvent{
Meta: &v1.Pod{},
Object: nil,
DeleteStateUnknown: false,
}},
{"A Pod with pipePod annotation", false, event.DeleteEvent{
Meta: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{task.PipePodAnnotation: "true"},
},
},
Object: nil,
DeleteStateUnknown: false,
}},
}
filter := eventFilter()
for _, test := range testParams {
diff := filter.Delete(test.e)
assert.Equal(t, test.allowed, diff, test.name)
}
}
func Test_fetchNewExecutionPlan(t *testing.T) {
ov := &v1beta1.OperatorVersion{
ObjectMeta: metav1.ObjectMeta{Name: "foo-operator", Namespace: "default"},
TypeMeta: metav1.TypeMeta{Kind: "OperatorVersion", APIVersion: "kudo.dev/v1beta1"},
Spec: v1beta1.OperatorVersionSpec{Plans: map[string]v1beta1.Plan{"cleanup": {}}},
}
idle := &v1beta1.Instance{
TypeMeta: metav1.TypeMeta{APIVersion: "kudo.dev/v1beta1", Kind: "Instance"},
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "test"},
Spec: v1beta1.InstanceSpec{
PlanExecution: v1beta1.PlanExecution{
PlanName: "",
UID: "",
},
},
}
if err := idle.AnnotateSnapshot(); err != nil {
t.Fatalf("failed to annotate instance snaphot: %v", err)
}
deploy := "deploy"
cleanup := "cleanup"
backup := "backup"
testUUID := uuid.NewUUID()
deploying := idle.DeepCopy()
deploying.Spec.PlanExecution = v1beta1.PlanExecution{PlanName: deploy, UID: testUUID}
if err := deploying.AnnotateSnapshot(); err != nil {
t.Fatalf("failed to annotate instance snaphot: %v", err)
}
deleted := deploying.DeepCopy()
deleted.ObjectMeta.DeletionTimestamp = &metav1.Time{Time: time.Date(2019, 10, 17, 1, 1, 1, 1, time.UTC)}
deleted.ObjectMeta.Finalizers = []string{"kudo.dev.instance.cleanup"}
uninstalling := deleted.DeepCopy()
uninstalling.Spec.PlanExecution.PlanName = cleanup
if err := uninstalling.AnnotateSnapshot(); err != nil {
t.Fatalf("failed to annotate instance snaphot: %v", err)
}
tests := []struct {
name string
i *v1beta1.Instance
ov *v1beta1.OperatorVersion
want *string
wantErr bool
}{
{
name: "no change means no new plan",
i: idle,
ov: ov,
want: nil,
wantErr: false,
},
{
name: "deploy plan is scheduled without new UID",
i: func() *v1beta1.Instance {
i := idle.DeepCopy()
i.Spec.PlanExecution.PlanName = deploy
return i
}(),
ov: ov,
want: &deploy,
wantErr: false,
},
{
name: "deploy plan is scheduled WITH new UID",
i: func() *v1beta1.Instance {
i := idle.DeepCopy()
i.Spec.PlanExecution.PlanName = deploy
i.Spec.PlanExecution.UID = uuid.NewUUID()
return i
}(),
ov: ov,
want: &deploy,
wantErr: false,
},
{
name: "no new plan was scheduled WITH a new UID",
i: func() *v1beta1.Instance {
i := idle.DeepCopy()
i.Spec.PlanExecution.UID = uuid.NewUUID()
return i
}(),
ov: ov,
want: nil,
wantErr: false,
},
{
name: "new plan overrides deploy plan",
i: func() *v1beta1.Instance {
i := deploying.DeepCopy()
i.Spec.PlanExecution.PlanName = backup
return i
}(),
ov: ov,
want: &backup,
wantErr: false,
},
{
name: "new plan overrides deploy plan WITH new UID",
i: func() *v1beta1.Instance {
i := deploying.DeepCopy()
i.Spec.PlanExecution.PlanName = backup
i.Spec.PlanExecution.UID = uuid.NewUUID()
return i
}(),
ov: ov,
want: &backup,
wantErr: false,
},
{
name: "cleanup is scheduled for a deleted instance",
i: deleted,
ov: ov,
want: &cleanup,
wantErr: false,
},
{
name: "cleanup is NOT scheduled when it is already scheduled",
i: uninstalling,
ov: ov,
want: nil,
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
got, err := fetchNewExecutionPlan(tt.i, tt.ov)
assert.Equal(t, tt.wantErr, err != nil, "expected error %v, but got %v", tt.wantErr, err)
assert.Equal(t, tt.want, got, "expected '%s' plan returned but got: '%s'", stringPtrToString(tt.want), stringPtrToString(got))
})
}
}
func stringPtrToString(p *string) string {
if p != nil {
return *p
}
return "<nil>"
}