top_unix_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. - -
  42. 100 baz
  43. `), []int{42, 43}, false},
  44. {[]byte(` UID COMMAND
  45. 42 foo
  46. 43 bar
  47. - -
  48. 100 baz
  49. `), []int{42, 43}, true},
  50. // unicode space (U+2003, 0xe2 0x80 0x83)
  51. {[]byte(` PID COMMAND
  52. 42 foo
  53. 43 bar
  54. - -
  55. 100 baz
  56. `), []int{42, 43}, true},
  57. // the first space is U+2003, the second one is ascii.
  58. {[]byte(` PID COMMAND
  59. 42 foo
  60. 43 bar
  61. 100 baz
  62. `), []int{42, 43}, true},
  63. }
  64. for _, f := range tests {
  65. _, err := parsePSOutput(f.output, f.pids)
  66. t.Logf("tested %q, got err=%v", string(f.output), err)
  67. if f.errExpected && err == nil {
  68. t.Fatalf("expected error, got %v (%q)", err, string(f.output))
  69. }
  70. if !f.errExpected && err != nil {
  71. t.Fatalf("expected nil, got %v (%q)", err, string(f.output))
  72. }
  73. }
  74. }