Skip to content

Commit

Permalink
[hotfix-2.2] Fix help command not working when session id envs not set (
Browse files Browse the repository at this point in the history
#220)

* Add test for bug #216

(cherry picked from commit 0174187)

* Fix #216 and adapt tests

(cherry picked from commit d2036d5)

* fix env variable in test

(cherry picked from commit c5d8996)
  • Loading branch information
petersutter authored Feb 17, 2023
1 parent 5b2c91f commit 0b63294
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 20 deletions.
32 changes: 32 additions & 0 deletions pkg/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package cmd_test

import (
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -80,4 +81,35 @@ var _ = Describe("Gardenctl command", func() {
})
})
})

Context("when running the help command", func() {
var (
sessionID string
termSessionID string
)

BeforeEach(func() {
sessionID = os.Getenv("GCTL_SESSION_ID")
termSessionID = os.Getenv("TERM_SESSION_ID")

Expect(os.Unsetenv("GCTL_SESSION_ID")).To(Succeed())
Expect(os.Unsetenv("TERM_SESSION_ID")).To(Succeed())
})

AfterEach(func() {
// restore env variables
Expect(os.Setenv("GCTL_SESSION_ID", sessionID)).To(Succeed())
Expect(os.Setenv("TERM_SESSION_ID", termSessionID)).To(Succeed())
})

It("should succeed without session IDs", func() {
args := []string{
"help",
}

cmd := cmd.NewGardenctlCommand(util.NewFactoryImpl(), streams)
cmd.SetArgs(args)
Expect(cmd.Execute()).To(Succeed())
})
})
})
5 changes: 1 addition & 4 deletions pkg/cmd/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"runtime"

"github.com/spf13/cobra"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/gardener/gardenctl-v2/internal/util"
"github.com/gardener/gardenctl-v2/pkg/cmd/base"
Expand Down Expand Up @@ -57,9 +56,7 @@ here https://github.com/gardener/gardenctl-v2/tree/master/pkg/cmd/env/templates.
persistentFlags := cmd.PersistentFlags()
o.AddFlags(persistentFlags)

manager, err := f.Manager()
utilruntime.Must(err)
manager.TargetFlags().AddFlags(persistentFlags)
f.TargetFlags().AddFlags(persistentFlags)
flags.RegisterCompletionFuncsForTargetFlags(cmd, f, ioStreams, persistentFlags)

