@@ -25,6 +25,7 @@ import (
25
25
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26
26
27
27
appsv1 "k8s.io/api/apps/v1"
28
+ coordinationv1 "k8s.io/api/coordination/v1"
28
29
corev1 "k8s.io/api/core/v1"
29
30
"k8s.io/apimachinery/pkg/api/errors"
30
31
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -284,6 +285,118 @@ var _ = Describe("Fake client", func() {
284
285
Expect (obj .ObjectMeta .ResourceVersion ).To (Equal ("1" ))
285
286
})
286
287
288
+ It ("should allow updates with non-set ResourceVersion for a resource that allows unconditional updates" , func () {
289
+ By ("Updating a new configmap" )
290
+ newcm := & corev1.ConfigMap {
291
+ TypeMeta : metav1.TypeMeta {
292
+ APIVersion : "v1" ,
293
+ Kind : "ConfigMap" ,
294
+ },
295
+ ObjectMeta : metav1.ObjectMeta {
296
+ Name : "test-cm" ,
297
+ Namespace : "ns2" ,
298
+ },
299
+ Data : map [string ]string {
300
+ "test-key" : "new-value" ,
301
+ },
302
+ }
303
+ err := cl .Update (context .Background (), newcm )
304
+ Expect (err ).To (BeNil ())
305
+
306
+ By ("Getting the configmap" )
307
+ namespacedName := types.NamespacedName {
308
+ Name : "test-cm" ,
309
+ Namespace : "ns2" ,
310
+ }
311
+ obj := & corev1.ConfigMap {}
312
+ err = cl .Get (context .Background (), namespacedName , obj )
313
+ Expect (err ).To (BeNil ())
314
+ Expect (obj ).To (Equal (newcm ))
315
+ Expect (obj .ObjectMeta .ResourceVersion ).To (Equal ("1" ))
316
+ })
317
+
318
+ It ("should reject updates with non-set ResourceVersion for a resource that doesn't allow unconditional updates" , func () {
319
+ By ("Creating a new binding" )
320
+ binding := & corev1.Binding {
321
+ TypeMeta : metav1.TypeMeta {
322
+ APIVersion : "v1" ,
323
+ Kind : "Binding" ,
324
+ },
325
+ ObjectMeta : metav1.ObjectMeta {
326
+ Name : "test-binding" ,
327
+ Namespace : "ns2" ,
328
+ },
329
+ Target : corev1.ObjectReference {
330
+ Kind : "ConfigMap" ,
331
+ APIVersion : "v1" ,
332
+ Namespace : cm .Namespace ,
333
+ Name : cm .Name ,
334
+ },
335
+ }
336
+ Expect (cl .Create (context .Background (), binding )).To (Succeed ())
337
+
338
+ By ("Updating the binding with a new resource lacking resource version" )
339
+ newBinding := & corev1.Binding {
340
+ TypeMeta : metav1.TypeMeta {
341
+ APIVersion : "v1" ,
342
+ Kind : "Binding" ,
343
+ },
344
+ ObjectMeta : metav1.ObjectMeta {
345
+ Name : binding .Name ,
346
+ Namespace : binding .Namespace ,
347
+ },
348
+ Target : corev1.ObjectReference {
349
+ Namespace : binding .Namespace ,
350
+ Name : "blue" ,
351
+ },
352
+ }
353
+ Expect (cl .Update (context .Background (), newBinding )).NotTo (Succeed ())
354
+ })
355
+
356
+ It ("should allow create on update for a resource that allows create on update" , func () {
357
+ By ("Creating a new lease with update" )
358
+ lease := & coordinationv1.Lease {
359
+ TypeMeta : metav1.TypeMeta {
360
+ APIVersion : "coordination.k8s.io/v1" ,
361
+ Kind : "Lease" ,
362
+ },
363
+ ObjectMeta : metav1.ObjectMeta {
364
+ Name : "test-lease" ,
365
+ Namespace : "ns2" ,
366
+ },
367
+ Spec : coordinationv1.LeaseSpec {},
368
+ }
369
+ Expect (cl .Create (context .Background (), lease )).To (Succeed ())
370
+
371
+ By ("Getting the lease" )
372
+ namespacedName := types.NamespacedName {
373
+ Name : lease .Name ,
374
+ Namespace : lease .Namespace ,
375
+ }
376
+ obj := & coordinationv1.Lease {}
377
+ Expect (cl .Get (context .Background (), namespacedName , obj )).To (Succeed ())
378
+ Expect (obj ).To (Equal (lease ))
379
+ Expect (obj .ObjectMeta .ResourceVersion ).To (Equal ("1" ))
380
+ })
381
+
382
+ It ("should reject create on update for a resource that does not allow create on update" , func () {
383
+ By ("Attemping to create a new configmap with update" )
384
+ newcm := & corev1.ConfigMap {
385
+ TypeMeta : metav1.TypeMeta {
386
+ APIVersion : "v1" ,
387
+ Kind : "ConfigMap" ,
388
+ },
389
+ ObjectMeta : metav1.ObjectMeta {
390
+ Name : "different-test-cm" ,
391
+ Namespace : "ns2" ,
392
+ },
393
+ Data : map [string ]string {
394
+ "test-key" : "new-value" ,
395
+ },
396
+ }
397
+ Expect (cl .Update (context .Background (), newcm )).NotTo (Succeed ())
398
+ })
399
+
287
400
It ("should reject updates with non-matching ResourceVersion" , func () {
288
401
By ("Updating a new configmap" )
289
402
newcm := & corev1.ConfigMap {
@@ -427,6 +540,7 @@ var _ = Describe("Fake client", func() {
427
540
scheme := runtime .NewScheme ()
428
541
Expect (corev1 .AddToScheme (scheme )).To (Succeed ())
429
542
Expect (appsv1 .AddToScheme (scheme )).To (Succeed ())
543
+ Expect (coordinationv1 .AddToScheme (scheme )).To (Succeed ())
430
544
cl = NewFakeClientWithScheme (scheme , dep , dep2 , cm )
431
545
close (done )
432
546
})
0 commit comments