From ed06e7e29d35a1e4694bc52745a927ba1c476816 Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Fri, 9 Feb 2024 14:00:57 -0500 Subject: [PATCH] :bug: Fakeclient: Do not consider an apply patch to be a strategic merge patch The fakeclient currently considers an apply patch to be a strategic merge patch. This is completely wrong, those are different things and apply patches are not supported in the fakeclientl, because in order to support them it would need the entire SSA logic which isn't implemented in upstream yet. --- pkg/client/fake/client.go | 4 ++-- pkg/client/fake/client_test.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pkg/client/fake/client.go b/pkg/client/fake/client.go index 790a1faab1..ec9f456185 100644 --- a/pkg/client/fake/client.go +++ b/pkg/client/fake/client.go @@ -947,7 +947,7 @@ func dryPatch(action testing.PatchActionImpl, tracker testing.ObjectTracker) (ru if err := json.Unmarshal(modified, obj); err != nil { return nil, err } - case types.StrategicMergePatchType, types.ApplyPatchType: + case types.StrategicMergePatchType: mergedByte, err := strategicpatch.StrategicMergePatch(old, action.GetPatch(), obj) if err != nil { return nil, err @@ -956,7 +956,7 @@ func dryPatch(action testing.PatchActionImpl, tracker testing.ObjectTracker) (ru return nil, err } default: - return nil, fmt.Errorf("PatchType is not supported") + return nil, fmt.Errorf("%s PatchType is not supported", action.GetPatchType()) } return obj, nil } diff --git a/pkg/client/fake/client_test.go b/pkg/client/fake/client_test.go index d299175ba9..e44989bb24 100644 --- a/pkg/client/fake/client_test.go +++ b/pkg/client/fake/client_test.go @@ -359,6 +359,26 @@ var _ = Describe("Fake client", func() { Expect(list.Items).To(ConsistOf(*dep2)) }) + It("should reject apply patches, they are not supported in the fake client", func() { + By("Creating a new configmap") + cm := &corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1", + Kind: "ConfigMap", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "new-test-cm", + Namespace: "ns2", + }, + } + err := cl.Create(context.Background(), cm) + Expect(err).ToNot(HaveOccurred()) + + cm.Data = map[string]string{"foo": "bar"} + err = cl.Patch(context.Background(), cm, client.Apply, client.ForceOwnership) + Expect(err).To(MatchError("application/apply-patch+yaml PatchType is not supported")) + }) + It("should be able to Create", func() { By("Creating a new configmap") newcm := &corev1.ConfigMap{