...
1 package kclient
2
3 import (
4 "errors"
5 "testing"
6
7 apierrors "k8s.io/apimachinery/pkg/api/errors"
8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9 "k8s.io/apimachinery/pkg/runtime/schema"
10 )
11
12 func TestClient_TryWithBlockOwnerDeletion(t *testing.T) {
13 type args struct {
14 ownerReference metav1.OwnerReference
15 exec func() func(ownerReference metav1.OwnerReference) error
16 }
17 tests := []struct {
18 name string
19 args args
20 wantErr bool
21 }{
22 {
23 name: "first call is ok",
24 args: args{
25 ownerReference: metav1.OwnerReference{},
26 exec: func() func(ownerReference metav1.OwnerReference) error {
27 calls := 0
28 return func(ownerReference metav1.OwnerReference) error {
29 calls++
30 if calls > 1 {
31 t.Errorf("only one call should happen")
32 }
33 return nil
34 }
35 },
36 },
37 wantErr: false,
38 },
39 {
40 name: "first call fails with non forbidden error",
41 args: args{
42 ownerReference: metav1.OwnerReference{},
43 exec: func() func(ownerReference metav1.OwnerReference) error {
44 calls := 0
45 return func(ownerReference metav1.OwnerReference) error {
46 calls++
47 if calls > 1 {
48 t.Errorf("only one call should happen")
49 }
50 return errors.New("an error call 1")
51 }
52 },
53 },
54 wantErr: true,
55 },
56 {
57 name: "first call fails with forbidden error, second call is ok",
58 args: args{
59 ownerReference: metav1.OwnerReference{},
60 exec: func() func(ownerReference metav1.OwnerReference) error {
61 calls := 0
62 return func(ownerReference metav1.OwnerReference) error {
63 calls++
64 switch calls {
65 case 1:
66 return apierrors.NewForbidden(schema.GroupResource{}, "aname", errors.New("an error"))
67 case 2:
68 return nil
69 default:
70 t.Errorf("only two calls should happen")
71 return nil
72 }
73 }
74 },
75 },
76 wantErr: false,
77 },
78 {
79 name: "first call fails with forbidden error, second call fails with error",
80 args: args{
81 ownerReference: metav1.OwnerReference{},
82 exec: func() func(ownerReference metav1.OwnerReference) error {
83 calls := 0
84 return func(ownerReference metav1.OwnerReference) error {
85 calls++
86 switch calls {
87 case 1:
88 return apierrors.NewForbidden(schema.GroupResource{}, "aname", errors.New("an error"))
89 case 2:
90 return errors.New("an error")
91 default:
92 t.Errorf("only two calls should happen")
93 return nil
94 }
95 }
96 },
97 },
98 wantErr: true,
99 },
100 }
101 for _, tt := range tests {
102 t.Run(tt.name, func(t *testing.T) {
103 c := Client{}
104 if err := c.TryWithBlockOwnerDeletion(tt.args.ownerReference, tt.args.exec()); (err != nil) != tt.wantErr {
105 t.Errorf("Client.TryWithBlockOwnerDeletion() error = %v, wantErr %v", err, tt.wantErr)
106 }
107 })
108 }
109 }
110
View as plain text