@@ -3240,7 +3240,7 @@ var _ = Describe("DelegatingClient", func() {
3240
3240
})
3241
3241
3242
3242
var _ = Describe ("Patch" , func () {
3243
- Describe ("CreateMergePatch " , func () {
3243
+ Describe ("MergeFrom " , func () {
3244
3244
var cm * corev1.ConfigMap
3245
3245
3246
3246
BeforeEach (func () {
@@ -3303,6 +3303,98 @@ var _ = Describe("Patch", func() {
3303
3303
Expect (data ).To (Equal ([]byte (fmt .Sprintf (`{"metadata":{"annotations":{"%s":"%s"},"resourceVersion":"%s"}}` , annotationKey , annotationValue , cm .ResourceVersion ))))
3304
3304
})
3305
3305
})
3306
+
3307
+ Describe ("StrategicMergeFrom" , func () {
3308
+ var dep * appsv1.Deployment
3309
+
3310
+ BeforeEach (func () {
3311
+ dep = & appsv1.Deployment {
3312
+ ObjectMeta : metav1.ObjectMeta {
3313
+ Namespace : metav1 .NamespaceDefault ,
3314
+ Name : "dep" ,
3315
+ ResourceVersion : "10" ,
3316
+ },
3317
+ Spec : appsv1.DeploymentSpec {
3318
+ Template : corev1.PodTemplateSpec {
3319
+ Spec : corev1.PodSpec {Containers : []corev1.Container {{
3320
+ Name : "main" ,
3321
+ Image : "foo:v1" ,
3322
+ }, {
3323
+ Name : "sidecar" ,
3324
+ Image : "bar:v1" ,
3325
+ }}},
3326
+ },
3327
+ },
3328
+ }
3329
+ })
3330
+
3331
+ It ("creates a strategic merge patch with the modifications applied during the mutation" , func () {
3332
+ By ("creating a strategic merge patch" )
3333
+ patch := client .StrategicMergeFrom (dep .DeepCopy ())
3334
+
3335
+ By ("returning a patch with type StrategicMergePatchType" )
3336
+ Expect (patch .Type ()).To (Equal (types .StrategicMergePatchType ))
3337
+
3338
+ By ("updating the main container's image" )
3339
+ for i , c := range dep .Spec .Template .Spec .Containers {
3340
+ if c .Name == "main" {
3341
+ c .Image = "foo:v2"
3342
+ }
3343
+ dep .Spec .Template .Spec .Containers [i ] = c
3344
+ }
3345
+
3346
+ By ("computing the patch data" )
3347
+ data , err := patch .Data (dep )
3348
+
3349
+ By ("returning no error" )
3350
+ Expect (err ).NotTo (HaveOccurred ())
3351
+
3352
+ By ("returning a patch with data only containing the image change" )
3353
+ Expect (data ).To (Equal ([]byte (`{"spec":{"template":{"spec":{"$setElementOrder/containers":[{"name":"main"},` +
3354
+ `{"name":"sidecar"}],"containers":[{"image":"foo:v2","name":"main"}]}}}}` )))
3355
+ })
3356
+
3357
+ It ("creates a strategic merge patch with the modifications applied during the mutation, using optimistic locking" , func () {
3358
+ By ("creating a strategic merge patch" )
3359
+ patch := client .StrategicMergeFromWithOptions (dep .DeepCopy (), client.MergeFromWithOptimisticLock {})
3360
+
3361
+ By ("returning a patch with type StrategicMergePatchType" )
3362
+ Expect (patch .Type ()).To (Equal (types .StrategicMergePatchType ))
3363
+
3364
+ By ("updating the main container's image" )
3365
+ for i , c := range dep .Spec .Template .Spec .Containers {
3366
+ if c .Name == "main" {
3367
+ c .Image = "foo:v2"
3368
+ }
3369
+ dep .Spec .Template .Spec .Containers [i ] = c
3370
+ }
3371
+
3372
+ By ("computing the patch data" )
3373
+ data , err := patch .Data (dep )
3374
+
3375
+ By ("returning no error" )
3376
+ Expect (err ).NotTo (HaveOccurred ())
3377
+
3378
+ By ("returning a patch with data containing the image change and the resourceVersion change" )
3379
+ Expect (data ).To (Equal ([]byte (fmt .Sprintf (`{"metadata":{"resourceVersion":"%s"},` +
3380
+ `"spec":{"template":{"spec":{"$setElementOrder/containers":[{"name":"main"},{"name":"sidecar"}],"containers":[{"image":"foo:v2","name":"main"}]}}}}` ,
3381
+ dep .ResourceVersion ))))
3382
+ })
3383
+
3384
+ It ("fails if the resourceVersion was changed" , func () {
3385
+ By ("creating a strategic merge patch" )
3386
+ patch := client .StrategicMergeFromWithOptions (dep .DeepCopy (), client.MergeFromWithOptimisticLock {})
3387
+
3388
+ By ("updating the resourceVersion" )
3389
+ dep .SetResourceVersion ("42" )
3390
+
3391
+ By ("computing the patch data" )
3392
+ _ , err := patch .Data (dep )
3393
+
3394
+ By ("returning no error" )
3395
+ Expect (err ).To (MatchError (ContainSubstring ("precondition failed" )))
3396
+ })
3397
+ })
3306
3398
})
3307
3399
3308
3400
var _ = Describe ("IgnoreNotFound" , func () {
0 commit comments