forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
procstat_test.go
388 lines (322 loc) · 8.72 KB
/
procstat_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package procstat
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/influxdata/telegraf/testutil"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/process"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func init() {
execCommand = mockExecCommand
}
func mockExecCommand(arg0 string, args ...string) *exec.Cmd {
args = append([]string{"-test.run=TestMockExecCommand", "--", arg0}, args...)
cmd := exec.Command(os.Args[0], args...)
cmd.Stderr = os.Stderr
return cmd
}
func TestMockExecCommand(t *testing.T) {
var cmd []string
for _, arg := range os.Args {
if string(arg) == "--" {
cmd = []string{}
continue
}
if cmd == nil {
continue
}
cmd = append(cmd, string(arg))
}
if cmd == nil {
return
}
cmdline := strings.Join(cmd, " ")
if cmdline == "systemctl show TestGather_systemdUnitPIDs" {
fmt.Printf(`PIDFile=
GuessMainPID=yes
MainPID=11408
ControlPID=0
ExecMainPID=11408
`)
os.Exit(0)
}
fmt.Printf("command not found\n")
os.Exit(1)
}
type testPgrep struct {
pids []PID
err error
}
func pidFinder(pids []PID, err error) func() (PIDFinder, error) {
return func() (PIDFinder, error) {
return &testPgrep{
pids: pids,
err: err,
}, nil
}
}
func (pg *testPgrep) PidFile(path string) ([]PID, error) {
return pg.pids, pg.err
}
func (pg *testPgrep) Pattern(pattern string) ([]PID, error) {
return pg.pids, pg.err
}
func (pg *testPgrep) Uid(user string) ([]PID, error) {
return pg.pids, pg.err
}
func (pg *testPgrep) FullPattern(pattern string) ([]PID, error) {
return pg.pids, pg.err
}
type testProc struct {
pid PID
tags map[string]string
}
func newTestProc(pid PID) (Process, error) {
proc := &testProc{
tags: make(map[string]string),
}
return proc, nil
}
func (p *testProc) PID() PID {
return p.pid
}
func (p *testProc) Username() (string, error) {
return "testuser", nil
}
func (p *testProc) Tags() map[string]string {
return p.tags
}
func (p *testProc) IOCounters() (*process.IOCountersStat, error) {
return &process.IOCountersStat{}, nil
}
func (p *testProc) MemoryInfo() (*process.MemoryInfoStat, error) {
return &process.MemoryInfoStat{}, nil
}
func (p *testProc) Name() (string, error) {
return "test_proc", nil
}
func (p *testProc) NumCtxSwitches() (*process.NumCtxSwitchesStat, error) {
return &process.NumCtxSwitchesStat{}, nil
}
func (p *testProc) NumFDs() (int32, error) {
return 0, nil
}
func (p *testProc) NumThreads() (int32, error) {
return 0, nil
}
func (p *testProc) Percent(interval time.Duration) (float64, error) {
return 0, nil
}
func (p *testProc) Times() (*cpu.TimesStat, error) {
return &cpu.TimesStat{}, nil
}
func (p *testProc) RlimitUsage(gatherUsage bool) ([]process.RlimitStat, error) {
return []process.RlimitStat{}, nil
}
var pid PID = PID(42)
var exe string = "foo"
func TestGather_CreateProcessErrorOk(t *testing.T) {
var acc testutil.Accumulator
p := Procstat{
Exe: exe,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: func(PID) (Process, error) {
return nil, fmt.Errorf("createProcess error")
},
}
require.NoError(t, acc.GatherError(p.Gather))
}
func TestGather_CreatePIDFinderError(t *testing.T) {
var acc testutil.Accumulator
p := Procstat{
createPIDFinder: func() (PIDFinder, error) {
return nil, fmt.Errorf("createPIDFinder error")
},
createProcess: newTestProc,
}
require.Error(t, acc.GatherError(p.Gather))
}
func TestGather_ProcessName(t *testing.T) {
var acc testutil.Accumulator
p := Procstat{
Exe: exe,
ProcessName: "custom_name",
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.Equal(t, "custom_name", acc.TagValue("procstat", "process_name"))
}
func TestGather_NoProcessNameUsesReal(t *testing.T) {
var acc testutil.Accumulator
pid := PID(os.Getpid())
p := Procstat{
Exe: exe,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.True(t, acc.HasTag("procstat", "process_name"))
}
func TestGather_NoPidTag(t *testing.T) {
var acc testutil.Accumulator
p := Procstat{
Exe: exe,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.True(t, acc.HasInt32Field("procstat", "pid"))
assert.False(t, acc.HasTag("procstat", "pid"))
}
func TestGather_PidTag(t *testing.T) {
var acc testutil.Accumulator
p := Procstat{
Exe: exe,
PidTag: true,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.Equal(t, "42", acc.TagValue("procstat", "pid"))
assert.False(t, acc.HasInt32Field("procstat", "pid"))
}
func TestGather_Prefix(t *testing.T) {
var acc testutil.Accumulator
p := Procstat{
Exe: exe,
Prefix: "custom_prefix",
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.True(t, acc.HasInt32Field("procstat", "custom_prefix_num_fds"))
}
func TestGather_Exe(t *testing.T) {
var acc testutil.Accumulator
p := Procstat{
Exe: exe,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.Equal(t, exe, acc.TagValue("procstat", "exe"))
}
func TestGather_User(t *testing.T) {
var acc testutil.Accumulator
user := "ada"
p := Procstat{
User: user,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.Equal(t, user, acc.TagValue("procstat", "user"))
}
func TestGather_Pattern(t *testing.T) {
var acc testutil.Accumulator
pattern := "foo"
p := Procstat{
Pattern: pattern,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.Equal(t, pattern, acc.TagValue("procstat", "pattern"))
}
func TestGather_MissingPidMethod(t *testing.T) {
var acc testutil.Accumulator
p := Procstat{
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.Error(t, acc.GatherError(p.Gather))
}
func TestGather_PidFile(t *testing.T) {
var acc testutil.Accumulator
pidfile := "/path/to/pidfile"
p := Procstat{
PidFile: pidfile,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.Equal(t, pidfile, acc.TagValue("procstat", "pidfile"))
}
func TestGather_PercentFirstPass(t *testing.T) {
var acc testutil.Accumulator
pid := PID(os.Getpid())
p := Procstat{
Pattern: "foo",
PidTag: true,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: NewProc,
}
require.NoError(t, acc.GatherError(p.Gather))
assert.True(t, acc.HasFloatField("procstat", "cpu_time_user"))
assert.False(t, acc.HasFloatField("procstat", "cpu_usage"))
}
func TestGather_PercentSecondPass(t *testing.T) {
var acc testutil.Accumulator
pid := PID(os.Getpid())
p := Procstat{
Pattern: "foo",
PidTag: true,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: NewProc,
}
require.NoError(t, acc.GatherError(p.Gather))
require.NoError(t, acc.GatherError(p.Gather))
assert.True(t, acc.HasFloatField("procstat", "cpu_time_user"))
assert.True(t, acc.HasFloatField("procstat", "cpu_usage"))
}
func TestGather_systemdUnitPIDs(t *testing.T) {
p := Procstat{
createPIDFinder: pidFinder([]PID{}, nil),
SystemdUnit: "TestGather_systemdUnitPIDs",
}
var acc testutil.Accumulator
pids, tags, err := p.findPids(&acc)
require.NoError(t, err)
assert.Equal(t, []PID{11408}, pids)
assert.Equal(t, "TestGather_systemdUnitPIDs", tags["systemd_unit"])
}
func TestGather_cgroupPIDs(t *testing.T) {
//no cgroups in windows
if runtime.GOOS == "windows" {
t.Skip("no cgroups in windows")
}
td, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(td)
err = ioutil.WriteFile(filepath.Join(td, "cgroup.procs"), []byte("1234\n5678\n"), 0644)
require.NoError(t, err)
p := Procstat{
createPIDFinder: pidFinder([]PID{}, nil),
CGroup: td,
}
var acc testutil.Accumulator
pids, tags, err := p.findPids(&acc)
require.NoError(t, err)
assert.Equal(t, []PID{1234, 5678}, pids)
assert.Equal(t, td, tags["cgroup"])
}
func TestProcstatLookupMetric(t *testing.T) {
p := Procstat{
createPIDFinder: pidFinder([]PID{543}, nil),
Exe: "-Gsys",
}
var acc testutil.Accumulator
err := acc.GatherError(p.Gather)
require.NoError(t, err)
require.Equal(t, len(p.procs)+1, len(acc.Metrics))
}