Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow patches with empty files with multiple newlines or comments #5771

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/internal/builtins/PatchTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions plugin/builtin/patchtransformer/PatchTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ func (p *plugin) Config(h *resmap.PluginHelpers, c []byte) error {
patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText))
patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText))

if (errSM == nil && errJson == nil) ||
(patchesSM != nil && patchesJson != nil) {
if ((errSM == nil && errJson == nil) ||
(patchesSM != nil && patchesJson != nil)) &&
(len(patchesSM) > 0 && len(patchesJson) > 0) {
return fmt.Errorf(
"illegally qualifies as both an SM and JSON patch: %s",
p.patchSource)
Expand Down
29 changes: 29 additions & 0 deletions plugin/builtin/patchtransformer/PatchTransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,32 @@ spec:
name: test-deployment
`)
}

func TestPatchTransformerPatchEmpty(t *testing.T) {
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PatchTransformer")
defer th.Reset()

th.RunTransformerAndCheckError(`
`, someDeploymentResources, func(t *testing.T, err error) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stormqueen1990 This is an example of what I mean.

Here I'm testing the input to be a file with only a new line. Based on my new code, this should give no error, but I'm getting a specific error from the tests extra code saying that the input to RunTransformerAndCheckError can't be empty.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're missing a go generate step for this code. The plugin/builtin/patchtransformer/PatchTransformer.go file you updated is a scaffold for the plugin generated under api/internal/builtins/PatchTransformer.go; therefore, you will need to run go generate plugin/builtin/patchtransformer/PatchTransformer.go to update the final built-in plugin code.

You can also run the prow checks locally using make prow-presubmit-check from the root of the repository.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much @stormqueen1990

I've just generated the plugin and pushed the change.

Regarding the test, I run them locally, but I'm given an error that IMO shouldn't happen, as it's complaining of exactly what I want to test:

2024/11/19 15:09:31 compiler created: /Users/juliochana/github/kustomize/plugin/builtin/patchtransformer/PatchTransformer.so
    harnessenhanced.go:219: config: '
        '
    harnessenhanced.go:220: Err: expected 1 resource, found 0 in [10]
--- FAIL: TestPatchTransformerPatchEmpty (0.71s)
=== RUN   TestPatchTransformerPatchEmptyOnlyComments
2024/11/19 15:09:32 compiler created: /Users/juliochana/github/kustomize/plugin/builtin/patchtransformer/PatchTransformer.so
    harnessenhanced.go:219: config: '
        # File with only comments

        # Is a virtually empty yaml
        '
    harnessenhanced.go:220: Err: expected 1 resource, found 0 in [10 35 32 70 105 108 101 32 119 105 116 104 32 111 110 108 121 32 99 111 109 109 101 110 116 115 10 10 35 32 73 115 32 97 32 118 105 114 116 117 97 108 108 121 32 101 109 112 116 121 32 121 97 109 108 10]
--- FAIL: TestPatchTransformerPatchEmptyOnlyComments (0.68s)
FAIL
FAIL	command-line-arguments	17.627s
FAIL
**** FAILURE in ./plugin/builtin/patchtransformer

I'm adding an "empty file" (for kustomize) and with my change, it should work. Could you help me improving this testing scaffolding so it stops complaining about the empty file?

if err != nil {
t.Fatalf("unexpected error")
}
})
}

func TestPatchTransformerPatchEmptyOnlyComments(t *testing.T) {
th := kusttest_test.MakeEnhancedHarness(t).
PrepBuiltin("PatchTransformer")
defer th.Reset()

th.RunTransformerAndCheckError(`
# File with only comments

# Is a virtually empty yaml
`, someDeploymentResources, func(t *testing.T, err error) {
if err != nil {
t.Fatalf("unexpected error")
}
})
}