...

Source file src/github.com/prometheus/procfs/proc_status_test.go

Documentation: github.com/prometheus/procfs

     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  
    14  package procfs
    15  
    16  import (
    17  	"reflect"
    18  	"testing"
    19  )
    20  
    21  func TestProcStatus(t *testing.T) {
    22  	p, err := getProcFixtures(t).Proc(26231)
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  
    27  	s, err := p.NewStatus()
    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: "Tgid", want: 26231, have: s.TGID},
    39  		{name: "NSpid", want: 1, have: int(s.NSpids[0])},
    40  		{name: "VmPeak", want: 58472 * 1024, have: int(s.VmPeak)},
    41  		{name: "VmSize", want: 58440 * 1024, have: int(s.VmSize)},
    42  		{name: "VmLck", want: 0 * 1024, have: int(s.VmLck)},
    43  		{name: "VmPin", want: 0 * 1024, have: int(s.VmPin)},
    44  		{name: "VmHWM", want: 8028 * 1024, have: int(s.VmHWM)},
    45  		{name: "VmRSS", want: 6716 * 1024, have: int(s.VmRSS)},
    46  		{name: "RssAnon", want: 2092 * 1024, have: int(s.RssAnon)},
    47  		{name: "RssFile", want: 4624 * 1024, have: int(s.RssFile)},
    48  		{name: "RssShmem", want: 0 * 1024, have: int(s.RssShmem)},
    49  		{name: "VmData", want: 2580 * 1024, have: int(s.VmData)},
    50  		{name: "VmStk", want: 136 * 1024, have: int(s.VmStk)},
    51  		{name: "VmExe", want: 948 * 1024, have: int(s.VmExe)},
    52  		{name: "VmLib", want: 6816 * 1024, have: int(s.VmLib)},
    53  		{name: "VmPTE", want: 128 * 1024, have: int(s.VmPTE)},
    54  		{name: "VmPMD", want: 12 * 1024, have: int(s.VmPMD)},
    55  		{name: "VmSwap", want: 660 * 1024, have: int(s.VmSwap)},
    56  		{name: "HugetlbPages", want: 0 * 1024, have: int(s.HugetlbPages)},
    57  		{name: "VoluntaryCtxtSwitches", want: 4742839, have: int(s.VoluntaryCtxtSwitches)},
    58  		{name: "NonVoluntaryCtxtSwitches", want: 1727500, have: int(s.NonVoluntaryCtxtSwitches)},
    59  		{name: "TotalCtxtSwitches", want: 4742839 + 1727500, have: int(s.TotalCtxtSwitches())},
    60  	} {
    61  		if test.want != test.have {
    62  			t.Errorf("want %s %d, have %d", test.name, test.want, test.have)
    63  		}
    64  	}
    65  }
    66  
    67  func TestProcStatusName(t *testing.T) {
    68  	p, err := getProcFixtures(t).Proc(26231)
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	s, err := p.NewStatus()
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	if want, have := "prometheus", s.Name; want != have {
    77  		t.Errorf("want name %s, have %s", want, have)
    78  	}
    79  }
    80  
    81  func TestProcStatusNameTrim(t *testing.T) {
    82  	p, err := getProcFixtures(t).Proc(26235)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	s, err := p.NewStatus()
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	if want, have := "kube-proxy", s.Name; want != have {
    91  		t.Errorf("want name %s, have %s", want, have)
    92  	}
    93  }
    94  
    95  func TestProcStatusUIDs(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{"1000", "1000", "1000", "0"}, s.UIDs; want != have {
   107  		t.Errorf("want uids %s, have %s", want, have)
   108  	}
   109  }
   110  
   111  func TestProcStatusGIDs(t *testing.T) {
   112  	p, err := getProcFixtures(t).Proc(26231)
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	s, err := p.NewStatus()
   118  	if err != nil {
   119  		t.Fatal(err)
   120  	}
   121  
   122  	if want, have := [4]string{"1001", "1001", "1001", "0"}, s.GIDs; want != have {
   123  		t.Errorf("want uids %s, have %s", want, have)
   124  	}
   125  }
   126  
   127  func TestCpusAllowedList(t *testing.T) {
   128  	p, err := getProcFixtures(t).Proc(26231)
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  
   133  	s, err := p.NewStatus()
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  
   138  	if want, have := []uint64{0, 1, 2, 3, 4, 5, 6, 7}, s.CpusAllowedList; !reflect.DeepEqual(want, have) {
   139  		t.Errorf("want CpusAllowedList %v, have %v", want, have)
   140  	}
   141  }
   142  

View as plain text