Skip to content

Commit

Permalink
Add target flags to target view command (#289)
Browse files Browse the repository at this point in the history
* Add target flags tests for gtv

* Add target flags to gtv

* make gen-markdown
  • Loading branch information
petersutter authored May 11, 2023
1 parent 6d5f195 commit 291dfaa
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
9 changes: 7 additions & 2 deletions docs/help/gardenctl_target_view.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/target/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down
51 changes: 45 additions & 6 deletions pkg/cmd/target/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 291dfaa

Please sign in to comment.