cpu_test.go 1.05 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
package fs2

import (
	"fmt"
	"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)

	tmpCPUMaxFile := filepath.Join(tmpCgroupDir, "cpu.max")
	DefaultCPULimit := float64(2)

	type cpuTest struct {
		period string
		quota  string
		limit  float64
	}
	cpuTests := []cpuTest{
		{period: "100000", quota: "max", 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(tmpCPUMaxFile, []byte(fmt.Sprintf("%s %s", test.quota, test.period)), 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)
		}
	}
}