1// Copyright 2018 The Prometheus Authors 2// Licensed under the Apache License, Version 2.0 (the "License"); 3// you may not use this file except in compliance with the License. 4// You may obtain a copy of the License at 5// 6// http://www.apache.org/licenses/LICENSE-2.0 7// 8// Unless required by applicable law or agreed to in writing, software 9// distributed under the License is distributed on an "AS IS" BASIS, 10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11// See the License for the specific language governing permissions and 12// limitations under the License. 13 14package procfs 15 16import ( 17 "os" 18 "testing" 19) 20 21func TestProcStat(t *testing.T) { 22 p, err := getProcFixtures(t).Proc(26231) 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 s, err := p.Stat() 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 for _, test := range []struct { 33 name string 34 want int 35 have int 36 }{ 37 {name: "pid", want: 26231, have: s.PID}, 38 {name: "user time", want: 1677, have: int(s.UTime)}, 39 {name: "system time", want: 44, have: int(s.STime)}, 40 {name: "start time", want: 82375, have: int(s.Starttime)}, 41 {name: "virtual memory size", want: 56274944, have: int(s.VSize)}, 42 {name: "resident set size", want: 1981, have: s.RSS}, 43 } { 44 if test.want != test.have { 45 t.Errorf("want %s %d, have %d", test.name, test.want, test.have) 46 } 47 } 48} 49 50func TestProcStatComm(t *testing.T) { 51 s1, err := testProcStat(26231) 52 if err != nil { 53 t.Fatal(err) 54 } 55 if want, have := "vim", s1.Comm; want != have { 56 t.Errorf("want comm %s, have %s", want, have) 57 } 58 59 s2, err := testProcStat(584) 60 if err != nil { 61 t.Fatal(err) 62 } 63 if want, have := "(a b ) ( c d) ", s2.Comm; want != have { 64 t.Errorf("want comm %s, have %s", want, have) 65 } 66} 67 68func TestProcStatVirtualMemory(t *testing.T) { 69 s, err := testProcStat(26231) 70 if err != nil { 71 t.Fatal(err) 72 } 73 74 if want, have := 56274944, int(s.VirtualMemory()); want != have { 75 t.Errorf("want virtual memory %d, have %d", want, have) 76 } 77} 78 79func TestProcStatResidentMemory(t *testing.T) { 80 s, err := testProcStat(26231) 81 if err != nil { 82 t.Fatal(err) 83 } 84 85 if want, have := 1981*os.Getpagesize(), s.ResidentMemory(); want != have { 86 t.Errorf("want resident memory %d, have %d", want, have) 87 } 88} 89 90func TestProcStatStartTime(t *testing.T) { 91 s, err := testProcStat(26231) 92 if err != nil { 93 t.Fatal(err) 94 } 95 96 time, err := s.StartTime() 97 if err != nil { 98 t.Fatal(err) 99 } 100 if want, have := 1418184099.75, time; want != have { 101 t.Errorf("want start time %f, have %f", want, have) 102 } 103} 104 105func TestProcStatCPUTime(t *testing.T) { 106 s, err := testProcStat(26231) 107 if err != nil { 108 t.Fatal(err) 109 } 110 111 if want, have := 17.21, s.CPUTime(); want != have { 112 t.Errorf("want cpu time %f, have %f", want, have) 113 } 114} 115 116func testProcStat(pid int) (ProcStat, error) { 117 fs, err := NewFS(procTestFixtures) 118 if err != nil { 119 return ProcStat{}, err 120 } 121 p, err := fs.Proc(pid) 122 if err != nil { 123 return ProcStat{}, err 124 } 125 126 return p.Stat() 127} 128