-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_test.go
120 lines (102 loc) · 2.2 KB
/
process_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
package libspector
import (
"fmt"
"os/exec"
"path/filepath"
"runtime"
"testing"
"time"
)
func TestParseLStart(t *testing.T) {
var err error
switch runtime.GOOS {
case "linux":
_, err = parseLStart("Sun Jul 17 13:30:26 2016")
case "darwin":
_, err = parseLStart("Sun 17 Jul 13:30:26 2016")
}
if err != nil {
t.Error(err)
}
}
func TestAllProcesses(t *testing.T) {
procs, err := AllProcesses()
if err != nil {
t.Error(err)
}
if len(procs) == 0 {
t.Errorf("There should be at least an init process")
}
noTime := time.Time{}
for i, proc := range procs {
pid := proc.PID()
if pid == 0 {
t.Errorf("invalid pid at %d of %d", i, len(procs))
}
started, err := proc.Started()
if err != nil {
t.Errorf("err getting start time for pid %d: %s", pid, err)
}
if started == noTime {
t.Errorf("missing start time for pid: %d", pid)
}
}
}
func TestProcessCommand(t *testing.T) {
script, err := filepath.Abs("test_script.sh")
if err != nil {
t.Error(err)
}
cmd := exec.Command(script, "1", "hello", "douche", "4")
err = cmd.Start()
if err != nil {
t.Error(err)
}
defer cmd.Process.Kill()
procs, err := FindProcess("test_script")
if err != nil {
t.Error(err)
}
if len(procs) == 0 {
t.Error("failed to find process")
}
fullCommand, err := procs[0].CommandArgs()
if err != nil {
t.Error(err)
}
if fullCommand != fmt.Sprintf("sh %s 1 hello douche 4", script) {
t.Errorf("CommandArgs was wrong: %s", fullCommand)
}
commandName, err := procs[0].CommandName()
if err != nil {
t.Error(err)
}
if commandName != "sh" {
t.Errorf("CommandName was wrong: %s", commandName)
}
}
func TestFindProcess(t *testing.T) {
// Find some shells, any shells. Hopefully there are shells.
query := "sh"
procs, err := FindProcess(query)
if err != nil {
t.Error(err)
}
if len(procs) == 0 {
t.Error("failed to find process")
}
noTime := time.Time{}
for i, proc := range procs {
pid := proc.PID()
if pid == 0 {
t.Errorf("invalid pid at %d of %d", i, len(procs))
}
started, err := proc.Started()
if err != nil {
t.Errorf("err getting start time for pid %d: %s", pid, err)
}
if started == noTime {
t.Errorf("missing start time for pid: %d", pid)
}
}
}