-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc_test.go
74 lines (54 loc) · 1.37 KB
/
proc_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
package proc
import (
"os/exec"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetProcessNegativePid(t *testing.T) {
assert.Nil(t, GetProcessInfo(-1))
}
func TestGetProcessZero(t *testing.T) {
assert.Nil(t, GetProcessInfo(0))
}
func TestGetProcessMissingPid(t *testing.T) {
assert.Nil(t, GetProcessInfo(99999999))
}
func TestProcessFields(t *testing.T) {
cmd := exec.Command("sleep", "5")
cmd.Start()
process := GetProcessInfo(cmd.Process.Pid)
assert.NotNil(t, process)
assert.Equal(t, "sleep", process.Command)
assert.Equal(t, []string{"sleep", "5"}, process.CommandLine)
cmd.Process.Kill()
cmd.Wait()
}
func TestLongCommandLine(t *testing.T) {
cmd := exec.Command("dd", "if=/dev/zero", "of=/dev/null")
cmd.Start()
process := GetProcessInfo(cmd.Process.Pid)
assert.NotNil(t, process)
assert.Equal(t, "dd", process.Command)
assert.Equal(t, []string{"dd", "if=/dev/zero", "of=/dev/null"}, process.CommandLine)
cmd.Process.Kill()
cmd.Wait()
}
func TestGetProcessGoRoutines(t *testing.T) {
cmd := exec.Command("sleep", "5")
cmd.Start()
count := 100
var wg sync.WaitGroup
wg.Add(count)
for i := 0; i < count; i++ {
go func() {
assert.NotNil(t, GetProcessInfo(cmd.Process.Pid))
wg.Done()
}()
}
wg.Wait()
}
func TestGetAllProcesses(t *testing.T) {
processes := GetAllProcessesInfo()
assert.Greater(t, len(processes), 10)
}