-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
fargate_test.go
98 lines (87 loc) · 3.64 KB
/
fargate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package delete
import (
"bytes"
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
"github.com/weaveworks/eksctl/pkg/fargate"
)
var _ = Describe("delete", func() {
Describe("delete fargateprofile", func() {
It("requires the cluster's name, and if missing, prints an error and the usage", func() {
cmd := newMockDeleteFargateProfileCmd("fargateprofile")
out, err := cmd.execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Error: --cluster must be set"))
Expect(out).To(ContainSubstring("Usage:"))
})
It("requires a profile name, and if missing, prints an error and the usage", func() {
cmd := newMockDeleteFargateProfileCmd("fargateprofile", "--cluster", "foo")
out, err := cmd.execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Error: invalid Fargate profile: empty name"))
Expect(out).To(ContainSubstring("Usage:"))
})
It("requires a profile name, which can be provided as an argument", func() {
cmd := newMockDeleteFargateProfileCmd("fargateprofile", "--cluster", "foo", "fp-default")
_, err := cmd.execute()
Expect(err).To(Not(HaveOccurred()))
Expect(cmd.cmd.ClusterConfig.Metadata.Name).To(Equal("foo"))
Expect(cmd.options.ProfileName).To(Equal("fp-default"))
})
It("requires a profile name, which can be provided via the --name flag", func() {
cmd := newMockDeleteFargateProfileCmd("fargateprofile", "--cluster", "foo", "--name", "fp-default")
_, err := cmd.execute()
Expect(err).To(Not(HaveOccurred()))
Expect(cmd.cmd.ClusterConfig.Metadata.Name).To(Equal("foo"))
Expect(cmd.options.ProfileName).To(Equal("fp-default"))
})
It("supports the cluster name to be provided by a ClusterConfig file, but still requires a profile name, and if missing, prints an error and the usage", func() {
cmd := newMockDeleteFargateProfileCmd("fargateprofile", "-f", "../../../examples/01-simple-cluster.yaml")
out, err := cmd.execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Error: invalid Fargate profile: empty name"))
Expect(out).To(ContainSubstring("Usage:"))
})
It("supports the cluster name to be provided by a ClusterConfig file, and requires a profile name provided via the --name flag", func() {
cmd := newMockDeleteFargateProfileCmd("fargateprofile", "-f", "../../../examples/01-simple-cluster.yaml", "--name", "fp-default")
_, err := cmd.execute()
Expect(err).To(Not(HaveOccurred()))
Expect(cmd.cmd.ClusterConfig.Metadata.Name).To(Equal("cluster-1"))
Expect(cmd.options.ProfileName).To(Equal("fp-default"))
})
})
})
func newMockDeleteFargateProfileCmd(args ...string) *mockDeleteFargateProfileCmd {
mockCmd := &mockDeleteFargateProfileCmd{}
grouping := cmdutils.NewGrouping()
parentCmd := cmdutils.NewVerbCmd("delete", "", "")
cmdutils.AddResourceCmd(grouping, parentCmd, func(cmd *cmdutils.Cmd) {
deleteFargateProfileWithRunFunc(cmd, func(cmd *cmdutils.Cmd, options *fargate.Options) error {
mockCmd.cmd = cmd
mockCmd.options = options
return nil // no-op, to only test input aggregation & validation.
})
})
parentCmd.SetArgs(args)
mockCmd.parentCmd = parentCmd
return mockCmd
}
type mockDeleteFargateProfileCmd struct {
parentCmd *cobra.Command
cmd *cmdutils.Cmd
options *fargate.Options
}
func (c mockDeleteFargateProfileCmd) execute() (string, error) {
outBuf := new(bytes.Buffer)
errBuf := new(bytes.Buffer)
c.parentCmd.SetOut(outBuf)
c.parentCmd.SetErr(errBuf)
err := c.parentCmd.Execute()
if err != nil {
err = errors.New(errBuf.String())
}
return outBuf.String(), err
}