forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_test.go
More file actions
125 lines (103 loc) · 3.78 KB
/
add_test.go
File metadata and controls
125 lines (103 loc) · 3.78 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
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package managedresource_test
import (
"context"
"errors"
. "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/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
resourcesv1alpha1 "github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
. "github.com/gardener/gardener/pkg/resourcemanager/controller/managedresource"
"github.com/gardener/gardener/pkg/resourcemanager/predicate"
mockclient "github.com/gardener/gardener/third_party/mock/controller-runtime/client"
)
var _ = Describe("#MapSecretToManagedResources", func() {
var (
ctx = context.TODO()
c *mockclient.MockClient
ctrl *gomock.Controller
m handler.MapFunc
secret *corev1.Secret
filter *predicate.ClassFilter
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
c = mockclient.NewMockClient(ctrl)
secret = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "mr-secret",
Namespace: "mr-namespace",
},
}
filter = predicate.NewClassFilter("seed")
m = (&Reconciler{SourceClient: c}).MapSecretToManagedResources(filter)
})
AfterEach(func() {
ctrl.Finish()
})
It("should do nothing, if Object is nil", func() {
requests := m(ctx, nil)
Expect(requests).To(BeEmpty())
})
It("should do nothing, if Object is not a Secret", func() {
requests := m(ctx, &corev1.Pod{})
Expect(requests).To(BeEmpty())
})
It("should do nothing, if list fails", func() {
c.EXPECT().List(ctx, gomock.AssignableToTypeOf(&resourcesv1alpha1.ManagedResourceList{}), client.InNamespace(secret.Namespace)).
Return(errors.New("fake"))
requests := m(ctx, secret)
Expect(requests).To(BeEmpty())
})
It("should do nothing, if there are no ManagedResources", func() {
c.EXPECT().List(ctx, gomock.AssignableToTypeOf(&resourcesv1alpha1.ManagedResourceList{}), client.InNamespace(secret.Namespace))
requests := m(ctx, secret)
Expect(requests).To(BeEmpty())
})
It("should do nothing, if there are no ManagedResources we are responsible for", func() {
mr := resourcesv1alpha1.ManagedResource{
Spec: resourcesv1alpha1.ManagedResourceSpec{Class: ptr.To("other")},
}
c.EXPECT().List(ctx, gomock.AssignableToTypeOf(&resourcesv1alpha1.ManagedResourceList{}), client.InNamespace(secret.Namespace)).
DoAndReturn(func(_ context.Context, list runtime.Object, _ ...client.ListOption) error {
list.(*resourcesv1alpha1.ManagedResourceList).Items = []resourcesv1alpha1.ManagedResource{mr}
return nil
})
requests := m(ctx, secret)
Expect(requests).To(BeEmpty())
})
It("should correctly map to ManagedResources that reference the secret", func() {
mr := resourcesv1alpha1.ManagedResource{
ObjectMeta: metav1.ObjectMeta{
Name: "mr",
Namespace: secret.Namespace,
},
Spec: resourcesv1alpha1.ManagedResourceSpec{
Class: ptr.To(filter.ResourceClass()),
SecretRefs: []corev1.LocalObjectReference{{Name: secret.Name}},
},
}
c.EXPECT().List(ctx, gomock.AssignableToTypeOf(&resourcesv1alpha1.ManagedResourceList{}), client.InNamespace(secret.Namespace)).
DoAndReturn(func(_ context.Context, list runtime.Object, _ ...client.ListOption) error {
list.(*resourcesv1alpha1.ManagedResourceList).Items = []resourcesv1alpha1.ManagedResource{mr}
return nil
})
requests := m(ctx, secret)
Expect(requests).To(ConsistOf(
reconcile.Request{NamespacedName: types.NamespacedName{
Name: mr.Name,
Namespace: mr.Namespace,
}},
))
})
})