diff --git a/test/cli_run_blkio_test.go b/test/cli_run_blkio_test.go index e28c27fcd..91a0be197 100644 --- a/test/cli_run_blkio_test.go +++ b/test/cli_run_blkio_test.go @@ -4,11 +4,13 @@ import ( "encoding/json" "fmt" "io/ioutil" + "strconv" "strings" "github.com/alibaba/pouch/apis/types" "github.com/alibaba/pouch/test/command" "github.com/alibaba/pouch/test/environment" + "github.com/alibaba/pouch/test/util" "github.com/go-check/check" "github.com/gotestyourself/gotestyourself/icmd" @@ -37,8 +39,9 @@ func (suite *PouchRunBlkioSuite) TearDownTest(c *check.C) { // TestRunBlockIOWeight tests running container with --blkio-weight flag. func (suite *PouchRunBlkioSuite) TestRunBlockIOWeight(c *check.C) { cname := "TestRunBlockIOWeight" + strvalue := "100" - res := command.PouchRun("run", "-d", "--blkio-weight", "100", + res := command.PouchRun("run", "-d", "--blkio-weight", strvalue, "--name", cname, busyboxImage, "sleep", "10000") defer DelContainerForceMultyTime(c, cname) res.Assert(c, icmd.Success) @@ -52,19 +55,29 @@ func (suite *PouchRunBlkioSuite) TestRunBlockIOWeight(c *check.C) { c.Errorf("failed to decode inspect output: %v", err) } - c.Assert(result[0].HostConfig.BlkioWeight, check.Equals, uint16(100)) + value, _ := strconv.Atoi(strvalue) + c.Assert(result[0].HostConfig.BlkioWeight, check.Equals, uint16(value)) // test if cgroup has record the real value containerID := result[0].ID path := fmt.Sprintf( "/sys/fs/cgroup/blkio/default/%s/blkio.weight", containerID) - checkFileContains(c, path, "100") + checkFileContains(c, path, strvalue) + + // test if the value is correct in container + blkioWeightFile := "/sys/fs/cgroup/blkio/blkio.weight" + res = command.PouchRun("exec", cname, "cat", blkioWeightFile) + res.Assert(c, icmd.Success) + + out := res.Stdout() + c.Assert(out, check.Equals, strvalue+"\n") } // TestRunBlockIOWeightDevice tests running container // with --blkio-weight-device flag. func (suite *PouchRunBlkioSuite) TestRunBlockIOWeightDevice(c *check.C) { cname := "TestRunBlockIOWeightDevice" + value := 100 testDisk, found := environment.FindDisk() if !found { c.Skip("fail to find available disk for blkio test") @@ -80,7 +93,7 @@ func (suite *PouchRunBlkioSuite) TestRunBlockIOWeightDevice(c *check.C) { }) res := command.PouchRun("run", "-d", - "--blkio-weight-device", testDisk+":100", + "--blkio-weight-device", testDisk+":"+strconv.Itoa(value), "--name", cname, busyboxImage, "sleep", "10000") defer DelContainerForceMultyTime(c, cname) res.Assert(c, icmd.Success) @@ -98,7 +111,28 @@ func (suite *PouchRunBlkioSuite) TestRunBlockIOWeightDevice(c *check.C) { c.Assert(result[0].HostConfig.BlkioWeightDevice[0].Path, check.Equals, testDisk) c.Assert(result[0].HostConfig.BlkioWeightDevice[0].Weight, - check.Equals, uint16(100)) + check.Equals, uint16(value)) + + number, exist := util.GetMajMinNumOfDevice(testDisk) + if !exist { + c.Skip("fail to get major:minor device number") + } + + expected := fmt.Sprintf("%s %d\n", number, value) + + // test if the value is correct on the host + containerID := result[0].ID + path := fmt.Sprintf( + "/sys/fs/cgroup/blkio/default/%s/blkio.weight_device", containerID) + checkFileContains(c, path, strings.Trim(expected, "\n")) + + // test if the value is correct in container + blkioWeightDevFile := "/sys/fs/cgroup/blkio/blkio.weight_device" + res = command.PouchRun("exec", cname, "cat", blkioWeightDevFile) + res.Assert(c, icmd.Success) + + out := res.Stdout() + c.Assert(out, check.Equals, expected) } // TestRunWithBlkioWeight is to verify --specific Blkio Weight diff --git a/test/util/util.go b/test/util/util.go index f079c98c7..5346e0ba7 100644 --- a/test/util/util.go +++ b/test/util/util.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" "time" + + "github.com/gotestyourself/gotestyourself/icmd" ) // WaitTimeout wait at most timeout nanoseconds, @@ -54,3 +56,13 @@ func TrimAllSpaceAndNewline(input string) string { return output } + +// GetMajMinNumOfDevice is used for getting major:minor device number +func GetMajMinNumOfDevice(device string) (string, bool) { + cmd := fmt.Sprintf("lsblk -d -o MAJ:MIN %s | sed /MAJ:MIN/d | awk '{print $1}'", device) + number := icmd.RunCommand("bash", "-c", cmd).Stdout() + if number != "" { + return strings.Trim(number, "\n"), true + } + return "", false +}