Debian Codegraph @

internal/proc/stat_test.go

raw symbols SCIP
1 // Copyright 2018 psgo authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package proc
16
17 import (
18 "errors"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 )
23
24 var (
25 statFile = "31404 (gedit) R 2109 2128 2128 0 -1 4194304 13153 328 0 0 590 55 0 0 20 0 6 0 1331588 419667968 19515 18446744073709551615 94120519110656 94120519115256 140737253236304 0 0 0 0 4096 0 0 0 0 17 2 0 0 62588346 0 0 94120521215368 94120521216168 94120544436224 140737253242331 140737253242369 140737253242369 140737253244905 0"
26 statFileSpace = "31405 (ge d it) R 2109 2128 2128 0 -1 4194304 13153 328 0 0 590 55 0 0 20 0 6 0 1331588 419667968 19515 18446744073709551615 94120519110656 94120519115256 140737253236304 0 0 0 0 4096 0 0 0 0 17 2 0 0 62588346 0 0 94120521215368 94120521216168 94120544436224 140737253242331 140737253242369 140737253242369 140737253244905 0"
27 statFileParen = "31406 (ged)it) R 2109 2128 2128 0 -1 4194304 13153 328 0 0 590 55 0 0 20 0 6 0 1331588 419667968 19515 18446744073709551615 94120519110656 94120519115256 140737253236304 0 0 0 0 4096 0 0 0 0 17 2 0 0 62588346 0 0 94120521215368 94120521216168 94120544436224 140737253242331 140737253242369 140737253242369 140737253244905 0"
28 )
29
30 func testReadStat(file string) (string, error) {
31 switch file {
32 case "/proc/31404/stat":
33 return statFile, nil
34 case "/proc/31405/stat":
35 return statFileSpace, nil
36 case "/proc/31406/stat":
37 return statFileParen, nil
38 }
39 return "", errors.New("unimplemented test case")
40 }
41
42 func TestParseStat(t *testing.T) {
43 readStat = testReadStat
44
45 s, err := ParseStat("31404")
46
47 assert.Nil(t, err)
48 assert.NotNil(t, s)
49
50 assert.Equal(t, "31404", s.Pid)
51 assert.Equal(t, "gedit", s.Comm)
52 assert.Equal(t, "R", s.State)
53 assert.Equal(t, "2109", s.Ppid)
54 assert.Equal(t, "2128", s.Pgrp)
55 assert.Equal(t, "2128", s.Session)
56 assert.Equal(t, "0", s.TtyNr)
57 assert.Equal(t, "-1", s.Tpgid)
58 assert.Equal(t, "4194304", s.Flags)
59 assert.Equal(t, "13153", s.Minflt)
60 assert.Equal(t, "328", s.Cminflt)
61 assert.Equal(t, "0", s.Majflt)
62 assert.Equal(t, "0", s.Cmajflt)
63 assert.Equal(t, "590", s.Utime)
64 assert.Equal(t, "55", s.Stime)
65 assert.Equal(t, "0", s.Cutime)
66 assert.Equal(t, "0", s.Cstime)
67 assert.Equal(t, "20", s.Priority)
68 assert.Equal(t, "0", s.Nice)
69 assert.Equal(t, "6", s.NumThreads)
70 assert.Equal(t, "0", s.Itrealvalue)
71 assert.Equal(t, "1331588", s.Starttime)
72 assert.Equal(t, "419667968", s.Vsize)
73
74 s2, err := ParseStat("31405")
75
76 assert.Nil(t, err)
77 assert.NotNil(t, s2)
78
79 assert.Equal(t, "ge d it", s2.Comm)
80
81 s3, err := ParseStat("31406")
82
83 assert.Nil(t, err)
84 assert.NotNil(t, s3)
85
86 assert.Equal(t, "ged)it", s3.Comm)
87 }