Skip to content

Commit 7120abe

Browse files
author
Antoine Pelisse
committed
fieldmanager: Add failing test for no-op apply actually writing to etcd
1 parent 39681aa commit 7120abe

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/handlers/fieldmanager/fieldmanager_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/ioutil"
2323
"net/http"
2424
"path/filepath"
25+
"reflect"
2526
"strings"
2627
"testing"
2728
"time"
@@ -733,3 +734,52 @@ func TestApplySuccessWithNoManagedFields(t *testing.T) {
733734
t.Fatalf("failed to apply object: %v", err)
734735
}
735736
}
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

Comments
 (0)