Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 193 lines (163 sloc) 3.319 kB
fb874eb @dougm Initial import
dougm authored
1 // Copyright (c) 2012 VMware, Inc.
2
3 package sigar
4
5 import (
35d6c1b @dougm Start process interfaces
dougm authored
6 "os"
34b9338 @dougm Add ProcExe api
dougm authored
7 "path/filepath"
9d112dd @dougm Add Cpu and CpuList APIs
dougm authored
8 "runtime"
fb874eb @dougm Initial import
dougm authored
9 "testing"
10 )
11
12 func TestLoadAverage(t *testing.T) {
13 avg := LoadAverage{}
14 if err := avg.Get(); err != nil {
15 t.Error(err)
16 }
17 }
18
19 func TestUptime(t *testing.T) {
20 uptime := Uptime{}
21 if err := uptime.Get(); err != nil {
22 t.Error(err)
23 }
24 if uptime.Length <= 0 {
25 t.Errorf("Invalid uptime.Length=%d", uptime.Length)
26 }
27 }
28
29 func TestMem(t *testing.T) {
30 mem := Mem{}
31 err := mem.Get()
32 if err != nil {
33 t.Error(err)
34 }
35 if mem.Total <= 0 {
36 t.Errorf("Invalid mem.Total=%d", mem.Total)
37 }
38
39 if (mem.Used + mem.Free) > mem.Total {
40 t.Errorf("Invalid mem.Used=%d or mem.Free=%d",
41 mem.Used, mem.Free)
42 }
43 }
44
45 func TestSwap(t *testing.T) {
46 swap := Swap{}
47 err := swap.Get()
48 if err != nil {
49 t.Error(err)
50 }
51 if (swap.Used + swap.Free) > swap.Total {
52 t.Errorf("Invalid swap.Used=%d or swap.Free=%d",
53 swap.Used, swap.Free)
54 }
55 }
56
9d112dd @dougm Add Cpu and CpuList APIs
dougm authored
57 func TestCpu(t *testing.T) {
58 cpu := Cpu{}
59 err := cpu.Get()
60 if err != nil {
61 t.Error(err)
62 }
63 }
64
65 func TestCpuList(t *testing.T) {
66 cpulist := CpuList{}
67 err := cpulist.Get()
68 if err != nil {
69 t.Error(err)
70 }
71 nsigar := len(cpulist.List)
72 numcpu := runtime.NumCPU()
73 if nsigar != numcpu {
74 t.Errorf("CpuList num mismatch: sigar=%d, runtime=%d",
75 nsigar, numcpu)
76 }
77 }
78
fb874eb @dougm Initial import
dougm authored
79 func TestFileSystemList(t *testing.T) {
80 fslist := FileSystemList{}
81 err := fslist.Get()
82 if err != nil {
83 t.Error(err)
84 }
85
86 if len(fslist.List) <= 0 {
87 t.Error("Empty FileSystemList")
88 }
89 }
90
91 func TestFileSystemUsage(t *testing.T) {
92 fsusage := FileSystemUsage{}
93 err := fsusage.Get("/")
94 if err != nil {
95 t.Error(err)
96 }
97
98 err = fsusage.Get("T O T A L L Y B O G U S")
99 if err == nil {
100 t.Error("FileSystemUsage.Get should have failed")
101 }
102 }
35d6c1b @dougm Start process interfaces
dougm authored
103
104 func TestProcList(t *testing.T) {
105 pids := ProcList{}
106 err := pids.Get()
107 if err != nil {
108 t.Error(err)
109 }
110
111 if len(pids.List) <= 2 {
112 t.Errorf("invalid ProcList %v", pids)
113 }
114
115 err = pids.Get()
116 if err != nil {
117 t.Error(err)
118 }
119 }
120
121 const invalidPid = 666666
122
123 func TestProcState(t *testing.T) {
124 state := ProcState{}
125 err := state.Get(os.Getppid())
126 if err != nil {
127 t.Error(err)
128 }
129
130 if state.State != RunStateRun && state.State != RunStateSleep {
131 t.Error("Invalid ProcState.State '%v'", state.State)
132 }
133
134 if state.Name != "go" { // our parent is "go test"
135 t.Error("Invalid ProcState.Name '%v'", state.Name)
136 }
137
138 err = state.Get(invalidPid)
139 if err == nil {
140 t.Error("Invalid ProcState.Get('%d')", invalidPid)
141 }
142 }
143
144 func TestProcMem(t *testing.T) {
145 mem := ProcMem{}
146 err := mem.Get(os.Getppid())
147 if err != nil {
148 t.Error(err)
149 }
150
151 err = mem.Get(invalidPid)
152 if err == nil {
153 t.Error("Invalid ProcMem.Get('%d')", invalidPid)
154 }
155 }
156
157 func TestProcTime(t *testing.T) {
158 time := ProcTime{}
159 err := time.Get(os.Getppid())
160 if err != nil {
161 t.Error(err)
162 }
163
164 err = time.Get(invalidPid)
165 if err == nil {
166 t.Error("Invalid ProcTime.Get('%d')", invalidPid)
167 }
168 }
ffca5cf @dougm ProcArgs - get process arguments
dougm authored
169
170 func TestProcArgs(t *testing.T) {
171 args := ProcArgs{}
172 err := args.Get(os.Getppid())
173 if err != nil {
174 t.Error(err)
175 }
176
177 if len(args.List) < 2 {
178 t.Errorf("invalid ProcArgs %s", args.List)
179 }
180 }
34b9338 @dougm Add ProcExe api
dougm authored
181
182 func TestProcExe(t *testing.T) {
183 exe := ProcExe{}
184 err := exe.Get(os.Getppid())
185 if err != nil {
186 t.Error(err)
187 }
188
189 if filepath.Base(exe.Name) != "go" {
190 t.Errorf("Invalid ProcExe.Name '%v'", exe.Name)
191 }
192 }
Something went wrong with that request. Please try again.