forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreconciler_test.go
More file actions
213 lines (179 loc) · 9.75 KB
/
reconciler_test.go
File metadata and controls
213 lines (179 loc) · 9.75 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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package cloudprofile_test
import (
"context"
"errors"
"fmt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
. "github.com/gardener/gardener/pkg/controllermanager/controller/cloudprofile"
mockclient "github.com/gardener/gardener/third_party/mock/controller-runtime/client"
)
var _ = Describe("Reconciler", func() {
const finalizerName = "gardener"
var (
ctx = context.TODO()
ctrl *gomock.Controller
c *mockclient.MockClient
cloudProfileName string
fakeErr error
reconciler reconcile.Reconciler
cloudProfile *gardencorev1beta1.CloudProfile
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
c = mockclient.NewMockClient(ctrl)
cloudProfileName = "test-cloudprofile"
fakeErr = errors.New("fake err")
reconciler = &Reconciler{Client: c, Recorder: &record.FakeRecorder{}}
cloudProfile = &gardencorev1beta1.CloudProfile{
ObjectMeta: metav1.ObjectMeta{
Name: cloudProfileName,
ResourceVersion: "42",
},
}
})
AfterEach(func() {
ctrl.Finish()
})
It("should return nil because object not found", func() {
c.EXPECT().Get(gomock.Any(), client.ObjectKey{Name: cloudProfileName}, gomock.AssignableToTypeOf(&gardencorev1beta1.CloudProfile{})).Return(apierrors.NewNotFound(schema.GroupResource{}, ""))
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: cloudProfileName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
})
It("should return err because object reading failed", func() {
c.EXPECT().Get(gomock.Any(), client.ObjectKey{Name: cloudProfileName}, gomock.AssignableToTypeOf(&gardencorev1beta1.CloudProfile{})).Return(fakeErr)
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: cloudProfileName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).To(MatchError(fakeErr))
})
Context("when deletion timestamp not set", func() {
BeforeEach(func() {
c.EXPECT().Get(gomock.Any(), client.ObjectKey{Name: cloudProfileName}, gomock.AssignableToTypeOf(&gardencorev1beta1.CloudProfile{})).DoAndReturn(func(_ context.Context, _ client.ObjectKey, obj *gardencorev1beta1.CloudProfile, _ ...client.GetOption) error {
*obj = *cloudProfile
return nil
})
})
It("should ensure the finalizer (error)", func() {
errToReturn := apierrors.NewNotFound(schema.GroupResource{}, cloudProfileName)
c.EXPECT().Patch(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.CloudProfile{}), gomock.Any()).DoAndReturn(func(_ context.Context, o client.Object, patch client.Patch, _ ...client.PatchOption) error {
Expect(patch.Data(o)).To(BeEquivalentTo(fmt.Sprintf(`{"metadata":{"finalizers":["%s"],"resourceVersion":"42"}}`, finalizerName)))
return errToReturn
})
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: cloudProfileName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).To(MatchError(err))
})
It("should ensure the finalizer (no error)", func() {
c.EXPECT().Patch(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.CloudProfile{}), gomock.Any()).DoAndReturn(func(_ context.Context, o client.Object, patch client.Patch, _ ...client.PatchOption) error {
Expect(patch.Data(o)).To(BeEquivalentTo(fmt.Sprintf(`{"metadata":{"finalizers":["%s"],"resourceVersion":"42"}}`, finalizerName)))
return nil
})
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: cloudProfileName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
})
})
Context("when deletion timestamp set", func() {
BeforeEach(func() {
now := metav1.Now()
cloudProfile.DeletionTimestamp = &now
cloudProfile.Finalizers = []string{finalizerName}
c.EXPECT().Get(gomock.Any(), client.ObjectKey{Name: cloudProfileName}, gomock.AssignableToTypeOf(&gardencorev1beta1.CloudProfile{})).DoAndReturn(func(_ context.Context, _ client.ObjectKey, obj *gardencorev1beta1.CloudProfile, _ ...client.GetOption) error {
*obj = *cloudProfile
return nil
})
})
It("should do nothing because finalizer is not present", func() {
cloudProfile.Finalizers = nil
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: cloudProfileName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
})
It("should return an error because Shoot referencing CloudProfile exists", func() {
c.EXPECT().List(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.NamespacedCloudProfileList{}), gomock.Eq(client.MatchingFields{"spec.parent.name": cloudProfileName})).DoAndReturn(func(_ context.Context, obj *gardencorev1beta1.NamespacedCloudProfileList, _ ...client.ListOption) error {
(&gardencorev1beta1.NamespacedCloudProfileList{}).DeepCopyInto(obj)
return nil
})
c.EXPECT().List(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.ShootList{})).DoAndReturn(func(_ context.Context, obj *gardencorev1beta1.ShootList, _ ...client.ListOption) error {
(&gardencorev1beta1.ShootList{Items: []gardencorev1beta1.Shoot{
{
ObjectMeta: metav1.ObjectMeta{Name: "test-shoot", Namespace: "test-namespace"},
Spec: gardencorev1beta1.ShootSpec{
CloudProfileName: &cloudProfileName,
},
},
}}).DeepCopyInto(obj)
return nil
})
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: cloudProfileName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).To(MatchError(ContainSubstring("Cannot delete CloudProfile")))
})
It("should return an error because NamespacedCloudProfile referencing CloudProfile exists", func() {
c.EXPECT().List(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.NamespacedCloudProfileList{}), gomock.Eq(client.MatchingFields{"spec.parent.name": cloudProfileName})).DoAndReturn(func(_ context.Context, obj *gardencorev1beta1.NamespacedCloudProfileList, _ ...client.ListOption) error {
(&gardencorev1beta1.NamespacedCloudProfileList{Items: []gardencorev1beta1.NamespacedCloudProfile{
{
ObjectMeta: metav1.ObjectMeta{Name: "test-namespacedprofile", Namespace: "test-namespace"},
Spec: gardencorev1beta1.NamespacedCloudProfileSpec{
Parent: gardencorev1beta1.CloudProfileReference{
Kind: "CloudProfile",
Name: cloudProfileName,
},
},
},
}}).DeepCopyInto(obj)
return nil
})
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: cloudProfileName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).To(MatchError(ContainSubstring("Cannot delete CloudProfile")))
})
It("should remove the finalizer (error)", func() {
c.EXPECT().List(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.NamespacedCloudProfileList{}), gomock.Eq(client.MatchingFields{"spec.parent.name": cloudProfileName})).DoAndReturn(func(_ context.Context, obj *gardencorev1beta1.NamespacedCloudProfileList, _ ...client.ListOption) error {
(&gardencorev1beta1.NamespacedCloudProfileList{}).DeepCopyInto(obj)
return nil
})
c.EXPECT().List(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.ShootList{})).DoAndReturn(func(_ context.Context, obj *gardencorev1beta1.ShootList, _ ...client.ListOption) error {
(&gardencorev1beta1.ShootList{}).DeepCopyInto(obj)
return nil
})
c.EXPECT().Patch(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.CloudProfile{}), gomock.Any()).DoAndReturn(func(_ context.Context, o client.Object, patch client.Patch, _ ...client.PatchOption) error {
Expect(patch.Data(o)).To(BeEquivalentTo(`{"metadata":{"finalizers":null,"resourceVersion":"42"}}`))
return fakeErr
})
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: cloudProfileName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).To(MatchError(fakeErr))
})
It("should remove the finalizer (no error)", func() {
c.EXPECT().List(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.NamespacedCloudProfileList{}), gomock.Eq(client.MatchingFields{"spec.parent.name": cloudProfileName})).DoAndReturn(func(_ context.Context, obj *gardencorev1beta1.NamespacedCloudProfileList, _ ...client.ListOption) error {
(&gardencorev1beta1.NamespacedCloudProfileList{}).DeepCopyInto(obj)
return nil
})
c.EXPECT().List(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.ShootList{})).DoAndReturn(func(_ context.Context, obj *gardencorev1beta1.ShootList, _ ...client.ListOption) error {
(&gardencorev1beta1.ShootList{}).DeepCopyInto(obj)
return nil
})
c.EXPECT().Patch(gomock.Any(), gomock.AssignableToTypeOf(&gardencorev1beta1.CloudProfile{}), gomock.Any()).DoAndReturn(func(_ context.Context, o client.Object, patch client.Patch, _ ...client.PatchOption) error {
Expect(patch.Data(o)).To(BeEquivalentTo(`{"metadata":{"finalizers":null,"resourceVersion":"42"}}`))
return nil
})
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: cloudProfileName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
})
})
})