Files
kustomize/kustomize/commands/edit/add/addpatch_test.go
Karl Isenberg 43868688d5 Use require for Error and NoError
Assert keeps going after failure, but require immediately fails
the tests, making it easier to find the output related to the test
failure, rather than having to comb through a bunch of subsequent
assertion failures. For equality tests, we may or may not want to
continue, but for error checks we almost always want to immediately
fail the test. Exceptions can be changed as-needed.
2024-03-20 13:19:18 -07:00

114 lines
3.0 KiB
Go

// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package add
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
const (
patchFileName = "myWonderfulPatch.yaml"
patchFileContent = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
`
kind = "myKind"
group = "myGroup"
version = "myVersion"
name = "myName"
namespace = "myNamespace"
annotationSelector = "myAnnotationSelector"
labelSelector = "myLabelSelector"
)
func TestAddPatchWithFilePath(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()
err := fSys.WriteFile(patchFileName, []byte(patchFileContent))
require.NoError(t, err)
testutils_test.WriteTestKustomization(fSys)
cmd := newCmdAddPatch(fSys)
args := []string{
"--path", patchFileName,
"--kind", kind,
"--group", group,
"--version", version,
"--name", name,
"--namespace", namespace,
"--annotation-selector", annotationSelector,
"--label-selector", labelSelector,
}
cmd.SetArgs(args)
require.NoError(t, cmd.Execute())
content, err := testutils_test.ReadTestKustomization(fSys)
require.NoError(t, err)
for i := 1; i < len(args); i += 2 {
assert.Contains(t, string(content), args[i])
}
}
func TestAddPatchWithPatchContent(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()
err := fSys.WriteFile(patchFileName, []byte(patchFileContent))
require.NoError(t, err)
testutils_test.WriteTestKustomization(fSys)
cmd := newCmdAddPatch(fSys)
args := []string{
"--patch", patchFileContent,
"--kind", kind,
"--group", group,
"--version", version,
"--name", name,
"--namespace", namespace,
"--annotation-selector", annotationSelector,
"--label-selector", labelSelector,
}
cmd.SetArgs(args)
require.NoError(t, cmd.Execute())
content, err := testutils_test.ReadTestKustomization(fSys)
require.NoError(t, err)
for i := 1; i < len(args); i += 2 {
assert.Contains(t, string(content), strings.Trim(args[i], " \n"))
}
}
func TestAddPatchAlreadyThere(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()
err := fSys.WriteFile(patchFileName, []byte(patchFileContent))
require.NoError(t, err)
testutils_test.WriteTestKustomization(fSys)
cmd := newCmdAddPatch(fSys)
args := []string{
"--path", patchFileName,
"--kind", kind,
"--group", group,
"--version", version,
"--name", name,
"--namespace", namespace,
"--annotation-selector", annotationSelector,
"--label-selector", labelSelector,
}
cmd.SetArgs(args)
require.NoError(t, cmd.Execute())
// adding an existing patch shouldn't return an error
require.NoError(t, cmd.Execute())
}
func TestAddPatchNoArgs(t *testing.T) {
fSys := filesys.MakeEmptyDirInMemory()
cmd := newCmdAddPatch(fSys)
err := cmd.Execute()
require.Error(t, err)
assert.Equal(t, "must provide either patch or path", err.Error())
}