forked from elastic/gosigar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigar_interface_test.go
164 lines (138 loc) · 3.8 KB
/
sigar_interface_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
package gosigar_test
import (
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
. "github.com/elastic/gosigar"
"github.com/stretchr/testify/assert"
)
const invalidPid = 666666
func TestCpu(t *testing.T) {
cpu := Cpu{}
assert.NoError(t, cpu.Get())
}
func TestLoadAverage(t *testing.T) {
avg := LoadAverage{}
assert.NoError(t, skipNotImplemented(t, avg.Get(), "windows"))
}
func TestUptime(t *testing.T) {
uptime := Uptime{}
if assert.NoError(t, uptime.Get()) {
assert.True(t, uptime.Length > 0, "Uptime (%f) must be positive", uptime.Length)
}
}
func TestMem(t *testing.T) {
mem := Mem{}
if assert.NoError(t, mem.Get()) {
assert.True(t, mem.Total > 0, "mem.Total (%d) must be positive", mem.Total)
assert.True(t, (mem.Used+mem.Free) <= mem.Total,
"mem.Used (%d) + mem.Free (%d) must <= mem.Total (%d)",
mem.Used, mem.Free, mem.Total)
}
}
func TestSwap(t *testing.T) {
swap := Swap{}
if assert.NoError(t, swap.Get()) {
assert.True(t, (swap.Used+swap.Free) <= swap.Total,
"swap.Used (%d) + swap.Free (%d) must <= swap.Total (%d)",
swap.Used, swap.Free, swap.Total)
}
}
func TestCpuList(t *testing.T) {
cpuList := CpuList{}
if assert.NoError(t, cpuList.Get()) {
numCore := len(cpuList.List)
numCpu := runtime.NumCPU()
assert.True(t, numCore >= numCpu, "Number of cores (%d) >= number of logical CPUs (%d)",
numCore, numCpu)
}
}
func TestFileSystemList(t *testing.T) {
fsList := FileSystemList{}
if assert.NoError(t, fsList.Get()) {
assert.True(t, len(fsList.List) > 0)
}
}
func TestFileSystemUsage(t *testing.T) {
root := "/"
if runtime.GOOS == "windows" {
root = `C:\`
}
fsusage := FileSystemUsage{}
if assert.NoError(t, fsusage.Get(root)) {
assert.True(t, fsusage.Total > 0)
}
assert.Error(t, fsusage.Get("T O T A L L Y B O G U S"))
}
func TestProcList(t *testing.T) {
pids := ProcList{}
if assert.NoError(t, pids.Get()) {
assert.True(t, len(pids.List) > 2)
}
}
func TestProcState(t *testing.T) {
u, err := user.Current()
if err != nil {
t.Fatal(err)
}
state := ProcState{}
if assert.NoError(t, state.Get(os.Getppid())) {
assert.Contains(t, []RunState{RunStateRun, RunStateSleep}, state.State)
assert.Regexp(t, "go(.exe)?", state.Name)
assert.Equal(t, u.Username, state.Username)
assert.True(t, state.Ppid > 0, "ppid=%v is non-positive", state.Ppid)
}
assert.Error(t, state.Get(invalidPid))
}
func TestProcMem(t *testing.T) {
mem := ProcMem{}
assert.NoError(t, mem.Get(os.Getppid()))
assert.Error(t, mem.Get(invalidPid))
}
func TestProcTime(t *testing.T) {
procTime := ProcTime{}
if assert.NoError(t, procTime.Get(os.Getppid())) {
// Sanity check the start time of the "go test" process.
delta := time.Now().Sub(time.Unix(0, int64(procTime.StartTime*uint64(time.Millisecond))))
assert.True(t, delta > 0 && delta < 2*time.Minute, "ProcTime.StartTime differs by %v", delta)
}
assert.Error(t, procTime.Get(invalidPid))
}
func TestProcArgs(t *testing.T) {
args := ProcArgs{}
if assert.NoError(t, args.Get(os.Getppid())) {
assert.NotEmpty(t, args.List)
}
}
func TestProcEnv(t *testing.T) {
env := &ProcEnv{}
if assert.NoError(t, skipNotImplemented(t, env.Get(os.Getpid()), "windows", "openbsd")) {
assert.True(t, len(env.Vars) > 0, "env is empty")
for k, v := range env.Vars {
assert.Equal(t, strings.TrimSpace(os.Getenv(k)), v)
}
}
}
func TestProcExe(t *testing.T) {
exe := ProcExe{}
if assert.NoError(t, skipNotImplemented(t, exe.Get(os.Getppid()), "windows")) {
assert.Regexp(t, "go(.exe)?", filepath.Base(exe.Name))
}
}
func skipNotImplemented(t testing.TB, err error, goos ...string) error {
for _, os := range goos {
if runtime.GOOS == os {
if err == nil {
t.Fatal("expected ErrNotImplemented")
} else if IsNotImplemented(err) {
t.Skipf("Skipping test on %s", runtime.GOOS)
}
break
}
}
return err
}