Merge pull request #5030 from koba1t/fix/error_when_no_path_match

be error when no path matching
This commit is contained in:
Kubernetes Prow Robot
2023-06-07 07:42:13 -07:00
committed by GitHub
5 changed files with 51 additions and 17 deletions

View File

@@ -7,6 +7,7 @@ import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/api/provider"
"sigs.k8s.io/kustomize/api/types"
"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) {
fSys := filesys.MakeFsInMemory()
want := "foo"

View File

@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/api/konfig"
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
@@ -38,7 +39,7 @@ func TestAddComponentHappyPath(t *testing.T) {
}
func TestAddComponentAlreadyThere(t *testing.T) {
fSys := filesys.MakeFsInMemory()
fSys := filesys.MakeEmptyDirInMemory()
err := fSys.WriteFile(componentFileName, []byte(componentFileContent))
require.NoError(t, err)
testutils_test.WriteTestKustomization(fSys)
@@ -51,19 +52,19 @@ func TestAddComponentAlreadyThere(t *testing.T) {
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) {
fSys := filesys.MakeFsInMemory()
err := fSys.WriteFile(componentFileName, []byte(componentFileContent))
require.NoError(t, err)
fSys := filesys.MakeEmptyDirInMemory()
testutils_test.WriteTestKustomization(fSys)
cmd := newCmdAddComponent(fSys)
args := []string{componentFileName}
args := []string{konfig.DefaultKustomizationFileName()}
require.NoError(t, cmd.RunE(cmd, args))
content, err := testutils_test.ReadTestKustomization(fSys)
require.NoError(t, err)
assert.NotContains(t, string(content), componentFileName)
assert.NotContains(t, string(content), konfig.DefaultKustomizationFileName())
}
func TestAddComponentNoArgs(t *testing.T) {
@@ -73,3 +74,13 @@ func TestAddComponentNoArgs(t *testing.T) {
err := cmd.Execute()
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")
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/kustomize/api/konfig"
testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
@@ -57,7 +58,7 @@ replacements:
}
func TestAddResourceAlreadyThere(t *testing.T) {
fSys := filesys.MakeFsInMemory()
fSys := filesys.MakeEmptyDirInMemory()
err := fSys.WriteFile(resourceFileName, []byte(resourceFileContent))
require.NoError(t, err)
testutils_test.WriteTestKustomization(fSys)
@@ -70,20 +71,20 @@ func TestAddResourceAlreadyThere(t *testing.T) {
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) {
fSys := filesys.MakeFsInMemory()
err := fSys.WriteFile(resourceFileName, []byte(resourceFileContent))
require.NoError(t, err)
fSys := filesys.MakeEmptyDirInMemory()
testutils_test.WriteTestKustomization(fSys)
cmd := newCmdAddResource(fSys)
args := []string{resourceFileName}
args := []string{konfig.DefaultKustomizationFileName()}
assert.NoError(t, cmd.RunE(cmd, args))
content, err := testutils_test.ReadTestKustomization(fSys)
assert.NoError(t, err)
assert.NotContains(t, string(content), resourceFileName)
assert.NotContains(t, string(content), konfig.DefaultKustomizationFileName())
}
func TestAddResourceNoArgs(t *testing.T) {
@@ -94,3 +95,13 @@ func TestAddResourceNoArgs(t *testing.T) {
assert.Error(t, err)
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")
}

View File

@@ -30,8 +30,8 @@ func GlobPatterns(fSys filesys.FileSystem, patterns []string) ([]string, error)
return result, nil
}
// 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.
// 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.
// 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) {
var result []string
for _, pattern := range patterns {
@@ -42,7 +42,7 @@ func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []
if len(files) == 0 {
loader, err := ldr.New(pattern)
if err != nil {
log.Printf("%s has no match", pattern)
return nil, fmt.Errorf("%s has no match: %w", pattern, err)
} else {
result = append(result, pattern)
if loader != nil {

View File

@@ -89,8 +89,11 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
}
// test load invalid file
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{"http://invalid"})
if err != nil {
invalidURL := "http://invalid"
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)
}
if len(resources) > 0 {