@@ -386,6 +386,49 @@ func TestServerSideApply(t *testing.T) {
386386 g .Expect (p0 .HasChanges ()).To (BeFalse ())
387387 g .Expect (p0 .HasSpecChanges ()).To (BeFalse ())
388388 })
389+ t .Run ("Error on object which has another uid due to immutability" , func (t * testing.T ) {
390+ g := NewWithT (t )
391+
392+ // Get the current object (assumes tests to be run in sequence).
393+ original := obj .DeepCopy ()
394+ g .Expect (env .GetAPIReader ().Get (ctx , client .ObjectKeyFromObject (original ), original )).To (Succeed ())
395+
396+ // Create a patch helper for a modified object with some changes to what previously applied by th topology manager.
397+ modified := obj .DeepCopy ()
398+ g .Expect (unstructured .SetNestedField (modified .Object , "changed" , "spec" , "controlPlaneEndpoint" , "host" )).To (Succeed ())
399+ g .Expect (unstructured .SetNestedField (modified .Object , "changed-by-topology-controller" , "spec" , "bar" )).To (Succeed ())
400+
401+ // Set an other uid to original
402+ original .SetUID ("a-wrong-one" )
403+ modified .SetUID ("" )
404+
405+ // Create a patch helper which should fail because original's real UID changed.
406+ p0 , err := NewServerSidePatchHelper (original , modified , env .GetClient ())
407+ g .Expect (err ).ToNot (HaveOccurred ())
408+ g .Expect (p0 .HasChanges ()).To (BeTrue ())
409+ g .Expect (p0 .HasSpecChanges ()).To (BeTrue ())
410+ g .Expect (p0 .Patch (ctx )).ToNot (Succeed ())
411+ })
412+ t .Run ("Error on object which does not exist (anymore) but was expected to get updated" , func (t * testing.T ) {
413+ original := builder .TestInfrastructureCluster (ns .Name , "obj3" ).WithSpecFields (map [string ]interface {}{
414+ "spec.controlPlaneEndpoint.host" : "1.2.3.4" ,
415+ "spec.controlPlaneEndpoint.port" : int64 (1234 ),
416+ "spec.foo" : "" , // this field is then explicitly ignored by the patch helper
417+ }).Build ()
418+
419+ modified := original .DeepCopy ()
420+ g .Expect (unstructured .SetNestedField (modified .Object , "changed" , "spec" , "controlPlaneEndpoint" , "host" )).To (Succeed ())
421+
422+ // Set a not existing uid to the not existing original object
423+ original .SetUID ("does-not-exist" )
424+
425+ // Create a patch helper which should fail because original does not exist.
426+ p0 , err := NewServerSidePatchHelper (original , modified , env .GetClient ())
427+ g .Expect (err ).ToNot (HaveOccurred ())
428+ g .Expect (p0 .HasChanges ()).To (BeTrue ())
429+ g .Expect (p0 .HasSpecChanges ()).To (BeTrue ())
430+ g .Expect (p0 .Patch (ctx )).ToNot (Succeed ())
431+ })
389432}
390433
391434func TestServerSideApply_CleanupLegacyManagedFields (t * testing.T ) {
0 commit comments