cpuacct_test.go 1.24 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
package fs

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

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

	tmpCPUPeriodFile := filepath.Join(tmpCgroupDir, "cpu.cfs_period_us")
	tmpCPUQuotaFile := filepath.Join(tmpCgroupDir, "cpu.cfs_quota_us")
	DefaultCPULimit := float64(2)

	type cpuTest struct {
		period string
		quota  string
		limit  float64
	}
	cpuTests := []cpuTest{
		{period: "100000", quota: defaultCPU, limit: DefaultCPULimit},
		{period: "100000", quota: "200000", limit: float64(2)},
		{period: "100000", quota: "0", limit: DefaultCPULimit},
		{period: "0", quota: "200000", limit: DefaultCPULimit},
	}
	for i, test := range cpuTests {
		err = ioutil.WriteFile(tmpCPUPeriodFile, []byte(test.period), 0)
		if err != nil {
			t.Fatal(i, "Failed to write to temporary file", err)
		}
		err = ioutil.WriteFile(tmpCPUQuotaFile, []byte(test.quota), 0)
		if err != nil {
			t.Fatal(i, "Failed to write to temporary file", err)
		}

		cpuLimit := GetCPULimit(tmpCgroupDir, DefaultCPULimit)
		if cpuLimit != test.limit {
			t.Fatalf("%d cpuLimit(%f) != expected(%f)", i, cpuLimit, test.limit)
		}
	}
}