diff --git a/kustomize/commands/create/create_test.go b/kustomize/commands/create/create_test.go index ec695235a3..5b325ddb7c 100644 --- a/kustomize/commands/create/create_test.go +++ b/kustomize/commands/create/create_test.go @@ -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" diff --git a/kustomize/commands/edit/add/addcomponent_test.go b/kustomize/commands/edit/add/addcomponent_test.go index e354861915..e1a137060f 100644 --- a/kustomize/commands/edit/add/addcomponent_test.go +++ b/kustomize/commands/edit/add/addcomponent_test.go @@ -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") +} diff --git a/kustomize/commands/edit/add/addresource_test.go b/kustomize/commands/edit/add/addresource_test.go index d4a344185e..d35f162623 100644 --- a/kustomize/commands/edit/add/addresource_test.go +++ b/kustomize/commands/edit/add/addresource_test.go @@ -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") +} diff --git a/kustomize/commands/internal/util/util.go b/kustomize/commands/internal/util/util.go index c084909cdf..94004e9978 100644 --- a/kustomize/commands/internal/util/util.go +++ b/kustomize/commands/internal/util/util.go @@ -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 { diff --git a/kustomize/commands/internal/util/util_test.go b/kustomize/commands/internal/util/util_test.go index 21c2e70c34..631e1145f8 100644 --- a/kustomize/commands/internal/util/util_test.go +++ b/kustomize/commands/internal/util/util_test.go @@ -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 {