forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconciler_test.go
102 lines (86 loc) · 2.62 KB
/
reconciler_test.go
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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package managedresource
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
var _ = Describe("Controller", func() {
Describe("#injectLabels", func() {
var (
obj, expected *unstructured.Unstructured
labels map[string]string
)
BeforeEach(func() {
obj = &unstructured.Unstructured{Object: map[string]any{}}
expected = obj.DeepCopy()
})
It("do nothing as labels is nil", func() {
labels = nil
Expect(injectLabels(obj, labels)).To(Succeed())
Expect(obj).To(Equal(expected))
})
It("do nothing as labels is empty", func() {
labels = map[string]string{}
Expect(injectLabels(obj, labels)).To(Succeed())
Expect(obj).To(Equal(expected))
})
It("should correctly inject labels into the object's metadata", func() {
labels = map[string]string{
"inject": "me",
}
expected.SetLabels(labels)
Expect(injectLabels(obj, labels)).To(Succeed())
Expect(obj).To(Equal(expected))
})
It("should correctly inject labels into the object's pod template's metadata", func() {
labels = map[string]string{
"inject": "me",
}
// add .spec.template to object
Expect(unstructured.SetNestedMap(obj.Object, map[string]any{
"template": map[string]any{},
}, "spec")).To(Succeed())
expected = obj.DeepCopy()
Expect(unstructured.SetNestedMap(expected.Object, map[string]any{
"inject": "me",
}, "spec", "template", "metadata", "labels")).To(Succeed())
expected.SetLabels(labels)
Expect(injectLabels(obj, labels)).To(Succeed())
Expect(obj).To(Equal(expected))
})
It("should correctly inject labels into the object's volumeClaimTemplates' metadata", func() {
labels = map[string]string{
"inject": "me",
}
// add .spec.volumeClaimTemplates to object
Expect(unstructured.SetNestedMap(obj.Object, map[string]any{
"volumeClaimTemplates": []any{
map[string]any{
"metadata": map[string]any{
"name": "volume-claim-name",
},
},
},
}, "spec")).To(Succeed())
expected = obj.DeepCopy()
Expect(unstructured.SetNestedMap(expected.Object, map[string]any{
"volumeClaimTemplates": []any{
map[string]any{
"metadata": map[string]any{
"name": "volume-claim-name",
"labels": map[string]any{
"inject": "me",
},
},
},
},
}, "spec")).To(Succeed())
expected.SetLabels(labels)
Expect(injectLabels(obj, labels)).To(Succeed())
Expect(obj).To(Equal(expected))
})
})
})