io_test.go 1.45 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
package fs2

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)

	tmpIOMaxFile := filepath.Join(tmpCgroupDir, "io.max")
	devInfos := []types.DevInfo{{Name: "test-sda", Major: "1", Minor: "8"}}

	type ioTest struct {
		limit    uint64
		expected string
	}
	// 实际格式 8:0 rbytes=0 wbytes=12288 rios=0 wios=3 dbytes=0 dios=0
	// 因写文件无法模拟真实的cgroup设置,使用非真实的期望结果做判断
	ioTests := []ioTest{
		{limit: 10 * 1024 * 1024, expected: "1:8 rbps=104857601:8 wbps=10485760"},
		{limit: 0, expected: "1:8 rbps=max1:8 wbps=max"},
	}
	for i, test := range ioTests {
		// 清除测试环境
		os.Remove(tmpIOMaxFile)

		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")
		}

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