From c724cb717815ceb9007090b6c4184f99617ab809 Mon Sep 17 00:00:00 2001 From: Karen Bradshaw Date: Tue, 14 May 2019 10:54:28 -0400 Subject: [PATCH 1/3] add test for empty patch file --- pkg/patch/transformer/patchjson6902json.go | 7 +++- .../transformer/patchjson6902json_test.go | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/pkg/patch/transformer/patchjson6902json.go b/pkg/patch/transformer/patchjson6902json.go index d4d6c0abb..9e65a290b 100644 --- a/pkg/patch/transformer/patchjson6902json.go +++ b/pkg/patch/transformer/patchjson6902json.go @@ -19,7 +19,7 @@ package transformer import ( "fmt" - "github.com/evanphx/json-patch" + jsonpatch "github.com/evanphx/json-patch" "github.com/pkg/errors" "sigs.k8s.io/kustomize/pkg/resid" "sigs.k8s.io/kustomize/pkg/resmap" @@ -42,6 +42,11 @@ func newPatchJson6902JSONTransformer( id resid.ResId, rawOp []byte) (transformers.Transformer, error) { op := rawOp var err error + + if len(op) == 0 { + return nil, fmt.Errorf("json patch file is empty %v", id) + } + if !isJsonFormat(op) { // if it isn't JSON, try to parse it as YAML op, err = yaml.YAMLToJSON(rawOp) diff --git a/pkg/patch/transformer/patchjson6902json_test.go b/pkg/patch/transformer/patchjson6902json_test.go index 8e5c751cc..7dcefb12c 100644 --- a/pkg/patch/transformer/patchjson6902json_test.go +++ b/pkg/patch/transformer/patchjson6902json_test.go @@ -163,3 +163,40 @@ func TestJsonPatchJSONTransformer_UnHappyTransform(t *testing.T) { t.Fatalf("expected error didn't happen, but got %v", err) } } + +func TestJsonPatchJSONTransformer_EmptyPatchFile(t *testing.T) { + rf := resource.NewFactory( + kunstruct.NewKunstructuredFactoryImpl()) + id := resid.NewResId(deploy, "deploy1") + base := resmap.ResMap{ + id: rf.FromMap( + map[string]interface{}{ + "apiVersion": "apps/v1", + "kind": "Deployment", + "metadata": map[string]interface{}{ + "name": "deploy1", + }, + "spec": map[string]interface{}{ + "template": map[string]interface{}{}, + }, + }), + } + + operations := []byte(``) + + jpt, err := newPatchJson6902JSONTransformer(id, operations) + + if err == nil { + t.Fatalf("unexpected error : %v", err) + + if jpt != nil { + err = jpt.Transform(base) + } + } + + if err != nil { + if !strings.HasPrefix(err.Error(), "json patch file is empty") { + t.Fatalf("expected error didn't happen, but got %v", err) + } + } +} From 2aa7e30aff82be0a1b81d464db003b5b828d67b5 Mon Sep 17 00:00:00 2001 From: Karen Bradshaw Date: Tue, 14 May 2019 12:35:30 -0400 Subject: [PATCH 2/3] minimize test --- .../transformer/patchjson6902json_test.go | 24 ++----------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/pkg/patch/transformer/patchjson6902json_test.go b/pkg/patch/transformer/patchjson6902json_test.go index 7dcefb12c..2c072d854 100644 --- a/pkg/patch/transformer/patchjson6902json_test.go +++ b/pkg/patch/transformer/patchjson6902json_test.go @@ -165,38 +165,18 @@ func TestJsonPatchJSONTransformer_UnHappyTransform(t *testing.T) { } func TestJsonPatchJSONTransformer_EmptyPatchFile(t *testing.T) { - rf := resource.NewFactory( - kunstruct.NewKunstructuredFactoryImpl()) id := resid.NewResId(deploy, "deploy1") - base := resmap.ResMap{ - id: rf.FromMap( - map[string]interface{}{ - "apiVersion": "apps/v1", - "kind": "Deployment", - "metadata": map[string]interface{}{ - "name": "deploy1", - }, - "spec": map[string]interface{}{ - "template": map[string]interface{}{}, - }, - }), - } - operations := []byte(``) - jpt, err := newPatchJson6902JSONTransformer(id, operations) + _, err := newPatchJson6902JSONTransformer(id, operations) if err == nil { t.Fatalf("unexpected error : %v", err) - - if jpt != nil { - err = jpt.Transform(base) - } } if err != nil { if !strings.HasPrefix(err.Error(), "json patch file is empty") { - t.Fatalf("expected error didn't happen, but got %v", err) + t.Fatalf("expected %s, but got %v", "json patch file is empty", err) } } } From c836de5ca8a342a6c7dd773eb8ea891eb017c1d7 Mon Sep 17 00:00:00 2001 From: Karen Bradshaw Date: Tue, 14 May 2019 13:03:24 -0400 Subject: [PATCH 3/3] update error msg --- pkg/patch/transformer/patchjson6902json_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/patch/transformer/patchjson6902json_test.go b/pkg/patch/transformer/patchjson6902json_test.go index 2c072d854..fbad24988 100644 --- a/pkg/patch/transformer/patchjson6902json_test.go +++ b/pkg/patch/transformer/patchjson6902json_test.go @@ -171,7 +171,7 @@ func TestJsonPatchJSONTransformer_EmptyPatchFile(t *testing.T) { _, err := newPatchJson6902JSONTransformer(id, operations) if err == nil { - t.Fatalf("unexpected error : %v", err) + t.Fatalf("expected an error") } if err != nil {