-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
fargate_test.go
144 lines (132 loc) · 5.77 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package create
import (
"bytes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils"
)
var _ = Describe("create", func() {
Describe("create fargateprofile", func() {
It("requires the cluster's name, and if missing, prints an error and the usage", func() {
cmd := newMockCreateFargateProfileCmd("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 Kubernetes namespace to be provided, and if missing, prints an error and the usage", func() {
cmd := newMockCreateFargateProfileCmd("fargateprofile", "--cluster", "foo")
out, err := cmd.execute()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Error: invalid Fargate profile: empty selector namespace"))
Expect(out).To(ContainSubstring("Usage:"))
})
It("requires at least a Kubernetes namespace to be provided, in which case it generates the profile name", func() {
cmd := newMockCreateFargateProfileCmd("fargateprofile", "--cluster", "foo", "--namespace", "default")
_, err := cmd.execute()
Expect(err).To(Not(HaveOccurred()))
Expect(cmd.cmd.ClusterConfig.Metadata.Name).To(Equal("foo"))
profiles := cmd.cmd.ClusterConfig.FargateProfiles
Expect(profiles).To(HaveLen(1))
profile := profiles[0]
Expect(profile.Name).To(MatchRegexp("fp-[abcdef0123456789]{8}"))
Expect(profile.Selectors).To(HaveLen(1))
selector := profile.Selectors[0]
Expect(selector.Namespace).To(Equal("default"))
})
It("the profile name can be provided as an argument", func() {
cmd := newMockCreateFargateProfileCmd("fargateprofile", "--cluster", "foo", "--namespace", "default", "fp-default")
_, err := cmd.execute()
Expect(err).To(Not(HaveOccurred()))
Expect(cmd.cmd.ClusterConfig.Metadata.Name).To(Equal("foo"))
profiles := cmd.cmd.ClusterConfig.FargateProfiles
Expect(profiles).To(HaveLen(1))
profile := profiles[0]
Expect(profile.Name).To(Equal("fp-default"))
Expect(profile.Selectors).To(HaveLen(1))
selector := profile.Selectors[0]
Expect(selector.Namespace).To(Equal("default"))
})
It("the profile name can be provided via the --name flag", func() {
cmd := newMockCreateFargateProfileCmd("fargateprofile", "--cluster", "foo", "--namespace", "default", "--name", "fp-default")
_, err := cmd.execute()
Expect(err).To(Not(HaveOccurred()))
Expect(cmd.cmd.ClusterConfig.Metadata.Name).To(Equal("foo"))
profiles := cmd.cmd.ClusterConfig.FargateProfiles
Expect(profiles).To(HaveLen(1))
profile := profiles[0]
Expect(profile.Name).To(Equal("fp-default"))
Expect(profile.Selectors).To(HaveLen(1))
selector := profile.Selectors[0]
Expect(selector.Namespace).To(Equal("default"))
})
It("the fargate profile with tags", func() {
cmd := newMockCreateFargateProfileCmd("fargateprofile", "--cluster", "foo", "--tags", "env=dev,name=fp-default", "--namespace", "default", "fp-default")
_, err := cmd.execute()
Expect(err).To(Not(HaveOccurred()))
Expect(cmd.cmd.ClusterConfig.Metadata.Name).To(Equal("foo"))
profiles := cmd.cmd.ClusterConfig.FargateProfiles
Expect(profiles).To(HaveLen(1))
profile := profiles[0]
Expect(profile.Name).To(Equal("fp-default"))
Expect(profile.Selectors).To(HaveLen(1))
selector := profile.Selectors[0]
Expect(selector.Namespace).To(Equal("default"))
Expect(profile.Tags).To(HaveKeyWithValue("env", "dev"))
Expect(profile.Tags).To(HaveKeyWithValue("name", "fp-default"))
})
It("supports all arguments to be provided by a ClusterConfig file", func() {
cmd := newMockCreateFargateProfileCmd("fargateprofile", "-f", "../../../examples/16-fargate-profile.yaml")
_, err := cmd.execute()
Expect(err).To(Not(HaveOccurred()))
Expect(cmd.cmd.ClusterConfig.Metadata.Name).To(Equal("cluster-16"))
profiles := cmd.cmd.ClusterConfig.FargateProfiles
Expect(profiles).To(HaveLen(2))
Expect(profiles[0].Name).To(Equal("fp-default"))
Expect(profiles[0].Selectors).To(HaveLen(2))
Expect(profiles[0].Selectors[0].Namespace).To(Equal("default"))
Expect(profiles[0].Selectors[0].Labels).To(BeEmpty())
Expect(profiles[0].Selectors[1].Namespace).To(Equal("kube-system"))
Expect(profiles[0].Selectors[1].Labels).To(BeEmpty())
Expect(profiles[1].Name).To(Equal("fp-dev"))
Expect(profiles[1].Selectors).To(HaveLen(1))
Expect(profiles[1].Selectors[0].Namespace).To(Equal("dev"))
Expect(profiles[1].Selectors[0].Labels).To(HaveLen(2))
Expect(profiles[1].Selectors[0].Labels).To(HaveKeyWithValue("env", "dev"))
Expect(profiles[1].Selectors[0].Labels).To(HaveKeyWithValue("checks", "passed"))
Expect(profiles[1].Tags).To(HaveKeyWithValue("env", "dev"))
Expect(profiles[1].Tags).To(HaveKeyWithValue("name", "fp-dev"))
})
})
})
func newMockCreateFargateProfileCmd(args ...string) *mockCreateFargateProfileCmd {
mockCmd := &mockCreateFargateProfileCmd{}
grouping := cmdutils.NewGrouping()
parentCmd := cmdutils.NewVerbCmd("create", "", "")
cmdutils.AddResourceCmd(grouping, parentCmd, func(cmd *cmdutils.Cmd) {
createFargateProfileWithRunFunc(cmd, func(cmd *cmdutils.Cmd) error {
mockCmd.cmd = cmd
return nil // no-op, to only test input aggregation & validation.
})
})
parentCmd.SetArgs(args)
mockCmd.parentCmd = parentCmd
return mockCmd
}
type mockCreateFargateProfileCmd struct {
parentCmd *cobra.Command
cmd *cmdutils.Cmd
}
func (c mockCreateFargateProfileCmd) 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
}