-
Notifications
You must be signed in to change notification settings - Fork 6
/
args_test.go
367 lines (325 loc) · 9.38 KB
/
args_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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2015 PagerDuty, Inc., et al.
// Copyright 2016-2017 Tim Heckman
// Use of this source code is governed by the BSD 3-Clause
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"runtime"
"github.com/tideland/golib/logger"
. "gopkg.in/check.v1"
)
func (t *TestSuite) Test_binArgs_parse(c *C) {
var output string
var err error
const Arg0 = "/usr/loca/bin/cronner"
//
// assert that label is required and is validated
//
args := &binArgs{}
cli := []string{Arg0}
output, err = args.parse(cli)
c.Assert(err, Not(IsNil))
c.Check(len(output), Equals, 0)
c.Check(err.Error(), Equals, "cron label '' is invalid, it can only be alphanumeric with underscores, periods, and spaces")
args = &binArgs{}
cli = []string{
Arg0,
"-l", "invalid^label",
}
output, err = args.parse(cli)
c.Assert(err, Not(IsNil))
c.Check(len(output), Equals, 0)
c.Check(err.Error(), Equals, "cron label 'invalid^label' is invalid, it can only be alphanumeric with underscores, periods, and spaces")
//
// assert that a command is required
//
args = &binArgs{}
cli = []string{
Arg0,
"-l", "test",
}
output, err = args.parse(cli)
c.Assert(err, Not(IsNil))
c.Check(len(output), Equals, 0)
c.Check(err.Error(), Equals, "you must specify a command to run either using by adding it to the end, or using the command flag")
//
// assert that version (-v/--version) printing works
//
args = &binArgs{}
cli = []string{
Arg0,
"-V",
}
verOut := fmt.Sprintf("cronner v%s built with %s\nCopyright 2015 PagerDuty, Inc.\nCopyright 2016-2017 Tim Heckman\nReleased under the BSD 3-Clause License\n", Version, runtime.Version())
output, err = args.parse(cli)
c.Assert(err, IsNil)
c.Check(output, Equals, verOut)
c.Check(args.Version, Equals, true)
args = &binArgs{}
cli = []string{
Arg0,
"--version",
}
output, err = args.parse(cli)
c.Assert(err, IsNil)
c.Check(output, Equals, verOut)
c.Check(args.Version, Equals, true)
//
// assert the default values
//
args = &binArgs{}
cli = []string{
Arg0,
"-l", "test",
"--", "/bin/true",
}
output, err = args.parse(cli)
c.Assert(err, IsNil)
c.Check(len(output), Equals, 0)
c.Check(args.LockDir, Equals, "/var/lock")
c.Check(args.AllEvents, Equals, false)
c.Check(args.FailEvent, Equals, false)
c.Check(args.LogFail, Equals, false)
c.Check(args.EventGroup, Equals, "")
c.Check(args.Group, Equals, "")
c.Check(args.Lock, Equals, false)
c.Check(args.LogPath, Equals, "/var/log/cronner")
c.Check(args.LogLevel, Equals, "error")
c.Check(args.Namespace, Equals, "cronner")
c.Check(args.Parent, Equals, false)
c.Check(args.Passthru, Equals, false)
c.Check(args.Sensitive, Equals, false)
c.Check(args.Tags, HasLen, 0)
c.Check(args.Version, Equals, false)
c.Check(args.WarnAfter, Equals, uint64(0))
c.Check(args.WaitSeconds, Equals, uint64(0))
//
// assert that the short flags work
//
args = &binArgs{}
cli = []string{
Arg0,
"-d", "/var/testlock",
"-e",
"-E",
"-F",
"-G", "test_group",
"-g", "metric_group",
"-H", "test_host",
"-k",
"-l", "test",
"-L", "info",
"-N", "testcronner",
"-P",
"-p",
"-s",
"-t", "tag1",
"-t", "tag2",
"-w", "42",
"-W", "84",
"--", "/bin/true",
}
output, err = args.parse(cli)
c.Assert(err, IsNil)
// because we're parsing args we've just overridden this in the parser
// so set it back to the value from SetUpSuite()
logger.SetLevel(logger.LevelFatal)
c.Check(len(output), Equals, 0)
c.Check(args.LockDir, Equals, "/var/testlock")
c.Check(args.AllEvents, Equals, true)
c.Check(args.FailEvent, Equals, true)
c.Check(args.LogFail, Equals, true)
c.Check(args.EventGroup, Equals, "test_group")
c.Check(args.Group, Equals, "metric_group")
c.Check(args.StatsdHost, Equals, "test_host")
c.Check(args.Lock, Equals, true)
c.Check(args.Label, Equals, "test")
c.Check(args.LogLevel, Equals, "info")
c.Check(args.Namespace, Equals, "testcronner")
c.Check(args.Parent, Equals, true)
c.Check(args.Passthru, Equals, true)
c.Check(args.Sensitive, Equals, true)
c.Assert(args.Tags, HasLen, 2)
c.Check(args.Tags[0], Equals, "tag1")
c.Check(args.Tags[1], Equals, "tag2")
c.Check(args.Version, Equals, false)
c.Check(args.WarnAfter, Equals, uint64(42))
c.Check(args.WaitSeconds, Equals, uint64(84))
c.Check(args.Cmd, Equals, "/bin/true")
c.Check(len(args.CmdArgs), Equals, 0)
//
// assert that long flags work
//
args = &binArgs{}
cli = []string{
Arg0,
"--lock-dir", "/var/testlock",
"--event",
"--event-fail",
"--log-fail",
"--event-group", "test_group",
"--group", "metric_group",
"--statsd-host", "test_host",
"--lock",
"--label", "test",
"--log-path", "/var/log/testcronner",
"--log-level", "info",
"--namespace", "testcronner",
"--use-parent",
"--passthru",
"--sensitive",
"--tag", "tag1",
"--tag", "tag2",
"--warn-after", "42",
"--wait-secs", "84",
"--", "/bin/true",
}
output, err = args.parse(cli)
c.Assert(err, IsNil)
logger.SetLevel(logger.LevelFatal)
c.Check(len(output), Equals, 0)
c.Check(args.LockDir, Equals, "/var/testlock")
c.Check(args.AllEvents, Equals, true)
c.Check(args.FailEvent, Equals, true)
c.Check(args.LogFail, Equals, true)
c.Check(args.EventGroup, Equals, "test_group")
c.Check(args.Group, Equals, "metric_group")
c.Check(args.StatsdHost, Equals, "test_host")
c.Check(args.Lock, Equals, true)
c.Check(args.Label, Equals, "test")
c.Check(args.LogPath, Equals, "/var/log/testcronner")
c.Check(args.LogLevel, Equals, "info")
c.Check(args.Namespace, Equals, "testcronner")
c.Check(args.Parent, Equals, true)
c.Check(args.Passthru, Equals, true)
c.Check(args.Sensitive, Equals, true)
c.Assert(args.Tags, HasLen, 2)
c.Check(args.Tags[0], Equals, "tag1")
c.Check(args.Tags[1], Equals, "tag2")
c.Check(args.Version, Equals, false)
c.Check(args.WarnAfter, Equals, uint64(42))
c.Check(args.WaitSeconds, Equals, uint64(84))
c.Check(args.Cmd, Equals, "/bin/true")
c.Check(len(args.CmdArgs), Equals, 0)
//
// assert that long flags work with --flag=value syntax
//
args = &binArgs{}
cli = []string{
Arg0,
"--lock-dir=/var/testlock",
"--event-group=test_group",
"--group=metric_group",
"--statsd-host=test_host",
"--label=test",
"--log-path=/var/log/testcronner",
"--log-level=info",
"--namespace=testcronner",
"--tag=tag1",
"--tag=tag2",
"--warn-after=42",
"--wait-secs=84",
"--", "/bin/true",
}
output, err = args.parse(cli)
c.Assert(err, IsNil)
logger.SetLevel(logger.LevelFatal)
c.Check(len(output), Equals, 0)
c.Check(args.LockDir, Equals, "/var/testlock")
c.Check(args.EventGroup, Equals, "test_group")
c.Check(args.Group, Equals, "metric_group")
c.Check(args.StatsdHost, Equals, "test_host")
c.Check(args.Label, Equals, "test")
c.Check(args.LogPath, Equals, "/var/log/testcronner")
c.Check(args.LogLevel, Equals, "info")
c.Check(args.Namespace, Equals, "testcronner")
c.Assert(args.Tags, HasLen, 2)
c.Check(args.Tags[0], Equals, "tag1")
c.Check(args.Tags[1], Equals, "tag2")
c.Check(args.Version, Equals, false)
c.Check(args.WarnAfter, Equals, uint64(42))
c.Check(args.WaitSeconds, Equals, uint64(84))
c.Check(args.Cmd, Equals, "/bin/true")
c.Check(len(args.CmdArgs), Equals, 0)
//
// assert that optional tags are validated
//
args = &binArgs{}
cli = []string{
Arg0,
"-l", "test",
"-t", "世界_hello:valid",
"-t", "invalid^tag",
"--", "/bin/true",
}
output, err = args.parse(cli)
c.Assert(err, Not(IsNil))
c.Check(len(output), Equals, 0)
c.Check(err.Error(), Equals, "tag 'invalid^tag' is invalid, it can only be alphanumeric with underscores, periods, colons, minuses and slashes")
args = &binArgs{}
cli = []string{
Arg0,
"-l", "test",
"-t", "_invalid:tag",
"--", "/bin/true",
}
output, err = args.parse(cli)
c.Assert(err, Not(IsNil))
c.Check(len(output), Equals, 0)
c.Check(err.Error(), Equals, "tag '_invalid:tag' is invalid, it must start with a letter")
args = &binArgs{}
tag := "tag_exceeds_200_chars_7eUJgOC2VnpCSsgcgq66Aj5cjiOCxvu736AQHu2zWy0" +
"booxAh2B7vrTfbb59w6YovAZ1HzSGfGxVomAzsAhxVUkSrfnBWb6Xs5WpV7RQAGqu" +
"uCIvPOv5rymEeJaefyligXrsN2VBlSQpXD5M3540VvK90JauGuqIkaCOQeKU2rgrF" +
"NU632ShyW3WOGxipoKuVGpKyvnN"
cli = []string{
Arg0,
"-l", "test",
"-t", tag,
"--", "/bin/true",
}
output, err = args.parse(cli)
c.Assert(err, Not(IsNil))
c.Check(len(output), Equals, 0)
c.Check(err.Error(), Equals, fmt.Sprintf("tag '%v' is invalid, tags must be less than 200 characters", tag))
//
// argument parsing regression tests
//
//
// parse() function should always discard element 0 in the slice.
//
args = &binArgs{}
cli = []string{
"--lock-dir=/var/testlock",
"--event-group=test_group",
"--label=test",
"--", "/bin/true",
}
output, err = args.parse(cli)
c.Assert(err, IsNil)
logger.SetLevel(logger.LevelFatal)
c.Check(len(output), Equals, 0)
c.Check(args.LockDir, Not(Equals), "/var/testlock")
c.Check(args.EventGroup, Equals, "test_group")
c.Check(args.Label, Equals, "test")
c.Check(args.Cmd, Equals, "/bin/true")
c.Check(len(args.CmdArgs), Equals, 0)
//
// parse() function should allow spaces in command line arguments
//
args = &binArgs{}
cli = []string{
Arg0,
"--label=test",
"--", "/bin/true", `some string`,
}
output, err = args.parse(cli)
c.Assert(err, IsNil)
logger.SetLevel(logger.LevelFatal)
c.Check(len(output), Equals, 0)
c.Check(args.Label, Equals, "test")
c.Check(args.Cmd, Equals, "/bin/true")
c.Assert(len(args.CmdArgs), Equals, 1)
c.Check(args.CmdArgs[0], Equals, "some string")
}