-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathsuite_test.go
More file actions
172 lines (155 loc) · 6.33 KB
/
Copy pathsuite_test.go
File metadata and controls
172 lines (155 loc) · 6.33 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package options_test
import (
"context"
"flag"
"os"
"testing"
"time"
"github.com/samber/lo"
coreoptions "sigs.k8s.io/karpenter/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/test"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "sigs.k8s.io/karpenter/pkg/utils/testing"
)
var ctx context.Context
func TestAPIs(t *testing.T) {
ctx = TestContextWithLogger(t)
RegisterFailHandler(Fail)
RunSpecs(t, "Options")
}
var _ = Describe("Options", func() {
var fs *coreoptions.FlagSet
var opts *options.Options
BeforeEach(func() {
fs = &coreoptions.FlagSet{
FlagSet: flag.NewFlagSet("karpenter", flag.ContinueOnError),
}
opts = &options.Options{}
})
AfterEach(func() {
os.Clearenv()
})
It("should correctly override default vars when CLI flags are set", func() {
opts.AddFlags(fs)
err := opts.Parse(fs,
"--cluster-ca-bundle", "env-bundle",
"--cluster-name", "env-cluster",
"--cluster-endpoint", "https://env-cluster",
"--isolated-vpc",
"--vm-memory-overhead-percent", "0.1",
"--interruption-queue", "env-cluster",
"--reserved-enis", "10",
"--disable-dry-run",
"--subnet-refresh-interval", "15m",
"--security-group-refresh-interval", "15m")
Expect(err).ToNot(HaveOccurred())
expectOptionsEqual(opts, test.Options(test.OptionsFields{
ClusterCABundle: lo.ToPtr("env-bundle"),
ClusterName: lo.ToPtr("env-cluster"),
ClusterEndpoint: lo.ToPtr("https://env-cluster"),
IsolatedVPC: lo.ToPtr(true),
VMMemoryOverheadPercent: lo.ToPtr[float64](0.1),
InterruptionQueue: lo.ToPtr("env-cluster"),
ReservedENIs: lo.ToPtr(10),
DisableDryRun: lo.ToPtr(true),
SubnetRefreshInterval: lo.ToPtr(15 * time.Minute),
SecurityGroupRefreshInterval: lo.ToPtr(15 * time.Minute),
}))
})
It("should correctly fallback to env vars when CLI flags aren't set", func() {
os.Setenv("CLUSTER_CA_BUNDLE", "env-bundle")
os.Setenv("CLUSTER_NAME", "env-cluster")
os.Setenv("CLUSTER_ENDPOINT", "https://env-cluster")
os.Setenv("ISOLATED_VPC", "true")
os.Setenv("VM_MEMORY_OVERHEAD_PERCENT", "0.1")
os.Setenv("INTERRUPTION_QUEUE", "env-cluster")
os.Setenv("RESERVED_ENIS", "10")
os.Setenv("DISABLE_DRY_RUN", "false")
os.Setenv("SUBNET_REFRESH_INTERVAL", "15m")
os.Setenv("SECURITY_GROUP_REFRESH_INTERVAL", "15m")
// Add flags after we set the environment variables so that the parsing logic correctly refers
// to the new environment variable values
opts.AddFlags(fs)
err := opts.Parse(fs)
Expect(err).ToNot(HaveOccurred())
expectOptionsEqual(opts, test.Options(test.OptionsFields{
ClusterCABundle: lo.ToPtr("env-bundle"),
ClusterName: lo.ToPtr("env-cluster"),
ClusterEndpoint: lo.ToPtr("https://env-cluster"),
IsolatedVPC: lo.ToPtr(true),
VMMemoryOverheadPercent: lo.ToPtr[float64](0.1),
InterruptionQueue: lo.ToPtr("env-cluster"),
ReservedENIs: lo.ToPtr(10),
DisableDryRun: lo.ToPtr(false),
SubnetRefreshInterval: lo.ToPtr(15 * time.Minute),
SecurityGroupRefreshInterval: lo.ToPtr(15 * time.Minute),
}))
})
It("should correctly use default security-group-refresh-interval when not specified", func() {
opts.AddFlags(fs)
err := opts.Parse(fs, "--cluster-name", "test-cluster")
Expect(err).ToNot(HaveOccurred())
Expect(opts.SecurityGroupRefreshInterval).To(Equal(time.Minute))
})
It("should correctly use default subnet-refresh-interval when not specified", func() {
opts.AddFlags(fs)
err := opts.Parse(fs, "--cluster-name", "test-cluster")
Expect(err).ToNot(HaveOccurred())
Expect(opts.SubnetRefreshInterval).To(Equal(time.Minute))
})
Context("Validation", func() {
BeforeEach(func() {
opts.AddFlags(fs)
})
It("should fail when cluster name is not set", func() {
err := opts.Parse(fs)
Expect(err).To(HaveOccurred())
})
It("should fail when clusterEndpoint is invalid (not absolute)", func() {
err := opts.Parse(fs, "--cluster-name", "test-cluster", "--cluster-endpoint", "00000000000000000000000.gr7.us-west-2.eks.amazonaws.com")
Expect(err).To(HaveOccurred())
})
It("should fail when vmMemoryOverheadPercent is negative", func() {
err := opts.Parse(fs, "--cluster-name", "test-cluster", "--vm-memory-overhead-percent", "-0.01")
Expect(err).To(HaveOccurred())
})
It("should fail when reservedENIs is negative", func() {
err := opts.Parse(fs, "--cluster-name", "test-cluster", "--reserved-enis", "-1")
Expect(err).To(HaveOccurred())
})
It("should fail when subnet-refresh-interval is less than 1m", func() {
err := opts.Parse(fs, "--cluster-name", "test-cluster", "--subnet-refresh-interval", "30s")
Expect(err).To(HaveOccurred())
})
It("should fail when security-group-refresh-interval is less than 1m", func() {
err := opts.Parse(fs, "--cluster-name", "test-cluster", "--security-group-refresh-interval", "30s")
Expect(err).To(HaveOccurred())
})
})
})
func expectOptionsEqual(optsA *options.Options, optsB *options.Options) {
GinkgoHelper()
Expect(optsA.ClusterCABundle).To(Equal(optsB.ClusterCABundle))
Expect(optsA.ClusterName).To(Equal(optsB.ClusterName))
Expect(optsA.ClusterEndpoint).To(Equal(optsB.ClusterEndpoint))
Expect(optsA.IsolatedVPC).To(Equal(optsB.IsolatedVPC))
Expect(optsA.VMMemoryOverheadPercent).To(Equal(optsB.VMMemoryOverheadPercent))
Expect(optsA.InterruptionQueue).To(Equal(optsB.InterruptionQueue))
Expect(optsA.ReservedENIs).To(Equal(optsB.ReservedENIs))
Expect(optsA.DisableDryRun).To(Equal(optsB.DisableDryRun))
Expect(optsA.SubnetRefreshInterval).To(Equal(optsB.SubnetRefreshInterval))
Expect(optsA.SecurityGroupRefreshInterval).To(Equal(optsB.SecurityGroupRefreshInterval))
}