From 392a33d425f54f8d9a177bafdcaf7f5c84123d8a Mon Sep 17 00:00:00 2001 From: Daniel Guns Date: Tue, 6 May 2025 19:30:57 -0300 Subject: [PATCH] Change error reporting in get.go from logger.Failure to fmt.Errorf to give non 0 exit code Signed-off-by: Daniel Guns --- cmd/flux/get.go | 4 +-- cmd/flux/get_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/cmd/flux/get.go b/cmd/flux/get.go index 17766f38..53c4721f 100644 --- a/cmd/flux/get.go +++ b/cmd/flux/get.go @@ -184,13 +184,13 @@ func (get getCommand) run(cmd *cobra.Command, args []string) error { if get.list.len() == 0 { if len(args) > 0 { - logger.Failuref("%s object '%s' not found in %s namespace", + return fmt.Errorf("%s object '%s' not found in %s namespace", get.kind, args[0], namespaceNameOrAny(getArgs.allNamespaces, *kubeconfigArgs.Namespace), ) } else if !getAll { - logger.Failuref("no %s objects found in %s namespace", + return fmt.Errorf("no %s objects found in %s namespace", get.kind, namespaceNameOrAny(getArgs.allNamespaces, *kubeconfigArgs.Namespace), ) diff --git a/cmd/flux/get_test.go b/cmd/flux/get_test.go index 62a1b725..30d74ab2 100644 --- a/cmd/flux/get_test.go +++ b/cmd/flux/get_test.go @@ -19,7 +19,10 @@ limitations under the License. package main -import "testing" +import ( + "fmt" + "testing" +) func Test_GetCmd(t *testing.T) { tmpl := map[string]string{ @@ -59,3 +62,76 @@ func Test_GetCmd(t *testing.T) { }) } } + +func Test_GetCmdErrors(t *testing.T) { + tmpl := map[string]string{ + "fluxns": allocateNamespace("flux-system"), + } + testEnv.CreateObjectFile("./testdata/get/objects.yaml", tmpl, t) + + tests := []struct { + name string + args string + assert assertFunc + }{ + { + name: "specific object not found", + args: "get kustomization non-existent-resource -n " + tmpl["fluxns"], + assert: assertError(fmt.Sprintf("Kustomization object 'non-existent-resource' not found in \"%s\" namespace", tmpl["fluxns"])), + }, + { + name: "no objects found in namespace", + args: "get helmrelease -n " + tmpl["fluxns"], + assert: assertError(fmt.Sprintf("no HelmRelease objects found in \"%s\" namespace", tmpl["fluxns"])), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := cmdTestCase{ + args: tt.args, + assert: tt.assert, + } + cmd.runTestCmd(t) + }) + } +} + +func Test_GetCmdSuccess(t *testing.T) { + tmpl := map[string]string{ + "fluxns": allocateNamespace("flux-system"), + } + testEnv.CreateObjectFile("./testdata/get/objects.yaml", tmpl, t) + + tests := []struct { + name string + args string + assert assertFunc + }{ + { + name: "list sources git", + args: "get sources git -n " + tmpl["fluxns"], + assert: assertSuccess(), + }, + { + name: "get help", + args: "get --help", + assert: assertSuccess(), + }, + { + name: "get with all namespaces flag", + args: "get sources git -A", + assert: assertSuccess(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cmd := cmdTestCase{ + args: tt.args, + assert: tt.assert, + } + cmd.runTestCmd(t) + }) + } +}