-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_svc_test.go
751 lines (642 loc) · 22 KB
/
data_svc_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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
package main
import (
"errors"
"fmt"
"testing"
)
type dataSvcTestCase struct {
Setup func(svc DataSvc)
Action func(svc DataSvc)
Teardown func(svc DataSvc)
Mocks dataSvcMocks
}
type dataSvcMocks struct {
DB Datastore
HA HAProxy
}
func (c dataSvcTestCase) execute() {
svc := NewDataSvc(c.Mocks.DB, c.Mocks.HA)
// perform setup
if c.Setup != nil {
c.Setup(svc)
}
// perform action
c.Action(svc)
// perform teardown
if c.Teardown != nil {
c.Teardown(svc)
}
}
func defaultMocks() dataSvcMocks {
return dataSvcMocks{
DB: testHelpers.NewDatastoreMock(),
HA: testHelpers.NewHAProxyMock(),
}
}
var bsData = BackendTestData{}
var fsData = FrontendTestData{}
// ----------------------------------------------
// frontendSvcImpl.GetAll TESTS
// ----------------------------------------------
// Tests the "happy path" for the frontendSvcImpl.GetAll() function.
func Test_frontendSvcImpl_GetAll(t *testing.T) {
f1 := fsData.OneFrontend()
f2 := fsData.OtherFrontend()
setup := func(svc DataSvc) {
// add frontend
derr := svc.SaveFrontend(f1)
assert.EnsureNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: %v", derr)
derr = svc.SaveFrontend(f2)
assert.EnsureNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// retrieve and validate data
frontends, derr := svc.GetAllFrontends()
assert.EnsureNil(t, derr, "frontendSvcImpl.GetAll() returned an unexpected error: %v", derr)
assert.EnsureEqual(t, len(frontends), 2, "frontendSvcImpl.GetAll() returned an unexpected number of values")
assert.Equal(t, frontends[0], f1, "frontendSvcImpl.GetAll() returned an unexpected value")
assert.Equal(t, frontends[1], f2, "frontendSvcImpl.GetAll() returned an unexpected value")
}
testCase := dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}
testCase.execute()
}
// ----------------------------------------------
// frontendSvcImpl.Get TESTS
// ----------------------------------------------
// Tests the "happy path" for the frontendSvcImpl.Get() function.
func Test_frontendSvcImpl_Get(t *testing.T) {
f := fsData.OneFrontend()
setup := func(svc DataSvc) {
// add frontend
derr := svc.SaveFrontend(f)
assert.EnsureNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// retrieve and validate data
returnedFrontend, derr := svc.GetFrontend(f.Name)
assert.EnsureNil(t, derr, "frontendSvcImpl.Get() returned an unexpected error: %v", derr)
assert.Equal(t, returnedFrontend, f, "frontendSvcImpl.Get() returned an unexpected value")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
// ----------------------------------------------
// frontendSvcImpl.Save TESTS
// ----------------------------------------------
// Tests the "happy path" for creating a frontend with the frontendSvcImpl.Save() function.
func Test_frontendSvcImpl_Save_Create(t *testing.T) {
f := fsData.OneFrontend()
testAction := func(svc DataSvc) {
// add frontend
derr := svc.SaveFrontend(f)
assert.EnsureNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: %v", derr)
// validate added data
returnedFrontend, derr := svc.GetFrontend(f.Name)
assert.EnsureNil(t, derr, "frontendSvcImpl.Get() returned an unexpected error: %v", derr)
assert.Equal(t, returnedFrontend, f, "frontendSvcImpl.Get() returned an unexpected value")
}
dataSvcTestCase{
Setup: nil,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
// Tests the "happy path" for updating a frontend with the frontendSvcImpl.Save() function.
func Test_frontendSvcImpl_Save_Update(t *testing.T) {
f := fsData.OneFrontend()
setup := func(svc DataSvc) {
// add frontend
derr := svc.SaveFrontend(f)
assert.EnsureNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// update frontend
f.Mode = "default"
derr := svc.SaveFrontend(f)
assert.EnsureNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: %v", derr)
// validate updated data
returnedFrontend, derr := svc.GetFrontend(f.Name)
assert.EnsureNil(t, derr, "frontendSvcImpl.Get() returned an unexpected error: %v", derr)
assert.Equal(t, returnedFrontend, f, "frontendSvcImpl.Get() returned an unexpected value")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
func Test_frontendSvcImpl_Add_NoNameProvided(t *testing.T) {
f := fsData.OneFrontend()
f.Name = ""
testAction := func(svc DataSvc) {
// attempt to add frontend, validate errors
derr := svc.SaveFrontend(f)
assert.EnsureNotNil(t, derr, "frontendSvcImpl.Save() failed to return an expected error")
assert.Equal(t, derr.Type, ErrBadData, fmt.Sprintf("frontendSvcImpl.Save() returned an unexpected error type: '%v'", derr.Type.String()))
}
dataSvcTestCase{
Setup: nil,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
func Test_frontendSvcImpl_Save_CreateSyncError(t *testing.T) {
f := fsData.OneFrontend()
setup := func(svc DataSvc) {
derr := svc.SaveFrontend(f)
assert.EnsureNotNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: %v", derr.Error())
}
testAction := func(svc DataSvc) {
// attempt to add frontend, validate errors
derr := svc.SaveFrontend(f)
assert.EnsureNotNil(t, derr, "frontendSvcImpl.Save() failed to return an expected error")
assert.Equal(t, derr.Type, ErrSync, fmt.Sprintf("frontendSvcImpl.Save() returned an unexpected error type: '%v'", derr.Type.String()))
}
ha := testHelpers.NewHAProxyMock()
ha.writeConfigAction = func(frontends Frontends, backends Backends) error {
return errors.New("test")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: dataSvcMocks{
DB: testHelpers.NewDatastoreMock(),
HA: ha,
},
}.execute()
}
func Test_frontendSvcImpl_Save_UpdateSyncError(t *testing.T) {
f := fsData.OneFrontend()
original := struct {
Name string
Mode string
}{
Name: f.Name,
Mode: f.Mode,
}
counter := 0
ha := testHelpers.NewHAProxyMock()
ha.writeConfigAction = func(frontends Frontends, backends Backends) error {
counter++
if counter >= 2 {
return errors.New("test")
}
return nil
}
db := testHelpers.NewDatastoreMock()
setup := func(svc DataSvc) {
derr := svc.SaveFrontend(f)
assert.EnsureNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: '%v'", derr)
}
testAction := func(svc DataSvc) {
// attempt to update frontend, validate errors
derr := svc.SaveFrontend(f)
assert.EnsureNotNil(t, derr, "frontendSvcImpl.Save() failed to return an expected error")
assert.Equal(t, derr.Type, ErrSync, fmt.Sprintf("frontendSvcImpl.Save() returned an unexpected error type: '%v'", derr.Type.String()))
}
teardown := func(svc DataSvc) {
// assert data was rolled back
f, derr := svc.GetFrontend(original.Name)
assert.EnsureNil(t, derr, "frontendSvcImpl.Get() returned an unexpected error: '%v'", derr)
assert.Equal(t, f.Mode, original.Mode, "frontendSbcImpl.Save() failed to rollback the delete action")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: teardown,
Mocks: dataSvcMocks{DB: db, HA: ha},
}.execute()
}
// ----------------------------------------------
// frontendSvcImpl.Delete TESTS
// ----------------------------------------------
// Tests the "happy path" for the frontendSvcImpl.Delete() function.
func Test_frontendSvcImpl_Delete(t *testing.T) {
f := fsData.OneFrontend()
setup := func(svc DataSvc) {
// add frontend
derr := svc.SaveFrontend(f)
assert.EnsureNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// delete frontend
derr := svc.DeleteFrontend(f.Name)
assert.EnsureNil(t, derr, "frontendSvcImpl.Delete() returned an unexpected error: %v", derr)
// assert that frontend was deleted
returnedFrontend, derr := svc.GetFrontend("f.Name")
assert.Nil(t, derr, "frontendSvcImpl.Get() returned an unexpected error: %v", derr)
assert.Nil(t, returnedFrontend, "frontendSvcImpl.Delete() failed to delete the frontend")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
func Test_frontendSvcImpl_Delete_NonExistentFrontend(t *testing.T) {
f := fsData.OneFrontend()
testAction := func(svc DataSvc) {
// attempt to update frontend, validate errors
derr := svc.DeleteFrontend(f.Name)
assert.EnsureNotNil(t, derr, fmt.Sprintf("frontendSvcImpl.Delete() failed to return an expected error"))
assert.Equal(t, derr.Type, ErrNotFound, fmt.Sprintf("frontendSvcImpl.Delete() returned an unexpected error type: '%v'", derr.Type.String()))
}
dataSvcTestCase{
Setup: nil,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
func Test_frontendSvcImpl_Delete_SyncError(t *testing.T) {
f := fsData.OneFrontend()
setup := func(svc DataSvc) {
derr := svc.SaveFrontend(f)
assert.EnsureNil(t, derr, "frontendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// attempt to update frontend, validate errors
derr := svc.DeleteFrontend(f.Name)
assert.EnsureNotNil(t, derr, fmt.Sprintf("frontendSvcImpl.Delete() failed to return an expected error"))
assert.Equal(t, derr.Type, ErrSync, fmt.Sprintf("frontendSvcImpl.Delete() returned an unexpected error type: '%v'", derr.Type.String()))
}
pass := 0
ha := testHelpers.NewHAProxyMock()
ha.writeConfigAction = func(frontends Frontends, backends Backends) error {
pass++
if pass == 2 {
return errors.New("test")
}
return nil
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: dataSvcMocks{
DB: testHelpers.NewDatastoreMock(),
HA: ha,
},
}.execute()
}
// // ----------------------------------------------
// // frontendSvcImpl.getFrontendKey TESTS
// // ----------------------------------------------
// // Tests the "happy path" for the frontendSvcImpl.getFrontendKey() function.
// func Test_frontendSvcImpl_getFrontendKey(t *testing.T) {
// // setup test
// svc := &dataSvcImpl{}
// testCases := []struct{ Name, Result string }{
// {
// Name: "name-without_spaces",
// Result: "name-without_spaces",
// },
// {
// Name: "",
// Result: "",
// },
// {
// Name: "name with spaces",
// Result: "name_with_spaces",
// },
// }
// for _, testcase := range testCases {
// frontend := &Frontend{Name: testcase.Name}
// actual := svc.getFrontendKey(frontend)
// assert.Equal(t, actual, testcase.Result, fmt.Sprintf("frontendSvcImpl.getFrontendKey() return an unexpected value with input '%v'", testcase.Name))
// }
// }
// // This test generates 1000 random UTF-8 strings for the Frontend name field
// // and uses the generated strings to test the getFrontendKey function.
// func Test_frontendSvcImpl_getFrontendKey_QuickCheck(t *testing.T) {
// // setup test
// svc := &frontendSvcImpl{}
// count := 0
// f := func(name string) bool {
// count++
// t.Logf("Test %d: Testing getFrontendKey with name (%v)\n", count, name)
// frontend := &Frontend{Name: name}
// actual := svc.getFrontendKey(frontend)
// return strings.Replace(name, " ", "_", -1) == actual
// }
// config := &quick.Config{MaxCount: 1000}
// if err := quick.Check(f, config); err != nil {
// t.Error(err)
// }
// }
// ----------------------------------------------
// backendSvcImpl.GetAll TESTS
// ----------------------------------------------
// Tests the "happy path" for the backendSvcImpl.GetAll() function.
func Test_backendSvcImpl_GetAll(t *testing.T) {
b1 := bsData.OneBackend()
b2 := bsData.OtherBackend()
setup := func(svc DataSvc) {
// add backend
derr := svc.SaveBackend(b1)
assert.EnsureNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: %v", derr)
derr = svc.SaveBackend(b2)
assert.EnsureNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// retrieve and validate data
backends, derr := svc.GetAllBackends()
assert.EnsureNil(t, derr, "backendSvcImpl.GetAll() returned an unexpected error: %v", derr)
assert.EnsureEqual(t, len(backends), 2, "backendSvcImpl.GetAll() returned an unexpected number of values")
assert.Equal(t, backends[0], b1, "backendSvcImpl.GetAll() returned an unexpected value")
assert.Equal(t, backends[1], b2, "backendSvcImpl.GetAll() returned an unexpected value")
}
testCase := dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}
testCase.execute()
}
// ----------------------------------------------
// backendSvcImpl.Get TESTS
// ----------------------------------------------
// Tests the "happy path" for the backendSvcImpl.Get() function.
func Test_backendSvcImpl_Get(t *testing.T) {
b := bsData.OneBackend()
setup := func(svc DataSvc) {
// add backend
derr := svc.SaveBackend(b)
assert.EnsureNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// retrieve and validate data
returnedBackend, derr := svc.GetBackend(b.Name)
assert.EnsureNil(t, derr, "backendSvcImpl.Get() returned an unexpected error: %v", derr)
assert.Equal(t, returnedBackend, b, "backendSvcImpl.Get() returned an unexpected value")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
// ----------------------------------------------
// backendSvcImpl.Save TESTS
// ----------------------------------------------
// Tests the "happy path" for creating a backend with the backendSvcImpl.Save() function.
func Test_backendSvcImpl_Save_Create(t *testing.T) {
b := bsData.OneBackend()
testAction := func(svc DataSvc) {
// add backend
derr := svc.SaveBackend(b)
assert.EnsureNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: %v", derr)
// validate added data
returnedBackend, derr := svc.GetBackend(b.Name)
assert.EnsureNil(t, derr, "backendSvcImpl.Get() returned an unexpected error: %v", derr)
assert.Equal(t, returnedBackend, b, "backendSvcImpl.Get() returned an unexpected value")
}
dataSvcTestCase{
Setup: nil,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
// Tests the "happy path" for updating a backend with the backendSvcImpl.Save() function.
func Test_backendSvcImpl_Save_Update(t *testing.T) {
b := bsData.OneBackend()
setup := func(svc DataSvc) {
// add backend
derr := svc.SaveBackend(b)
assert.EnsureNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// update backend
b.Mode = "default"
derr := svc.SaveBackend(b)
assert.EnsureNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: %v", derr)
// validate updated data
returnedBackend, derr := svc.GetBackend(b.Name)
assert.EnsureNil(t, derr, "backendSvcImpl.Get() returned an unexpected error: %v", derr)
assert.Equal(t, returnedBackend, b, "backendSvcImpl.Get() returned an unexpected value")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
func Test_backendSvcImpl_Save_NoNameProvided(t *testing.T) {
b := bsData.OneBackend()
b.Name = ""
testAction := func(svc DataSvc) {
// attempt to add backend, validate errors
derr := svc.SaveBackend(b)
assert.EnsureNotNil(t, derr, "backendSvcImpl.Save() failed to return an expected error")
assert.Equal(t, derr.Type, ErrBadData, fmt.Sprintf("backendSvcImpl.Save() returned an unexpected error type: '%v'", derr.Type.String()))
}
dataSvcTestCase{
Setup: nil,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
func Test_backendSvcImpl_Save_CreateSyncError(t *testing.T) {
b := bsData.OneBackend()
setup := func(svc DataSvc) {
derr := svc.SaveBackend(b)
assert.EnsureNotNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: %v", derr.Error())
}
testAction := func(svc DataSvc) {
// attempt to add backend, validate errors
derr := svc.SaveBackend(b)
assert.EnsureNotNil(t, derr, "backendSvcImpl.Save() failed to return an expected error")
assert.Equal(t, derr.Type, ErrSync, fmt.Sprintf("backendSvcImpl.Save() returned an unexpected error type: '%v'", derr.Type.String()))
}
ha := testHelpers.NewHAProxyMock()
ha.writeConfigAction = func(frontends Frontends, backends Backends) error {
return errors.New("test")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: dataSvcMocks{
DB: testHelpers.NewDatastoreMock(),
HA: ha,
},
}.execute()
}
func Test_backendSvcImpl_Save_UpdateSyncError(t *testing.T) {
b := bsData.OneBackend()
original := struct {
Name string
Mode string
}{
Name: b.Name,
Mode: b.Mode,
}
counter := 0
ha := testHelpers.NewHAProxyMock()
ha.writeConfigAction = func(frontends Frontends, backends Backends) error {
counter++
if counter >= 2 {
return errors.New("test")
}
return nil
}
db := testHelpers.NewDatastoreMock()
setup := func(svc DataSvc) {
derr := svc.SaveBackend(b)
assert.EnsureNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: '%v'", derr)
}
testAction := func(svc DataSvc) {
// attempt to update backend, validate errors
derr := svc.SaveBackend(b)
assert.EnsureNotNil(t, derr, "backendSvcImpl.Save() failed to return an expected error")
assert.Equal(t, derr.Type, ErrSync, fmt.Sprintf("backendSvcImpl.Save() returned an unexpected error type: '%v'", derr.Type.String()))
}
teardown := func(svc DataSvc) {
// assert data was rolled back
f, derr := svc.GetBackend(original.Name)
assert.EnsureNil(t, derr, "backendSvcImpl.Get() returned an unexpected error: '%v'", derr)
assert.Equal(t, f.Mode, original.Mode, "backendSbcImpl.Save() failed to rollback the delete action")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: teardown,
Mocks: dataSvcMocks{DB: db, HA: ha},
}.execute()
}
// ----------------------------------------------
// backendSvcImpl.Delete TESTS
// ----------------------------------------------
// Tests the "happy path" for the backendSvcImpl.Delete() function.
func Test_backendSvcImpl_Delete(t *testing.T) {
b := bsData.OneBackend()
setup := func(svc DataSvc) {
// add backend
derr := svc.SaveBackend(b)
assert.EnsureNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// delete backend
derr := svc.DeleteBackend(b.Name)
assert.EnsureNil(t, derr, "backendSvcImpl.Delete() returned an unexpected error: %v", derr)
// assert that backend was deleted
returnedBackend, derr := svc.GetBackend("test001-1.2.5")
assert.Nil(t, derr, "backendSvcImpl.Get() returned an unexpected error: %v", derr)
assert.Nil(t, returnedBackend, "backendSvcImpl.Delete() failed to delete the backend")
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
func Test_backendSvcImpl_Delete_NonExistentBackend(t *testing.T) {
b := bsData.OneBackend()
testAction := func(svc DataSvc) {
// attempt to update backend, validate errors
derr := svc.DeleteBackend(b.Name)
assert.EnsureNotNil(t, derr, fmt.Sprintf("backendSvcImpl.Delete() failed to return an expected error"))
assert.Equal(t, derr.Type, ErrNotFound, fmt.Sprintf("backendSvcImpl.Delete() returned an unexpected error type: '%v'", derr.Type.String()))
}
dataSvcTestCase{
Setup: nil,
Action: testAction,
Teardown: nil,
Mocks: defaultMocks(),
}.execute()
}
func Test_backendSvcImpl_Delete_SyncError(t *testing.T) {
b := bsData.OneBackend()
setup := func(svc DataSvc) {
derr := svc.SaveBackend(b)
assert.EnsureNil(t, derr, "backendSvcImpl.Save() returned an unexpected error: %v", derr)
}
testAction := func(svc DataSvc) {
// attempt to update backend, validate errors
derr := svc.DeleteBackend(b.Name)
assert.EnsureNotNil(t, derr, fmt.Sprintf("backendSvcImpl.Delete() failed to return an expected error"))
assert.Equal(t, derr.Type, ErrSync, fmt.Sprintf("backendSvcImpl.Delete() returned an unexpected error type: '%v'", derr.Type.String()))
}
pass := 0
ha := testHelpers.NewHAProxyMock()
ha.writeConfigAction = func(frontends Frontends, backends Backends) error {
pass++
if pass == 2 {
return errors.New("test")
}
return nil
}
dataSvcTestCase{
Setup: setup,
Action: testAction,
Teardown: nil,
Mocks: dataSvcMocks{
DB: testHelpers.NewDatastoreMock(),
HA: ha,
},
}.execute()
}
// // ----------------------------------------------
// // backendSvcImpl.getBackendKey TESTS
// // ----------------------------------------------
// func Test_backendSvcImpl_getBackendKey(t *testing.T) {
// // setup test
// svc := &dataSvcImpl{}
// testCases := []struct{ Name, Result string }{
// {
// Name: "name-without_spaces",
// Result: "name-without_spaces",
// },
// {
// Name: "",
// Result: "",
// },
// {
// Name: "name with spaces",
// Result: "name_with_spaces",
// },
// }
// for _, testcase := range testCases {
// backend := &Backend{Name: testcase.Name}
// actual := svc.getBackendKey(backend)
// assert.Equal(t, actual, testcase.Result, fmt.Sprintf("backendSvcImpl.getBackendKey() return an unexpected value with input '%v'", testcase.Name))
// }
// }
// // This test generates 1000 random UTF-8 strings for the Backend name field
// // and uses the generated strings to test the getBackendKey function.
// func Test_backendSvcImpl_getBackendKey_QuickCheck(t *testing.T) {
// // setup test
// svc := &backendSvcImpl{}
// count := 0
// f := func(name string) bool {
// count++
// t.Logf("Test %d: Testing getBackendKey with name (%v)\n", count, name)
// backend := &Backend{Name: name}
// actual := svc.getBackendKey(backend)
// return strings.Replace(name, " ", "_", -1) == actual
// }
// config := &quick.Config{MaxCount: 1000}
// if err := quick.Check(f, config); err != nil {
// t.Error(err)
// }
// }