8
8
"testing"
9
9
10
10
"github.com/moby/sys/mountinfo"
11
- "github.com/sirupsen/logrus"
12
11
)
13
12
14
13
const fedoraMountinfo = `15 35 0:3 / /proc rw,nosuid,nodev,noexec,relatime shared:5 - proc proc rw
@@ -448,19 +447,6 @@ func TestFindCgroupMountpointAndRoot(t *testing.T) {
448
447
}
449
448
}
450
449
451
- func BenchmarkGetHugePageSize (b * testing.B ) {
452
- var (
453
- output []string
454
- err error
455
- )
456
- for i := 0 ; i < b .N ; i ++ {
457
- output , err = GetHugePageSize ()
458
- }
459
- if err != nil || len (output ) == 0 {
460
- b .Fatal ("unexpected results" )
461
- }
462
- }
463
-
464
450
func BenchmarkGetHugePageSizeImpl (b * testing.B ) {
465
451
var (
466
452
input = []string {"hugepages-1048576kB" , "hugepages-2048kB" , "hugepages-32768kB" , "hugepages-64kB" }
@@ -477,71 +463,78 @@ func BenchmarkGetHugePageSizeImpl(b *testing.B) {
477
463
478
464
func TestGetHugePageSizeImpl (t * testing.T ) {
479
465
testCases := []struct {
466
+ doc string
480
467
input []string
481
468
output []string
482
469
isErr bool
483
- isWarn bool
484
470
}{
485
471
{
472
+ doc : "normal input" ,
486
473
input : []string {"hugepages-1048576kB" , "hugepages-2048kB" , "hugepages-32768kB" , "hugepages-64kB" },
487
474
output : []string {"1GB" , "2MB" , "32MB" , "64KB" },
488
475
},
489
476
{
477
+ doc : "empty input" ,
490
478
input : []string {},
491
479
output : []string {},
492
480
},
493
- { // not a number
481
+ {
482
+ doc : "not a number" ,
494
483
input : []string {"hugepages-akB" },
495
484
isErr : true ,
496
485
},
497
- { // no prefix (silently skipped)
486
+ {
487
+ doc : "no prefix (silently skipped)" ,
498
488
input : []string {"1024kB" },
499
489
},
500
- { // invalid prefix (silently skipped)
490
+ {
491
+ doc : "invalid prefix (silently skipped)" ,
501
492
input : []string {"whatever-1024kB" },
502
493
},
503
- { // invalid suffix (skipped with a warning)
504
- input : []string {"hugepages-1024gB" },
505
- isWarn : true ,
494
+ {
495
+ doc : "invalid suffix" ,
496
+ input : []string {"hugepages-1024gB" },
497
+ isErr : true ,
506
498
},
507
- { // no suffix (skipped with a warning)
508
- input : []string {"hugepages-1024" },
509
- isWarn : true ,
499
+ {
500
+ doc : "no suffix" ,
501
+ input : []string {"hugepages-1024" },
502
+ isErr : true ,
503
+ },
504
+ {
505
+ doc : "mixed valid and invalid entries" ,
506
+ input : []string {"hugepages-4194304kB" , "hugepages-2048kB" , "hugepages-akB" , "hugepages-64kB" },
507
+ output : []string {"4GB" , "2MB" , "64KB" },
508
+ isErr : true ,
509
+ },
510
+ {
511
+ doc : "more mixed valid and invalid entries" ,
512
+ input : []string {"hugepages-2048kB" , "hugepages-kB" , "hugepages-64kB" },
513
+ output : []string {"2MB" , "64KB" },
514
+ isErr : true ,
510
515
},
511
516
}
512
517
513
- // Need to catch warnings.
514
- savedOut := logrus .StandardLogger ().Out
515
- defer logrus .SetOutput (savedOut )
516
- warns := new (bytes.Buffer )
517
- logrus .SetOutput (warns )
518
-
519
518
for _ , c := range testCases {
520
- warns .Reset ()
521
- output , err := getHugePageSizeFromFilenames (c .input )
522
- if err != nil {
523
- if ! c .isErr {
524
- t .Errorf ("input %v, expected nil, got error: %v" , c .input , err )
519
+ c := c
520
+ t .Run (c .doc , func (t * testing.T ) {
521
+ output , err := getHugePageSizeFromFilenames (c .input )
522
+ t .Log ("input:" , c .input , "; output:" , output , "; err:" , err )
523
+ if err != nil {
524
+ if ! c .isErr {
525
+ t .Errorf ("input %v, expected nil, got error: %v" , c .input , err )
526
+ }
527
+ // no more checks
528
+ return
525
529
}
526
- // no more checks
527
- continue
528
- }
529
- if c .isErr {
530
- t .Errorf ("input %v, expected error, got error: nil, output: %v" , c .input , output )
531
- // no more checks
532
- continue
533
- }
534
- // check for warnings
535
- if c .isWarn && warns .Len () == 0 {
536
- t .Errorf ("input %v, expected a warning, got none" , c .input )
537
- }
538
- if ! c .isWarn && warns .Len () > 0 {
539
- t .Errorf ("input %v, unexpected warning: %s" , c .input , warns .String ())
540
- }
541
- // check output
542
- if len (output ) != len (c .output ) || (len (output ) > 0 && ! reflect .DeepEqual (output , c .output )) {
543
- t .Errorf ("input %v, expected %v, got %v" , c .input , c .output , output )
544
- }
530
+ if c .isErr {
531
+ t .Errorf ("input %v, expected error, got error: nil, output: %v" , c .input , output )
532
+ }
533
+ // check output
534
+ if len (output ) != len (c .output ) || (len (output ) > 0 && ! reflect .DeepEqual (output , c .output )) {
535
+ t .Errorf ("input %v, expected %v, got %v" , c .input , c .output , output )
536
+ }
537
+ })
545
538
}
546
539
}
547
540
0 commit comments