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
135 lines (111 loc) · 4.76 KB
/
reconciler_test.go
File metadata and controls
135 lines (111 loc) · 4.76 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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package quota
import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/events"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
securityv1alpha1 "github.com/gardener/gardener/pkg/apis/security/v1alpha1"
"github.com/gardener/gardener/pkg/client/kubernetes"
. "github.com/gardener/gardener/pkg/utils/test/matchers"
)
var _ = Describe("Reconciler", func() {
const finalizerName = "gardener"
var (
ctx = context.TODO()
fakeClient client.Client
reconciler reconcile.Reconciler
quotaName string
quota *gardencorev1beta1.Quota
secretBinding *gardencorev1beta1.SecretBinding
credentialsBinding *securityv1alpha1.CredentialsBinding
)
BeforeEach(func() {
fakeClient = fakeclient.NewClientBuilder().WithScheme(kubernetes.GardenScheme).Build()
quotaName = "test-quota"
reconciler = &Reconciler{Client: fakeClient, Recorder: &events.FakeRecorder{}}
quota = &gardencorev1beta1.Quota{
ObjectMeta: metav1.ObjectMeta{
Name: quotaName,
},
}
secretBinding = &gardencorev1beta1.SecretBinding{
ObjectMeta: metav1.ObjectMeta{Name: "test-secretbinding", Namespace: "test-namespace"},
Quotas: []corev1.ObjectReference{
{
Name: quotaName,
},
},
}
credentialsBinding = &securityv1alpha1.CredentialsBinding{
ObjectMeta: metav1.ObjectMeta{Name: "test-credentialsbinding", Namespace: "test-namespace"},
Quotas: []corev1.ObjectReference{
{
Name: quotaName,
},
},
}
})
It("should return nil because object not found", func() {
Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(quota), &gardencorev1beta1.Quota{})).To(BeNotFoundError())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: quotaName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
})
Context("when deletion timestamp not set", func() {
BeforeEach(func() {
Expect(fakeClient.Create(ctx, quota)).To(Succeed())
})
It("should ensure the finalizer", func() {
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: quotaName}})
Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(quota), quota)).To(Succeed())
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(quota.GetFinalizers()).Should(ConsistOf(finalizerName))
})
})
Context("when deletion timestamp set", func() {
BeforeEach(func() {
quota.Finalizers = []string{finalizerName}
Expect(fakeClient.Create(ctx, quota)).To(Succeed())
Expect(fakeClient.Delete(ctx, quota)).To(Succeed())
})
It("should do nothing because finalizer is not present", func() {
Expect(fakeClient.Create(ctx, secretBinding)).To(Succeed())
Expect(fakeClient.Create(ctx, credentialsBinding)).To(Succeed())
patch := client.MergeFrom(quota.DeepCopy())
quota.Finalizers = nil
Expect(fakeClient.Patch(ctx, quota, patch)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: quotaName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
})
It("should return an error because SecretBinding referencing Quota exists", func() {
Expect(fakeClient.Create(ctx, secretBinding)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: quotaName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).To(MatchError(ContainSubstring("cannot delete Quota")))
})
It("should return an error because CredentialsBinding referencing Quota exists", func() {
Expect(fakeClient.Create(ctx, credentialsBinding)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: quotaName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).To(MatchError(ContainSubstring("cannot delete Quota")))
})
It("should remove the finalizer because no SecretBinding or CredentialsBinding are referencing the Quota", func() {
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: quotaName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(ctx, client.ObjectKeyFromObject(quota), quota)).To(BeNotFoundError())
})
})
})