mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-10 16:42:51 +00:00
Merge pull request #5030 from koba1t/fix/error_when_no_path_match
be error when no path matching
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"sigs.k8s.io/kustomize/api/provider"
|
"sigs.k8s.io/kustomize/api/provider"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
|
"sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile"
|
||||||
@@ -53,6 +54,14 @@ func TestCreateWithResources(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateWithResourcesWithFileNotFound(t *testing.T) {
|
||||||
|
fSys := filesys.MakeEmptyDirInMemory()
|
||||||
|
assert.NoError(t, fSys.WriteFile("foo.yaml", []byte("")))
|
||||||
|
opts := createFlags{resources: "foo.yaml,bar.yaml"}
|
||||||
|
err := runCreate(opts, fSys, factory)
|
||||||
|
assert.EqualError(t, err, "bar.yaml has no match: must build at directory: not a valid directory: 'bar.yaml' doesn't exist")
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateWithNamespace(t *testing.T) {
|
func TestCreateWithNamespace(t *testing.T) {
|
||||||
fSys := filesys.MakeFsInMemory()
|
fSys := filesys.MakeFsInMemory()
|
||||||
want := "foo"
|
want := "foo"
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
|
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
|
||||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||||
)
|
)
|
||||||
@@ -38,7 +39,7 @@ func TestAddComponentHappyPath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddComponentAlreadyThere(t *testing.T) {
|
func TestAddComponentAlreadyThere(t *testing.T) {
|
||||||
fSys := filesys.MakeFsInMemory()
|
fSys := filesys.MakeEmptyDirInMemory()
|
||||||
err := fSys.WriteFile(componentFileName, []byte(componentFileContent))
|
err := fSys.WriteFile(componentFileName, []byte(componentFileContent))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
testutils_test.WriteTestKustomization(fSys)
|
testutils_test.WriteTestKustomization(fSys)
|
||||||
@@ -51,19 +52,19 @@ func TestAddComponentAlreadyThere(t *testing.T) {
|
|||||||
assert.NoError(t, cmd.RunE(cmd, args))
|
assert.NoError(t, cmd.RunE(cmd, args))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test for trying to add the kustomization.yaml file itself for resources.
|
||||||
|
// This adding operation is not allowed.
|
||||||
func TestAddKustomizationFileAsComponent(t *testing.T) {
|
func TestAddKustomizationFileAsComponent(t *testing.T) {
|
||||||
fSys := filesys.MakeFsInMemory()
|
fSys := filesys.MakeEmptyDirInMemory()
|
||||||
err := fSys.WriteFile(componentFileName, []byte(componentFileContent))
|
|
||||||
require.NoError(t, err)
|
|
||||||
testutils_test.WriteTestKustomization(fSys)
|
testutils_test.WriteTestKustomization(fSys)
|
||||||
|
|
||||||
cmd := newCmdAddComponent(fSys)
|
cmd := newCmdAddComponent(fSys)
|
||||||
args := []string{componentFileName}
|
args := []string{konfig.DefaultKustomizationFileName()}
|
||||||
require.NoError(t, cmd.RunE(cmd, args))
|
require.NoError(t, cmd.RunE(cmd, args))
|
||||||
|
|
||||||
content, err := testutils_test.ReadTestKustomization(fSys)
|
content, err := testutils_test.ReadTestKustomization(fSys)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.NotContains(t, string(content), componentFileName)
|
assert.NotContains(t, string(content), konfig.DefaultKustomizationFileName())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddComponentNoArgs(t *testing.T) {
|
func TestAddComponentNoArgs(t *testing.T) {
|
||||||
@@ -73,3 +74,13 @@ func TestAddComponentNoArgs(t *testing.T) {
|
|||||||
err := cmd.Execute()
|
err := cmd.Execute()
|
||||||
assert.EqualError(t, err, "must specify a component file")
|
assert.EqualError(t, err, "must specify a component file")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddComponentFileNotFound(t *testing.T) {
|
||||||
|
fSys := filesys.MakeEmptyDirInMemory()
|
||||||
|
|
||||||
|
cmd := newCmdAddComponent(fSys)
|
||||||
|
args := []string{componentFileName}
|
||||||
|
|
||||||
|
err := cmd.RunE(cmd, args)
|
||||||
|
assert.EqualError(t, err, componentFileName+" has no match: must build at directory: not a valid directory: '"+componentFileName+"' doesn't exist")
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"sigs.k8s.io/kustomize/api/konfig"
|
||||||
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
|
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
|
||||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||||
)
|
)
|
||||||
@@ -57,7 +58,7 @@ replacements:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAddResourceAlreadyThere(t *testing.T) {
|
func TestAddResourceAlreadyThere(t *testing.T) {
|
||||||
fSys := filesys.MakeFsInMemory()
|
fSys := filesys.MakeEmptyDirInMemory()
|
||||||
err := fSys.WriteFile(resourceFileName, []byte(resourceFileContent))
|
err := fSys.WriteFile(resourceFileName, []byte(resourceFileContent))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
testutils_test.WriteTestKustomization(fSys)
|
testutils_test.WriteTestKustomization(fSys)
|
||||||
@@ -70,20 +71,20 @@ func TestAddResourceAlreadyThere(t *testing.T) {
|
|||||||
assert.NoError(t, cmd.RunE(cmd, args))
|
assert.NoError(t, cmd.RunE(cmd, args))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test for trying to add the kustomization.yaml file itself for resources.
|
||||||
|
// This adding operation is not allowed.
|
||||||
func TestAddKustomizationFileAsResource(t *testing.T) {
|
func TestAddKustomizationFileAsResource(t *testing.T) {
|
||||||
fSys := filesys.MakeFsInMemory()
|
fSys := filesys.MakeEmptyDirInMemory()
|
||||||
err := fSys.WriteFile(resourceFileName, []byte(resourceFileContent))
|
|
||||||
require.NoError(t, err)
|
|
||||||
testutils_test.WriteTestKustomization(fSys)
|
testutils_test.WriteTestKustomization(fSys)
|
||||||
|
|
||||||
cmd := newCmdAddResource(fSys)
|
cmd := newCmdAddResource(fSys)
|
||||||
args := []string{resourceFileName}
|
args := []string{konfig.DefaultKustomizationFileName()}
|
||||||
assert.NoError(t, cmd.RunE(cmd, args))
|
assert.NoError(t, cmd.RunE(cmd, args))
|
||||||
|
|
||||||
content, err := testutils_test.ReadTestKustomization(fSys)
|
content, err := testutils_test.ReadTestKustomization(fSys)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
assert.NotContains(t, string(content), resourceFileName)
|
assert.NotContains(t, string(content), konfig.DefaultKustomizationFileName())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddResourceNoArgs(t *testing.T) {
|
func TestAddResourceNoArgs(t *testing.T) {
|
||||||
@@ -94,3 +95,13 @@ func TestAddResourceNoArgs(t *testing.T) {
|
|||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Equal(t, "must specify a resource file", err.Error())
|
assert.Equal(t, "must specify a resource file", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddResourceFileNotFound(t *testing.T) {
|
||||||
|
fSys := filesys.MakeEmptyDirInMemory()
|
||||||
|
|
||||||
|
cmd := newCmdAddResource(fSys)
|
||||||
|
args := []string{resourceFileName}
|
||||||
|
|
||||||
|
err := cmd.RunE(cmd, args)
|
||||||
|
assert.EqualError(t, err, resourceFileName+" has no match: must build at directory: not a valid directory: '"+resourceFileName+"' doesn't exist")
|
||||||
|
}
|
||||||
|
|||||||
@@ -30,8 +30,8 @@ func GlobPatterns(fSys filesys.FileSystem, patterns []string) ([]string, error)
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlobPatterns accepts a slice of glob strings and returns the set of
|
// GlobPatterns accepts a slice of glob strings and returns the set of matching file paths. If files are not found, will try load from remote.
|
||||||
// matching file paths. If files are not found, will try load from remote.
|
// It returns an error if there are no matching files or it can't load from remote.
|
||||||
func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []string) ([]string, error) {
|
func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []string) ([]string, error) {
|
||||||
var result []string
|
var result []string
|
||||||
for _, pattern := range patterns {
|
for _, pattern := range patterns {
|
||||||
@@ -42,7 +42,7 @@ func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []
|
|||||||
if len(files) == 0 {
|
if len(files) == 0 {
|
||||||
loader, err := ldr.New(pattern)
|
loader, err := ldr.New(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("%s has no match", pattern)
|
return nil, fmt.Errorf("%s has no match: %w", pattern, err)
|
||||||
} else {
|
} else {
|
||||||
result = append(result, pattern)
|
result = append(result, pattern)
|
||||||
if loader != nil {
|
if loader != nil {
|
||||||
|
|||||||
@@ -89,8 +89,11 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test load invalid file
|
// test load invalid file
|
||||||
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{"http://invalid"})
|
invalidURL := "http://invalid"
|
||||||
if err != nil {
|
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected error but did not receive one")
|
||||||
|
} else if err.Error() != invalidURL+" has no match: "+invalidURL+" not exist" {
|
||||||
t.Fatalf("unexpected load error: %v", err)
|
t.Fatalf("unexpected load error: %v", err)
|
||||||
}
|
}
|
||||||
if len(resources) > 0 {
|
if len(resources) > 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user