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
128 lines (105 loc) · 4.84 KB
/
reconciler_test.go
File metadata and controls
128 lines (105 loc) · 4.84 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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package controllerregistrationfinalizer_test
import (
"context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/gardener/gardener/pkg/api/indexer"
"github.com/gardener/gardener/pkg/apis/core"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/gardener/gardener/pkg/client/kubernetes"
. "github.com/gardener/gardener/pkg/controllermanager/controller/controllerregistration/controllerregistrationfinalizer"
. "github.com/gardener/gardener/pkg/utils/test/matchers"
)
var _ = Describe("ControllerRegistration", func() {
var (
ctx = context.TODO()
ctrl *gomock.Controller
c client.Client
reconciler *Reconciler
controllerRegistration *gardencorev1beta1.ControllerRegistration
controllerRegistrationName = "controllerRegistration"
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
c = fakeclient.NewClientBuilder().
WithScheme(kubernetes.GardenScheme).
WithIndex(&gardencorev1beta1.ControllerInstallation{}, core.RegistrationRefName, indexer.ControllerInstallationRegistrationRefNameIndexerFunc).
Build()
})
AfterEach(func() {
ctrl.Finish()
})
Describe("Reconciler", func() {
BeforeEach(func() {
reconciler = &Reconciler{Client: c}
controllerRegistration = &gardencorev1beta1.ControllerRegistration{
ObjectMeta: metav1.ObjectMeta{
Name: controllerRegistrationName,
},
}
})
It("should return nil because object not found", func() {
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: controllerRegistrationName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
})
Context("deletion timestamp not set", func() {
BeforeEach(func() {
Expect(c.Create(ctx, controllerRegistration)).To(Succeed())
})
It("should ensure the finalizer", func() {
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: controllerRegistrationName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(c.Get(ctx, client.ObjectKeyFromObject(controllerRegistration), controllerRegistration)).To(Succeed())
Expect(controllerRegistration.Finalizers).To(ConsistOf("core.gardener.cloud/controllerregistration"))
})
})
Context("deletion timestamp set", func() {
BeforeEach(func() {
Expect(c.Create(ctx, controllerRegistration)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: controllerRegistrationName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(c.Get(ctx, client.ObjectKeyFromObject(controllerRegistration), controllerRegistration)).To(Succeed())
Expect(controllerRegistration.Finalizers).To(ConsistOf("core.gardener.cloud/controllerregistration"))
Expect(c.Delete(ctx, controllerRegistration)).To(Succeed())
})
It("should return an error because installation referencing controllerRegistration exists", func() {
controllerInstallation := &gardencorev1beta1.ControllerInstallation{
ObjectMeta: metav1.ObjectMeta{
Name: "controllerInstallation",
},
Spec: gardencorev1beta1.ControllerInstallationSpec{
RegistrationRef: corev1.ObjectReference{
Name: controllerRegistrationName,
},
},
}
controllerInstallation2 := controllerInstallation.DeepCopy()
controllerInstallation2.Name = "controllerInstallation-2"
Expect(c.Create(ctx, controllerInstallation)).To(Succeed())
Expect(c.Create(ctx, controllerInstallation2)).To(Succeed())
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: controllerRegistrationName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).To(MatchError(ContainSubstring("cannot remove finalizer of ControllerRegistration %q because still found ControllerInstallations: [%s %s]", controllerRegistration.Name, controllerInstallation.Name, controllerInstallation2.Name)))
})
It("should remove the finalizer", func() {
result, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{Name: controllerRegistrationName}})
Expect(result).To(Equal(reconcile.Result{}))
Expect(err).NotTo(HaveOccurred())
Expect(c.Get(ctx, client.ObjectKeyFromObject(controllerRegistration), controllerRegistration)).To(BeNotFoundError())
})
})
})
})