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 new file mode 100644 index 0000000..bfbe96d --- /dev/null +++ b/process/process_linux_test.go @@ -0,0 +1,29 @@ +// +build linux + +package process + +import ( + "context" + "io/ioutil" + "strconv" + "testing" + + "github.com/shirou/gopsutil/internal/common" +) + +func Test_fillFromStatusWithContext(t *testing.T) { + pids, err := ioutil.ReadDir("testdata/linux/") + if err != nil { + t.Error(err) + } + f := common.MockEnv("HOST_PROC", "testdata/linux") + defer f() + 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/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 new file mode 100644 index 0000000..ada0ab8 --- /dev/null +++ b/v3/process/process_linux_test.go @@ -0,0 +1,29 @@ +// +build linux + +package process + +import ( + "context" + "io/ioutil" + "strconv" + "testing" + + "github.com/shirou/gopsutil/v3/internal/common" +) + +func Test_fillFromStatusWithContext(t *testing.T) { + pids, err := ioutil.ReadDir("testdata/linux/") + if err != nil { + t.Error(err) + } + f := common.MockEnv("HOST_PROC", "testdata/linux") + defer f() + 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