/
process_linux_test.go
176 lines (160 loc) · 4.17 KB
/
process_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//go:build linux
// +build linux
package process
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Process_splitProcStat(t *testing.T) {
expectedFieldsNum := 53
statLineContent := make([]string, expectedFieldsNum-1)
for i := 0; i < expectedFieldsNum-1; i++ {
statLineContent[i] = strconv.Itoa(i + 1)
}
cases := []string{
"ok",
"ok)",
"(ok",
"ok )",
"ok )(",
"ok )()",
"() ok )()",
"() ok (()",
" ) ok )",
"(ok) (ok)",
}
consideredFields := []int{4, 7, 10, 11, 12, 13, 14, 15, 18, 22, 42}
commandNameIndex := 2
for _, expectedName := range cases {
statLineContent[commandNameIndex-1] = "(" + expectedName + ")"
statLine := strings.Join(statLineContent, " ")
t.Run(fmt.Sprintf("name: %s", expectedName), func(t *testing.T) {
parsedStatLine := splitProcStat([]byte(statLine))
assert.Equal(t, expectedName, parsedStatLine[commandNameIndex])
for _, idx := range consideredFields {
expected := strconv.Itoa(idx)
parsed := parsedStatLine[idx]
assert.Equal(
t, expected, parsed,
"field %d (index from 1 as in man proc) must be %q but %q is received",
idx, expected, parsed,
)
}
})
}
}
func Test_Process_splitProcStat_fromFile(t *testing.T) {
pids, err := ioutil.ReadDir("testdata/linux/")
if err != nil {
t.Error(err)
}
t.Setenv("HOST_PROC", "testdata/linux")
for _, pid := range pids {
pid, err := strconv.ParseInt(pid.Name(), 0, 32)
if err != nil {
continue
}
statFile := fmt.Sprintf("testdata/linux/%d/stat", pid)
if _, err := os.Stat(statFile); err != nil {
continue
}
contents, err := ioutil.ReadFile(statFile)
assert.NoError(t, err)
pidStr := strconv.Itoa(int(pid))
ppid := "68044" // TODO: how to pass ppid to test?
fields := splitProcStat(contents)
assert.Equal(t, fields[1], pidStr)
assert.Equal(t, fields[2], "test(cmd).sh")
assert.Equal(t, fields[3], "S")
assert.Equal(t, fields[4], ppid)
assert.Equal(t, fields[5], pidStr) // pgrp
assert.Equal(t, fields[6], ppid) // session
assert.Equal(t, fields[8], pidStr) // tpgrp
assert.Equal(t, fields[18], "20") // priority
assert.Equal(t, fields[20], "1") // num threads
assert.Equal(t, fields[52], "0") // exit code
}
}
func Test_fillFromCommWithContext(t *testing.T) {
pids, err := ioutil.ReadDir("testdata/linux/")
if err != nil {
t.Error(err)
}
t.Setenv("HOST_PROC", "testdata/linux")
for _, pid := range pids {
pid, err := strconv.ParseInt(pid.Name(), 0, 32)
if err != nil {
continue
}
if _, err := os.Stat(fmt.Sprintf("testdata/linux/%d/status", pid)); err != nil {
continue
}
p, _ := NewProcess(int32(pid))
if err := p.fillFromCommWithContext(); err != nil {
t.Error(err)
}
}
}
func Test_fillFromStatusWithContext(t *testing.T) {
pids, err := ioutil.ReadDir("testdata/linux/")
if err != nil {
t.Error(err)
}
t.Setenv("HOST_PROC", "testdata/linux")
for _, pid := range pids {
pid, err := strconv.ParseInt(pid.Name(), 0, 32)
if err != nil {
continue
}
if _, err := os.Stat(fmt.Sprintf("testdata/linux/%d/status", pid)); err != nil {
continue
}
p, _ := NewProcess(int32(pid))
if err := p.fillFromStatus(); err != nil {
t.Error(err)
}
}
}
func Benchmark_fillFromCommWithContext(b *testing.B) {
b.Setenv("HOST_PROC", "testdata/linux")
pid := 1060
p, _ := NewProcess(int32(pid))
for i := 0; i < b.N; i++ {
p.fillFromCommWithContext()
}
}
func Benchmark_fillFromStatusWithContext(b *testing.B) {
b.Setenv("HOST_PROC", "testdata/linux")
pid := 1060
p, _ := NewProcess(int32(pid))
for i := 0; i < b.N; i++ {
p.fillFromStatus()
}
}
func Test_fillFromTIDStatWithContext_lx_brandz(t *testing.T) {
pids, err := ioutil.ReadDir("testdata/lx_brandz/")
if err != nil {
t.Error(err)
}
t.Setenv("HOST_PROC", "testdata/lx_brandz")
for _, pid := range pids {
pid, err := strconv.ParseInt(pid.Name(), 0, 32)
if err != nil {
continue
}
if _, err := os.Stat(fmt.Sprintf("testdata/lx_brandz/%d/stat", pid)); err != nil {
continue
}
p, _ := NewProcess(int32(pid))
_, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStat(-1)
if err != nil {
t.Error(err)
}
assert.Equal(t, float64(0), cpuTimes.Iowait)
}
}