@@ -22,6 +22,7 @@ import (
22
22
"io/ioutil"
23
23
"net/http"
24
24
"path/filepath"
25
+ "reflect"
25
26
"strings"
26
27
"testing"
27
28
"time"
@@ -733,3 +734,52 @@ func TestApplySuccessWithNoManagedFields(t *testing.T) {
733
734
t .Fatalf ("failed to apply object: %v" , err )
734
735
}
735
736
}
737
+
738
+ // Run an update and apply, and make sure that nothing has changed.
739
+ func TestNoOpChanges (t * testing.T ) {
740
+ f := NewTestFieldManager (schema .FromAPIVersionAndKind ("v1" , "Pod" ))
741
+
742
+ obj := & unstructured.Unstructured {Object : map [string ]interface {}{}}
743
+ if err := yaml .Unmarshal ([]byte (`{
744
+ "apiVersion": "v1",
745
+ "kind": "Pod",
746
+ "metadata": {
747
+ "labels": {
748
+ "a": "b"
749
+ },
750
+ }
751
+ }` ), & obj .Object ); err != nil {
752
+ t .Fatalf ("error decoding YAML: %v" , err )
753
+ }
754
+ if err := f .Apply (obj , "fieldmanager_test_apply" , false ); err != nil {
755
+ t .Fatalf ("failed to apply object: %v" , err )
756
+ }
757
+ before := f .liveObj .DeepCopyObject ()
758
+ // Wait to make sure the timestamp is different
759
+ time .Sleep (time .Second )
760
+ // Applying with a different fieldmanager will create an entry..
761
+ if err := f .Apply (obj , "fieldmanager_test_apply_other" , false ); err != nil {
762
+ t .Fatalf ("failed to update object: %v" , err )
763
+ }
764
+ if reflect .DeepEqual (before , f .liveObj ) {
765
+ t .Fatalf ("Applying no-op apply with new manager didn't change object: \n %v" , f .liveObj )
766
+ }
767
+ before = f .liveObj .DeepCopyObject ()
768
+ // Wait to make sure the timestamp is different
769
+ time .Sleep (time .Second )
770
+ if err := f .Update (obj , "fieldmanager_test_update" ); err != nil {
771
+ t .Fatalf ("failed to update object: %v" , err )
772
+ }
773
+ if ! reflect .DeepEqual (before , f .liveObj ) {
774
+ t .Fatalf ("No-op update has changed the object:\n %v\n ---\n %v" , before , f .liveObj )
775
+ }
776
+ before = f .liveObj .DeepCopyObject ()
777
+ // Wait to make sure the timestamp is different
778
+ time .Sleep (time .Second )
779
+ if err := f .Apply (obj , "fieldmanager_test_apply" , true ); err != nil {
780
+ t .Fatalf ("failed to re-apply object: %v" , err )
781
+ }
782
+ if ! reflect .DeepEqual (before , f .liveObj ) {
783
+ t .Fatalf ("No-op apply has changed the object:\n %v\n ---\n %v" , before , f .liveObj )
784
+ }
785
+ }
0 commit comments