diff --git a/dynamic/fake/simple_test.go b/dynamic/fake/simple_test.go index 183420fc..85e22446 100644 --- a/dynamic/fake/simple_test.go +++ b/dynamic/fake/simple_test.go @@ -181,11 +181,11 @@ func TestPatch(t *testing.T) { patchBytes: []byte(`[{"op": "add", "path": "/spec/newvalue", "value": "dummy"}]`), wantErrMsg: "invalid JSON document", }, { - name: "merge patch fails as unsupported", - object: newUnstructured(testAPIVersion, testKind, testNamespace, testName), - patchType: types.MergePatchType, - patchBytes: []byte(`{}`), - wantErrMsg: "PatchType is not supported", + name: "merge patch works with simple replacement", + object: newUnstructuredWithSpec(map[string]interface{}{"foo": "bar"}), + patchType: types.MergePatchType, + patchBytes: []byte(`{ "spec": { "foo": "baz" } }`), + expectedPatchedObject: newUnstructuredWithSpec(map[string]interface{}{"foo": "baz"}), }, // TODO: Add tests for strategic merge using v1.Pod for example to ensure the test cases // demonstrate expected use cases. diff --git a/testing/fixture.go b/testing/fixture.go index 055b5b29..993fcf6a 100644 --- a/testing/fixture.go +++ b/testing/fixture.go @@ -20,7 +20,7 @@ import ( "fmt" "sync" - "github.com/evanphx/json-patch" + jsonpatch "github.com/evanphx/json-patch" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -138,7 +138,7 @@ func ObjectReaction(tracker ObjectTracker) ReactionFunc { if err != nil { return true, nil, err } - // Only supports strategic merge patch and JSONPatch as coded. + switch action.GetPatchType() { case types.JSONPatchType: patch, err := jsonpatch.DecodePatch(action.GetPatch()) @@ -152,6 +152,15 @@ func ObjectReaction(tracker ObjectTracker) ReactionFunc { if err = json.Unmarshal(modified, obj); err != nil { return true, nil, err } + case types.MergePatchType: + modified, err := jsonpatch.MergePatch(old, action.GetPatch()) + if err != nil { + return true, nil, err + } + + if err := json.Unmarshal(modified, obj); err != nil { + return true, nil, err + } case types.StrategicMergePatchType: mergedByte, err := strategicpatch.StrategicMergePatch(old, action.GetPatch(), obj) if err != nil {