Files
kustomize/pkg/commands/edit/remove/removepatch_test.go
2019-06-24 21:25:01 +09:00

149 lines
3.5 KiB
Go

/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package remove
import (
"fmt"
"sigs.k8s.io/kustomize/v3/pkg/fs"
"sigs.k8s.io/kustomize/v3/pkg/patch"
"strings"
"testing"
)
const (
patchFileContent = `
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
`
)
func makeKustomizationPatchFS() fs.FileSystem {
fakeFS := fs.MakeFakeFS()
patches := []string{"patch1.yaml", "patch2.yaml"}
fakeFS.WriteTestKustomizationWith([]byte(
fmt.Sprintf("patchesStrategicMerge:\n - %s",
strings.Join(patches, "\n - "))))
for _, p := range patches {
fakeFS.WriteFile(p, []byte(patchFileContent))
}
fakeFS.WriteFile("patch3.yaml", []byte(patchFileContent))
return fakeFS
}
func TestRemovePatch(t *testing.T) {
fakeFS := makeKustomizationPatchFS()
cmd := newCmdRemovePatch(fakeFS)
args := []string{"patch1.yaml"}
err := cmd.RunE(cmd, args)
if err != nil {
t.Errorf("unexpected error %v", err)
}
m := readKustomizationFS(t, fakeFS)
for _, k := range args {
if patch.Exist(m.PatchesStrategicMerge, k) {
t.Errorf("%s must be deleted", k)
}
}
}
func TestRemovePatchMultipleArgs(t *testing.T) {
fakeFS := makeKustomizationPatchFS()
cmd := newCmdRemovePatch(fakeFS)
args := []string{"patch1.yaml", "patch2.yaml"}
err := cmd.RunE(cmd, args)
if err != nil {
t.Errorf("unexpected error %v", err)
}
m := readKustomizationFS(t, fakeFS)
for _, k := range args {
if patch.Exist(m.PatchesStrategicMerge, k) {
t.Errorf("%s must be deleted", k)
}
}
}
func TestRemovePatchGlob(t *testing.T) {
fakeFS := makeKustomizationPatchFS()
cmd := newCmdRemovePatch(fakeFS)
args := []string{"patch*.yaml"}
err := cmd.RunE(cmd, args)
if err != nil {
t.Errorf("unexpected error %v", err)
}
m := readKustomizationFS(t, fakeFS)
if len(m.PatchesStrategicMerge) != 0 {
t.Errorf("all patch must be deleted")
}
}
func TestRemovePatchNotDefinedInKustomization(t *testing.T) {
fakeFS := makeKustomizationPatchFS()
cmd := newCmdRemovePatch(fakeFS)
args := []string{"patch3.yaml"}
err := cmd.RunE(cmd, args)
if err != nil {
t.Errorf("unexpected error %v", err)
}
m := readKustomizationFS(t, fakeFS)
for _, k := range []string{"patch1.yaml", "patch2.yaml"} {
if !patch.Exist(m.PatchesStrategicMerge, k) {
t.Errorf("%s must exist", k)
}
}
}
func TestRemovePatchNotExist(t *testing.T) {
fakeFS := makeKustomizationPatchFS()
cmd := newCmdRemovePatch(fakeFS)
args := []string{"patch4.yaml"}
err := cmd.RunE(cmd, args)
if err != nil {
t.Errorf("unexpected error %v", err)
}
m := readKustomizationFS(t, fakeFS)
for _, k := range []string{"patch1.yaml", "patch2.yaml"} {
if !patch.Exist(m.PatchesStrategicMerge, k) {
t.Errorf("%s must exist", k)
}
}
}
func TestRemovePatchNoArgs(t *testing.T) {
fakeFS := makeKustomizationPatchFS()
cmd := newCmdRemovePatch(fakeFS)
err := cmd.RunE(cmd, nil)
if err == nil {
t.Errorf("expected an error")
}
if err.Error() != "must specify a patch file" {
t.Errorf("incorrect error: %v", err.Error())
}
}