Skip to content

Commit 1da84d1

Browse files
committed
libct/cg: TestGetHugePageSizeImpl: use t.Run
Move test case comments to doc strings, and use t.Run. Suggested-by: Sebastiaan van Stijn <github@gone.nl> Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent f13a932 commit 1da84d1

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

libcontainer/cgroups/utils_test.go

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -463,65 +463,78 @@ func BenchmarkGetHugePageSizeImpl(b *testing.B) {
463463

464464
func TestGetHugePageSizeImpl(t *testing.T) {
465465
testCases := []struct {
466+
doc string
466467
input []string
467468
output []string
468469
isErr bool
469470
}{
470471
{
472+
doc: "normal input",
471473
input: []string{"hugepages-1048576kB", "hugepages-2048kB", "hugepages-32768kB", "hugepages-64kB"},
472474
output: []string{"1GB", "2MB", "32MB", "64KB"},
473475
},
474476
{
477+
doc: "empty input",
475478
input: []string{},
476479
output: []string{},
477480
},
478-
{ // not a number
481+
{
482+
doc: "not a number",
479483
input: []string{"hugepages-akB"},
480484
isErr: true,
481485
},
482-
{ // no prefix (silently skipped)
486+
{
487+
doc: "no prefix (silently skipped)",
483488
input: []string{"1024kB"},
484489
},
485-
{ // invalid prefix (silently skipped)
490+
{
491+
doc: "invalid prefix (silently skipped)",
486492
input: []string{"whatever-1024kB"},
487493
},
488-
{ // invalid suffix
494+
{
495+
doc: "invalid suffix",
489496
input: []string{"hugepages-1024gB"},
490497
isErr: true,
491498
},
492-
{ // no suffix
499+
{
500+
doc: "no suffix",
493501
input: []string{"hugepages-1024"},
494502
isErr: true,
495503
},
496-
{ // mixed valid and invalid entries
504+
{
505+
doc: "mixed valid and invalid entries",
497506
input: []string{"hugepages-4194304kB", "hugepages-2048kB", "hugepages-akB", "hugepages-64kB"},
498507
output: []string{"4GB", "2MB", "64KB"},
499508
isErr: true,
500509
},
501-
{ // mixed valid and invalid entries
510+
{
511+
doc: "more mixed valid and invalid entries",
502512
input: []string{"hugepages-2048kB", "hugepages-kB", "hugepages-64kB"},
503513
output: []string{"2MB", "64KB"},
504514
isErr: true,
505515
},
506516
}
507517

508518
for _, c := range testCases {
509-
output, err := getHugePageSizeFromFilenames(c.input)
510-
t.Log("input:", c.input, "; output:", output, "; err:", err)
511-
if err != nil {
512-
if !c.isErr {
513-
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
514529
}
515-
// no more checks
516-
continue
517-
}
518-
if c.isErr {
519-
t.Errorf("input %v, expected error, got error: nil, output: %v", c.input, output)
520-
}
521-
// check output
522-
if len(output) != len(c.output) || (len(output) > 0 && !reflect.DeepEqual(output, c.output)) {
523-
t.Errorf("input %v, expected %v, got %v", c.input, c.output, output)
524-
}
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+
})
525538
}
526539
}
527540

0 commit comments

Comments
 (0)