-
Notifications
You must be signed in to change notification settings - Fork 0
/
encio_id_linux_test.go
98 lines (88 loc) · 2.87 KB
/
encio_id_linux_test.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package encio
import (
"os"
"path/filepath"
"testing"
)
type testFiles struct {
dockerCgroup string
linuxCgroup string
newDockerCgroup string
}
// createTestCgroupFiles creates mock files for tests
func createTestCgroupFiles(t *testing.T, dir string) *testFiles {
// docker cgroup setup
var files = testFiles{
dockerCgroup: filepath.Join(dir, "docker.cgroup"),
linuxCgroup: filepath.Join(dir, "linux.cgroup"),
newDockerCgroup: filepath.Join(dir, "docker_new.cgroup"),
}
if err := os.WriteFile(files.dockerCgroup, []byte(dockerCgroup), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(files.linuxCgroup, []byte(linuxCgroup), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(files.newDockerCgroup, []byte(newDockerCgroup), 0644); err != nil {
t.Fatal(err)
}
return &files
}
func Test_isInContainer(t *testing.T) {
dir := t.TempDir()
files := createTestCgroupFiles(t, dir)
// TEST
type args struct {
cgroupPath string
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{"docker", args{files.dockerCgroup}, true, false},
{"linux", args{files.linuxCgroup}, false, false},
{"docker new", args{files.newDockerCgroup}, true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := isInContainer(tt.args.cgroupPath)
if (err != nil) != tt.wantErr {
t.Errorf("isInContainer() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("isInContainer() = %v, want %v", got, tt.want)
}
})
}
}
const (
dockerCgroup = `13:name=systemd:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
12:pids:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
11:hugetlb:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
10:net_prio:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
9:perf_event:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
8:net_cls:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
7:freezer:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
6:devices:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
5:memory:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
4:blkio:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
3:cpuacct:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
2:cpu:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee
1:cpuset:/docker/bc9f0894926991e3064b731c26d86af6df7390c0e6453e6027f9545aba5809ee`
linuxCgroup = `11:cpuset:/
10:pids:/init.scope
9:perf_event:/
8:memory:/init.scope
7:blkio:/
6:devices:/init.scope
5:rdma:/
4:net_cls,net_prio:/
3:freezer:/
2:cpu,cpuacct:/
1:name=systemd:/init.scope
0::/init.scope`
newDockerCgroup = `0::/`
)