From 661977966f816986b229b1c58cebce200b630ff6 Mon Sep 17 00:00:00 2001 From: Peter Sutter Date: Fri, 5 May 2023 09:59:42 +0200 Subject: [PATCH 1/3] Add target flags tests for gtv --- pkg/cmd/target/view_test.go | 51 ++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/target/view_test.go b/pkg/cmd/target/view_test.go index f9126535..7d9de75d 100644 --- a/pkg/cmd/target/view_test.go +++ b/pkg/cmd/target/view_test.go @@ -7,14 +7,19 @@ package target_test import ( "fmt" + "os" + "path/filepath" + "github.com/golang/mock/gomock" + "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - internalfake "github.com/gardener/gardenctl-v2/internal/fake" "github.com/gardener/gardenctl-v2/internal/util" + utilmocks "github.com/gardener/gardenctl-v2/internal/util/mocks" cmdtarget "github.com/gardener/gardenctl-v2/pkg/cmd/target" "github.com/gardener/gardenctl-v2/pkg/target" + targetmocks "github.com/gardener/gardenctl-v2/pkg/target/mocks" ) var _ = Describe("Target View Command", func() { @@ -27,19 +32,41 @@ var _ = Describe("Target View Command", func() { var ( streams util.IOStreams out *util.SafeBytesBuffer - factory *internalfake.Factory - targetProvider *internalfake.TargetProvider + ctrl *gomock.Controller + factory *utilmocks.MockFactory + manager *targetmocks.MockManager + targetProvider target.TargetProvider currentTarget target.Target + sessionDir string ) BeforeEach(func() { + ctrl = gomock.NewController(GinkgoT()) + factory = utilmocks.NewMockFactory(ctrl) + manager = targetmocks.NewMockManager(ctrl) + + factory.EXPECT().Manager().Return(manager, nil) + streams, _, out, _ = util.NewTestIOStreams() + + targetFlags := target.NewTargetFlags("", "", "", "", false) + factory.EXPECT().TargetFlags().Return(targetFlags).AnyTimes() + + sessionID := uuid.New().String() + sessionDir = filepath.Join(os.TempDir(), "garden", sessionID) + Expect(os.MkdirAll(sessionDir, os.ModePerm)) currentTarget = target.NewTarget(gardenName, projectName, "", shootName) + targetProvider = target.NewTargetProvider(filepath.Join(sessionDir, "target.yaml"), targetFlags) + Expect(targetProvider.Write(currentTarget)).To(Succeed()) + + manager.EXPECT().CurrentTarget().DoAndReturn(func() (target.Target, error) { + return targetProvider.Read() + }) }) - JustBeforeEach(func() { - targetProvider = internalfake.NewFakeTargetProvider(currentTarget) - factory = internalfake.NewFakeFactory(nil, nil, nil, targetProvider) + AfterEach(func() { + Expect(os.RemoveAll(sessionDir)).To(Succeed()) + ctrl.Finish() }) It("should print current target information", func() { @@ -48,6 +75,18 @@ var _ = Describe("Target View Command", func() { Expect(cmd.RunE(cmd, nil)).To(Succeed()) Expect(out.String()).To(Equal(fmt.Sprintf("garden:\"%s\", project:\"%s\", shoot:\"%s\"", gardenName, projectName, shootName))) }) + + Context("when target flags given", func() { + It("should print current target information", func() { + cmd := cmdtarget.NewCmdView(factory, streams) + cmd.SetArgs([]string{"--shoot", "myshoot2"}) + Expect(cmd.Execute()).To(Succeed()) + Expect(cmd.Flag("shoot").Value.String()).To(Equal("myshoot2")) + + // here we need the real manager + Expect(out.String()).To(Equal(fmt.Sprintf("garden:\"%s\", project:\"%s\", shoot:\"%s\"", gardenName, projectName, "myshoot2"))) + }) + }) }) var _ = Describe("Target View Options", func() { From 184fa62170e361e1508cbc05b8a7d153b1e4cbe1 Mon Sep 17 00:00:00 2001 From: Peter Sutter Date: Fri, 5 May 2023 10:02:19 +0200 Subject: [PATCH 2/3] Add target flags to gtv --- pkg/cmd/target/view.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/cmd/target/view.go b/pkg/cmd/target/view.go index 8a1d9850..0e343885 100644 --- a/pkg/cmd/target/view.go +++ b/pkg/cmd/target/view.go @@ -12,6 +12,7 @@ import ( "github.com/gardener/gardenctl-v2/internal/util" "github.com/gardener/gardenctl-v2/pkg/cmd/base" + "github.com/gardener/gardenctl-v2/pkg/flags" ) // NewCmdView returns a new target view command. @@ -35,6 +36,9 @@ func NewCmdView(f util.Factory, ioStreams util.IOStreams) *cobra.Command { o.AddFlags(cmd.Flags()) + f.TargetFlags().AddFlags(cmd.Flags()) + flags.RegisterCompletionFuncsForTargetFlags(cmd, f, ioStreams, cmd.Flags()) + return cmd } From 0e2cd38c15e3e76ab5e4b640d838424b633e8bab Mon Sep 17 00:00:00 2001 From: Peter Sutter Date: Fri, 5 May 2023 10:02:36 +0200 Subject: [PATCH 3/3] make gen-markdown --- docs/help/gardenctl_target_view.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/help/gardenctl_target_view.md b/docs/help/gardenctl_target_view.md index a71ff642..14bd94df 100644 --- a/docs/help/gardenctl_target_view.md +++ b/docs/help/gardenctl_target_view.md @@ -9,8 +9,13 @@ gardenctl target view [flags] ### Options ``` - -h, --help help for view - -o, --output string One of 'yaml' or 'json'. + --control-plane target control plane of shoot, use together with shoot argument + --garden string target the given garden cluster + -h, --help help for view + -o, --output string One of 'yaml' or 'json'. + --project string target the given project + --seed string target the given seed cluster + --shoot string target the given shoot cluster ``` ### Options inherited from parent commands