forked from aws/aws-node-termination-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
298 lines (277 loc) · 11.1 KB
/
config_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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2016-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 config_test
import (
"bytes"
"flag"
"os"
"testing"
"github.com/aws/aws-node-termination-handler/pkg/config"
h "github.com/aws/aws-node-termination-handler/pkg/test"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func resetFlagsForTest() {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
os.Args = []string{"cmd"}
}
func TestParseCliArgsEnvSuccess(t *testing.T) {
t.Setenv("USE_PROVIDER_ID", "true")
t.Setenv("DELETE_LOCAL_DATA", "false")
t.Setenv("DRY_RUN", "true")
t.Setenv("ENABLE_SCHEDULED_EVENT_DRAINING", "true")
t.Setenv("ENABLE_SPOT_INTERRUPTION_DRAINING", "false")
t.Setenv("ENABLE_ASG_LIFECYCLE_DRAINING", "false")
t.Setenv("ENABLE_SQS_TERMINATION_DRAINING", "false")
t.Setenv("ENABLE_REBALANCE_MONITORING", "true")
t.Setenv("ENABLE_REBALANCE_DRAINING", "true")
t.Setenv("GRACE_PERIOD", "12345")
t.Setenv("IGNORE_DAEMON_SETS", "false")
t.Setenv("KUBERNETES_SERVICE_HOST", "KUBERNETES_SERVICE_HOST")
t.Setenv("KUBERNETES_SERVICE_PORT", "KUBERNETES_SERVICE_PORT")
t.Setenv("NODE_NAME", "NODE_NAME")
t.Setenv("NODE_TERMINATION_GRACE_PERIOD", "12345")
t.Setenv("INSTANCE_METADATA_URL", "INSTANCE_METADATA_URL")
t.Setenv("POD_TERMINATION_GRACE_PERIOD", "12345")
t.Setenv("WEBHOOK_URL", "WEBHOOK_URL")
t.Setenv("WEBHOOK_HEADERS", "WEBHOOK_HEADERS")
t.Setenv("WEBHOOK_TEMPLATE", "WEBHOOK_TEMPLATE")
t.Setenv("METADATA_TRIES", "100")
t.Setenv("CORDON_ONLY", "false")
nthConfig, err := config.ParseCliArgs()
h.Ok(t, err)
// Assert all the values were set
h.Equals(t, true, nthConfig.UseProviderId)
h.Equals(t, false, nthConfig.DeleteLocalData)
h.Equals(t, true, nthConfig.DryRun)
h.Equals(t, true, nthConfig.EnableScheduledEventDraining)
h.Equals(t, false, nthConfig.EnableSpotInterruptionDraining)
h.Equals(t, false, nthConfig.EnableASGLifecycleDraining)
h.Equals(t, false, nthConfig.EnableSQSTerminationDraining)
h.Equals(t, true, nthConfig.EnableRebalanceMonitoring)
h.Equals(t, true, nthConfig.EnableRebalanceDraining)
h.Equals(t, false, nthConfig.IgnoreDaemonSets)
h.Equals(t, "KUBERNETES_SERVICE_HOST", nthConfig.KubernetesServiceHost)
h.Equals(t, "KUBERNETES_SERVICE_PORT", nthConfig.KubernetesServicePort)
h.Equals(t, "NODE_NAME", nthConfig.NodeName)
h.Equals(t, 12345, nthConfig.NodeTerminationGracePeriod)
h.Equals(t, "INSTANCE_METADATA_URL", nthConfig.MetadataURL)
h.Equals(t, 12345, nthConfig.PodTerminationGracePeriod)
h.Equals(t, "WEBHOOK_URL", nthConfig.WebhookURL)
h.Equals(t, "WEBHOOK_HEADERS", nthConfig.WebhookHeaders)
h.Equals(t, "WEBHOOK_TEMPLATE", nthConfig.WebhookTemplate)
h.Equals(t, 100, nthConfig.MetadataTries)
h.Equals(t, false, nthConfig.CordonOnly)
// Check that env vars were set
value, ok := os.LookupEnv("KUBERNETES_SERVICE_HOST")
h.Equals(t, true, ok)
h.Equals(t, "KUBERNETES_SERVICE_HOST", value)
value, ok = os.LookupEnv("KUBERNETES_SERVICE_PORT")
h.Equals(t, true, ok)
h.Equals(t, "KUBERNETES_SERVICE_PORT", value)
}
func TestParseCliArgsSuccess(t *testing.T) {
resetFlagsForTest()
os.Args = []string{
"cmd",
"--use-provider-id=true",
"--delete-local-data=false",
"--dry-run=true",
"--enable-scheduled-event-draining=true",
"--enable-spot-interruption-draining=false",
"--enable-asg-lifecycle-draining=false",
"--enable-sqs-termination-draining=false",
"--enable-rebalance-monitoring=true",
"--enable-rebalance-draining=true",
"--ignore-daemon-sets=false",
"--kubernetes-service-host=KUBERNETES_SERVICE_HOST",
"--kubernetes-service-port=KUBERNETES_SERVICE_PORT",
"--node-name=NODE_NAME",
"--node-termination-grace-period=12345",
"--metadata-url=INSTANCE_METADATA_URL",
"--pod-termination-grace-period=12345",
"--webhook-url=WEBHOOK_URL",
"--webhook-headers=WEBHOOK_HEADERS",
"--webhook-template=WEBHOOK_TEMPLATE",
"--metadata-tries=100",
"--cordon-only=false",
}
nthConfig, err := config.ParseCliArgs()
h.Ok(t, err)
// Assert all the values were set
h.Equals(t, true, nthConfig.UseProviderId)
h.Equals(t, false, nthConfig.DeleteLocalData)
h.Equals(t, true, nthConfig.DryRun)
h.Equals(t, true, nthConfig.EnableScheduledEventDraining)
h.Equals(t, false, nthConfig.EnableSpotInterruptionDraining)
h.Equals(t, false, nthConfig.EnableASGLifecycleDraining)
h.Equals(t, false, nthConfig.EnableSQSTerminationDraining)
h.Equals(t, true, nthConfig.EnableRebalanceMonitoring)
h.Equals(t, true, nthConfig.EnableRebalanceDraining)
h.Equals(t, false, nthConfig.IgnoreDaemonSets)
h.Equals(t, "KUBERNETES_SERVICE_HOST", nthConfig.KubernetesServiceHost)
h.Equals(t, "KUBERNETES_SERVICE_PORT", nthConfig.KubernetesServicePort)
h.Equals(t, "NODE_NAME", nthConfig.NodeName)
h.Equals(t, 12345, nthConfig.NodeTerminationGracePeriod)
h.Equals(t, "INSTANCE_METADATA_URL", nthConfig.MetadataURL)
h.Equals(t, 12345, nthConfig.PodTerminationGracePeriod)
h.Equals(t, "WEBHOOK_URL", nthConfig.WebhookURL)
h.Equals(t, "WEBHOOK_HEADERS", nthConfig.WebhookHeaders)
h.Equals(t, "WEBHOOK_TEMPLATE", nthConfig.WebhookTemplate)
h.Equals(t, 100, nthConfig.MetadataTries)
h.Equals(t, false, nthConfig.CordonOnly)
h.Equals(t, false, nthConfig.EnablePrometheus)
// Check that env vars were set
value, ok := os.LookupEnv("KUBERNETES_SERVICE_HOST")
h.Equals(t, true, ok)
h.Equals(t, "KUBERNETES_SERVICE_HOST", value)
}
func TestParseCliArgsOverrides(t *testing.T) {
resetFlagsForTest()
t.Setenv("USE_PROVIDER_ID", "true")
t.Setenv("DELETE_LOCAL_DATA", "true")
t.Setenv("DRY_RUN", "false")
t.Setenv("ENABLE_SCHEDULED_EVENT_DRAINING", "false")
t.Setenv("ENABLE_SPOT_INTERRUPTION_DRAINING", "true")
t.Setenv("ENABLE_ASG_LIFECYCLE_DRAINING", "true")
t.Setenv("ENABLE_SQS_TERMINATION_DRAINING", "false")
t.Setenv("ENABLE_REBALANCE_MONITORING", "true")
t.Setenv("ENABLE_REBALANCE_DRAINING", "true")
t.Setenv("GRACE_PERIOD", "99999")
t.Setenv("IGNORE_DAEMON_SETS", "true")
t.Setenv("KUBERNETES_SERVICE_HOST", "no")
t.Setenv("KUBERNETES_SERVICE_PORT", "no")
t.Setenv("NODE_NAME", "no")
t.Setenv("NODE_TERMINATION_GRACE_PERIOD", "99999")
t.Setenv("INSTANCE_METADATA_URL", "no")
t.Setenv("POD_TERMINATION_GRACE_PERIOD", "99999")
t.Setenv("WEBHOOK_URL", "no")
t.Setenv("WEBHOOK_HEADERS", "no")
t.Setenv("WEBHOOK_TEMPLATE", "no")
t.Setenv("METADATA_TRIES", "100")
t.Setenv("CORDON_ONLY", "true")
os.Args = []string{
"cmd",
"--use-provider-id=false",
"--delete-local-data=false",
"--dry-run=true",
"--enable-scheduled-event-draining=true",
"--enable-spot-interruption-draining=false",
"--enable-asg-lifecycle-draining=false",
"--enable-sqs-termination-draining=true",
"--enable-rebalance-monitoring=false",
"--enable-rebalance-draining=false",
"--ignore-daemon-sets=false",
"--kubernetes-service-host=KUBERNETES_SERVICE_HOST",
"--kubernetes-service-port=KUBERNETES_SERVICE_PORT",
"--node-name=NODE_NAME",
"--node-termination-grace-period=12345",
"--metadata-url=INSTANCE_METADATA_URL",
"--pod-termination-grace-period=12345",
"--webhook-url=WEBHOOK_URL",
"--webhook-headers=WEBHOOK_HEADERS",
"--webhook-template=WEBHOOK_TEMPLATE",
"--metadata-tries=101",
"--cordon-only=false",
"--enable-prometheus-server=true",
"--prometheus-server-port=2112",
}
nthConfig, err := config.ParseCliArgs()
h.Ok(t, err)
// Assert all the values were set
h.Equals(t, false, nthConfig.UseProviderId)
h.Equals(t, false, nthConfig.DeleteLocalData)
h.Equals(t, true, nthConfig.DryRun)
h.Equals(t, true, nthConfig.EnableScheduledEventDraining)
h.Equals(t, false, nthConfig.EnableSpotInterruptionDraining)
h.Equals(t, false, nthConfig.EnableASGLifecycleDraining)
h.Equals(t, true, nthConfig.EnableSQSTerminationDraining)
h.Equals(t, false, nthConfig.EnableRebalanceMonitoring)
h.Equals(t, false, nthConfig.EnableRebalanceDraining)
h.Equals(t, false, nthConfig.IgnoreDaemonSets)
h.Equals(t, "KUBERNETES_SERVICE_HOST", nthConfig.KubernetesServiceHost)
h.Equals(t, "KUBERNETES_SERVICE_PORT", nthConfig.KubernetesServicePort)
h.Equals(t, "NODE_NAME", nthConfig.NodeName)
h.Equals(t, 12345, nthConfig.NodeTerminationGracePeriod)
h.Equals(t, "INSTANCE_METADATA_URL", nthConfig.MetadataURL)
h.Equals(t, 12345, nthConfig.PodTerminationGracePeriod)
h.Equals(t, "WEBHOOK_URL", nthConfig.WebhookURL)
h.Equals(t, "WEBHOOK_HEADERS", nthConfig.WebhookHeaders)
h.Equals(t, "WEBHOOK_TEMPLATE", nthConfig.WebhookTemplate)
h.Equals(t, 101, nthConfig.MetadataTries)
h.Equals(t, false, nthConfig.CordonOnly)
h.Equals(t, true, nthConfig.EnablePrometheus)
h.Equals(t, 2112, nthConfig.PrometheusPort)
// Check that env vars were set
value, ok := os.LookupEnv("KUBERNETES_SERVICE_HOST")
h.Equals(t, true, ok)
h.Equals(t, "KUBERNETES_SERVICE_HOST", value)
}
func TestParseCliArgsWithGracePeriodSuccess(t *testing.T) {
resetFlagsForTest()
t.Setenv("POD_TERMINATION_GRACE_PERIOD", "")
t.Setenv("NODE_NAME", "bla")
t.Setenv("GRACE_PERIOD", "12")
nthConfig, err := config.ParseCliArgs()
h.Ok(t, err)
h.Equals(t, 12, nthConfig.PodTerminationGracePeriod)
}
func TestParseCliArgsMissingNodeNameFailure(t *testing.T) {
resetFlagsForTest()
t.Setenv("NODE_NAME", "")
_, err := config.ParseCliArgs()
h.Assert(t, err != nil, "Failed to return error when node-name not provided")
}
func TestParseCliArgsCreateFlagsFailure(t *testing.T) {
resetFlagsForTest()
t.Setenv("DELETE_LOCAL_DATA", "something not true or false")
_, err := config.ParseCliArgs()
h.Assert(t, err != nil, "Failed to return error when creating flags")
}
func TestParseCliArgsAWSSession(t *testing.T) {
resetFlagsForTest()
t.Setenv("ENABLE_SQS_TERMINATION_DRAINING", "true")
t.Setenv("AWS_REGION", "us-weast-1")
t.Setenv("NODE_NAME", "node")
nthConfig, err := config.ParseCliArgs()
h.Ok(t, err)
h.Assert(t, nthConfig.AWSRegion == "us-weast-1", "Should find region as us-weast-1")
}
func TestPrint_Human(t *testing.T) {
resetFlagsForTest()
t.Setenv("NODE_NAME", "node")
t.Setenv("JSON_LOGGING", "false")
nthConfig, err := config.ParseCliArgs()
h.Ok(t, err)
var printBuf bytes.Buffer
log.Logger = log.Output(zerolog.ConsoleWriter{Out: &printBuf})
nthConfig.Print()
var humanBuf bytes.Buffer
log.Logger = log.Output(zerolog.ConsoleWriter{Out: &humanBuf})
nthConfig.PrintHumanConfigArgs()
h.Assert(t, humanBuf.String() == printBuf.String(), "Should have printed non-JSON formatted config values")
}
func TestPrint_JSON(t *testing.T) {
resetFlagsForTest()
t.Setenv("NODE_NAME", "node")
t.Setenv("JSON_LOGGING", "true")
nthConfig, err := config.ParseCliArgs()
h.Ok(t, err)
var printBuf bytes.Buffer
log.Logger = log.Output(zerolog.ConsoleWriter{Out: &printBuf})
nthConfig.Print()
var jsonBuf bytes.Buffer
log.Logger = log.Output(zerolog.ConsoleWriter{Out: &jsonBuf})
nthConfig.PrintJsonConfigArgs()
h.Assert(t, jsonBuf.String() == printBuf.String(), "Should have printed JSON formatted config values")
}