top_unix_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //+build !windows
  2. package daemon
  3. import (
  4. "testing"
  5. )
  6. func TestContainerTopValidatePSArgs(t *testing.T) {
  7. tests := map[string]bool{
  8. "ae -o uid=PID": true,
  9. "ae -o \"uid= PID\"": true, // ascii space (0x20)
  10. "ae -o \"uid= PID\"": false, // unicode space (U+2003, 0xe2 0x80 0x83)
  11. "ae o uid=PID": true,
  12. "aeo uid=PID": true,
  13. "ae -O uid=PID": true,
  14. "ae -o pid=PID2 -o uid=PID": true,
  15. "ae -o pid=PID": false,
  16. "ae -o pid=PID -o uid=PIDX": true, // FIXME: we do not need to prohibit this
  17. "aeo pid=PID": false,
  18. "ae": false,
  19. "": false,
  20. }
  21. for psArgs, errExpected := range tests {
  22. err := validatePSArgs(psArgs)
  23. t.Logf("tested %q, got err=%v", psArgs, err)
  24. if errExpected && err == nil {
  25. t.Fatalf("expected error, got %v (%q)", err, psArgs)
  26. }
  27. if !errExpected && err != nil {
  28. t.Fatalf("expected nil, got %v (%q)", err, psArgs)
  29. }
  30. }
  31. }
  32. func TestContainerTopParsePSOutput(t *testing.T) {
  33. tests := []struct {
  34. output []byte
  35. pids []int
  36. errExpected bool
  37. }{
  38. {[]byte(` PID COMMAND
  39. 42 foo
  40. 43 bar
  41. 100 baz
  42. `), []int{42, 43}, false},
  43. {[]byte(` UID COMMAND
  44. 42 foo
  45. 43 bar
  46. 100 baz
  47. `), []int{42, 43}, true},
  48. // unicode space (U+2003, 0xe2 0x80 0x83)
  49. {[]byte(` PID COMMAND
  50. 42 foo
  51. 43 bar
  52. 100 baz
  53. `), []int{42, 43}, true},
  54. // the first space is U+2003, the second one is ascii.
  55. {[]byte(` PID COMMAND
  56. 42 foo
  57. 43 bar
  58. 100 baz
  59. `), []int{42, 43}, true},
  60. }
  61. for _, f := range tests {
  62. _, err := parsePSOutput(f.output, f.pids)
  63. t.Logf("tested %q, got err=%v", string(f.output), err)
  64. if f.errExpected && err == nil {
  65. t.Fatalf("expected error, got %v (%q)", err, string(f.output))
  66. }
  67. if !f.errExpected && err != nil {
  68. t.Fatalf("expected nil, got %v (%q)", err, string(f.output))
  69. }
  70. }
  71. }