1
16
17 package client_test
18
19 import (
20 . "github.com/onsi/ginkgo/v2"
21 . "github.com/onsi/gomega"
22
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/apimachinery/pkg/fields"
25 "k8s.io/apimachinery/pkg/labels"
26 "k8s.io/utils/ptr"
27 "sigs.k8s.io/controller-runtime/pkg/client"
28 )
29
30 var _ = Describe("ListOptions", func() {
31 It("Should set LabelSelector", func() {
32 labelSelector, err := labels.Parse("a=b")
33 Expect(err).NotTo(HaveOccurred())
34 o := &client.ListOptions{LabelSelector: labelSelector}
35 newListOpts := &client.ListOptions{}
36 o.ApplyToList(newListOpts)
37 Expect(newListOpts).To(Equal(o))
38 })
39 It("Should set FieldSelector", func() {
40 o := &client.ListOptions{FieldSelector: fields.Nothing()}
41 newListOpts := &client.ListOptions{}
42 o.ApplyToList(newListOpts)
43 Expect(newListOpts).To(Equal(o))
44 })
45 It("Should set Namespace", func() {
46 o := &client.ListOptions{Namespace: "my-ns"}
47 newListOpts := &client.ListOptions{}
48 o.ApplyToList(newListOpts)
49 Expect(newListOpts).To(Equal(o))
50 })
51 It("Should set Raw", func() {
52 o := &client.ListOptions{Raw: &metav1.ListOptions{FieldSelector: "Hans"}}
53 newListOpts := &client.ListOptions{}
54 o.ApplyToList(newListOpts)
55 Expect(newListOpts).To(Equal(o))
56 })
57 It("Should set Limit", func() {
58 o := &client.ListOptions{Limit: int64(1)}
59 newListOpts := &client.ListOptions{}
60 o.ApplyToList(newListOpts)
61 Expect(newListOpts).To(Equal(o))
62 })
63 It("Should set Continue", func() {
64 o := &client.ListOptions{Continue: "foo"}
65 newListOpts := &client.ListOptions{}
66 o.ApplyToList(newListOpts)
67 Expect(newListOpts).To(Equal(o))
68 })
69 It("Should not set anything", func() {
70 o := &client.ListOptions{}
71 newListOpts := &client.ListOptions{}
72 o.ApplyToList(newListOpts)
73 Expect(newListOpts).To(Equal(o))
74 })
75 })
76
77 var _ = Describe("GetOptions", func() {
78 It("Should set Raw", func() {
79 o := &client.GetOptions{Raw: &metav1.GetOptions{ResourceVersion: "RV0"}}
80 newGetOpts := &client.GetOptions{}
81 o.ApplyToGet(newGetOpts)
82 Expect(newGetOpts).To(Equal(o))
83 })
84 })
85
86 var _ = Describe("CreateOptions", func() {
87 It("Should set DryRun", func() {
88 o := &client.CreateOptions{DryRun: []string{"Hello", "Theodore"}}
89 newCreateOpts := &client.CreateOptions{}
90 o.ApplyToCreate(newCreateOpts)
91 Expect(newCreateOpts).To(Equal(o))
92 })
93 It("Should set FieldManager", func() {
94 o := &client.CreateOptions{FieldManager: "FieldManager"}
95 newCreateOpts := &client.CreateOptions{}
96 o.ApplyToCreate(newCreateOpts)
97 Expect(newCreateOpts).To(Equal(o))
98 })
99 It("Should set Raw", func() {
100 o := &client.CreateOptions{Raw: &metav1.CreateOptions{DryRun: []string{"Bye", "Theodore"}}}
101 newCreateOpts := &client.CreateOptions{}
102 o.ApplyToCreate(newCreateOpts)
103 Expect(newCreateOpts).To(Equal(o))
104 })
105 It("Should not set anything", func() {
106 o := &client.CreateOptions{}
107 newCreateOpts := &client.CreateOptions{}
108 o.ApplyToCreate(newCreateOpts)
109 Expect(newCreateOpts).To(Equal(o))
110 })
111 })
112
113 var _ = Describe("DeleteOptions", func() {
114 It("Should set GracePeriodSeconds", func() {
115 o := &client.DeleteOptions{GracePeriodSeconds: ptr.To(int64(42))}
116 newDeleteOpts := &client.DeleteOptions{}
117 o.ApplyToDelete(newDeleteOpts)
118 Expect(newDeleteOpts).To(Equal(o))
119 })
120 It("Should set Preconditions", func() {
121 o := &client.DeleteOptions{Preconditions: &metav1.Preconditions{}}
122 newDeleteOpts := &client.DeleteOptions{}
123 o.ApplyToDelete(newDeleteOpts)
124 Expect(newDeleteOpts).To(Equal(o))
125 })
126 It("Should set PropagationPolicy", func() {
127 policy := metav1.DeletePropagationBackground
128 o := &client.DeleteOptions{PropagationPolicy: &policy}
129 newDeleteOpts := &client.DeleteOptions{}
130 o.ApplyToDelete(newDeleteOpts)
131 Expect(newDeleteOpts).To(Equal(o))
132 })
133 It("Should set Raw", func() {
134 o := &client.DeleteOptions{Raw: &metav1.DeleteOptions{}}
135 newDeleteOpts := &client.DeleteOptions{}
136 o.ApplyToDelete(newDeleteOpts)
137 Expect(newDeleteOpts).To(Equal(o))
138 })
139 It("Should set DryRun", func() {
140 o := &client.DeleteOptions{DryRun: []string{"Hello", "Pippa"}}
141 newDeleteOpts := &client.DeleteOptions{}
142 o.ApplyToDelete(newDeleteOpts)
143 Expect(newDeleteOpts).To(Equal(o))
144 })
145 It("Should not set anything", func() {
146 o := &client.DeleteOptions{}
147 newDeleteOpts := &client.DeleteOptions{}
148 o.ApplyToDelete(newDeleteOpts)
149 Expect(newDeleteOpts).To(Equal(o))
150 })
151 })
152
153 var _ = Describe("UpdateOptions", func() {
154 It("Should set DryRun", func() {
155 o := &client.UpdateOptions{DryRun: []string{"Bye", "Pippa"}}
156 newUpdateOpts := &client.UpdateOptions{}
157 o.ApplyToUpdate(newUpdateOpts)
158 Expect(newUpdateOpts).To(Equal(o))
159 })
160 It("Should set FieldManager", func() {
161 o := &client.UpdateOptions{FieldManager: "Hello Boris"}
162 newUpdateOpts := &client.UpdateOptions{}
163 o.ApplyToUpdate(newUpdateOpts)
164 Expect(newUpdateOpts).To(Equal(o))
165 })
166 It("Should set Raw", func() {
167 o := &client.UpdateOptions{Raw: &metav1.UpdateOptions{}}
168 newUpdateOpts := &client.UpdateOptions{}
169 o.ApplyToUpdate(newUpdateOpts)
170 Expect(newUpdateOpts).To(Equal(o))
171 })
172 It("Should not set anything", func() {
173 o := &client.UpdateOptions{}
174 newUpdateOpts := &client.UpdateOptions{}
175 o.ApplyToUpdate(newUpdateOpts)
176 Expect(newUpdateOpts).To(Equal(o))
177 })
178 })
179
180 var _ = Describe("PatchOptions", func() {
181 It("Should set DryRun", func() {
182 o := &client.PatchOptions{DryRun: []string{"Bye", "Boris"}}
183 newPatchOpts := &client.PatchOptions{}
184 o.ApplyToPatch(newPatchOpts)
185 Expect(newPatchOpts).To(Equal(o))
186 })
187 It("Should set Force", func() {
188 o := &client.PatchOptions{Force: ptr.To(true)}
189 newPatchOpts := &client.PatchOptions{}
190 o.ApplyToPatch(newPatchOpts)
191 Expect(newPatchOpts).To(Equal(o))
192 })
193 It("Should set FieldManager", func() {
194 o := &client.PatchOptions{FieldManager: "Hello Julian"}
195 newPatchOpts := &client.PatchOptions{}
196 o.ApplyToPatch(newPatchOpts)
197 Expect(newPatchOpts).To(Equal(o))
198 })
199 It("Should set Raw", func() {
200 o := &client.PatchOptions{Raw: &metav1.PatchOptions{}}
201 newPatchOpts := &client.PatchOptions{}
202 o.ApplyToPatch(newPatchOpts)
203 Expect(newPatchOpts).To(Equal(o))
204 })
205 It("Should not set anything", func() {
206 o := &client.PatchOptions{}
207 newPatchOpts := &client.PatchOptions{}
208 o.ApplyToPatch(newPatchOpts)
209 Expect(newPatchOpts).To(Equal(o))
210 })
211 })
212
213 var _ = Describe("DeleteAllOfOptions", func() {
214 It("Should set ListOptions", func() {
215 o := &client.DeleteAllOfOptions{ListOptions: client.ListOptions{Raw: &metav1.ListOptions{}}}
216 newDeleteAllOfOpts := &client.DeleteAllOfOptions{}
217 o.ApplyToDeleteAllOf(newDeleteAllOfOpts)
218 Expect(newDeleteAllOfOpts).To(Equal(o))
219 })
220 It("Should set DeleleteOptions", func() {
221 o := &client.DeleteAllOfOptions{DeleteOptions: client.DeleteOptions{GracePeriodSeconds: ptr.To(int64(44))}}
222 newDeleteAllOfOpts := &client.DeleteAllOfOptions{}
223 o.ApplyToDeleteAllOf(newDeleteAllOfOpts)
224 Expect(newDeleteAllOfOpts).To(Equal(o))
225 })
226 })
227
228 var _ = Describe("MatchingLabels", func() {
229 It("Should produce an invalid selector when given invalid input", func() {
230 matchingLabels := client.MatchingLabels(map[string]string{"k": "axahm2EJ8Phiephe2eixohbee9eGeiyees1thuozi1xoh0GiuH3diewi8iem7Nui"})
231 listOpts := &client.ListOptions{}
232 matchingLabels.ApplyToList(listOpts)
233
234 r, _ := listOpts.LabelSelector.Requirements()
235 _, err := labels.NewRequirement(r[0].Key(), r[0].Operator(), r[0].Values().List())
236 Expect(err).To(HaveOccurred())
237 expectedErrMsg := `values[0][k]: Invalid value: "axahm2EJ8Phiephe2eixohbee9eGeiyees1thuozi1xoh0GiuH3diewi8iem7Nui": must be no more than 63 characters`
238 Expect(err.Error()).To(Equal(expectedErrMsg))
239 })
240
241 It("Should add matchingLabels to existing selector", func() {
242 listOpts := &client.ListOptions{}
243
244 matchingLabels := client.MatchingLabels(map[string]string{"k": "v"})
245 matchingLabels2 := client.MatchingLabels(map[string]string{"k2": "v2"})
246
247 matchingLabels.ApplyToList(listOpts)
248 Expect(listOpts.LabelSelector.String()).To(Equal("k=v"))
249
250 matchingLabels2.ApplyToList(listOpts)
251 Expect(listOpts.LabelSelector.String()).To(Equal("k=v,k2=v2"))
252 })
253 })
254
255 var _ = Describe("FieldOwner", func() {
256 It("Should apply to PatchOptions", func() {
257 o := &client.PatchOptions{FieldManager: "bar"}
258 t := client.FieldOwner("foo")
259 t.ApplyToPatch(o)
260 Expect(o.FieldManager).To(Equal("foo"))
261 })
262 It("Should apply to CreateOptions", func() {
263 o := &client.CreateOptions{FieldManager: "bar"}
264 t := client.FieldOwner("foo")
265 t.ApplyToCreate(o)
266 Expect(o.FieldManager).To(Equal("foo"))
267 })
268 It("Should apply to UpdateOptions", func() {
269 o := &client.UpdateOptions{FieldManager: "bar"}
270 t := client.FieldOwner("foo")
271 t.ApplyToUpdate(o)
272 Expect(o.FieldManager).To(Equal("foo"))
273 })
274 It("Should apply to SubResourcePatchOptions", func() {
275 o := &client.SubResourcePatchOptions{PatchOptions: client.PatchOptions{FieldManager: "bar"}}
276 t := client.FieldOwner("foo")
277 t.ApplyToSubResourcePatch(o)
278 Expect(o.FieldManager).To(Equal("foo"))
279 })
280 It("Should apply to SubResourceCreateOptions", func() {
281 o := &client.SubResourceCreateOptions{CreateOptions: client.CreateOptions{FieldManager: "bar"}}
282 t := client.FieldOwner("foo")
283 t.ApplyToSubResourceCreate(o)
284 Expect(o.FieldManager).To(Equal("foo"))
285 })
286 It("Should apply to SubResourceUpdateOptions", func() {
287 o := &client.SubResourceUpdateOptions{UpdateOptions: client.UpdateOptions{FieldManager: "bar"}}
288 t := client.FieldOwner("foo")
289 t.ApplyToSubResourceUpdate(o)
290 Expect(o.FieldManager).To(Equal("foo"))
291 })
292 })
293
294 var _ = Describe("ForceOwnership", func() {
295 It("Should apply to PatchOptions", func() {
296 o := &client.PatchOptions{}
297 t := client.ForceOwnership
298 t.ApplyToPatch(o)
299 Expect(*o.Force).To(BeTrue())
300 })
301 It("Should apply to SubResourcePatchOptions", func() {
302 o := &client.SubResourcePatchOptions{PatchOptions: client.PatchOptions{}}
303 t := client.ForceOwnership
304 t.ApplyToSubResourcePatch(o)
305 Expect(*o.Force).To(BeTrue())
306 })
307 })
308
309 var _ = Describe("HasLabels", func() {
310 It("Should produce hasLabels in given order", func() {
311 listOpts := &client.ListOptions{}
312
313 hasLabels := client.HasLabels([]string{"labelApe", "labelFox"})
314 hasLabels.ApplyToList(listOpts)
315 Expect(listOpts.LabelSelector.String()).To(Equal("labelApe,labelFox"))
316 })
317
318 It("Should add hasLabels to existing hasLabels selector", func() {
319 listOpts := &client.ListOptions{}
320
321 hasLabel := client.HasLabels([]string{"labelApe"})
322 hasLabel.ApplyToList(listOpts)
323
324 hasOtherLabel := client.HasLabels([]string{"labelFox"})
325 hasOtherLabel.ApplyToList(listOpts)
326 Expect(listOpts.LabelSelector.String()).To(Equal("labelApe,labelFox"))
327 })
328
329 It("Should add hasLabels to existing matchingLabels", func() {
330 listOpts := &client.ListOptions{}
331
332 matchingLabels := client.MatchingLabels(map[string]string{"k": "v"})
333 matchingLabels.ApplyToList(listOpts)
334
335 hasLabel := client.HasLabels([]string{"labelApe"})
336 hasLabel.ApplyToList(listOpts)
337 Expect(listOpts.LabelSelector.String()).To(Equal("k=v,labelApe"))
338 })
339 })
340
View as plain text