Something went wrong. Try again.
forked from shirou/gopsutil
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305// SPDX-License-Identifier: BSD-3-Clause//go:build linux
package process
import ( "context" "fmt" "os" "strconv" "strings" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require")
func TestFillFromfdWithContext(t *testing.T) { type expect struct { numFDs int32 openFiles []OpenFilesStat err error } type testCase struct { name string pid int32 expected *expect }
cases := []testCase{ { name: "good path", pid: 1, expected: &expect{ numFDs: 3, openFiles: []OpenFilesStat{ { Path: "/foo", Fd: 0, }, { Path: "/bar", Fd: 1, }, { Path: "/baz", Fd: 2, }, }, }, }, }
t.Setenv("HOST_PROC", "testdata/linux") for _, tt := range cases { t.Run(tt.name, func(t *testing.T) { p, err := NewProcess(tt.pid) require.NoError(t, err) numFDs, openFiles, err := p.fillFromfdWithContext(context.TODO()) if tt.expected.err != nil { assert.ErrorContains(t, err, tt.expected.err.Error()) return }
//nolint:testifylint // false positive assert.NoError(t, err) assert.Equal(t, tt.expected.numFDs, numFDs) assert.ElementsMatch(t, tt.expected.openFiles, openFiles) }) }}
func TestSplitProcStat(t *testing.T) { expectedFieldsNum := 53 statLineContent := make([]string, expectedFieldsNum-1) for i := 0; i < expectedFieldsNum-1; i++ { statLineContent[i] = strconv.Itoa(i + 1) }
cases := []string{ "ok", "ok)", "(ok", "ok )", "ok )(", "ok )()", "() ok )()", "() ok (()", " ) ok )", "(ok) (ok)", }
consideredFields := []int{4, 7, 10, 11, 12, 13, 14, 15, 18, 22, 42}
commandNameIndex := 2 for _, expectedName := range cases { statLineContent[commandNameIndex-1] = "(" + expectedName + ")" statLine := strings.Join(statLineContent, " ") t.Run("name: "+expectedName, func(t *testing.T) { parsedStatLine := splitProcStat([]byte(statLine)) assert.Equal(t, expectedName, parsedStatLine[commandNameIndex]) for _, idx := range consideredFields { expected := strconv.Itoa(idx) parsed := parsedStatLine[idx] assert.Equalf( t, expected, parsed, "field %d (index from 1 as in man proc) must be %q but %q is received", idx, expected, parsed, ) } }) }}
func TestSplitProcStat_malformed(t *testing.T) { // Malformed/truncated stat contents must not panic; splitProcStat // returns nil so callers' field-count checks can reject them. cases := []string{ "", // empty "123", // no parentheses "123 name S 4", // no parentheses, plain fields "123 (name", // missing closing paren "name) S 4", // missing opening paren "123 (name)", // ')' as final byte, no trailing fields } for _, content := range cases { t.Run(content, func(t *testing.T) { assert.NotPanics(t, func() { _ = splitProcStat([]byte(content)) }) }) }}
func TestSplitProcStat_fromFile(t *testing.T) { pids, err := os.ReadDir("testdata/linux/") require.NoError(t, err) t.Setenv("HOST_PROC", "testdata/linux") for _, pid := range pids { pid, err := strconv.ParseInt(pid.Name(), 0, 32) if err != nil { continue } statFile := fmt.Sprintf("testdata/linux/%d/stat", pid) if _, err := os.Stat(statFile); err != nil { continue } contents, err := os.ReadFile(statFile) require.NoError(t, err)
pidStr := strconv.Itoa(int(pid))
ppid := "68044" // TODO: how to pass ppid to test?
fields := splitProcStat(contents) assert.Equal(t, pidStr, fields[1]) assert.Equal(t, "test(cmd).sh", fields[2]) assert.Equal(t, "S", fields[3]) assert.Equal(t, ppid, fields[4]) assert.Equal(t, pidStr, fields[5]) // pgrp assert.Equal(t, ppid, fields[6]) // session assert.Equal(t, pidStr, fields[8]) // tpgrp assert.Equal(t, "20", fields[18]) // priority assert.Equal(t, "1", fields[20]) // num threads assert.Equal(t, "0", fields[52]) // exit code }}
func TestFillFromCommWithContext(t *testing.T) { pids, err := os.ReadDir("testdata/linux/") require.NoError(t, err) t.Setenv("HOST_PROC", "testdata/linux") for _, pid := range pids { pid, err := strconv.ParseInt(pid.Name(), 0, 32) if err != nil { continue } if _, err := os.Stat(fmt.Sprintf("testdata/linux/%d/status", pid)); err != nil { continue } p, _ := NewProcess(int32(pid)) assert.NoError(t, p.fillFromCommWithContext(context.Background())) }}
func TestFillFromStatusWithContext(t *testing.T) { pids, err := os.ReadDir("testdata/linux/") require.NoError(t, err) t.Setenv("HOST_PROC", "testdata/linux") for _, pid := range pids { pid, err := strconv.ParseInt(pid.Name(), 0, 32) if err != nil { continue } if _, err := os.Stat(fmt.Sprintf("testdata/linux/%d/status", pid)); err != nil { continue } p, _ := NewProcess(int32(pid)) assert.NoError(t, p.fillFromStatus()) }}
func Benchmark_fillFromCommWithContext(b *testing.B) { b.Setenv("HOST_PROC", "testdata/linux") pid := 1060 p, _ := NewProcess(int32(pid)) for i := 0; i < b.N; i++ { p.fillFromCommWithContext(context.Background()) }}
func Benchmark_fillFromStatusWithContext(b *testing.B) { b.Setenv("HOST_PROC", "testdata/linux") pid := 1060 p, _ := NewProcess(int32(pid)) for i := 0; i < b.N; i++ { p.fillFromStatus() }}
func TestFillFromTIDStatWithContext_lx_brandz(t *testing.T) { pids, err := os.ReadDir("testdata/lx_brandz/") require.NoError(t, err) t.Setenv("HOST_PROC", "testdata/lx_brandz") for _, pid := range pids { pid, err := strconv.ParseInt(pid.Name(), 0, 32) if err != nil { continue } if _, err := os.Stat(fmt.Sprintf("testdata/lx_brandz/%d/stat", pid)); err != nil { continue } p, _ := NewProcess(int32(pid)) _, _, cpuTimes, _, _, _, _, err := p.fillFromTIDStat(-1) require.NoError(t, err) assert.Zero(t, cpuTimes.Iowait) }}
func TestProcessMemoryMaps(t *testing.T) { t.Setenv("HOST_PROC", "testdata/linux") pid := 1 p, err := NewProcess(int32(pid)) require.NoError(t, err) maps, err := p.MemoryMaps(false) require.NoError(t, err)
expected := &[]MemoryMapsStat{ { "[vvar]", 0, 1, 0, 3, 4, 5, 6, 7, 8, 9, }, { "", 0, 1, 2, 3, 4, 0, 6, 7, 8, 9, }, { "[vdso]", 0, 1, 2, 3, 4, 5, 0, 7, 8, 9, }, { "/usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1", 0, 1, 2, 3, 4, 5, 6, 7, 0, 9, }, }
require.Equal(t, expected, maps)}