Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hotfix-2.2] Fix help command not working when session id envs not set #220

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("GCTL_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