From 704ff2a1613bd92c3efb6d0522fe6b297bf7a479 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Mon, 6 Feb 2023 12:11:20 +0100 Subject: [PATCH] odo logs: Do not panic when no access to cluster/podman (#6561) * odo logs: Do not panic when no access to cluster/podman * Integration tests * Remove unnecessary code * Use defined errors --- pkg/odo/cli/logs/logs.go | 15 +++++++++++++++ tests/integration/cmd_logs_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/pkg/odo/cli/logs/logs.go b/pkg/odo/cli/logs/logs.go index 2912debb6e3..fc5a390246c 100644 --- a/pkg/odo/cli/logs/logs.go +++ b/pkg/odo/cli/logs/logs.go @@ -13,12 +13,15 @@ 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" "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 +92,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 kclient.NewNoConnectionError() + } + case commonflags.PlatformPodman: + if o.clientset.PodmanClient == nil { + return podman.NewPodmanNotFoundError(nil) + } + } + 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") } diff --git a/tests/integration/cmd_logs_test.go b/tests/integration/cmd_logs_test.go index 64a4ad9fe21..c89b550cca5 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,29 @@ 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("unable to access the 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("unable to access podman")) + }) + }) + + }) + for _, podman := range []bool{false, true} { podman := podman When("directory is empty", helper.LabelPodmanIf(podman, func() {