mirror of https://github.com/shirou/gopsutil
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
4 years ago
|
// +build linux
|
||
|
|
||
|
package process
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func Test_Process_splitProcStat(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(fmt.Sprintf("name: %s", 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.Equal(
|
||
|
t, expected, parsed,
|
||
|
"field %d (index from 1 as in man proc) must be %q but %q is received",
|
||
|
idx, expected, parsed,
|
||
|
)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|