2016-07-05 22:40:48 -04:00
|
|
|
|
//+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
|
2017-09-22 09:52:41 -04:00
|
|
|
|
pids []uint32
|
2016-07-05 22:40:48 -04:00
|
|
|
|
errExpected bool
|
|
|
|
|
}{
|
|
|
|
|
{[]byte(` PID COMMAND
|
|
|
|
|
42 foo
|
|
|
|
|
43 bar
|
2017-02-02 04:02:53 -05:00
|
|
|
|
- -
|
2016-07-05 22:40:48 -04:00
|
|
|
|
100 baz
|
2017-09-22 09:52:41 -04:00
|
|
|
|
`), []uint32{42, 43}, false},
|
2016-07-05 22:40:48 -04:00
|
|
|
|
{[]byte(` UID COMMAND
|
|
|
|
|
42 foo
|
|
|
|
|
43 bar
|
2017-02-02 04:02:53 -05:00
|
|
|
|
- -
|
2016-07-05 22:40:48 -04:00
|
|
|
|
100 baz
|
2017-09-22 09:52:41 -04:00
|
|
|
|
`), []uint32{42, 43}, true},
|
2016-07-05 22:40:48 -04:00
|
|
|
|
// unicode space (U+2003, 0xe2 0x80 0x83)
|
|
|
|
|
{[]byte(` PID COMMAND
|
|
|
|
|
42 foo
|
|
|
|
|
43 bar
|
2017-02-02 04:02:53 -05:00
|
|
|
|
- -
|
2016-07-05 22:40:48 -04:00
|
|
|
|
100 baz
|
2017-09-22 09:52:41 -04:00
|
|
|
|
`), []uint32{42, 43}, true},
|
2016-07-05 22:40:48 -04:00
|
|
|
|
// the first space is U+2003, the second one is ascii.
|
|
|
|
|
{[]byte(` PID COMMAND
|
|
|
|
|
42 foo
|
|
|
|
|
43 bar
|
|
|
|
|
100 baz
|
2017-09-22 09:52:41 -04:00
|
|
|
|
`), []uint32{42, 43}, true},
|
2016-07-05 22:40:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|