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	"testing"
18)
19
20func TestProcStatus(t *testing.T) {
21	p, err := getProcFixtures(t).Proc(26231)
22	if err != nil {
23		t.Fatal(err)
24	}
25
26	s, err := p.NewStatus()
27	if err != nil {
28		t.Fatal(err)
29	}
30
31	for _, test := range []struct {
32		name string
33		want int
34		have int
35	}{
36		{name: "Pid", want: 26231, have: s.PID},
37		{name: "Tgid", want: 26231, have: s.TGID},
38		{name: "VmPeak", want: 58472 * 1024, have: int(s.VmPeak)},
39		{name: "VmSize", want: 58440 * 1024, have: int(s.VmSize)},
40		{name: "VmLck", want: 0 * 1024, have: int(s.VmLck)},
41		{name: "VmPin", want: 0 * 1024, have: int(s.VmPin)},
42		{name: "VmHWM", want: 8028 * 1024, have: int(s.VmHWM)},
43		{name: "VmRSS", want: 6716 * 1024, have: int(s.VmRSS)},
44		{name: "RssAnon", want: 2092 * 1024, have: int(s.RssAnon)},
45		{name: "RssFile", want: 4624 * 1024, have: int(s.RssFile)},
46		{name: "RssShmem", want: 0 * 1024, have: int(s.RssShmem)},
47		{name: "VmData", want: 2580 * 1024, have: int(s.VmData)},
48		{name: "VmStk", want: 136 * 1024, have: int(s.VmStk)},
49		{name: "VmExe", want: 948 * 1024, have: int(s.VmExe)},
50		{name: "VmLib", want: 6816 * 1024, have: int(s.VmLib)},
51		{name: "VmPTE", want: 128 * 1024, have: int(s.VmPTE)},
52		{name: "VmPMD", want: 12 * 1024, have: int(s.VmPMD)},
53		{name: "VmSwap", want: 660 * 1024, have: int(s.VmSwap)},
54		{name: "HugetlbPages", want: 0 * 1024, have: int(s.HugetlbPages)},
55		{name: "VoluntaryCtxtSwitches", want: 4742839, have: int(s.VoluntaryCtxtSwitches)},
56		{name: "NonVoluntaryCtxtSwitches", want: 1727500, have: int(s.NonVoluntaryCtxtSwitches)},
57		{name: "TotalCtxtSwitches", want: 4742839 + 1727500, have: int(s.TotalCtxtSwitches())},
58	} {
59		if test.want != test.have {
60			t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
61		}
62	}
63}
64
65func TestProcStatusName(t *testing.T) {
66	p, err := getProcFixtures(t).Proc(26231)
67	if err != nil {
68		t.Fatal(err)
69	}
70	s, err := p.NewStatus()
71	if err != nil {
72		t.Fatal(err)
73	}
74	if want, have := "prometheus", s.Name; want != have {
75		t.Errorf("want name %s, have %s", want, have)
76	}
77}
78
79func TestProcStatusUIDs(t *testing.T) {
80	p, err := getProcFixtures(t).Proc(26231)
81	if err != nil {
82		t.Fatal(err)
83	}
84
85	s, err := p.NewStatus()
86	if err != nil {
87		t.Fatal(err)
88	}
89
90	if want, have := [4]string{"1000", "1000", "1000", "0"}, s.UIDs; want != have {
91		t.Errorf("want uids %s, have %s", want, have)
92	}
93}
94
95func TestProcStatusGIDs(t *testing.T) {
96	p, err := getProcFixtures(t).Proc(26231)
97	if err != nil {
98		t.Fatal(err)
99	}
100
101	s, err := p.NewStatus()
102	if err != nil {
103		t.Fatal(err)
104	}
105
106	if want, have := [4]string{"1001", "1001", "1001", "0"}, s.GIDs; want != have {
107		t.Errorf("want uids %s, have %s", want, have)
108	}
109}
110