@@ -19,6 +19,12 @@ import (
19
19
"testing"
20
20
)
21
21
22
+ const (
23
+ validTest = iota
24
+ handleError
25
+ unknownCommand
26
+ )
27
+
22
28
// MockOvsCLI is mocking the "ovs-dpctl (Open vSwitch) 2.10.7" version
23
29
type MockOvsCLI struct {
24
30
Version uint8
@@ -46,8 +52,14 @@ func (cli *MockOvsCLI) Exec(args ...string) ([]byte, error) {
46
52
out := "default limit=0\n zone=2,limit=1000000,count=0\n zone=3,limit=1000000,count=0"
47
53
return []byte (out ), nil
48
54
case "ct-set-limits" :
55
+ if cli .Version < 10 {
56
+ return []byte {}, errors .New ("ovs-dpctl: unknown command 'ct-get-limits'; use --help for help" )
57
+ }
49
58
return []byte {}, nil
50
59
case "ct-del-limits" :
60
+ if cli .Version < 10 {
61
+ return []byte {}, errors .New ("ovs-dpctl: unknown command 'ct-get-limits'; use --help for help" )
62
+ }
51
63
return []byte {}, nil
52
64
default :
53
65
return []byte {}, nil
@@ -198,14 +210,14 @@ func TestGetCTLimits(t *testing.T) {
198
210
desc : "Test invalid ovs-dpctl version " ,
199
211
dp : & DataPathService {
200
212
CLI : & MockOvsCLI {
201
- Version : 9 ,
213
+ Version : unknownCommand ,
202
214
},
203
215
},
204
216
dpName : "ovs-system" ,
205
217
zones : []uint64 {2 , 3 },
206
218
want : nil ,
207
219
err : "ovs-dpctl: unknown command 'ct-get-limits'; use --help for help" ,
208
- testCase : 9 ,
220
+ testCase : unknownCommand ,
209
221
},
210
222
{
211
223
desc : "Test valid ovs-dpctl version " ,
@@ -218,7 +230,7 @@ func TestGetCTLimits(t *testing.T) {
218
230
zones : []uint64 {2 , 3 },
219
231
want : out ,
220
232
err : "" ,
221
- testCase : 10 ,
233
+ testCase : validTest ,
222
234
},
223
235
{
224
236
desc : "Test valid ovs-dpctl ct-get-limits" ,
@@ -231,18 +243,18 @@ func TestGetCTLimits(t *testing.T) {
231
243
zones : []uint64 {},
232
244
want : out ,
233
245
err : "datapath name argument is mandatory" ,
234
- testCase : 11 ,
246
+ testCase : handleError ,
235
247
},
236
248
}
237
249
for _ , tt := range tests {
238
250
t .Run (tt .desc , func (t * testing.T ) {
239
251
results , err := tt .dp .GetCTLimits (tt .dpName , tt .zones )
240
252
switch tt .testCase {
241
- case 9 :
253
+ case unknownCommand :
242
254
if err .Error () != tt .err {
243
255
t .Errorf ("getting an error while trying to get conntrack limits %q" , err .Error ())
244
256
}
245
- case 10 :
257
+ case validTest :
246
258
if tt .want .defaultLimit ["default" ] != results .defaultLimit ["default" ] {
247
259
t .Errorf ("mismatched values, want %q but got %q" , tt .want .defaultLimit ["default" ], results .defaultLimit ["default" ])
248
260
}
@@ -251,7 +263,7 @@ func TestGetCTLimits(t *testing.T) {
251
263
t .Errorf ("mismatched values, want %q but got %q" , tt .want .zones [i ]["zone" ], z ["zone" ])
252
264
}
253
265
}
254
- case 11 :
266
+ case handleError :
255
267
if err != nil && err .Error () != tt .err {
256
268
t .Errorf (err .Error ())
257
269
}
@@ -310,7 +322,7 @@ func TestGetCTLimitsWithBinary(t *testing.T) {
310
322
zones : []uint64 {2 , 3 },
311
323
want : out ,
312
324
err : "" ,
313
- testCase : 10 ,
325
+ testCase : validTest ,
314
326
},
315
327
{
316
328
desc : "Test valid ovs-dpctl ct-get-limits system@ovs-system" ,
@@ -319,7 +331,7 @@ func TestGetCTLimitsWithBinary(t *testing.T) {
319
331
zones : []uint64 {},
320
332
want : outTest2 ,
321
333
err : "" ,
322
- testCase : 10 ,
334
+ testCase : validTest ,
323
335
},
324
336
{
325
337
desc : "Test valid ovs-dpctl ct-get-limits" ,
@@ -328,14 +340,14 @@ func TestGetCTLimitsWithBinary(t *testing.T) {
328
340
zones : []uint64 {},
329
341
want : out ,
330
342
err : "datapath name argument is mandatory" ,
331
- testCase : 11 ,
343
+ testCase : handleError ,
332
344
},
333
345
}
334
346
for _ , tt := range tests {
335
347
t .Run (tt .desc , func (t * testing.T ) {
336
348
results , err := tt .dp .GetCTLimits (tt .dpName , tt .zones )
337
349
switch tt .testCase {
338
- case 10 :
350
+ case validTest :
339
351
if tt .want .defaultLimit ["default" ] != results .defaultLimit ["default" ] {
340
352
t .Errorf ("mismatched values, want %q but got %q" , tt .want .defaultLimit ["default" ], results .defaultLimit ["default" ])
341
353
}
@@ -344,7 +356,7 @@ func TestGetCTLimitsWithBinary(t *testing.T) {
344
356
t .Errorf ("mismatched values, want %q but got %q" , tt .want .zones [i ]["zone" ], z ["zone" ])
345
357
}
346
358
}
347
- case 11 :
359
+ case handleError :
348
360
if err != nil && err .Error () != tt .err {
349
361
t .Errorf (err .Error ())
350
362
}
@@ -528,12 +540,12 @@ func TestSetCTLimitsWithBinary(t *testing.T) {
528
540
529
541
func TestDelCTLimits (t * testing.T ) {
530
542
var tests = []struct {
531
- desc string
532
- dp * DataPathService
533
- zones []uint64
534
- dpName string
535
- err string
536
- res int
543
+ desc string
544
+ dp * DataPathService
545
+ zones []uint64
546
+ dpName string
547
+ err string
548
+ testCase uint8
537
549
}{
538
550
{
539
551
desc : "Test del limit with valid argument" ,
@@ -542,9 +554,9 @@ func TestDelCTLimits(t *testing.T) {
542
554
Version : 10 ,
543
555
},
544
556
},
545
- zones : []uint64 {4 , 3 },
546
- dpName : "system@ovs-system" ,
547
- res : 0 ,
557
+ zones : []uint64 {4 , 3 },
558
+ dpName : "system@ovs-system" ,
559
+ testCase : validTest ,
548
560
},
549
561
{
550
562
desc : "Test del limit with datapath missing" ,
@@ -553,30 +565,30 @@ func TestDelCTLimits(t *testing.T) {
553
565
Version : 10 ,
554
566
},
555
567
},
556
- zones : []uint64 {4 , 3 },
557
- dpName : "" ,
558
- err : "datapath name is missing" ,
559
- res : 1 ,
568
+ zones : []uint64 {4 , 3 },
569
+ dpName : "" ,
570
+ err : "datapath name is missing" ,
571
+ testCase : handleError ,
560
572
},
561
573
{
562
- desc : "Test del limit with empty paramaters" ,
563
- dp : NewDataPathService (),
564
- zones : []uint64 {},
565
- dpName : "" ,
566
- err : "datapath name is missing" ,
567
- res : 1 ,
574
+ desc : "Test del limit with empty paramaters" ,
575
+ dp : NewDataPathService (),
576
+ zones : []uint64 {},
577
+ dpName : "" ,
578
+ err : "datapath name is missing" ,
579
+ testCase : handleError ,
568
580
},
569
581
}
570
582
for _ , tt := range tests {
571
583
t .Run (tt .desc , func (t * testing.T ) {
572
584
res , err := tt .dp .DelCTLimits (tt .dpName , tt .zones )
573
- switch tt .res {
574
- case 0 :
585
+ switch tt .testCase {
586
+ case validTest :
575
587
if err != nil {
576
588
t .Errorf ("got %q and %q" , res , err .Error ())
577
589
}
578
590
579
- case 1 :
591
+ case handleError :
580
592
if err .Error () != tt .err {
581
593
t .Errorf ("got %q and %q" , res , err .Error ())
582
594
}
@@ -594,47 +606,47 @@ func TestDelCTLimitsWithBinary(t *testing.T) {
594
606
}
595
607
596
608
var tests = []struct {
597
- desc string
598
- dp * DataPathService
599
- zones []uint64
600
- dpName string
601
- err string
602
- res int
609
+ desc string
610
+ dp * DataPathService
611
+ zones []uint64
612
+ dpName string
613
+ err string
614
+ testCase uint8
603
615
}{
604
616
{
605
- desc : "Test del limit with valid argument" ,
606
- dp : NewDataPathService (),
607
- zones : []uint64 {4 , 3 },
608
- dpName : "system@ovs-system" ,
609
- res : 0 ,
617
+ desc : "Test del limit with valid argument" ,
618
+ dp : NewDataPathService (),
619
+ zones : []uint64 {4 , 3 },
620
+ dpName : "system@ovs-system" ,
621
+ testCase : validTest ,
610
622
},
611
623
{
612
- desc : "Test del limit with datapath missing" ,
613
- dp : NewDataPathService (),
614
- zones : []uint64 {4 , 3 },
615
- dpName : "" ,
616
- err : "datapath name is missing" ,
617
- res : 1 ,
624
+ desc : "Test del limit with datapath missing" ,
625
+ dp : NewDataPathService (),
626
+ zones : []uint64 {4 , 3 },
627
+ dpName : "" ,
628
+ err : "datapath name is missing" ,
629
+ testCase : handleError ,
618
630
},
619
631
{
620
- desc : "Test del limit with empty paramaters" ,
621
- dp : NewDataPathService (),
622
- zones : []uint64 {},
623
- dpName : "" ,
624
- err : "datapath name is missing" ,
625
- res : 1 ,
632
+ desc : "Test del limit with empty paramaters" ,
633
+ dp : NewDataPathService (),
634
+ zones : []uint64 {},
635
+ dpName : "" ,
636
+ err : "datapath name is missing" ,
637
+ testCase : handleError ,
626
638
},
627
639
}
628
640
for _ , tt := range tests {
629
641
t .Run (tt .desc , func (t * testing.T ) {
630
642
res , err := tt .dp .DelCTLimits (tt .dpName , tt .zones )
631
- switch tt .res {
632
- case 0 :
643
+ switch tt .testCase {
644
+ case validTest :
633
645
if err != nil {
634
646
t .Errorf ("got %q and %q" , res , err .Error ())
635
647
}
636
648
637
- case 1 :
649
+ case handleError :
638
650
if err .Error () != tt .err {
639
651
t .Errorf ("got %q and %q" , res , err .Error ())
640
652
}
0 commit comments