for _, s := range validShells {
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var _ = Describe("Env Commands", func() {
factory.EXPECT().Manager().Return(manager, nil).AnyTimes()

targetFlags := target.NewTargetFlags("", "", "", "", false)
factory.EXPECT().TargetFlags().Return(targetFlags).AnyTimes()
manager.EXPECT().TargetFlags().Return(targetFlags).AnyTimes()

streams = util.IOStreams{}
Expand Down
4 changes: 1 addition & 3 deletions pkg/cmd/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ gardenctl kubeconfig --garden my-garden --project my-project`,

o.AddFlags(cmd.Flags())

manager, err := f.Manager()
utilruntime.Must(err)
manager.TargetFlags().AddFlags(cmd.Flags())
f.TargetFlags().AddFlags(cmd.Flags())
flags.RegisterCompletionFuncsForTargetFlags(cmd, f, ioStreams, cmd.Flags())

utilruntime.Must(cmd.RegisterFlagCompletionFunc("output", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/cmd/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ var _ = Describe("Kubeconfig Command - Options", func() {
manager.EXPECT().ClientConfig(ctx, t).Return(config, nil)

targetFlags := target.NewTargetFlags("", "", "", "", false)
manager.EXPECT().TargetFlags().Return(targetFlags).AnyTimes()
factory.EXPECT().TargetFlags().Return(targetFlags).AnyTimes()

streams, _, out, _ = util.NewTestIOStreams()
cmd = cmdkubeconfig.NewCmdKubeconfig(factory, streams)
})

It("should execute the kubeconfig subcommand", func() {
factory.EXPECT().Manager().Return(manager, nil).Times(2)

cmd = cmdkubeconfig.NewCmdKubeconfig(factory, streams)
cmd.SetArgs([]string{"--output", "yaml"})
Expect(cmd.Execute()).To(Succeed())
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"

"github.com/spf13/cobra"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/gardener/gardenctl-v2/internal/util"
"github.com/gardener/gardenctl-v2/pkg/cmd/base"
Expand Down Expand Up @@ -43,9 +42,7 @@ func NewCmdSSH(f util.Factory, o *SSHOptions) *cobra.Command {
o.AccessConfig.AddFlags(cmd.Flags())
RegisterCompletionFuncsForAccessConfigFlags(cmd, f, o.IOStreams, cmd.Flags())

manager, err := f.Manager()
utilruntime.Must(err)
manager.TargetFlags().AddFlags(cmd.Flags())
f.TargetFlags().AddFlags(cmd.Flags())
flags.RegisterCompletionFuncsForTargetFlags(cmd, f, o.IOStreams, cmd.Flags())

return cmd
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/ssh/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ var _ = Describe("SSH Command", func() {

It("should reject bad options", func() {
o := ssh.NewSSHOptions(streams)
cmd := ssh.NewCmdSSH(&util.FactoryImpl{}, o)
cmd := ssh.NewCmdSSH(util.NewFactoryImpl(), o)

Expect(cmd.RunE(cmd, nil)).NotTo(Succeed())
})
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/sshpatch/sshpatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"

"github.com/spf13/cobra"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/gardener/gardenctl-v2/internal/util"
"github.com/gardener/gardenctl-v2/pkg/cmd/base"
Expand Down Expand Up @@ -50,9 +49,7 @@ gardenctl ssh-patch cli-xxxxxxxx`,

ssh.RegisterCompletionFuncsForAccessConfigFlags(cmd, f, o.IOStreams, cmd.Flags())

manager, err := f.Manager()
utilruntime.Must(err)
manager.TargetFlags().AddFlags(cmd.Flags())
f.TargetFlags().AddFlags(cmd.Flags())
flags.RegisterCompletionFuncsForTargetFlags(cmd, f, o.IOStreams, cmd.Flags())

return cmd
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/sshpatch/sshpatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,23 @@ var _ = Describe("SSH Patch Command", func() {
ctrl = gomock.NewController(GinkgoT())
gardenClient = gcmocks.NewMockClient(ctrl)

targetFlags := target.NewTargetFlags("", "", "", "", false)

manager = targetmocks.NewMockManager(ctrl)
manager.EXPECT().ClientConfig(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, _ target.Target) (clientcmd.ClientConfig, error) {
// DoAndReturn allows us to modify the apiConfig within the testcase
clientcmdConfig := clientcmd.NewDefaultClientConfig(*apiConfig, nil)
return clientcmdConfig, nil
}).AnyTimes()
manager.EXPECT().CurrentTarget().Return(currentTarget, nil).AnyTimes()
manager.EXPECT().TargetFlags().Return(target.NewTargetFlags("", "", "", "", false)).AnyTimes()
manager.EXPECT().GardenClient(gomock.Eq(gardenName)).Return(gardenClient, nil).AnyTimes()

ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
clock = utilmocks.NewMockClock(ctrl)

factory = utilmocks.NewMockFactory(ctrl)
factory.EXPECT().Manager().Return(manager, nil).AnyTimes()
factory.EXPECT().TargetFlags().Return(targetFlags).AnyTimes()
factory.EXPECT().Context().Return(ctx).AnyTimes()
factory.EXPECT().Clock().Return(clock).AnyTimes()
fakeIPs := []string{"192.0.2.42", "2001:db8::8a2e:370:7334"}
Expand Down

0 comments on commit 0b63294

Please sign in to comment.