From e226cfe6aa3ac5200f9fd6fc864b5b4141563148 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Wed, 1 Feb 2023 18:05:24 +0100 Subject: [PATCH 1/4] odo logs: Do not panic when no access to cluster/podman --- pkg/odo/cli/logs/logs.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/odo/cli/logs/logs.go b/pkg/odo/cli/logs/logs.go index 2912debb6e3..2390be52a7b 100644 --- a/pkg/odo/cli/logs/logs.go +++ b/pkg/odo/cli/logs/logs.go @@ -19,6 +19,7 @@ import ( "github.com/redhat-developer/odo/pkg/devfile/location" "github.com/redhat-developer/odo/pkg/odo/commonflags" + fcontext "github.com/redhat-developer/odo/pkg/odo/commonflags/context" "github.com/redhat-developer/odo/pkg/odo/util" odoutil "github.com/redhat-developer/odo/pkg/odo/util" @@ -89,6 +90,18 @@ func (o *LogsOptions) Complete(ctx context.Context, cmdline cmdline.Cmdline, _ [ } func (o *LogsOptions) Validate(ctx context.Context) error { + + switch fcontext.GetPlatform(ctx, commonflags.PlatformCluster) { + case commonflags.PlatformCluster: + if o.clientset.KubernetesClient == nil { + return errors.New("you need access to a cluster to run this command") + } + case commonflags.PlatformPodman: + if o.clientset.PodmanClient == nil { + return errors.New("you need access to podman to run this command") + } + } + if o.devMode && o.deployMode { return errors.New("pass only one of --dev or --deploy flags; pass no flag to see logs for both modes") } From 6940e683bae7104676ee4c054e5c9accddf01689 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Thu, 2 Feb 2023 16:28:43 +0100 Subject: [PATCH 2/4] Integration tests --- tests/integration/cmd_logs_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/integration/cmd_logs_test.go b/tests/integration/cmd_logs_test.go index 64a4ad9fe21..79db6bc6c24 100644 --- a/tests/integration/cmd_logs_test.go +++ b/tests/integration/cmd_logs_test.go @@ -1,6 +1,7 @@ package integration import ( + "os" "path/filepath" "strings" "time" @@ -54,6 +55,31 @@ var _ = Describe("odo logs command tests", func() { helper.CommonAfterEach(commonVar) }) + When("in a devfile directory", func() { + BeforeEach(func() { + helper.CopyExample(filepath.Join("source", "nodejs"), commonVar.Context) + helper.Cmd("odo", "init", "--name", componentName, "--devfile-path", helper.GetExamplePath("source", "devfiles", "nodejs", "devfile.yaml")).ShouldPass() + }) + When("not connected to any cluster or podman", Label(helper.LabelNoCluster), func() { + It("odo logs should fail with an error message", func() { + cmd := helper.Cmd("odo", "logs") + stderr := cmd.ShouldFail().Err() + Expect(stderr).To(ContainSubstring("you need access to a cluster")) + }) + + It("odo logs --platform podman should fail with an error message", func() { + os.Setenv("PODMAN_CMD", "false") + defer os.Unsetenv("PODMAN_CMD") + cmd := getLogCommand(true) + stderr := cmd.ShouldFail().Err() + Expect(stderr).To(ContainSubstring("you need access to podman")) + }) + }) + + When("podman is not installed", func() { + }) + }) + for _, podman := range []bool{false, true} { podman := podman When("directory is empty", helper.LabelPodmanIf(podman, func() { From 19dd62a88a0088e520a323b49013dcfa624e0494 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Fri, 3 Feb 2023 08:45:12 +0100 Subject: [PATCH 3/4] Remove unnecessary code --- tests/integration/cmd_logs_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/cmd_logs_test.go b/tests/integration/cmd_logs_test.go index 79db6bc6c24..63be144c399 100644 --- a/tests/integration/cmd_logs_test.go +++ b/tests/integration/cmd_logs_test.go @@ -76,8 +76,6 @@ var _ = Describe("odo logs command tests", func() { }) }) - When("podman is not installed", func() { - }) }) for _, podman := range []bool{false, true} { From 15ffeea380352759a5d5be6b930f34a0a57fa74c Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Fri, 3 Feb 2023 13:14:54 +0100 Subject: [PATCH 4/4] Use defined errors --- pkg/odo/cli/logs/logs.go | 6 ++++-- tests/integration/cmd_logs_test.go | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/pkg/odo/cli/logs/logs.go b/pkg/odo/cli/logs/logs.go index 2390be52a7b..fc5a390246c 100644 --- a/pkg/odo/cli/logs/logs.go +++ b/pkg/odo/cli/logs/logs.go @@ -13,7 +13,9 @@ import ( "github.com/fatih/color" + "github.com/redhat-developer/odo/pkg/kclient" odolabels "github.com/redhat-developer/odo/pkg/labels" + "github.com/redhat-developer/odo/pkg/podman" "github.com/redhat-developer/odo/pkg/log" @@ -94,11 +96,11 @@ func (o *LogsOptions) Validate(ctx context.Context) error { switch fcontext.GetPlatform(ctx, commonflags.PlatformCluster) { case commonflags.PlatformCluster: if o.clientset.KubernetesClient == nil { - return errors.New("you need access to a cluster to run this command") + return kclient.NewNoConnectionError() } case commonflags.PlatformPodman: if o.clientset.PodmanClient == nil { - return errors.New("you need access to podman to run this command") + return podman.NewPodmanNotFoundError(nil) } } diff --git a/tests/integration/cmd_logs_test.go b/tests/integration/cmd_logs_test.go index 63be144c399..c89b550cca5 100644 --- a/tests/integration/cmd_logs_test.go +++ b/tests/integration/cmd_logs_test.go @@ -64,7 +64,7 @@ var _ = Describe("odo logs command tests", func() { It("odo logs should fail with an error message", func() { cmd := helper.Cmd("odo", "logs") stderr := cmd.ShouldFail().Err() - Expect(stderr).To(ContainSubstring("you need access to a cluster")) + Expect(stderr).To(ContainSubstring("unable to access the cluster")) }) It("odo logs --platform podman should fail with an error message", func() { @@ -72,7 +72,7 @@ var _ = Describe("odo logs command tests", func() { defer os.Unsetenv("PODMAN_CMD") cmd := getLogCommand(true) stderr := cmd.ShouldFail().Err() - Expect(stderr).To(ContainSubstring("you need access to podman")) + Expect(stderr).To(ContainSubstring("unable to access podman")) }) })