From dfd2d7600d62feac701300606b979c25fada6ac3 Mon Sep 17 00:00:00 2001 From: koba1t Date: Sat, 4 Feb 2023 00:57:56 +0900 Subject: [PATCH 1/6] be error when no path match --- kustomize/commands/internal/util/util.go | 2 +- kustomize/commands/internal/util/util_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kustomize/commands/internal/util/util.go b/kustomize/commands/internal/util/util.go index c084909cdf..8e60a5f6eb 100644 --- a/kustomize/commands/internal/util/util.go +++ b/kustomize/commands/internal/util/util.go @@ -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", pattern) } 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..b08b7cf7bf 100644 --- a/kustomize/commands/internal/util/util_test.go +++ b/kustomize/commands/internal/util/util_test.go @@ -90,7 +90,7 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) { // test load invalid file resources, err = GlobPatternsWithLoader(fSys, ldr, []string{"http://invalid"}) - if err != nil { + if err == nil { t.Fatalf("unexpected load error: %v", err) } if len(resources) > 0 { From 53a4134379196601d9ad85e8488426f860f8c20d Mon Sep 17 00:00:00 2001 From: koba1t Date: Thu, 9 Feb 2023 04:01:30 +0900 Subject: [PATCH 2/6] fix error handling and test --- kustomize/commands/internal/util/util.go | 2 +- kustomize/commands/internal/util/util_test.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/kustomize/commands/internal/util/util.go b/kustomize/commands/internal/util/util.go index 8e60a5f6eb..d68a6edca8 100644 --- a/kustomize/commands/internal/util/util.go +++ b/kustomize/commands/internal/util/util.go @@ -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 { - return nil, fmt.Errorf("%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 b08b7cf7bf..20d0b030a4 100644 --- a/kustomize/commands/internal/util/util_test.go +++ b/kustomize/commands/internal/util/util_test.go @@ -89,8 +89,9 @@ 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 && err.Error() != invalidURL+" has no match: "+invalidURL+" not exist" { t.Fatalf("unexpected load error: %v", err) } if len(resources) > 0 { From cd49194383cdb9be8ce0438c8aee602ce7ca73e9 Mon Sep 17 00:00:00 2001 From: koba1t Date: Thu, 9 Feb 2023 05:35:45 +0900 Subject: [PATCH 3/6] fix tests --- kustomize/commands/edit/add/addcomponent_test.go | 6 +++--- kustomize/commands/edit/add/addresource_test.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/kustomize/commands/edit/add/addcomponent_test.go b/kustomize/commands/edit/add/addcomponent_test.go index e354861915..3e99ff9107 100644 --- a/kustomize/commands/edit/add/addcomponent_test.go +++ b/kustomize/commands/edit/add/addcomponent_test.go @@ -38,7 +38,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) @@ -52,7 +52,7 @@ func TestAddComponentAlreadyThere(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) @@ -63,7 +63,7 @@ func TestAddKustomizationFileAsComponent(t *testing.T) { content, err := testutils_test.ReadTestKustomization(fSys) require.NoError(t, err) - assert.NotContains(t, string(content), componentFileName) + assert.Contains(t, string(content), componentFileName) } func TestAddComponentNoArgs(t *testing.T) { diff --git a/kustomize/commands/edit/add/addresource_test.go b/kustomize/commands/edit/add/addresource_test.go index d4a344185e..3602d89013 100644 --- a/kustomize/commands/edit/add/addresource_test.go +++ b/kustomize/commands/edit/add/addresource_test.go @@ -57,7 +57,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) @@ -71,7 +71,7 @@ func TestAddResourceAlreadyThere(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) @@ -83,7 +83,7 @@ func TestAddKustomizationFileAsResource(t *testing.T) { content, err := testutils_test.ReadTestKustomization(fSys) assert.NoError(t, err) - assert.NotContains(t, string(content), resourceFileName) + assert.Contains(t, string(content), resourceFileName) } func TestAddResourceNoArgs(t *testing.T) { From 694b3c9318d5658945c1e2e8defb4bef3bbd29b2 Mon Sep 17 00:00:00 2001 From: koba1t Date: Wed, 1 Mar 2023 04:11:55 +0900 Subject: [PATCH 4/6] Add the test cases for check the file when edit add command --- kustomize/commands/create/create_test.go | 9 +++++++++ kustomize/commands/edit/add/addcomponent_test.go | 10 ++++++++++ kustomize/commands/edit/add/addresource_test.go | 10 ++++++++++ 3 files changed, 29 insertions(+) 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 3e99ff9107..a6d58e3204 100644 --- a/kustomize/commands/edit/add/addcomponent_test.go +++ b/kustomize/commands/edit/add/addcomponent_test.go @@ -73,3 +73,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 3602d89013..c17621bd37 100644 --- a/kustomize/commands/edit/add/addresource_test.go +++ b/kustomize/commands/edit/add/addresource_test.go @@ -94,3 +94,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") +} From 8383b2832290dc969f2924a1c552852159bbaeed Mon Sep 17 00:00:00 2001 From: koba1t Date: Thu, 25 May 2023 04:25:10 +0900 Subject: [PATCH 5/6] fix test case handling and update a function comment --- kustomize/commands/internal/util/util.go | 4 ++-- kustomize/commands/internal/util/util_test.go | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/kustomize/commands/internal/util/util.go b/kustomize/commands/internal/util/util.go index d68a6edca8..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 { diff --git a/kustomize/commands/internal/util/util_test.go b/kustomize/commands/internal/util/util_test.go index 20d0b030a4..631e1145f8 100644 --- a/kustomize/commands/internal/util/util_test.go +++ b/kustomize/commands/internal/util/util_test.go @@ -91,7 +91,9 @@ func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) { // test load invalid file invalidURL := "http://invalid" resources, err = GlobPatternsWithLoader(fSys, ldr, []string{invalidURL}) - if err != nil && err.Error() != invalidURL+" has no match: "+invalidURL+" not exist" { + 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 { From 565cff2d07dad94b66729ddb9260c13b53442528 Mon Sep 17 00:00:00 2001 From: koba1t Date: Thu, 25 May 2023 05:13:33 +0900 Subject: [PATCH 6/6] update test add KustomizationFile --- kustomize/commands/edit/add/addcomponent_test.go | 9 +++++---- kustomize/commands/edit/add/addresource_test.go | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/kustomize/commands/edit/add/addcomponent_test.go b/kustomize/commands/edit/add/addcomponent_test.go index a6d58e3204..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" ) @@ -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.MakeEmptyDirInMemory() - err := fSys.WriteFile(componentFileName, []byte(componentFileContent)) - require.NoError(t, err) 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.Contains(t, string(content), componentFileName) + assert.NotContains(t, string(content), konfig.DefaultKustomizationFileName()) } func TestAddComponentNoArgs(t *testing.T) { diff --git a/kustomize/commands/edit/add/addresource_test.go b/kustomize/commands/edit/add/addresource_test.go index c17621bd37..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" ) @@ -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.MakeEmptyDirInMemory() - err := fSys.WriteFile(resourceFileName, []byte(resourceFileContent)) - require.NoError(t, err) 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.Contains(t, string(content), resourceFileName) + assert.NotContains(t, string(content), konfig.DefaultKustomizationFileName()) } func TestAddResourceNoArgs(t *testing.T) {