From 30a542332206b3ad1cbf1d1edced4c1afb55ad1c Mon Sep 17 00:00:00 2001 From: Irvin Lim Date: Fri, 7 Oct 2022 11:29:24 +0800 Subject: [PATCH] fix: Avoid trimming characters incorrectly from ProcStatus Signed-off-by: Irvin Lim --- proc_status.go | 6 ++--- proc_status_test.go | 14 ++++++++++ testdata/fixtures.ttar | 58 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/proc_status.go b/proc_status.go index 594022ded..3d8c06439 100644 --- a/proc_status.go +++ b/proc_status.go @@ -96,10 +96,10 @@ func (p Proc) NewStatus() (ProcStatus, error) { kv := strings.SplitN(line, ":", 2) // removes spaces - k := string(strings.TrimSpace(kv[0])) - v := string(strings.TrimSpace(kv[1])) + k := strings.TrimSpace(kv[0]) + v := strings.TrimSpace(kv[1]) // removes "kB" - v = string(bytes.Trim([]byte(v), " kB")) + v = strings.TrimSuffix(v, " kB") // value to int when possible // we can skip error check here, 'cause vKBytes is not used when value is a string diff --git a/proc_status_test.go b/proc_status_test.go index e132848af..3c9ab5986 100644 --- a/proc_status_test.go +++ b/proc_status_test.go @@ -76,6 +76,20 @@ func TestProcStatusName(t *testing.T) { } } +func TestProcStatusNameTrim(t *testing.T) { + p, err := getProcFixtures(t).Proc(26235) + if err != nil { + t.Fatal(err) + } + s, err := p.NewStatus() + if err != nil { + t.Fatal(err) + } + if want, have := "kube-proxy", s.Name; want != have { + t.Errorf("want name %s, have %s", want, have) + } +} + func TestProcStatusUIDs(t *testing.T) { p, err := getProcFixtures(t).Proc(26231) if err != nil { diff --git a/testdata/fixtures.ttar b/testdata/fixtures.ttar index 40b630739..8400ec765 100644 --- a/testdata/fixtures.ttar +++ b/testdata/fixtures.ttar @@ -748,6 +748,64 @@ Lines: 4 40000000-40015000 r-xp 00000000 03:01 61874 /lib/ld-2.3.2.so Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: fixtures/proc/26235 +Mode: 755 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: fixtures/proc/26235/status +Lines: 51 +Name: kube-proxy +Umask: 0022 +State: S (sleeping) +Tgid: 26235 +Ngid: 12345 +Pid: 26235 +PPid: 1234 +TracerPid: 0 +Uid: 0 0 0 0 +Gid: 0 0 0 0 +FDSize: 64 +Groups: +NStgid: 26235 1 +NSpid: 26235 1 +NSpgid: 26235 1 +NSsid: 26235 1 +VmPeak: 758200 kB +VmSize: 758200 kB +VmLck: 0 kB +VmPin: 0 kB +VmHWM: 61776 kB +VmRSS: 42652 kB +RssAnon: 24852 kB +RssFile: 17800 kB +RssShmem: 0 kB +VmData: 117136 kB +VmStk: 132 kB +VmExe: 21568 kB +VmLib: 4 kB +VmPTE: 264 kB +VmSwap: 0 kB +HugetlbPages: 0 kB +Threads: 51 +SigQ: 9/511324 +SigPnd: 0000000000000000 +ShdPnd: 0000000000000000 +SigBlk: 0000000000000000 +SigIgn: 0000000000000000 +SigCgt: fffffffc7fc1feff +CapInh: 0000003fffffffff +CapPrm: 0000003fffffffff +CapEff: 0000003fffffffff +CapBnd: 0000003fffffffff +CapAmb: 0000000000000000 +Seccomp: 0 +Cpus_allowed: ff +Cpus_allowed_list: 0-7 +Mems_allowed: 00000000,00000001 +Mems_allowed_list: 0 +voluntary_ctxt_switches: 4742839 +nonvoluntary_ctxt_switches: 1727500 +Mode: 644 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: fixtures/proc/584 Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -