2021-08-23 13:14:53 +00:00
|
|
|
|
//go:build !windows
|
|
|
|
|
// +build !windows
|
2016-07-06 02:40:48 +00:00
|
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-07-06 02:40:48 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
2019-10-16 01:55:17 +00:00
|
|
|
|
|
2020-02-07 13:39:24 +00:00
|
|
|
|
"gotest.tools/v3/assert"
|
2016-07-06 02:40:48 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestContainerTopValidatePSArgs(t *testing.T) {
|
|
|
|
|
tests := map[string]bool{
|
|
|
|
|
"ae -o uid=PID": true,
|
2023-07-05 10:13:09 +00:00
|
|
|
|
`ae -o "uid= PID"`: true, // ascii space (0x20)
|
|
|
|
|
`ae -o "uid= PID"`: false, // unicode space (U+2003, 0xe2 0x80 0x83)
|
2016-07-06 02:40:48 +00:00
|
|
|
|
"ae o uid=PID": true,
|
|
|
|
|
"aeo uid=PID": true,
|
|
|
|
|
"ae -O uid=PID": true,
|
|
|
|
|
"ae -o pid=PID2 -o uid=PID": true,
|
|
|
|
|
"ae -o pid=PID": false,
|
|
|
|
|
"ae -o pid=PID -o uid=PIDX": true, // FIXME: we do not need to prohibit this
|
|
|
|
|
"aeo pid=PID": false,
|
|
|
|
|
"ae": false,
|
|
|
|
|
"": false,
|
|
|
|
|
}
|
|
|
|
|
for psArgs, errExpected := range tests {
|
2019-10-16 01:55:17 +00:00
|
|
|
|
t.Run(psArgs, func(t *testing.T) {
|
|
|
|
|
err := validatePSArgs(psArgs)
|
|
|
|
|
if errExpected {
|
|
|
|
|
assert.ErrorContains(t, err, "", "psArgs: %q", psArgs)
|
|
|
|
|
} else {
|
|
|
|
|
assert.NilError(t, err, "psArgs: %q", psArgs)
|
|
|
|
|
}
|
|
|
|
|
})
|
2016-07-06 02:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestContainerTopParsePSOutput(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
output []byte
|
2017-09-22 13:52:41 +00:00
|
|
|
|
pids []uint32
|
2016-07-06 02:40:48 +00:00
|
|
|
|
errExpected bool
|
|
|
|
|
}{
|
2019-10-16 01:55:17 +00:00
|
|
|
|
{
|
|
|
|
|
output: []byte(` PID COMMAND
|
2016-07-06 02:40:48 +00:00
|
|
|
|
42 foo
|
|
|
|
|
43 bar
|
2017-02-02 09:02:53 +00:00
|
|
|
|
- -
|
2016-07-06 02:40:48 +00:00
|
|
|
|
100 baz
|
2019-10-16 01:55:17 +00:00
|
|
|
|
`),
|
|
|
|
|
pids: []uint32{42, 43},
|
|
|
|
|
errExpected: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
output: []byte(` UID COMMAND
|
2016-07-06 02:40:48 +00:00
|
|
|
|
42 foo
|
|
|
|
|
43 bar
|
2017-02-02 09:02:53 +00:00
|
|
|
|
- -
|
2016-07-06 02:40:48 +00:00
|
|
|
|
100 baz
|
2019-10-16 01:55:17 +00:00
|
|
|
|
`),
|
|
|
|
|
pids: []uint32{42, 43},
|
|
|
|
|
errExpected: true,
|
|
|
|
|
},
|
2016-07-06 02:40:48 +00:00
|
|
|
|
// unicode space (U+2003, 0xe2 0x80 0x83)
|
2019-10-16 01:55:17 +00:00
|
|
|
|
{
|
|
|
|
|
output: []byte(` PID COMMAND
|
2016-07-06 02:40:48 +00:00
|
|
|
|
42 foo
|
|
|
|
|
43 bar
|
2017-02-02 09:02:53 +00:00
|
|
|
|
- -
|
2016-07-06 02:40:48 +00:00
|
|
|
|
100 baz
|
2019-10-16 01:55:17 +00:00
|
|
|
|
`),
|
|
|
|
|
pids: []uint32{42, 43},
|
|
|
|
|
errExpected: true,
|
|
|
|
|
},
|
2016-07-06 02:40:48 +00:00
|
|
|
|
// the first space is U+2003, the second one is ascii.
|
2019-10-16 01:55:17 +00:00
|
|
|
|
{
|
|
|
|
|
output: []byte(` PID COMMAND
|
2016-07-06 02:40:48 +00:00
|
|
|
|
42 foo
|
|
|
|
|
43 bar
|
|
|
|
|
100 baz
|
2019-10-16 01:55:17 +00:00
|
|
|
|
`),
|
|
|
|
|
pids: []uint32{42, 43},
|
|
|
|
|
errExpected: true,
|
|
|
|
|
},
|
2016-07-06 02:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-16 01:55:17 +00:00
|
|
|
|
for _, tc := range tests {
|
|
|
|
|
tc := tc
|
|
|
|
|
t.Run(string(tc.output), func(t *testing.T) {
|
|
|
|
|
_, err := parsePSOutput(tc.output, tc.pids)
|
|
|
|
|
if tc.errExpected && err == nil {
|
|
|
|
|
t.Fatalf("expected error, got %v (%q)", err, string(tc.output))
|
|
|
|
|
}
|
|
|
|
|
if !tc.errExpected && err != nil {
|
|
|
|
|
t.Fatalf("expected nil, got %v (%q)", err, string(tc.output))
|
|
|
|
|
}
|
|
|
|
|
})
|
2016-07-06 02:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|