forked from kubevirt/controller-lifecycle-operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
207 lines (177 loc) · 6.82 KB
/
util_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
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
/*
Copyright 2020 The CDI Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sdk
import (
"reflect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const lastsAppliedConfigurationAnnotation = "lastAppliedConfiguration"
var _ = Describe("MergeLabelsAndAnnotations", func() {
It("Should properly merge labels and annotations, if no dest labels/anns", func() {
source := createPod("source", map[string]string{"l1": "test"}, map[string]string{"a1": "ann"})
dest := createPod("dest", nil, nil)
MergeLabelsAndAnnotations(&source.ObjectMeta, &dest.ObjectMeta)
Expect(dest.GetObjectMeta).ToNot(BeNil())
Expect(dest.GetLabels()["l1"]).To(Equal("test"))
Expect(dest.GetAnnotations()["a1"]).To(Equal("ann"))
})
It("Should properly merge labels and annotations, if no dest labels", func() {
source := createPod("source", map[string]string{"l1": "test"}, map[string]string{"a1": "ann"})
dest := createPod("dest", nil, map[string]string{"a1": "ann2"})
MergeLabelsAndAnnotations(&source.ObjectMeta, &dest.ObjectMeta)
Expect(dest.GetObjectMeta).ToNot(BeNil())
Expect(dest.GetLabels()["l1"]).To(Equal("test"))
// Check that dest is now equal to source
Expect(dest.GetAnnotations()["a1"]).To(Equal("ann"))
})
It("Should properly merge labels and annotations, if no dest labels, and different ann", func() {
source := createPod("source", map[string]string{"l1": "test"}, map[string]string{"a1": "ann"})
dest := createPod("dest", nil, map[string]string{"a2": "ann2"})
MergeLabelsAndAnnotations(&source.ObjectMeta, &dest.ObjectMeta)
Expect(dest.GetObjectMeta).ToNot(BeNil())
Expect(dest.GetLabels()["l1"]).To(Equal("test"))
Expect(dest.GetAnnotations()["a1"]).To(Equal("ann"))
Expect(dest.GetAnnotations()["a2"]).To(Equal("ann2"))
})
It("Should properly merge labels and annotations, if no dest ann", func() {
source := createPod("source", map[string]string{"l1": "test"}, map[string]string{"a1": "ann"})
dest := createPod("dest", map[string]string{"l1": "test2"}, nil)
MergeLabelsAndAnnotations(&source.ObjectMeta, &dest.ObjectMeta)
Expect(dest.GetObjectMeta).ToNot(BeNil())
// Check that dest is now equal to source
Expect(dest.GetLabels()["l1"]).To(Equal("test"))
Expect(dest.GetAnnotations()["a1"]).To(Equal("ann"))
})
It("Should properly merge labels and annotations, if no dest ann, and different label", func() {
source := createPod("source", map[string]string{"l1": "test"}, map[string]string{"a1": "ann"})
dest := createPod("dest", map[string]string{"l2": "test2"}, nil)
MergeLabelsAndAnnotations(&source.ObjectMeta, &dest.ObjectMeta)
Expect(dest.GetObjectMeta).ToNot(BeNil())
Expect(dest.GetLabels()["l1"]).To(Equal("test"))
Expect(dest.GetLabels()["l2"]).To(Equal("test2"))
Expect(dest.GetAnnotations()["a1"]).To(Equal("ann"))
})
// TODO: fix the problem with preserving unknown fields
PIt("will not merge CRD correctly", func() {
obj1 := &extv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "obj",
},
Spec: extv1.CustomResourceDefinitionSpec{
Group: "foo",
},
}
err := SetLastAppliedConfiguration(obj1, lastsAppliedConfigurationAnnotation)
Expect(err).ToNot(HaveOccurred())
obj2 := obj1.DeepCopy()
obj2.Spec.PreserveUnknownFields = true
obj3 := obj1.DeepCopy()
// not necessary but let's be explicit
obj3.Spec.PreserveUnknownFields = false
err = SetLastAppliedConfiguration(obj3, lastsAppliedConfigurationAnnotation)
Expect(err).ToNot(HaveOccurred())
obj4, err := MergeObject(obj3, obj2, lastsAppliedConfigurationAnnotation)
Expect(err).ToNot(HaveOccurred())
crd := obj4.(*extv1.CustomResourceDefinition)
Expect(crd.Spec.PreserveUnknownFields).To(BeFalse())
})
It("will merge CRD correctly", func() {
obj1 := &extv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "obj",
},
Spec: extv1.CustomResourceDefinitionSpec{
Group: "foo",
PreserveUnknownFields: true,
},
}
err := SetLastAppliedConfiguration(obj1, lastsAppliedConfigurationAnnotation)
Expect(err).ToNot(HaveOccurred())
obj2 := obj1.DeepCopy()
obj3 := obj1.DeepCopy()
// not necessary but let's be explicit
obj3.Spec.PreserveUnknownFields = false
err = SetLastAppliedConfiguration(obj3, lastsAppliedConfigurationAnnotation)
Expect(err).ToNot(HaveOccurred())
obj4, err := MergeObject(obj3, obj2, lastsAppliedConfigurationAnnotation)
Expect(err).ToNot(HaveOccurred())
crd := obj4.(*extv1.CustomResourceDefinition)
Expect(crd.Spec.PreserveUnknownFields).To(BeFalse())
})
})
var _ = Describe("StripStatusFromObject", func() {
It("Should not alter object without status", func() {
in := &corev1.PodList{}
out, err := StripStatusFromObject(in.DeepCopyObject())
Expect(err).ToNot(HaveOccurred())
Expect(reflect.DeepEqual(out, in)).To(BeTrue())
})
DescribeTable("Should strip object status", func(in, expected controllerutil.Object) {
out, err := StripStatusFromObject(in)
Expect(err).ToNot(HaveOccurred())
Expect(reflect.DeepEqual(out, in)).To(BeFalse())
Expect(reflect.DeepEqual(out, expected)).To(BeTrue())
},
Entry("status@Deployment",
&appsv1.Deployment{
Status: appsv1.DeploymentStatus{
Replicas: 128,
},
},
&appsv1.Deployment{Status: appsv1.DeploymentStatus{}},
),
Entry("Status@Pod",
&corev1.Pod{
Status: corev1.PodStatus{
PodIP: "pod-ip",
},
},
&corev1.Pod{Status: corev1.PodStatus{}},
),
)
It("Should strip object status", func() {
in := &appsv1.Deployment{
Status: appsv1.DeploymentStatus{
Replicas: 128,
},
}
expected := &appsv1.Deployment{
Status: appsv1.DeploymentStatus{},
}
out, err := StripStatusFromObject(in.DeepCopyObject())
Expect(err).ToNot(HaveOccurred())
Expect(reflect.DeepEqual(out, in)).To(BeFalse())
Expect(reflect.DeepEqual(out, expected)).To(BeTrue())
})
})
func createPod(name string, labels, annotations map[string]string) *corev1.Pod {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
if len(labels) > 0 {
pod.ObjectMeta.Labels = labels
}
if len(annotations) > 0 {
pod.ObjectMeta.Annotations = annotations
}
return pod
}