From abacce2177f4a63bb0b3fb6365b863fd1e373530 Mon Sep 17 00:00:00 2001 From: shirou Date: Sat, 8 May 2021 18:55:27 +0900 Subject: [PATCH 1/2] [process][linux] add test to parse fillFromStatus --- process/process_linux_test.go | 30 ++++++++++++++++++++++ process/testdata/linux/1/status | 37 +++++++++++++++++++++++++++ process/testdata/linux/1060/status | 47 +++++++++++++++++++++++++++++++++++ v3/process/process_linux_test.go | 30 ++++++++++++++++++++++ v3/process/testdata/linux/1/status | 37 +++++++++++++++++++++++++++ v3/process/testdata/linux/1060/status | 47 +++++++++++++++++++++++++++++++++++ 6 files changed, 228 insertions(+) create mode 100644 process/process_linux_test.go create mode 100644 process/testdata/linux/1/status create mode 100644 process/testdata/linux/1060/status create mode 100644 v3/process/process_linux_test.go create mode 100644 v3/process/testdata/linux/1/status create mode 100644 v3/process/testdata/linux/1060/status diff --git a/process/process_linux_test.go b/process/process_linux_test.go new file mode 100644 index 0000000..f4e8164 --- /dev/null +++ b/process/process_linux_test.go @@ -0,0 +1,30 @@ +// +build linux + +package process + +import ( + "context" + "io/ioutil" + "os" + "strconv" + "testing" +) + +func Test_fillFromStatusWithContext(t *testing.T) { + pids, err := ioutil.ReadDir("testdata/linux/") + if err != nil { + t.Error(err) + } + original := os.Getenv("HOST_PROC") + os.Setenv("HOST_PROC", "testdata/linux") + defer os.Setenv("HOST_PROC", original) + + for _, pid := range pids { + pid, _ := strconv.ParseInt(pid.Name(), 0, 32) + p, _ := NewProcess(int32(pid)) + + if err := p.fillFromStatusWithContext(context.Background()); err != nil { + t.Error(err) + } + } +} diff --git a/process/testdata/linux/1/status b/process/testdata/linux/1/status new file mode 100644 index 0000000..fda629d --- /dev/null +++ b/process/testdata/linux/1/status @@ -0,0 +1,37 @@ +Name: ksoftirqd/0 +Umask: 0000 +State: S (sleeping) +Tgid: 10 +Ngid: 0 +Pid: 10 +PPid: 2 +TracerPid: 0 +Uid: 0 0 0 0 +Gid: 0 0 0 0 +FDSize: 64 +Groups: +NStgid: 10 +NSpid: 10 +NSpgid: 0 +NSsid: 0 +Threads: 1 +SigQ: 0/27700 +SigPnd: 0000000000000000 +ShdPnd: 0000000000000000 +SigBlk: 0000000000000000 +SigIgn: ffffffffffffffff +SigCgt: 0000000000000000 +CapInh: 0000000000000000 +CapPrm: 0000003fffffffff +CapEff: 0000003fffffffff +CapBnd: 0000003fffffffff +CapAmb: 0000000000000000 +NoNewPrivs: 0 +Seccomp: 0 +Speculation_Store_Bypass: vulnerable +Cpus_allowed: 1 +Cpus_allowed_list: 0 +Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001 +Mems_allowed_list: 0 +voluntary_ctxt_switches: 76887 +nonvoluntary_ctxt_switches: 1771 diff --git a/process/testdata/linux/1060/status b/process/testdata/linux/1060/status new file mode 100644 index 0000000..beaa534 --- /dev/null +++ b/process/testdata/linux/1060/status @@ -0,0 +1,47 @@ +Name: server +Umask: 0022 +State: S (sleeping) +Tgid: 2549 +Ngid: 0 +Pid: 2549 +PPid: 1 +TracerPid: 0 +Uid: 107 107 107 107 +Gid: 113 113 113 113 +FDSize: 64 +Groups: 113 +VmPeak: 664744 kB +VmSize: 664744 kB +VmLck: 0 kB +VmPin: 0 kB +VmHWM: 2892 kB +VmRSS: 2892 kB +RssAnon: 524 kB +RssFile: 2368 kB +RssShmem: 0 kB +VmData: 5932 kB +VmStk: 132 kB +VmExe: 1304 kB +VmLib: 1180 kB +VmPTE: 44 kB +VmSwap: 0 kB +CoreDumping: 0 +THP_enabled: 1 +Threads: 5 +SigQ: 0/1823 +SigPnd: 00000000000000000000000000000000 +ShdPnd: 00000000000000000000000000000000 +SigBlk: 00000000000000000000000000000000 +SigIgn: 00000000000000000000000000000000 +SigCgt: fffffffffffffffffffffffe783ffeff +CapInh: 0000000000000000 +CapPrm: 0000000000000000 +CapEff: 0000000000000000 +CapBnd: 0000003fffffffff +CapAmb: 0000000000000000 +NoNewPrivs: 0 +Speculation_Store_Bypass: unknown +Cpus_allowed: 3 +Cpus_allowed_list: 0-1 +voluntary_ctxt_switches: 3 +nonvoluntary_ctxt_switches: 146 \ No newline at end of file diff --git a/v3/process/process_linux_test.go b/v3/process/process_linux_test.go new file mode 100644 index 0000000..f4e8164 --- /dev/null +++ b/v3/process/process_linux_test.go @@ -0,0 +1,30 @@ +// +build linux + +package process + +import ( + "context" + "io/ioutil" + "os" + "strconv" + "testing" +) + +func Test_fillFromStatusWithContext(t *testing.T) { + pids, err := ioutil.ReadDir("testdata/linux/") + if err != nil { + t.Error(err) + } + original := os.Getenv("HOST_PROC") + os.Setenv("HOST_PROC", "testdata/linux") + defer os.Setenv("HOST_PROC", original) + + for _, pid := range pids { + pid, _ := strconv.ParseInt(pid.Name(), 0, 32) + p, _ := NewProcess(int32(pid)) + + if err := p.fillFromStatusWithContext(context.Background()); err != nil { + t.Error(err) + } + } +} diff --git a/v3/process/testdata/linux/1/status b/v3/process/testdata/linux/1/status new file mode 100644 index 0000000..fda629d --- /dev/null +++ b/v3/process/testdata/linux/1/status @@ -0,0 +1,37 @@ +Name: ksoftirqd/0 +Umask: 0000 +State: S (sleeping) +Tgid: 10 +Ngid: 0 +Pid: 10 +PPid: 2 +TracerPid: 0 +Uid: 0 0 0 0 +Gid: 0 0 0 0 +FDSize: 64 +Groups: +NStgid: 10 +NSpid: 10 +NSpgid: 0 +NSsid: 0 +Threads: 1 +SigQ: 0/27700 +SigPnd: 0000000000000000 +ShdPnd: 0000000000000000 +SigBlk: 0000000000000000 +SigIgn: ffffffffffffffff +SigCgt: 0000000000000000 +CapInh: 0000000000000000 +CapPrm: 0000003fffffffff +CapEff: 0000003fffffffff +CapBnd: 0000003fffffffff +CapAmb: 0000000000000000 +NoNewPrivs: 0 +Seccomp: 0 +Speculation_Store_Bypass: vulnerable +Cpus_allowed: 1 +Cpus_allowed_list: 0 +Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001 +Mems_allowed_list: 0 +voluntary_ctxt_switches: 76887 +nonvoluntary_ctxt_switches: 1771 diff --git a/v3/process/testdata/linux/1060/status b/v3/process/testdata/linux/1060/status new file mode 100644 index 0000000..beaa534 --- /dev/null +++ b/v3/process/testdata/linux/1060/status @@ -0,0 +1,47 @@ +Name: server +Umask: 0022 +State: S (sleeping) +Tgid: 2549 +Ngid: 0 +Pid: 2549 +PPid: 1 +TracerPid: 0 +Uid: 107 107 107 107 +Gid: 113 113 113 113 +FDSize: 64 +Groups: 113 +VmPeak: 664744 kB +VmSize: 664744 kB +VmLck: 0 kB +VmPin: 0 kB +VmHWM: 2892 kB +VmRSS: 2892 kB +RssAnon: 524 kB +RssFile: 2368 kB +RssShmem: 0 kB +VmData: 5932 kB +VmStk: 132 kB +VmExe: 1304 kB +VmLib: 1180 kB +VmPTE: 44 kB +VmSwap: 0 kB +CoreDumping: 0 +THP_enabled: 1 +Threads: 5 +SigQ: 0/1823 +SigPnd: 00000000000000000000000000000000 +ShdPnd: 00000000000000000000000000000000 +SigBlk: 00000000000000000000000000000000 +SigIgn: 00000000000000000000000000000000 +SigCgt: fffffffffffffffffffffffe783ffeff +CapInh: 0000000000000000 +CapPrm: 0000000000000000 +CapEff: 0000000000000000 +CapBnd: 0000003fffffffff +CapAmb: 0000000000000000 +NoNewPrivs: 0 +Speculation_Store_Bypass: unknown +Cpus_allowed: 3 +Cpus_allowed_list: 0-1 +voluntary_ctxt_switches: 3 +nonvoluntary_ctxt_switches: 146 \ No newline at end of file From 2fa855f295c8b14cc454bd1e006f7f6c912d8036 Mon Sep 17 00:00:00 2001 From: shirou Date: Sat, 8 May 2021 21:18:46 +0900 Subject: [PATCH 2/2] add MockEnv as more generic function. --- internal/common/common.go | 10 ++++++++++ process/process_linux_test.go | 9 ++++----- v3/internal/common/common.go | 10 ++++++++++ v3/process/process_linux_test.go | 9 ++++----- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/internal/common/common.go b/internal/common/common.go index ebf70ea..f1e4154 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -352,6 +352,16 @@ func HostDev(combineWith ...string) string { return GetEnv("HOST_DEV", "/dev", combineWith...) } +// MockEnv set environment variable and return revert function. +// MockEnv should be used testing only. +func MockEnv(key string, value string) func() { + original := os.Getenv(key) + os.Setenv(key, value) + return func() { + os.Setenv(key, original) + } +} + // getSysctrlEnv sets LC_ALL=C in a list of env vars for use when running // sysctl commands (see DoSysctrl). func getSysctrlEnv(env []string) []string { diff --git a/process/process_linux_test.go b/process/process_linux_test.go index f4e8164..bfbe96d 100644 --- a/process/process_linux_test.go +++ b/process/process_linux_test.go @@ -5,9 +5,10 @@ package process import ( "context" "io/ioutil" - "os" "strconv" "testing" + + "github.com/shirou/gopsutil/internal/common" ) func Test_fillFromStatusWithContext(t *testing.T) { @@ -15,10 +16,8 @@ func Test_fillFromStatusWithContext(t *testing.T) { if err != nil { t.Error(err) } - original := os.Getenv("HOST_PROC") - os.Setenv("HOST_PROC", "testdata/linux") - defer os.Setenv("HOST_PROC", original) - + f := common.MockEnv("HOST_PROC", "testdata/linux") + defer f() for _, pid := range pids { pid, _ := strconv.ParseInt(pid.Name(), 0, 32) p, _ := NewProcess(int32(pid)) diff --git a/v3/internal/common/common.go b/v3/internal/common/common.go index ebf70ea..f1e4154 100644 --- a/v3/internal/common/common.go +++ b/v3/internal/common/common.go @@ -352,6 +352,16 @@ func HostDev(combineWith ...string) string { return GetEnv("HOST_DEV", "/dev", combineWith...) } +// MockEnv set environment variable and return revert function. +// MockEnv should be used testing only. +func MockEnv(key string, value string) func() { + original := os.Getenv(key) + os.Setenv(key, value) + return func() { + os.Setenv(key, original) + } +} + // getSysctrlEnv sets LC_ALL=C in a list of env vars for use when running // sysctl commands (see DoSysctrl). func getSysctrlEnv(env []string) []string { diff --git a/v3/process/process_linux_test.go b/v3/process/process_linux_test.go index f4e8164..ada0ab8 100644 --- a/v3/process/process_linux_test.go +++ b/v3/process/process_linux_test.go @@ -5,9 +5,10 @@ package process import ( "context" "io/ioutil" - "os" "strconv" "testing" + + "github.com/shirou/gopsutil/v3/internal/common" ) func Test_fillFromStatusWithContext(t *testing.T) { @@ -15,10 +16,8 @@ func Test_fillFromStatusWithContext(t *testing.T) { if err != nil { t.Error(err) } - original := os.Getenv("HOST_PROC") - os.Setenv("HOST_PROC", "testdata/linux") - defer os.Setenv("HOST_PROC", original) - + f := common.MockEnv("HOST_PROC", "testdata/linux") + defer f() for _, pid := range pids { pid, _ := strconv.ParseInt(pid.Name(), 0, 32) p, _ := NewProcess(int32(pid))