Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions test/cli_run_blkio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"time"

"github.com/gotestyourself/gotestyourself/icmd"
)

// WaitTimeout wait at most timeout nanoseconds,
Expand Down Expand Up @@ -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
}