1
2
3
4
5
6
7
8
9
10
11
12
13
14 package procfs
15
16 import (
17 "math"
18 "os"
19 "testing"
20 )
21
22 func TestProcStat(t *testing.T) {
23 p, err := getProcFixtures(t).Proc(26231)
24 if err != nil {
25 t.Fatal(err)
26 }
27
28 s, err := p.Stat()
29 if err != nil {
30 t.Fatal(err)
31 }
32
33
34 for _, test := range []struct {
35 name string
36 want int
37 have int
38 }{
39 {name: "pid", want: 26231, have: s.PID},
40 {name: "user time", want: 1677, have: int(s.UTime)},
41 {name: "system time", want: 44, have: int(s.STime)},
42 {name: "start time", want: 82375, have: int(s.Starttime)},
43 {name: "virtual memory size", want: 56274944, have: int(s.VSize)},
44 {name: "resident set size", want: 1981, have: s.RSS},
45 } {
46 if test.want != test.have {
47 t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
48 }
49 }
50
51
52 for _, test := range []struct {
53 name string
54 want uint64
55 have uint64
56 }{
57 {name: "RSS Limit", want: 18446744073709551615, have: s.RSSLimit},
58 {name: "delayacct_blkio_ticks", want: 31, have: s.DelayAcctBlkIOTicks},
59 } {
60 if test.want != test.have {
61 t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
62 }
63 }
64
65
66 for _, test := range []struct {
67 name string
68 want uint
69 have uint
70 }{
71 {name: "processor", want: 0, have: s.Processor},
72 {name: "rt_priority", want: 0, have: s.RTPriority},
73 {name: "policy", want: 0, have: s.Policy},
74 } {
75 if test.want != test.have {
76 t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
77 }
78 }
79 }
80
81 func TestProcStatLimits(t *testing.T) {
82 p, err := getProcFixtures(t).Proc(26232)
83 if err != nil {
84 t.Fatal(err)
85 }
86
87 s, err := p.Stat()
88 if err != nil {
89 t.Errorf("want not error, have %s", err)
90 }
91
92
93 for _, test := range []struct {
94 name string
95 want int
96 have int
97 }{
98 {name: "waited for children user time", want: math.MinInt64, have: s.CUTime},
99 {name: "waited for children system time", want: math.MaxInt64, have: s.CSTime},
100 } {
101 if test.want != test.have {
102 t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
103 }
104 }
105 }
106
107 func TestProcStatComm(t *testing.T) {
108 s1, err := testProcStat(26231)
109 if err != nil {
110 t.Fatal(err)
111 }
112 if want, have := "vim", s1.Comm; want != have {
113 t.Errorf("want comm %s, have %s", want, have)
114 }
115
116 s2, err := testProcStat(584)
117 if err != nil {
118 t.Fatal(err)
119 }
120 if want, have := "(a b ) ( c d) ", s2.Comm; want != have {
121 t.Errorf("want comm %s, have %s", want, have)
122 }
123 }
124
125 func TestProcStatVirtualMemory(t *testing.T) {
126 s, err := testProcStat(26231)
127 if err != nil {
128 t.Fatal(err)
129 }
130
131 if want, have := 56274944, int(s.VirtualMemory()); want != have {
132 t.Errorf("want virtual memory %d, have %d", want, have)
133 }
134 }
135
136 func TestProcStatResidentMemory(t *testing.T) {
137 s, err := testProcStat(26231)
138 if err != nil {
139 t.Fatal(err)
140 }
141
142 if want, have := 1981*os.Getpagesize(), s.ResidentMemory(); want != have {
143 t.Errorf("want resident memory %d, have %d", want, have)
144 }
145 }
146
147 func TestProcStatStartTime(t *testing.T) {
148 s, err := testProcStat(26231)
149 if err != nil {
150 t.Fatal(err)
151 }
152
153 time, err := s.StartTime()
154 if err != nil {
155 t.Fatal(err)
156 }
157 if want, have := 1418184099.75, time; want != have {
158 t.Errorf("want start time %f, have %f", want, have)
159 }
160 }
161
162 func TestProcStatCPUTime(t *testing.T) {
163 s, err := testProcStat(26231)
164 if err != nil {
165 t.Fatal(err)
166 }
167
168 if want, have := 17.21, s.CPUTime(); want != have {
169 t.Errorf("want cpu time %f, have %f", want, have)
170 }
171 }
172
173 func testProcStat(pid int) (ProcStat, error) {
174 fs, err := NewFS(procTestFixtures)
175 if err != nil {
176 return ProcStat{}, err
177 }
178 p, err := fs.Proc(pid)
179 if err != nil {
180 return ProcStat{}, err
181 }
182
183 return p.Stat()
184 }
185
View as plain text