add file for linux specific tests

This commit is contained in:
Sergey Kacheev 2021-04-05 12:42:44 +07:00
parent 07a870e63b
commit b3a9d75932
2 changed files with 54 additions and 42 deletions

View File

@ -0,0 +1,54 @@
// +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,
)
}
})
}
}

View File

@ -303,48 +303,6 @@ func Test_Process_Threads(t *testing.T) {
} }
} }
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,
)
}
})
}
}
func Test_Process_Name(t *testing.T) { func Test_Process_Name(t *testing.T) {
p := testGetProcess() p := testGetProcess()