-
Notifications
You must be signed in to change notification settings - Fork 4
/
context_test.go
656 lines (641 loc) · 17 KB
/
context_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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
package dads
import (
"fmt"
"os"
"reflect"
"regexp"
"testing"
"time"
testlib "github.com/LF-Engineering/da-ds/test"
)
// Copies Ctx structure
func copyContext(in *Ctx) *Ctx {
out := Ctx{
DS: in.DS,
DSPrefix: in.DSPrefix,
Debug: in.Debug,
DebugSQL: in.DebugSQL,
Retry: in.Retry,
ST: in.ST,
NCPUs: in.NCPUs,
NCPUsScale: in.NCPUsScale,
Enrich: in.Enrich,
RawIndex: in.RawIndex,
RichIndex: in.RichIndex,
Tag: in.Tag,
ESURL: in.ESURL,
AffiliationAPIURL: in.AffiliationAPIURL,
ESBulkSize: in.ESBulkSize,
ESScrollSize: in.ESScrollSize,
ESScrollWait: in.ESScrollWait,
DBHost: in.DBHost,
DBName: in.DBName,
DBUser: in.DBUser,
DBPass: in.DBPass,
DBPort: in.DBPort,
DBOpts: in.DBOpts,
DBConn: in.DBConn,
DBBulkSize: in.DBBulkSize,
NoRaw: in.NoRaw,
NoIdentities: in.NoIdentities,
NoCache: in.NoCache,
NoAffiliation: in.NoAffiliation,
DryRun: in.DryRun,
RefreshAffs: in.RefreshAffs,
OnlyIdentities: in.OnlyIdentities,
ForceFull: in.ForceFull,
LegacyUUID: in.LegacyUUID,
DropRich: in.DropRich,
DropRaw: in.DropRaw,
AllowFail: in.AllowFail,
Project: in.Project,
ProjectSlug: in.ProjectSlug,
ProjectFilter: in.ProjectFilter,
CheckAuthorID: in.CheckAuthorID,
Groups: in.Groups,
Category: in.Category,
DateFrom: in.DateFrom,
DateTo: in.DateTo,
OffsetFrom: in.OffsetFrom,
OffsetTo: in.OffsetTo,
ESScrollWaitSecs: in.ESScrollWaitSecs,
}
return &out
}
// Dynamically sets Ctx fields (uses map of field names into their new values)
func dynamicSetFields(t *testing.T, ctx *Ctx, fields map[string]interface{}) *Ctx {
// Prepare mapping field name -> index
valueOf := reflect.Indirect(reflect.ValueOf(*ctx))
nFields := valueOf.Type().NumField()
namesToIndex := make(map[string]int)
for i := 0; i < nFields; i++ {
namesToIndex[valueOf.Type().Field(i).Name] = i
}
// Iterate map of interface{} and set values
elem := reflect.ValueOf(ctx).Elem()
for fieldName, fieldValue := range fields {
// Check if structure actually contains this field
fieldIndex, ok := namesToIndex[fieldName]
if !ok {
t.Errorf("context has no field: \"%s\"", fieldName)
return ctx
}
field := elem.Field(fieldIndex)
fieldKind := field.Kind()
// Switch type that comes from interface
switch interfaceValue := fieldValue.(type) {
case int:
// Check if types match
if fieldKind != reflect.Int {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.SetInt(int64(interfaceValue))
case float64:
// Check if types match
if fieldKind != reflect.Float64 {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.SetFloat(float64(interfaceValue))
case bool:
// Check if types match
if fieldKind != reflect.Bool {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.SetBool(interfaceValue)
case string:
// Check if types match
if fieldKind != reflect.String {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.SetString(interfaceValue)
case time.Time:
// Check if types match
fieldType := field.Type()
if fieldType != reflect.TypeOf(time.Now()) {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.Set(reflect.ValueOf(fieldValue))
case *time.Time:
// Check if types match
fieldType := field.Type()
now := time.Now()
if fieldType != reflect.TypeOf(&now) {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.Set(reflect.ValueOf(fieldValue))
case time.Duration:
// Check if types match
fieldType := field.Type()
if fieldType != reflect.TypeOf(time.Now().Sub(time.Now())) {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.Set(reflect.ValueOf(fieldValue))
case []int:
// Check if types match
fieldType := field.Type()
if fieldType != reflect.TypeOf([]int{}) {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.Set(reflect.ValueOf(fieldValue))
case []int64:
// Check if types match
fieldType := field.Type()
if fieldType != reflect.TypeOf([]int64{}) {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.Set(reflect.ValueOf(fieldValue))
case []string:
// Check if types match
fieldType := field.Type()
if fieldType != reflect.TypeOf([]string{}) {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.Set(reflect.ValueOf(fieldValue))
case map[string]bool:
// Check if types match
fieldType := field.Type()
if fieldType != reflect.TypeOf(map[string]bool{}) {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.Set(reflect.ValueOf(fieldValue))
case map[string]map[bool]struct{}:
// Check if types match
fieldType := field.Type()
if fieldType != reflect.TypeOf(map[string]map[bool]struct{}{}) {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.Set(reflect.ValueOf(fieldValue))
case *regexp.Regexp:
// Check if types match
fieldType := field.Type()
if fieldType != reflect.TypeOf(regexp.MustCompile("a")) {
t.Errorf("trying to set value %v, type %T for field \"%s\", type %v", interfaceValue, interfaceValue, fieldName, fieldKind)
return ctx
}
field.Set(reflect.ValueOf(fieldValue))
default:
// Unknown type provided
t.Errorf("unknown type %T for field \"%s\"", interfaceValue, fieldName)
}
}
// Return dynamically updated structure
return ctx
}
func TestInit(t *testing.T) {
// This is the expected default struct state
defaultContext := Ctx{
DS: "ds",
DSPrefix: "DA_DS_",
Debug: 0,
DebugSQL: 0,
Retry: 5,
ST: false,
NCPUs: 0,
NCPUsScale: 1.0,
Enrich: false,
RawIndex: "",
Tag: "",
RichIndex: "",
ESURL: "",
AffiliationAPIURL: "",
ESBulkSize: 500,
ESScrollSize: 1000,
ESScrollWait: "10m",
DBHost: "",
DBName: "",
DBUser: "",
DBPass: "",
DBPort: "",
DBOpts: "",
DBConn: "",
DBBulkSize: 1000,
NoRaw: false,
NoIdentities: false,
NoCache: false,
NoAffiliation: false,
DryRun: false,
RefreshAffs: false,
OnlyIdentities: false,
ForceFull: false,
LegacyUUID: false,
DropRich: false,
DropRaw: false,
AllowFail: 0,
Project: "",
ProjectSlug: "",
ProjectFilter: false,
CheckAuthorID: false,
Groups: []string{""},
Category: "",
DateFrom: nil,
DateTo: nil,
OffsetFrom: -1.0,
OffsetTo: -1.0,
ESScrollWaitSecs: 600.0,
}
// Set fake data source name to "ds" which will create prefix "DA_DS_"
FatalOnError(os.Setenv("DA_DS", "ds"))
// Test cases
dtF := testlib.YMDHMS(2020, 9, 28, 9, 12, 17)
dtT := testlib.YMDHMS(2021, 1, 1, 0, 0, 0)
var testCases = []struct {
name string
environment map[string]string
expectedContext *Ctx
}{
{
"Default values",
map[string]string{},
&defaultContext,
},
{
"Setting debug levels and retry",
map[string]string{
"DA_DS_DEBUG": "2",
"DA_DS_DEBUG_SQL": "1",
"DA_DS_RETRY": "3",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"Debug": 2,
"DebugSQL": 1,
"Retry": 3,
},
),
},
{
"Setting negative debug level",
map[string]string{"DA_DS_DEBUG": "-1"},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{"Debug": -1},
),
},
{
"Setting ST (singlethreading) and NCPUs",
map[string]string{"DA_DS_ST": "1", "DA_DS_NCPUS": "1"},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{"ST": true, "NCPUs": 1},
),
},
{
"Setting NCPUs to 2",
map[string]string{"DA_DS_NCPUS": "2"},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{"ST": false, "NCPUs": 2},
),
},
{
"Setting NCPUs to 1 should also set ST mode",
map[string]string{"DA_DS_NCPUS": "1"},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{"ST": true, "NCPUs": 1},
),
},
{
"Setting NCPUs Scale to 1.5",
map[string]string{"DA_DS_NCPUS_SCALE": "1.5"},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{"ST": false, "NCPUsScale": 1.5},
),
},
{
"Setting enrich flag",
map[string]string{"DA_DS_ENRICH": "y"},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{"Enrich": true},
),
},
{
"Setting raw & rich index and tag",
map[string]string{
"DA_DS_RAW_INDEX": "ds-raw",
"DA_DS_RICH_INDEX": "ds-rich",
"DA_DS_TAG": "tag",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"RawIndex": "ds-raw",
"RichIndex": "ds-rich",
"Tag": "tag",
},
),
},
{
"Setting ES params",
map[string]string{
"DA_DS_ES_URL": "elastic.co",
"DA_DS_ES_BULK_SIZE": "500",
"DA_DS_ES_SCROLL_SIZE": "600",
"DA_DS_ES_SCROLL_WAIT": "30m",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"ESURL": "elastic.co",
"ESBulkSize": 500,
"ESScrollSize": 600,
"ESScrollWait": "30m",
"ESScrollWaitSecs": 1800.0,
},
),
},
{
"Setting affiliation DB params",
map[string]string{
"DA_DS_DB_HOST": "h",
"DA_DS_DB_NAME": "n",
"DA_DS_DB_USER": "u",
"DA_DS_DB_PASS": "p",
"DA_DS_DB_PORT": "o",
"DA_DS_DB_OPTS": "a=1&b=2",
"DA_DS_DB_CONN": "c",
"DA_DS_DB_BULK_SIZE": "500",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"DBHost": "h",
"DBName": "n",
"DBUser": "u",
"DBPass": "p",
"DBPort": "o",
"DBOpts": "a=1&b=2",
"DBConn": "c",
"DBBulkSize": 500,
},
),
},
{
"Setting re affiliate params",
map[string]string{
"DA_DS_NO_RAW": "1",
"DA_DS_REFRESH_AFFS": "y",
"DA_DS_ONLY_IDENTITIES": "x",
"DA_DS_FORCE_FULL": "t",
"DA_DS_NO_IDENTITIES": "+",
"DA_DS_CHECK_AID": "1",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"NoRaw": true,
"RefreshAffs": true,
"OnlyIdentities": true,
"ForceFull": true,
"NoIdentities": true,
"CheckAuthorID": true,
},
),
},
{
"Setting Affiliation API params",
map[string]string{
"DA_DS_AFFILIATION_API_URL": "my.url",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"AffiliationAPIURL": "my.url",
},
),
},
{
"Setting legacy UUID mode",
map[string]string{
"DA_DS_LEGACY_UUID": "1",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"LegacyUUID": true,
},
),
},
{
"Setting drop raw/rich modes",
map[string]string{
"DA_DS_DROP_RICH": "1",
"DA_DS_DROP_RAW": "1",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"DropRich": true,
"DropRaw": true,
},
),
},
{
"Setting allow fail mode",
map[string]string{
"DA_DS_ALLOW_FAIL": "1",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"AllowFail": 1,
},
),
},
{
"Setting no-cache/dry-run params",
map[string]string{
"DA_DS_NO_CACHE": "1",
"DA_DS_DRY_RUN": "1",
"DA_DS_NO_AFFILIATION": "1",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"NoCache": true,
"DryRun": true,
"NoAffiliation": true,
},
),
},
{
"Setting project, project slug, project filter, category, groups",
map[string]string{
"DA_DS_PROJECT": "ONAP",
"DA_DS_PROJECT_SLUG": "lfn/onap",
"DA_DS_PROJECT_FILTER": "1",
"DA_DS_CATEGORY": "issue",
"GROUPS": "a;b ; c ",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"Project": "ONAP",
"ProjectSlug": "lfn/onap",
"ProjectFilter": true,
"Category": "issue",
"Groups": []string{"a", "b", "c", "lfn/onap"},
},
),
},
{
"Setting legacy project slug",
map[string]string{
"PROJECT_SLUG": "lfn/onap",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"ProjectSlug": "lfn/onap",
"Groups": []string{"lfn/onap"},
},
),
},
{
"Setting date range",
map[string]string{
"DA_DS_DATE_FROM": "2020-09-28 09:12:17",
"DA_DS_DATE_TO": "2021",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"DateFrom": &dtF,
"DateTo": &dtT,
},
),
},
{
"Setting offset range",
map[string]string{
"DA_DS_OFFSET_FROM": "100",
"DA_DS_OFFSET_TO": "200.5",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"OffsetFrom": 100.0,
"OffsetTo": 200.5,
},
),
},
{
"Setting parameters to falsey values",
map[string]string{
"DA_DS_DEBUG": "false",
"DA_DS_DEBUG_SQL": "f",
"DA_DS_RETRY": "FALSE",
"DA_DS_NCPUS": "F",
"DA_DS_NCPUS_SCALE": "fAlSE",
"DA_DS_ST": "0",
"DA_DS_ENRICH": " faLSE ",
"DA_DS_NO_RAW": "0.00 ",
"DA_DS_NO_IDENTITIES": " F",
"DA_DS_NO_CACHE": "False ",
"DA_DS_NO_AFFILIATION": " \t No \t ",
"DA_DS_DRY_RUN": "0.0",
"DA_DS_REFRESH_AFFS": " 0. ",
"DA_DS_ONLY_IDENTITIES": " .0",
"DA_DS_FORCE_FULL": "",
"DA_DS_LEGACY_UUID": " ",
"DA_DS_CHECK_AID": "no",
},
dynamicSetFields(
t,
copyContext(&defaultContext),
map[string]interface{}{
"Debug": 0,
"DebugSQL": 0,
"Retry": 5,
"NCPUs": 0,
"NCPUsScale": 1.0,
"ST": false,
"Enrich": false,
"NoRaw": false,
"NoIdentities": false,
"NoCache": false,
"NoAffiliation": false,
"DryRun": false,
"RefreshAffs": false,
"OnlyIdentities": false,
"ForceFull": false,
"LegacyUUID": false,
"CheckAuthorID": false,
},
),
},
}
// Execute test cases
for index, test := range testCases {
var gotContext Ctx
// Remember initial environment
currEnv := make(map[string]string)
for key := range test.environment {
currEnv[key] = os.Getenv(key)
}
// Set new environment
for key, value := range test.environment {
err := os.Setenv(key, value)
if err != nil {
t.Errorf(err.Error())
}
}
// Initialize context while new environment is set
gotContext.Init()
// FIXME: this is a hack that should be removed, once BugZilla variable is only initialized in DS=bugzilla mode.
gotContext.BugZilla = nil
gotContext.PiperMail = nil
gotContext.GoogleGroups = nil
// Restore original environment
for key := range test.environment {
err := os.Setenv(key, currEnv[key])
if err != nil {
t.Errorf(err.Error())
}
}
// Check if we got expected context
got := fmt.Sprintf("%+v", gotContext)
expected := fmt.Sprintf("%+v", *test.expectedContext)
if got != expected {
t.Errorf(
"Test case number %d \"%s\"\nExpected:\n%+v\nGot:\n%+v\n",
index+1, test.name, expected, got,
)
}
}
}