Skip to content

Commit

Permalink
Merge pull request #5030 from koba1t/fix/error_when_no_path_match
Browse files Browse the repository at this point in the history
be error when no path matching
  • Loading branch information
k8s-ci-robot authored Jun 7, 2023
2 parents 0add0f9 + 565cff2 commit dce9426
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
9 changes: 9 additions & 0 deletions kustomize/commands/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
23 changes: 17 additions & 6 deletions kustomize/commands/edit/add/addcomponent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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")
}
23 changes: 17 additions & 6 deletions kustomize/commands/edit/add/addresource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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")
}
6 changes: 3 additions & 3 deletions kustomize/commands/internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions kustomize/commands/internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dce9426

Please sign in to comment.