@@ -21,7 +21,6 @@ import (
2121 "context"
2222 "fmt"
2323 "os"
24- "os/exec"
2524 "path/filepath"
2625 "strconv"
2726 "strings"
@@ -495,31 +494,33 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
495494 // For now, disable the test unless on a recent kernel.
496495 testutil .RequireKernelVersion (t , ">= 6.0.0-0" )
497496
498- // Create dummy device path
499- dummyDev := "/dev/dummy-zero"
500-
497+ const (
498+ weight = "150"
499+ deviceWeight = "100"
500+ readBps = "1048576"
501+ readIops = "1000"
502+ writeBps = "2097152"
503+ writeIops = "2000"
504+ )
505+ var lo * loopback.Loopback
501506 testCase .Setup = func (data test.Data , helpers test.Helpers ) {
502- // Create dummy device
503- helperCmd := exec .Command ("mknod" , dummyDev , "c" , "1" , "5" )
504- if out , err := helperCmd .CombinedOutput (); err != nil {
505- t .Fatalf ("cannot create %q: %q: %v" , dummyDev , string (out ), err )
506- }
507+ var err error
508+ lo , err = loopback .New (4096 )
509+ assert .NilError (t , err )
510+ t .Logf ("loopback device: %+v" , lo )
507511 }
508-
509512 testCase .Cleanup = func (data test.Data , helpers test.Helpers ) {
510- // Clean up the dummy device
511- if err := exec .Command ("rm" , "-f" , dummyDev ).Run (); err != nil {
512- t .Logf ("failed to remove device %s: %v" , dummyDev , err )
513+ if lo != nil {
514+ _ = lo .Close ()
513515 }
514516 }
515-
516517 testCase .SubTests = []* test.Case {
517518 {
518519 Description : "blkio-weight" ,
519520 Require : nerdtest .CGroupV2 ,
520521 Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
521522 return helpers .Command ("run" , "-d" , "--name" , data .Identifier (),
522- "--blkio-weight" , "150" ,
523+ "--blkio-weight" , weight ,
523524 testutil .AlpineImage , "sleep" , "infinity" )
524525 },
525526 Cleanup : func (data test.Data , helpers test.Helpers ) {
@@ -530,7 +531,7 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
530531 ExitCode : 0 ,
531532 Output : expect .All (
532533 func (stdout string , t tig.T ) {
533- assert .Assert (t , strings .Contains (helpers .Capture ("inspect" , "--format" , "{{.HostConfig.BlkioWeight}}" , data .Identifier ()), "150" ))
534+ assert .Assert (t , strings .Contains (helpers .Capture ("inspect" , "--format" , "{{.HostConfig.BlkioWeight}}" , data .Identifier ()), weight ))
534535 },
535536 ),
536537 }
@@ -541,7 +542,7 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
541542 Require : nerdtest .CGroupV2 ,
542543 Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
543544 return helpers .Command ("run" , "-d" , "--name" , data .Identifier (),
544- "--blkio-weight-device" , dummyDev + ":100" ,
545+ "--blkio-weight-device" , fmt . Sprintf ( "%s:%s" , lo . Device , deviceWeight ) ,
545546 testutil .AlpineImage , "sleep" , "infinity" )
546547 },
547548 Cleanup : func (data test.Data , helpers test.Helpers ) {
@@ -553,7 +554,11 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
553554 Output : expect .All (
554555 func (stdout string , t tig.T ) {
555556 inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioWeightDevice}}{{.Weight}}{{end}}" , data .Identifier ())
556- assert .Assert (t , strings .Contains (inspectOut , "100" ))
557+ assert .Assert (t , strings .Contains (inspectOut , deviceWeight ))
558+ },
559+ func (stdout string , t tig.T ) {
560+ inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioWeightDevice}}{{.Path}}{{end}}" , data .Identifier ())
561+ assert .Assert (t , strings .Contains (inspectOut , lo .Device ))
557562 },
558563 ),
559564 }
@@ -570,7 +575,7 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
570575 ),
571576 Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
572577 return helpers .Command ("run" , "-d" , "--name" , data .Identifier (),
573- "--device-read-bps" , dummyDev + ":1048576" ,
578+ "--device-read-bps" , fmt . Sprintf ( "%s:%s" , lo . Device , readBps ) ,
574579 testutil .AlpineImage , "sleep" , "infinity" )
575580 },
576581 Cleanup : func (data test.Data , helpers test.Helpers ) {
@@ -582,7 +587,11 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
582587 Output : expect .All (
583588 func (stdout string , t tig.T ) {
584589 inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioDeviceReadBps}}{{.Rate}}{{end}}" , data .Identifier ())
585- assert .Assert (t , strings .Contains (inspectOut , "1048576" ))
590+ assert .Assert (t , strings .Contains (inspectOut , readBps ))
591+ },
592+ func (stdout string , t tig.T ) {
593+ inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioDeviceReadBps}}{{.Path}}{{end}}" , data .Identifier ())
594+ assert .Assert (t , strings .Contains (inspectOut , lo .Device ))
586595 },
587596 ),
588597 }
@@ -599,7 +608,7 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
599608 ),
600609 Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
601610 return helpers .Command ("run" , "-d" , "--name" , data .Identifier (),
602- "--device-write-bps" , dummyDev + ":2097152" ,
611+ "--device-write-bps" , fmt . Sprintf ( "%s:%s" , lo . Device , writeBps ) ,
603612 testutil .AlpineImage , "sleep" , "infinity" )
604613 },
605614 Cleanup : func (data test.Data , helpers test.Helpers ) {
@@ -611,7 +620,11 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
611620 Output : expect .All (
612621 func (stdout string , t tig.T ) {
613622 inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioDeviceWriteBps}}{{.Rate}}{{end}}" , data .Identifier ())
614- assert .Assert (t , strings .Contains (inspectOut , "2097152" ))
623+ assert .Assert (t , strings .Contains (inspectOut , writeBps ))
624+ },
625+ func (stdout string , t tig.T ) {
626+ inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioDeviceWriteBps}}{{.Path}}{{end}}" , data .Identifier ())
627+ assert .Assert (t , strings .Contains (inspectOut , lo .Device ))
615628 },
616629 ),
617630 }
@@ -628,7 +641,7 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
628641 ),
629642 Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
630643 return helpers .Command ("run" , "-d" , "--name" , data .Identifier (),
631- "--device-read-iops" , dummyDev + ":1000" ,
644+ "--device-read-iops" , fmt . Sprintf ( "%s:%s" , lo . Device , readIops ) ,
632645 testutil .AlpineImage , "sleep" , "infinity" )
633646 },
634647 Cleanup : func (data test.Data , helpers test.Helpers ) {
@@ -640,7 +653,11 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
640653 Output : expect .All (
641654 func (stdout string , t tig.T ) {
642655 inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioDeviceReadIOps}}{{.Rate}}{{end}}" , data .Identifier ())
643- assert .Assert (t , strings .Contains (inspectOut , "1000" ))
656+ assert .Assert (t , strings .Contains (inspectOut , readIops ))
657+ },
658+ func (stdout string , t tig.T ) {
659+ inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioDeviceReadIOps}}{{.Path}}{{end}}" , data .Identifier ())
660+ assert .Assert (t , strings .Contains (inspectOut , lo .Device ))
644661 },
645662 ),
646663 }
@@ -657,7 +674,7 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
657674 ),
658675 Command : func (data test.Data , helpers test.Helpers ) test.TestableCommand {
659676 return helpers .Command ("run" , "-d" , "--name" , data .Identifier (),
660- "--device-write-iops" , dummyDev + ":2000" ,
677+ "--device-write-iops" , fmt . Sprintf ( "%s:%s" , lo . Device , writeIops ) ,
661678 testutil .AlpineImage , "sleep" , "infinity" )
662679 },
663680 Cleanup : func (data test.Data , helpers test.Helpers ) {
@@ -669,7 +686,11 @@ func TestRunBlkioSettingCgroupV2(t *testing.T) {
669686 Output : expect .All (
670687 func (stdout string , t tig.T ) {
671688 inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioDeviceWriteIOps}}{{.Rate}}{{end}}" , data .Identifier ())
672- assert .Assert (t , strings .Contains (inspectOut , "2000" ))
689+ assert .Assert (t , strings .Contains (inspectOut , writeIops ))
690+ },
691+ func (stdout string , t tig.T ) {
692+ inspectOut := helpers .Capture ("inspect" , "--format" , "{{range .HostConfig.BlkioDeviceWriteIOps}}{{.Path}}{{end}}" , data .Identifier ())
693+ assert .Assert (t , strings .Contains (inspectOut , lo .Device ))
673694 },
674695 ),
675696 }
0 commit comments