forked from getporter/operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstallation_controller_test.go
More file actions
647 lines (588 loc) · 21.9 KB
/
installation_controller_test.go
File metadata and controls
647 lines (588 loc) · 21.9 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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package controllers
import (
"context"
"fmt"
"testing"
"time"
v1 "get.porter.sh/operator/api/v1"
mocks "get.porter.sh/operator/mocks/grpc"
installationv1 "get.porter.sh/porter/gen/proto/go/porterapis/installation/v1alpha1"
porterv1alpha1 "get.porter.sh/porter/gen/proto/go/porterapis/porter/v1alpha1"
"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/structpb"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
func TestShouldInstall(t *testing.T) {
now := metav1.Now()
tests := map[string]struct {
wantTrue bool
delTimeStamp *metav1.Time
}{
"true": {wantTrue: true, delTimeStamp: &now},
"false": {wantTrue: false, delTimeStamp: nil},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
inst := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-ns",
Finalizers: []string{v1.FinalizerName},
DeletionTimestamp: test.delTimeStamp,
Annotations: map[string]string{v1.PorterDeletePolicyAnnotation: v1.PorterDeletePolicyDelete},
},
}
rec := setupInstallationController(inst)
isTrue := rec.shouldUninstall(inst)
if test.wantTrue {
assert.True(t, isTrue)
}
if !test.wantTrue {
assert.False(t, isTrue)
}
})
}
}
func TestShouldOrphan(t *testing.T) {
now := metav1.Now()
tests := map[string]struct {
wantTrue bool
delTimeStamp *metav1.Time
}{
"true": {wantTrue: true, delTimeStamp: &now},
"false": {wantTrue: false, delTimeStamp: nil},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
inst := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-ns",
Finalizers: []string{v1.FinalizerName},
DeletionTimestamp: test.delTimeStamp,
Annotations: map[string]string{v1.PorterDeletePolicyAnnotation: v1.PorterDeletePolicyOrphan},
},
}
rec := setupInstallationController(inst)
isTrue := rec.shouldOrphan(inst)
if test.wantTrue {
assert.True(t, isTrue)
}
if !test.wantTrue {
assert.False(t, isTrue)
}
})
}
}
func TestUninstallInstallation(t *testing.T) {
ctx := context.Background()
inst := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
}
rec := setupInstallationController(inst)
err := rec.uninstallInstallation(ctx, rec.Log, inst)
assert.NoError(t, err)
gotInstall := &v1.Installation{}
assert.NoError(t, rec.Get(ctx, types.NamespacedName{Name: "fake-install", Namespace: "fake-ns"}, gotInstall))
assert.NotEmpty(t, gotInstall.Status)
assert.Equal(t, v1.PhaseUnknown, gotInstall.Status.Phase)
}
func TestRetry(t *testing.T) {
ctx := context.Background()
inst := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
}
action := &v1.AgentAction{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-action",
Namespace: "fake-ns",
},
}
rec := setupInstallationController(inst, action)
err := rec.retry(ctx, rec.Log, inst, action)
assert.NoError(t, err)
}
func TestInstallationReconciler_Reconcile(t *testing.T) {
// long test is long
// Run through a full resource lifecycle: create, update, delete
ctx := context.Background()
namespace := "test"
name := "mybuns"
testdata := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name, Generation: 1}}
controller := setupInstallationController(testdata)
clientConn := &mocks.ClientConn{}
clientConn.On("Close").Return(nil)
controller.CreateGRPCClient = func(ctx context.Context) (porterv1alpha1.PorterClient, ClientConn, error) {
return nil, clientConn, fmt.Errorf("this is not needed for this test")
}
var inst v1.Installation
triggerReconcile := func() {
fullname := types.NamespacedName{Namespace: namespace, Name: name}
key := client.ObjectKey{Namespace: namespace, Name: name}
request := ctrl.Request{
NamespacedName: fullname,
}
result, err := controller.Reconcile(ctx, request)
require.NoError(t, err)
require.True(t, result.IsZero())
err = controller.Get(ctx, key, &inst)
if !apierrors.IsNotFound(err) {
require.NoError(t, err)
}
}
triggerReconcile()
// Verify the installation was picked up and the status initialized
assert.Equal(t, v1.PhaseUnknown, inst.Status.Phase, "New resources should be initialized to Phase: Unknown")
triggerReconcile()
// Verify an AgentAction was created and set on the status
require.NotNil(t, inst.Status.Action, "expected Action to be set")
var action v1.AgentAction
require.NoError(t, controller.Get(ctx, client.ObjectKey{Namespace: inst.Namespace, Name: inst.Status.Action.Name}, &action))
assert.Equal(t, "1", action.Labels[v1.LabelResourceGeneration], "The wrong action is set on the status")
// Mark the action as scheduled
action.Status.Phase = v1.PhasePending
action.Status.Conditions = []metav1.Condition{{Type: string(v1.ConditionScheduled), Status: metav1.ConditionTrue}}
action.ResourceVersion = ""
controller = setupInstallationController(testdata, &action)
assert.NoError(t, controller.Client.Status().Update(ctx, &action))
triggerReconcile()
// Verify the installation status was synced with the action
assert.Equal(t, v1.PhasePending, inst.Status.Phase, "incorrect Phase")
assert.True(t, apimeta.IsStatusConditionTrue(inst.Status.Conditions, string(v1.ConditionScheduled)))
// Mark the action as started
action.Status.Phase = v1.PhaseRunning
action.Status.Conditions = []metav1.Condition{{Type: string(v1.ConditionStarted), Status: metav1.ConditionTrue}}
require.NoError(t, controller.Status().Update(ctx, &action))
triggerReconcile()
// Verify that the installation status was synced with the action
assert.Equal(t, v1.PhaseRunning, inst.Status.Phase, "incorrect Phase")
assert.True(t, apimeta.IsStatusConditionTrue(inst.Status.Conditions, string(v1.ConditionStarted)))
// Complete the action
action.Status.Phase = v1.PhaseSucceeded
action.Status.Conditions = []metav1.Condition{{Type: string(v1.ConditionComplete), Status: metav1.ConditionTrue}}
require.NoError(t, controller.Status().Update(ctx, &action))
triggerReconcile()
// Verify that the installation status was synced with the action
require.NotNil(t, inst.Status.Action, "expected Action to still be set")
assert.Equal(t, v1.PhaseSucceeded, inst.Status.Phase, "incorrect Phase")
assert.True(t, apimeta.IsStatusConditionTrue(inst.Status.Conditions, string(v1.ConditionComplete)))
// Fail the action
action.Status.Phase = v1.PhaseFailed
action.Status.Conditions = []metav1.Condition{{Type: string(v1.ConditionFailed), Status: metav1.ConditionTrue}}
require.NoError(t, controller.Status().Update(ctx, &action))
triggerReconcile()
actionName := inst.Status.Action.Name
// Verify that the installation status shows the action is failed
require.NotNil(t, inst.Status.Action, "expected Action to still be set")
assert.Equal(t, v1.PhaseFailed, inst.Status.Phase, "incorrect Phase")
assert.True(t, apimeta.IsStatusConditionTrue(inst.Status.Conditions, string(v1.ConditionFailed)))
// Edit the installation spec
inst.Generation = 2
require.NoError(t, controller.Update(ctx, &inst))
triggerReconcile()
// Verify that the installation status was re-initialized
assert.Equal(t, int64(2), inst.Status.ObservedGeneration)
assert.Equal(t, v1.PhaseUnknown, inst.Status.Phase, "New resources should be initialized to Phase: Unknown")
assert.Empty(t, inst.Status.Conditions, "Conditions should have been reset")
// Retry the last action
lastAction := actionName
inst.Annotations = map[string]string{v1.AnnotationRetry: "retry-1"}
require.NoError(t, controller.Update(ctx, &inst))
triggerReconcile()
// Verify that action has retry set on it now
require.NotNil(t, inst.Status.Action, "Expected the action to still be set")
assert.Equal(t, lastAction, actionName, "Expected the action to be the same")
// get the latest version of the action
require.NoError(t, controller.Get(ctx, client.ObjectKey{Namespace: inst.Namespace, Name: inst.Status.Action.Name}, &action))
assert.NotEmpty(t, action.Annotations[v1.AnnotationRetry], "Expected the action to have its retry annotation set")
assert.Equal(t, int64(2), inst.Status.ObservedGeneration)
assert.NotEmpty(t, inst.Status.Action, "Expected the action to still be set")
assert.Equal(t, v1.PhaseUnknown, inst.Status.Phase, "New resources should be initialized to Phase: Unknown")
assert.Empty(t, inst.Status.Conditions, "Conditions should have been reset")
// Delete the installation (setting the delete timestamp directly instead of client.Delete because otherwise the fake client just removes it immediately)
// The fake client doesn't really follow finalizer logic
// metadata.Timestamp is immutable and not allowed to be set by the client
now := metav1.NewTime(time.Now())
inst.Generation = 3
inst.DeletionTimestamp = &now
require.NoError(t, controller.Delete(ctx, &inst))
//end of the lifecycle
}
func TestInstallationReconciler_createAgentAction(t *testing.T) {
controller := setupInstallationController()
inst := &v1.Installation{
TypeMeta: metav1.TypeMeta{
APIVersion: v1.GroupVersion.String(),
Kind: "Installation",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "myblog",
UID: "random-uid",
Generation: 1,
Labels: map[string]string{
"testLabel": "abc123",
},
Annotations: map[string]string{
v1.AnnotationRetry: "2021-2-2 12:00:00",
},
},
Spec: v1.InstallationSpec{
Namespace: "dev",
Name: "wordpress",
AgentConfig: &corev1.LocalObjectReference{Name: "myAgentConfig"},
},
}
action, err := controller.createAgentAction(context.Background(), logr.Discard(), inst)
require.NoError(t, err)
assert.Equal(t, "test", action.Namespace)
assert.Contains(t, action.Name, "myblog-")
assert.Len(t, action.OwnerReferences, 1, "expected an owner reference")
wantOwnerRef := metav1.OwnerReference{
APIVersion: v1.GroupVersion.String(),
Kind: "Installation",
Name: "myblog",
UID: "random-uid",
Controller: ptr.To(true),
BlockOwnerDeletion: ptr.To(true),
}
assert.Equal(t, wantOwnerRef, action.OwnerReferences[0], "incorrect owner reference")
assertContains(t, action.Annotations, v1.AnnotationRetry, inst.Annotations[v1.AnnotationRetry], "incorrect annotation")
assertContains(t, action.Labels, v1.LabelManaged, "true", "incorrect label")
assertContains(t, action.Labels, v1.LabelResourceKind, "Installation", "incorrect label")
assertContains(t, action.Labels, v1.LabelResourceName, "myblog", "incorrect label")
assertContains(t, action.Labels, v1.LabelResourceGeneration, "1", "incorrect label")
assertContains(t, action.Labels, "testLabel", "abc123", "incorrect label")
assert.Equal(t, inst.Spec.AgentConfig, action.Spec.AgentConfig, "incorrect AgentConfig reference")
assert.Equal(t, inst.Spec.AgentConfig, action.Spec.AgentConfig, "incorrect PorterConfig reference")
assert.Nilf(t, action.Spec.Command, "should use the default command for the agent")
assert.Equal(t, []string{"installation", "apply", "installation.yaml"}, action.Spec.Args, "incorrect agent arguments")
assert.Contains(t, action.Spec.Files, "installation.yaml")
assert.NotEmpty(t, action.Spec.Files["installation.yaml"], "expected installation.yaml to get set on the action")
assert.Empty(t, action.Spec.Env, "incorrect Env")
assert.Empty(t, action.Spec.EnvFrom, "incorrect EnvFrom")
assert.Empty(t, action.Spec.Volumes, "incorrect Volumes")
assert.Empty(t, action.Spec.VolumeMounts, "incorrect VolumeMounts")
}
func TestDeletionTimeStampInstallation(t *testing.T) {
action := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-ns",
DeletionTimestamp: ptr.To(metav1.Now()),
Finalizers: []string{v1.FinalizerName},
},
}
ctx := context.Background()
r := setupInstallationController(action)
_, err := r.Reconcile(ctx, ctrl.Request{})
assert.NoError(t, err)
}
func TestCreateInstallationOutputsCR(t *testing.T) {
ctx := context.Background()
install := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
Spec: v1.InstallationSpec{
Name: "install-name",
Namespace: "install-ns",
},
}
in := &installationv1.ListInstallationLatestOutputResponse{
Outputs: []*installationv1.PorterValue{
{
Name: "fake-output",
Type: "string",
Sensitive: false,
Value: structpb.NewStringValue("this is an output"),
},
},
}
tests := map[string]struct {
wantError bool
outputs *installationv1.ListInstallationLatestOutputResponse
}{
"success": {wantError: false, outputs: in},
"failure": {wantError: true, outputs: &installationv1.ListInstallationLatestOutputResponse{}},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
rec := setupInstallationController()
cr, err := rec.CreateInstallationOutputsCR(ctx, install, test.outputs)
if test.wantError {
assert.Error(t, err)
}
if !test.wantError {
assert.NoError(t, err)
assert.IsType(t, &v1.InstallationOutput{}, cr)
}
})
}
}
func TestCreateStatusOutputs(t *testing.T) {
ctx := context.Background()
rec := setupInstallationController()
install := &v1.InstallationOutput{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-ns",
},
Spec: v1.InstallationOutputSpec{
Name: "fake-porterName",
Namespace: "fake-porter-namespace",
},
}
in := &installationv1.ListInstallationLatestOutputResponse{
Outputs: []*installationv1.PorterValue{
{
Name: "fake-output",
Type: "string",
Sensitive: false,
Value: structpb.NewStringValue("this is an output"),
},
},
}
installOut, err := rec.CreateStatusOutputs(ctx, install, in)
assert.NoError(t, err)
assert.IsType(t, v1.InstallationOutputStatus{}, installOut.Status)
}
func TestCheckOrCreateInstallationOutputsCR(t *testing.T) {
ctx := context.Background()
output := &v1.InstallationOutput{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
}
install := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
Spec: v1.InstallationSpec{
Name: "fake-install",
Namespace: "fake-ns",
},
}
rec := setupInstallationController(output)
_, err := rec.CheckOrCreateInstallationOutputsCR(ctx, logr.Discard(), install)
assert.NoError(t, err)
}
func TestCheckOrCreateInstallationOutputsCRCreate(t *testing.T) {
ctx := context.Background()
grpcClient := &mocks.PorterClient{}
outputs := &installationv1.ListInstallationLatestOutputResponse{
Outputs: []*installationv1.PorterValue{
{
Name: "fake-output",
Type: "string",
Sensitive: false,
Value: structpb.NewStringValue("output that is fake"),
},
},
}
listInstallationRequest := &installationv1.ListInstallationLatestOutputRequest{Name: "fake-install", Namespace: ptr.To("fake-ns")}
grpcClient.On("ListInstallationLatestOutputs", ctx, listInstallationRequest).Return(outputs, nil)
rec := setupInstallationController()
install := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
Spec: v1.InstallationSpec{
Name: "fake-install",
Namespace: "fake-ns",
},
}
clientConn := &mocks.ClientConn{}
clientConn.On("Close").Return(nil)
rec.CreateGRPCClient = func(ctx context.Context) (porterv1alpha1.PorterClient, ClientConn, error) {
return grpcClient, clientConn, nil
}
_, err := rec.CheckOrCreateInstallationOutputsCR(ctx, logr.Discard(), install)
// NOTE: This errors because of the limitation we have with fake in
// controller-runtime. https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.15.0/pkg/client/fake
// There is some support for sub resources which
// can cause issues with tests if you're trying to update e.g.
// metadata and status in the same reconcile. We update the status in the
// same reconcile when creating the object but it can't find it after it
// creates it. This limitation isn't an issue when running live.
assert.Error(t, err)
}
func TestCheckOrCreateInstallationOutputsCRCreateFail(t *testing.T) {
ctx := context.Background()
grpcClient := &mocks.PorterClient{}
listInstallationRequest := &installationv1.ListInstallationLatestOutputRequest{Name: "fake-install", Namespace: ptr.To("fake-ns")}
grpcClient.On("ListInstallationLatestOutputs", ctx, listInstallationRequest).Return(nil, fmt.Errorf("this is an error"))
rec := setupInstallationController()
install := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
Spec: v1.InstallationSpec{
Name: "fake-install",
Namespace: "fake-ns",
},
}
_, err := rec.CheckOrCreateInstallationOutputsCR(ctx, logr.Discard(), install)
// NOTE: This will return nil if the output of the grpc call fails. We do not
// want to requeue if this fails. We will not include outputs of
// installations that do not have it stored in the grpc server.
assert.NoError(t, err)
}
func TestSetupWithManager(t *testing.T) {
r := &InstallationReconciler{}
scheme := runtime.NewScheme()
assert.NoError(t, v1.AddToScheme(scheme))
var restConfig *rest.Config
mgr, err := manager.New(restConfig, manager.Options{})
assert.Error(t, err)
err = r.SetupWithManager(mgr)
assert.Error(t, err)
}
func setupInstallationController(objs ...client.Object) *InstallationReconciler {
scheme := runtime.NewScheme()
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(v1.AddToScheme(scheme))
fakeBuilder := fake.NewClientBuilder()
fakeBuilder.WithScheme(scheme)
fakeBuilder.WithObjects(objs...).WithStatusSubresource(objs...)
fakeClient := fakeBuilder.Build()
clientConn := &mocks.ClientConn{}
clientConn.On("Close").Return(nil)
return &InstallationReconciler{
Log: logr.Discard(),
Client: fakeClient,
Recorder: record.NewFakeRecorder(42),
Scheme: scheme,
CreateGRPCClient: func(ctx context.Context) (porterv1alpha1.PorterClient, ClientConn, error) {
return &mocks.PorterClient{}, clientConn, fmt.Errorf("error with grpc")
},
}
}
func TestIsHandled(t *testing.T) {
ctx := context.Background()
inst := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
Spec: v1.InstallationSpec{
Name: "fake-install",
Namespace: "fake-ns",
},
}
scheme := runtime.NewScheme()
utilruntime.Must(v1.AddToScheme(scheme))
fakeBuilder := fake.NewClientBuilder()
fakeBuilder.WithScheme(scheme)
fakeBuilder.WithObjects(inst).WithStatusSubresource(inst)
fakeClient := fakeBuilder.Build()
client := interceptor.NewClient(fakeClient, interceptor.Funcs{
List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
return fmt.Errorf("this is an error")
},
})
r := &InstallationReconciler{
Client: client,
}
_, _, err := r.isHandled(ctx, logr.Discard(), inst)
assert.Error(t, err)
}
func TestApplyDeletionPolicyWithNoAnnotation(t *testing.T) {
tests := map[string]struct {
wantPolicy string
policy string
}{
"empty-string": {policy: "", wantPolicy: v1.PorterDeletePolicyDelete},
"policy-orphan": {policy: v1.PorterDeletePolicyOrphan, wantPolicy: v1.PorterDeletePolicyOrphan},
"policy-delete": {policy: v1.PorterDeletePolicyDelete, wantPolicy: v1.PorterDeletePolicyDelete},
}
ctx := context.Background()
inst := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
},
Spec: v1.InstallationSpec{
Name: "fake-install",
Namespace: "fake-ns",
},
}
scheme := runtime.NewScheme()
utilruntime.Must(v1.AddToScheme(scheme))
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(inst).WithStatusSubresource(inst).Build()
rc := &InstallationReconciler{
Client: fakeClient,
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := rc.applyDeletionPolicy(ctx, logr.Discard(), inst, test.policy)
assert.NoError(t, err)
assert.Equal(t, inst.GetAnnotations()[v1.PorterDeletePolicyAnnotation], test.wantPolicy)
})
}
}
func TestApplyDeletionPolicyWithAnnotation(t *testing.T) {
ctx := context.Background()
inst := &v1.Installation{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-install",
Namespace: "fake-ns",
Annotations: map[string]string{
v1.PorterDeletePolicyAnnotation: v1.PorterDeletePolicyDelete,
},
},
Spec: v1.InstallationSpec{
Name: "fake-install",
Namespace: "fake-ns",
},
}
scheme := runtime.NewScheme()
utilruntime.Must(v1.AddToScheme(scheme))
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(inst).WithStatusSubresource(inst).Build()
rc := &InstallationReconciler{
Client: fakeClient,
}
tests := map[string]struct {
policy string
wantPolicy string
}{
"no-policy": {policy: "", wantPolicy: v1.PorterDeletePolicyDelete},
"delete-update-to-orphan": {policy: v1.PorterDeletePolicyOrphan, wantPolicy: v1.PorterDeletePolicyOrphan},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := rc.applyDeletionPolicy(ctx, logr.Discard(), inst, test.policy)
assert.NoError(t, err)
assert.Equal(t, inst.GetAnnotations()[v1.PorterDeletePolicyAnnotation], test.wantPolicy)
})
}
}