-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmode_validate_test.go
More file actions
495 lines (409 loc) · 17.6 KB
/
Copy pathmode_validate_test.go
File metadata and controls
495 lines (409 loc) · 17.6 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package ferrite_test
import (
"os"
"strings"
"time"
"github.com/dogmatiq/ferrite"
)
func ExampleInit_validation() {
defer example()()
os.Setenv("FERRITE_BINARY", "PHZhbHVlPg==")
ferrite.
Binary("FERRITE_BINARY", "example binary").
Required()
os.Setenv("FERRITE_BINARY_SENSITIVE", "aHVudGVyMg==")
ferrite.
Binary("FERRITE_BINARY_SENSITIVE", "example sensitive binary").
WithSensitiveContent().
Required()
os.Setenv("FERRITE_BOOL", "true")
ferrite.
Bool("FERRITE_BOOL", "example bool").
Required()
os.Setenv("FERRITE_DIR", "/path/to/dir")
ferrite.
Dir("FERRITE_DIR", "example dir").
Required()
os.Setenv("FERRITE_DURATION", "3h20m")
ferrite.
Duration("FERRITE_DURATION", "example duration").
Required()
os.Setenv("FERRITE_ENUM", "foo")
ferrite.
Enum("FERRITE_ENUM", "example enum").
WithMembers("foo", "bar", "baz").
Required()
os.Setenv("FERRITE_FILE", "/path/to/file")
ferrite.
File("FERRITE_FILE", "example file").
Required()
os.Setenv("FERRITE_NETWORK_PORT", "8080")
ferrite.
NetworkPort("FERRITE_NETWORK_PORT", "example network port").
Required()
os.Setenv("FERRITE_NUM_FLOAT", "-123.45")
ferrite.
Float[float32]("FERRITE_NUM_FLOAT", "example float-point").
Required()
os.Setenv("FERRITE_NUM_SIGNED", "-123")
ferrite.
Signed[int16]("FERRITE_NUM_SIGNED", "example signed integer").
Required()
os.Setenv("FERRITE_NUM_UNSIGNED", "456")
ferrite.
Unsigned[uint16]("FERRITE_NUM_UNSIGNED", "example unsigned integer").
Required()
os.Setenv("FERRITE_STRING", "hello, world!")
ferrite.
String("FERRITE_STRING", "example string").
Required()
os.Setenv("FERRITE_STRING_SENSITIVE", "hunter2")
ferrite.
String("FERRITE_STRING_SENSITIVE", "example sensitive string").
WithSensitiveContent().
Required()
os.Setenv("FERRITE_TEXT", "text:hello")
ferrite.
TextEncoded[textValue]("FERRITE_TEXT", "example text").
Required()
os.Setenv("FERRITE_SVC_SERVICE_HOST", "host.example.org")
os.Setenv("FERRITE_SVC_SERVICE_PORT", "443")
ferrite.
KubernetesService("ferrite-svc").
Required()
os.Setenv("FERRITE_URL", "https://example.org")
ferrite.
URL("FERRITE_URL", "example URL").
Required()
ferrite.
String("FERRITE_XTRIGGER", "trigger failure for example").
Required()
ferrite.Init()
// Output:
// Environment Variables:
//
// FERRITE_BINARY example binary <base64> ✓ set to {12 bytes}
// FERRITE_BINARY_SENSITIVE example sensitive binary <base64> ✓ set to {12 bytes}
// FERRITE_BOOL example bool true | false ✓ set to true
// FERRITE_DIR example dir <string> ✓ set to /path/to/dir
// FERRITE_DURATION example duration 1ns ... ✓ set to 3h20m
// FERRITE_ENUM example enum foo | bar | baz ✓ set to foo
// FERRITE_FILE example file <string> ✓ set to /path/to/file
// FERRITE_NETWORK_PORT example network port <string> ✓ set to 8080
// FERRITE_NUM_FLOAT example float-point <float32> ✓ set to -123.45
// FERRITE_NUM_SIGNED example signed integer <int16> ✓ set to -123
// FERRITE_NUM_UNSIGNED example unsigned integer <uint16> ✓ set to 456
// FERRITE_STRING example string <string> ✓ set to 'hello, world!'
// FERRITE_STRING_SENSITIVE example sensitive string <string> ✓ set to *******
// FERRITE_SVC_SERVICE_HOST kubernetes "ferrite-svc" service host <string> ✓ set to host.example.org
// FERRITE_SVC_SERVICE_PORT kubernetes "ferrite-svc" service port <string> ✓ set to 443
// FERRITE_TEXT example text <string> ✓ set to text:hello
// FERRITE_URL example URL <string> ✓ set to https://example.org
// ❯ FERRITE_XTRIGGER trigger failure for example <string> ✗ undefined
//
// <process exited with error code 1>
}
func ExampleInit_validationWithDefaultValues() {
defer example()()
ferrite.
Binary("FERRITE_BINARY", "example binary").
WithEncodedDefault("PHZhbHVlPg==").
Required()
ferrite.
Binary("FERRITE_BINARY_SENSITIVE", "example sensitive binary").
WithEncodedDefault("aHVudGVyMg==").
WithSensitiveContent().
Required()
ferrite.
Bool("FERRITE_BOOL", "example bool").
WithDefault(true).
Required()
ferrite.
Dir("FERRITE_DIR", "example dir").
WithDefault("/path/to/dir").
Required()
ferrite.
Duration("FERRITE_DURATION", "example duration").
WithDefault(10 * time.Second).
Required()
ferrite.
Enum("FERRITE_ENUM", "example enum").
WithMembers("foo", "bar", "baz").
WithDefault("bar").
Required()
ferrite.
File("FERRITE_FILE", "example file").
WithDefault("/path/to/file").
Required()
ferrite.
NetworkPort("FERRITE_NETWORK_PORT", "example network port").
WithDefault("8080").
Required()
ferrite.
Float[float32]("FERRITE_NUM_FLOAT", "example float-point").
WithDefault(-123.45).
Required()
ferrite.
Signed[int16]("FERRITE_NUM_SIGNED", "example signed integer").
WithDefault(-123).
Required()
ferrite.
Unsigned[uint16]("FERRITE_NUM_UNSIGNED", "example unsigned integer").
WithDefault(123).
Required()
ferrite.
String("FERRITE_STRING", "example string").
WithDefault("hello, world!").
Required()
ferrite.
String("FERRITE_STRING_SENSITIVE", "example sensitive string").
WithDefault("hunter2").
WithSensitiveContent().
Required()
ferrite.
TextEncoded[textValue]("FERRITE_TEXT", "example text").
WithDefault(textValue{Data: "fallback"}).
Required()
ferrite.
KubernetesService("ferrite-svc").
WithDefault("host.example.org", "443").
Required()
ferrite.
URL("FERRITE_URL", "example URL").
WithDefault("https://example.org").
Required()
ferrite.
String("FERRITE_XTRIGGER", "trigger failure for example").
Required()
ferrite.Init()
// Output:
// Environment Variables:
//
// FERRITE_BINARY example binary [ <base64> ] = {12 bytes} ✓ using default value
// FERRITE_BINARY_SENSITIVE example sensitive binary [ <base64> ] = {12 bytes} ✓ using default value
// FERRITE_BOOL example bool [ true | false ] = true ✓ using default value
// FERRITE_DIR example dir [ <string> ] = /path/to/dir ✓ using default value
// FERRITE_DURATION example duration [ 1ns ... ] = 10s ✓ using default value
// FERRITE_ENUM example enum [ foo | bar | baz ] = bar ✓ using default value
// FERRITE_FILE example file [ <string> ] = /path/to/file ✓ using default value
// FERRITE_NETWORK_PORT example network port [ <string> ] = 8080 ✓ using default value
// FERRITE_NUM_FLOAT example float-point [ <float32> ] = -123.45 ✓ using default value
// FERRITE_NUM_SIGNED example signed integer [ <int16> ] = -123 ✓ using default value
// FERRITE_NUM_UNSIGNED example unsigned integer [ <uint16> ] = 123 ✓ using default value
// FERRITE_STRING example string [ <string> ] = 'hello, world!' ✓ using default value
// FERRITE_STRING_SENSITIVE example sensitive string [ <string> ] = ******* ✓ using default value
// FERRITE_SVC_SERVICE_HOST kubernetes "ferrite-svc" service host [ <string> ] = host.example.org ✓ using default value
// FERRITE_SVC_SERVICE_PORT kubernetes "ferrite-svc" service port [ <string> ] = 443 ✓ using default value
// FERRITE_TEXT example text [ <string> ] = text:fallback ✓ using default value
// FERRITE_URL example URL [ <string> ] = https://example.org ✓ using default value
// ❯ FERRITE_XTRIGGER trigger failure for example <string> ✗ undefined
//
// <process exited with error code 1>
}
func ExampleInit_validationWithOptionalValues() {
defer example()()
ferrite.
Binary("FERRITE_BINARY", "example binary").
Optional()
ferrite.
Binary("FERRITE_BINARY_SENSITIVE", "example sensitive binary").
WithSensitiveContent().
Optional()
ferrite.
Bool("FERRITE_BOOL", "example bool").
Optional()
ferrite.
Dir("FERRITE_DIR", "example dir").
Optional()
ferrite.
Duration("FERRITE_DURATION", "example duration").
Optional()
ferrite.
Enum("FERRITE_ENUM", "example enum").
WithMembers("foo", "bar", "baz").
Optional()
ferrite.
File("FERRITE_FILE", "example file").
Optional()
ferrite.
NetworkPort("FERRITE_NETWORK_PORT", "example network port").
Optional()
ferrite.
Float[float32]("FERRITE_NUM_FLOAT", "example float-point").
Optional()
ferrite.
Signed[int16]("FERRITE_NUM_SIGNED", "example signed integer").
Optional()
ferrite.
Unsigned[uint16]("FERRITE_NUM_UNSIGNED", "example unsigned integer").
Optional()
ferrite.
String("FERRITE_STRING", "example string").
Optional()
ferrite.
String("FERRITE_STRING_SENSITIVE", "example sensitive string").
WithSensitiveContent().
Optional()
ferrite.
TextEncoded[textValue]("FERRITE_TEXT", "example text").
Optional()
ferrite.
KubernetesService("ferrite-svc").
Optional()
ferrite.
URL("FERRITE_URL", "example URL").
Optional()
ferrite.
String("FERRITE_XTRIGGER", "trigger failure for example").
Required()
ferrite.Init()
// Output:
// Environment Variables:
//
// FERRITE_BINARY example binary [ <base64> ] • undefined
// FERRITE_BINARY_SENSITIVE example sensitive binary [ <base64> ] • undefined
// FERRITE_BOOL example bool [ true | false ] • undefined
// FERRITE_DIR example dir [ <string> ] • undefined
// FERRITE_DURATION example duration [ 1ns ... ] • undefined
// FERRITE_ENUM example enum [ foo | bar | baz ] • undefined
// FERRITE_FILE example file [ <string> ] • undefined
// FERRITE_NETWORK_PORT example network port [ <string> ] • undefined
// FERRITE_NUM_FLOAT example float-point [ <float32> ] • undefined
// FERRITE_NUM_SIGNED example signed integer [ <int16> ] • undefined
// FERRITE_NUM_UNSIGNED example unsigned integer [ <uint16> ] • undefined
// FERRITE_STRING example string [ <string> ] • undefined
// FERRITE_STRING_SENSITIVE example sensitive string [ <string> ] • undefined
// FERRITE_SVC_SERVICE_HOST kubernetes "ferrite-svc" service host [ <string> ] • undefined
// FERRITE_SVC_SERVICE_PORT kubernetes "ferrite-svc" service port [ <string> ] • undefined
// FERRITE_TEXT example text [ <string> ] • undefined
// FERRITE_URL example URL [ <string> ] • undefined
// ❯ FERRITE_XTRIGGER trigger failure for example <string> ✗ undefined
//
// <process exited with error code 1>
}
func ExampleInit_validationWithNonCanonicalValues() {
defer example()()
os.Setenv("FERRITE_DURATION", "3h 10m 0s")
ferrite.
Duration("FERRITE_DURATION", "example duration").
Required()
ferrite.
String("FERRITE_XTRIGGER", "trigger failure for example").
Required()
ferrite.Init()
// Output:
// Environment Variables:
//
// FERRITE_DURATION example duration 1ns ... ✓ set to '3h 10m 0s', equivalent to 3h10m
// ❯ FERRITE_XTRIGGER trigger failure for example <string> ✗ undefined
//
// <process exited with error code 1>
}
func ExampleInit_validationWithInvalidValues() {
defer example()()
os.Setenv("FERRITE_BINARY", "<invalid base64>")
ferrite.
Binary("FERRITE_BINARY", "example binary").
Required()
os.Setenv("FERRITE_BINARY_SENSITIVE", "<invalid base64>")
ferrite.
Binary("FERRITE_BINARY_SENSITIVE", "example sensitive binary").
WithSensitiveContent().
Required()
os.Setenv("FERRITE_BOOL", "yes")
ferrite.
Bool("FERRITE_BOOL", "example bool").
Required()
os.Setenv("FERRITE_DIR", "/path/to/dir")
ferrite.
Dir("FERRITE_DIR", "example dir").
WithMustExist().
Required()
os.Setenv("FERRITE_DURATION", "-+10s")
ferrite.
Duration("FERRITE_DURATION", "example duration").
Required()
os.Setenv("FERRITE_ENUM", "qux")
ferrite.
Enum("FERRITE_ENUM", "example enum").
WithMembers("foo", "bar", "baz").
Required()
os.Setenv("FERRITE_FILE", "/path/to/file")
ferrite.
File("FERRITE_FILE", "example file").
WithMustExist().
Required()
os.Setenv("FERRITE_NETWORK_PORT", "<invalid port>")
ferrite.
NetworkPort("FERRITE_NETWORK_PORT", "example network port").
Required()
os.Setenv("FERRITE_NUM_FLOAT", "-123w45")
ferrite.
Float[float32]("FERRITE_NUM_FLOAT", "example float-point").
Required()
os.Setenv("FERRITE_NUM_SIGNED", "123.3")
ferrite.
Signed[int16]("FERRITE_NUM_SIGNED", "example signed integer").
Required()
os.Setenv("FERRITE_NUM_UNSIGNED", "-123")
ferrite.
Unsigned[uint16]("FERRITE_NUM_UNSIGNED", "example unsigned integer").
Required()
os.Setenv("FERRITE_STRING", "foo bar")
ferrite.
String("FERRITE_STRING", "example string").
WithConstraint(
"must not contain whitespace",
func(s string) bool {
return !strings.ContainsRune(s, ' ')
},
).
Required()
os.Setenv("FERRITE_STRING_SENSITIVE", "foo bar")
ferrite.
String("FERRITE_STRING_SENSITIVE", "example sensitive string").
WithConstraint(
"must not contain whitespace",
func(s string) bool {
return !strings.ContainsRune(s, ' ')
},
).
WithSensitiveContent().
Required()
os.Setenv("FERRITE_TEXT", "not-text-prefixed")
ferrite.
TextEncoded[textValue]("FERRITE_TEXT", "example text").
Required()
os.Setenv("FERRITE_SVC_SERVICE_HOST", ".local")
os.Setenv("FERRITE_SVC_SERVICE_PORT", "https-")
ferrite.
KubernetesService("ferrite-svc").
Required()
os.Setenv("FERRITE_URL", "/relative/path")
ferrite.
URL("FERRITE_URL", "example URL").
Required()
ferrite.Init()
// Output:
// Environment Variables:
//
// ❯ FERRITE_BINARY example binary <base64> ✗ set to {16 bytes}, illegal base64 data at input byte 0
// ❯ FERRITE_BINARY_SENSITIVE example sensitive binary <base64> ✗ set to {16 bytes}, illegal base64 data at input byte 0
// ❯ FERRITE_BOOL example bool true | false ✗ set to yes, expected either true or false
// ❯ FERRITE_DIR example dir <string> ✗ set to /path/to/dir, expected the directory to exist
// ❯ FERRITE_DURATION example duration 1ns ... ✗ set to -+10s, expected duration
// ❯ FERRITE_ENUM example enum foo | bar | baz ✗ set to qux, expected foo, bar or baz
// ❯ FERRITE_FILE example file <string> ✗ set to /path/to/file, expected the file to exist
// ❯ FERRITE_NETWORK_PORT example network port <string> ✗ set to '<invalid port>', IANA service name must contain only ASCII letters, digits and hyphen
// ❯ FERRITE_NUM_FLOAT example float-point <float32> ✗ set to -123w45, expected float32
// ❯ FERRITE_NUM_SIGNED example signed integer <int16> ✗ set to 123.3, expected integer between -32768 and +32767
// ❯ FERRITE_NUM_UNSIGNED example unsigned integer <uint16> ✗ set to -123, expected integer between 0 and 65535
// ❯ FERRITE_STRING example string <string> ✗ set to 'foo bar', must not contain whitespace
// ❯ FERRITE_STRING_SENSITIVE example sensitive string <string> ✗ set to *******, must not contain whitespace
// ❯ FERRITE_SVC_SERVICE_HOST kubernetes "ferrite-svc" service host <string> ✗ set to .local, host must not begin or end with a dot
// ❯ FERRITE_SVC_SERVICE_PORT kubernetes "ferrite-svc" service port <string> ✗ set to https-, IANA service name must not begin or end with a hyphen
// ❯ FERRITE_TEXT example text <string> ✗ set to not-text-prefixed, expected text: prefix
// ❯ FERRITE_URL example URL <string> ✗ set to /relative/path, URL must have a scheme
//
// <process exited with error code 1>
}