cgroup_path_test.go 1.57 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
package cgroup

import "testing"

func TestParseRelativeCgroupPathV1(t *testing.T) {
	relativePath, err := parseRelativeCgroupPathV1("testdata/cgroup_v1", "memory")
	if err != nil {
		t.Fatal(err)
	}
	if relativePath != "/kubepods/besteffort/pod30d45881-d876-4898-a212-f8c95e1b08db/b5209b0109b0fc07d889f4f7878354670c8fbc6a977201be3a6f6aa389187f1b" {
		t.Fatal("parse cgroup_v1 memory failed")
	}

	relativePath, err = parseRelativeCgroupPathV1("testdata/cgroup_v1", "cpuacct")
	if err != nil {
		t.Fatal(err)
	}
	if relativePath != "/kubepods/besteffort/pod30d45881-d876-4898-a212-f8c95e1b08db/b5209b0109b0fc07d889f4f7878354670c8fbc6a977201be3a6f6aa389187f1b" {
		t.Fatal("parse cgroup_v1 cpuacct failed")
	}

	_, err = parseRelativeCgroupPathV1("testdata/cgroup_v1", "unknown")
	if err == nil {
		t.Fatal("parse cgroup_v1 unknown failed")
	}
}

func TestParseRelativeCgroupPathV2(t *testing.T) {
	relativePath, err := parseRelativeCgroupPathV2("testdata/cgroup_v2_k8s")
	if err != nil {
		t.Fatal(err)
	}
	if relativePath != "/kubepods.slice/kubepods-burstable.slice/xxxxxxxxxx-1654a9da9950616391265776d0c38f1ab55c95afbc5581ed9f7ff0c370b25ebf.scope" {
		t.Fatal("parse cgroup_v2_k8s failed")
	}

	relativePath, err = parseRelativeCgroupPathV2("testdata/cgroup_v2_docker")
	if err != nil {
		t.Fatal(err)
	}
	if relativePath != "/system.slice/docker-1654a9da9950616391265776d0c38f1ab55c95afbc5581ed9f7ff0c370b25ebf.scope" {
		t.Fatal("parse cgroup_v2_docker failed")
	}

	_, err = parseRelativeCgroupPathV2("testdata/cgroup_v2_systemd")
	if err == nil {
		t.Fatal("parse cgroup_v2_systemd failed")
	}
}