moby/daemon/top_unix_test.go
catinthesky a7497c39cd Fixing issue of docker top command failure when dealing with -m option
Changed per requested review to refactor to make it more logic clear.

Current output for "docker top <contianer-id> m" option,eg:
root@b2c7ec58399d:/go/src/github.com/docker/docker# docker top 755d5871ec45 am
PID                 TTY                 STAT                TIME                COMMAND
148                 pts/0               -                   0:00                bash
-                   -                   Ss+                 0:00                -

fixing issue:#30580

Signed-off-by: catinthesky <yaozaiyong@hotmail.com>
2017-03-10 02:50:21 +00:00

79 lines
1.9 KiB
Go
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//+build !windows
package daemon
import (
"testing"
)
func TestContainerTopValidatePSArgs(t *testing.T) {
tests := map[string]bool{
"ae -o uid=PID": true,
"ae -o \"uid= PID\"": true, // ascii space (0x20)
"ae -o \"uid=PID\"": false, // unicode space (U+2003, 0xe2 0x80 0x83)
"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 {
err := validatePSArgs(psArgs)
t.Logf("tested %q, got err=%v", psArgs, err)
if errExpected && err == nil {
t.Fatalf("expected error, got %v (%q)", err, psArgs)
}
if !errExpected && err != nil {
t.Fatalf("expected nil, got %v (%q)", err, psArgs)
}
}
}
func TestContainerTopParsePSOutput(t *testing.T) {
tests := []struct {
output []byte
pids []int
errExpected bool
}{
{[]byte(` PID COMMAND
42 foo
43 bar
- -
100 baz
`), []int{42, 43}, false},
{[]byte(` UID COMMAND
42 foo
43 bar
- -
100 baz
`), []int{42, 43}, true},
// unicode space (U+2003, 0xe2 0x80 0x83)
{[]byte(`PIDCOMMAND
42 foo
43 bar
- -
100 baz
`), []int{42, 43}, true},
// the first space is U+2003, the second one is ascii.
{[]byte(`PID COMMAND
42 foo
43 bar
100 baz
`), []int{42, 43}, true},
}
for _, f := range tests {
_, err := parsePSOutput(f.output, f.pids)
t.Logf("tested %q, got err=%v", string(f.output), err)
if f.errExpected && err == nil {
t.Fatalf("expected error, got %v (%q)", err, string(f.output))
}
if !f.errExpected && err != nil {
t.Fatalf("expected nil, got %v (%q)", err, string(f.output))
}
}
}