Skip to content

Commit

Permalink
odo logs: Do not panic when no access to cluster/podman (#6561)
Browse files Browse the repository at this point in the history
* odo logs: Do not panic when no access to cluster/podman

* Integration tests

* Remove unnecessary code

* Use defined errors
  • Loading branch information
feloy committed Feb 6, 2023
1 parent a035764 commit 704ff2a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/odo/cli/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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")
}
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/cmd_logs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package integration

import (
"os"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 704ff2a

Please sign in to comment.