summaryrefslogtreecommitdiff
path: root/daemon/daemon_unix_test.go
diff options
context:
space:
mode:
authorfrankyang <yyb196@gmail.com>2019-05-14 15:21:55 +0800
committerfrankyang <yyb196@gmail.com>2019-05-16 15:32:59 +0800
commitb9f31912deb511e732763e4fa5ecd0208b104eb2 (patch)
treec9f40e162583db1495f0a924975211497f5c3b69 /daemon/daemon_unix_test.go
parent3042254a87274ff5e9561f2da1a986a703dfc60f (diff)
downloaddocker-b9f31912deb511e732763e4fa5ecd0208b104eb2.tar.gz
bugfix: fetch the right device number which great than 255
Signed-off-by: frankyang <yyb196@gmail.com>
Diffstat (limited to 'daemon/daemon_unix_test.go')
-rw-r--r--daemon/daemon_unix_test.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/daemon/daemon_unix_test.go b/daemon/daemon_unix_test.go
index c8575ad8da..c884681b9a 100644
--- a/daemon/daemon_unix_test.go
+++ b/daemon/daemon_unix_test.go
@@ -6,12 +6,15 @@ import (
"errors"
"io/ioutil"
"os"
+ "path/filepath"
"testing"
+ "github.com/docker/docker/api/types/blkiodev"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/container"
"github.com/docker/docker/daemon/config"
"github.com/docker/docker/pkg/sysinfo"
+ "golang.org/x/sys/unix"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
@@ -376,3 +379,61 @@ func sysInfo(t *testing.T, opts ...func(*sysinfo.SysInfo)) sysinfo.SysInfo {
}
return si
}
+
+const (
+ // prepare major 0x1FD(509 in decimal) and minor 0x130(304)
+ DEVNO = 0x11FD30
+ MAJOR = 509
+ MINOR = 304
+ WEIGHT = 1024
+)
+
+func deviceTypeMock(t *testing.T, testAndCheck func(string)) {
+ if os.Getuid() != 0 {
+ t.Skip("root required") // for mknod
+ }
+
+ t.Parallel()
+
+ tempDir, err := ioutil.TempDir("", "tempDevDir"+t.Name())
+ assert.NilError(t, err, "create temp file")
+ tempFile := filepath.Join(tempDir, "dev")
+
+ defer os.RemoveAll(tempDir)
+
+ if err = unix.Mknod(tempFile, unix.S_IFCHR, DEVNO); err != nil {
+ t.Fatalf("mknod error %s(%x): %v", tempFile, DEVNO, err)
+ }
+
+ testAndCheck(tempFile)
+}
+
+func TestGetBlkioWeightDevices(t *testing.T) {
+ deviceTypeMock(t, func(tempFile string) {
+ mockResource := containertypes.Resources{
+ BlkioWeightDevice: []*blkiodev.WeightDevice{{Path: tempFile, Weight: WEIGHT}},
+ }
+
+ weightDevs, err := getBlkioWeightDevices(mockResource)
+
+ assert.NilError(t, err, "getBlkioWeightDevices")
+ assert.Check(t, is.Len(weightDevs, 1), "getBlkioWeightDevices")
+ assert.Check(t, weightDevs[0].Major == MAJOR, "get major device type")
+ assert.Check(t, weightDevs[0].Minor == MINOR, "get minor device type")
+ assert.Check(t, *weightDevs[0].Weight == WEIGHT, "get device weight")
+ })
+}
+
+func TestGetBlkioThrottleDevices(t *testing.T) {
+ deviceTypeMock(t, func(tempFile string) {
+ mockDevs := []*blkiodev.ThrottleDevice{{Path: tempFile, Rate: WEIGHT}}
+
+ retDevs, err := getBlkioThrottleDevices(mockDevs)
+
+ assert.NilError(t, err, "getBlkioThrottleDevices")
+ assert.Check(t, is.Len(retDevs, 1), "getBlkioThrottleDevices")
+ assert.Check(t, retDevs[0].Major == MAJOR, "get major device type")
+ assert.Check(t, retDevs[0].Minor == MINOR, "get minor device type")
+ assert.Check(t, retDevs[0].Rate == WEIGHT, "get device rate")
+ })
+}