Merge pull request #1067 from shirou/feature/add_testdata_process_linux_fillfrom

[process][linux] add test to parse fillFromStatus
This commit is contained in:
Lomanic 2021-05-09 11:24:02 +02:00 committed by GitHub
commit 37678fe053
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 246 additions and 0 deletions

View File

@ -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 {

View File

@ -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)
}
}
}

37
process/testdata/linux/1/status vendored Normal file
View File

@ -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

47
process/testdata/linux/1060/status vendored Normal file
View File

@ -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

View File

@ -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 {

View File

@ -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)
}
}
}

37
v3/process/testdata/linux/1/status vendored Normal file
View File

@ -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

47
v3/process/testdata/linux/1060/status vendored Normal file
View File

@ -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