blkio_test.go 1.63 KB
Newer Older
“李磊”'s avatar
“李磊” committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package fs

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"linkfog.com/public/lib/cgroup/types"
)

func TestSetIORWLimit(t *testing.T) {
	tmpCgroupDir, err := ioutil.TempDir("/tmp", "cgroup-")
	if err != nil {
		t.Fatal("Cannot create temporary file", err)
	}
	defer os.RemoveAll(tmpCgroupDir)

	tmpIORdLimitFile := filepath.Join(tmpCgroupDir, "blkio.throttle.read_bps_device")
	tmpIOWrLimitFile := filepath.Join(tmpCgroupDir, "blkio.throttle.write_bps_device")
	devInfos := []types.DevInfo{{Name: "test-sda", Major: "1", Minor: "8"}}

	type ioTest struct {
		limit    uint64
		expected string
	}
	ioTests := []ioTest{
		{limit: 10 * 1024 * 1024, expected: "1:8 10485760"},
		{limit: 0, expected: "1:8 0"},
	}
	for i, test := range ioTests {
		// 清除测试环境
		os.Remove(tmpIORdLimitFile)
		os.Remove(tmpIOWrLimitFile)

		devLmts, err := SetIORWLimit(tmpCgroupDir, devInfos, test.limit, test.limit)
		if err != nil {
			t.Fatal(i, err)
		}
		if devLmt, ok := devLmts["1:8"]; !ok {
			t.Fatal(i, "device 1:8 not exist")
		} else if !devLmt.RdLimit || !devLmt.WrLimit {
			t.Fatal(i, "device 1:8 rw limit error")
		}

		ioRdLimitContent, err := ioutil.ReadFile(tmpIORdLimitFile)
		if err != nil {
			t.Fatal(i, err)
		}
		if string(ioRdLimitContent) != test.expected {
			t.Logf("%d io read limit error, content: %s", i, string(ioRdLimitContent))
		}

		ioWrLimitContent, err := ioutil.ReadFile(tmpIOWrLimitFile)
		if err != nil {
			t.Fatal(i, err)
		}
		if string(ioWrLimitContent) != test.expected {
			t.Fatalf("%d io write limit error, content: %s", i, string(ioWrLimitContent))
		}
	}
}