top_unix_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //go:build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "testing"
  5. "gotest.tools/v3/assert"
  6. )
  7. func TestContainerTopValidatePSArgs(t *testing.T) {
  8. tests := map[string]bool{
  9. "ae -o uid=PID": true,
  10. `ae -o "uid= PID"`: true, // ascii space (0x20)
  11. `ae -o "uid= PID"`: false, // unicode space (U+2003, 0xe2 0x80 0x83)
  12. "ae o uid=PID": true,
  13. "aeo uid=PID": true,
  14. "ae -O uid=PID": true,
  15. "ae -o pid=PID2 -o uid=PID": true,
  16. "ae -o pid=PID": false,
  17. "ae -o pid=PID -o uid=PIDX": true, // FIXME: we do not need to prohibit this
  18. "aeo pid=PID": false,
  19. "ae": false,
  20. "": false,
  21. }
  22. for psArgs, errExpected := range tests {
  23. t.Run(psArgs, func(t *testing.T) {
  24. err := validatePSArgs(psArgs)
  25. if errExpected {
  26. assert.ErrorContains(t, err, "", "psArgs: %q", psArgs)
  27. } else {
  28. assert.NilError(t, err, "psArgs: %q", psArgs)
  29. }
  30. })
  31. }
  32. }
  33. func TestContainerTopParsePSOutput(t *testing.T) {
  34. tests := []struct {
  35. output []byte
  36. pids []uint32
  37. errExpected bool
  38. }{
  39. {
  40. output: []byte(` PID COMMAND
  41. 42 foo
  42. 43 bar
  43. - -
  44. 100 baz
  45. `),
  46. pids: []uint32{42, 43},
  47. errExpected: false,
  48. },
  49. {
  50. output: []byte(` UID COMMAND
  51. 42 foo
  52. 43 bar
  53. - -
  54. 100 baz
  55. `),
  56. pids: []uint32{42, 43},
  57. errExpected: true,
  58. },
  59. // unicode space (U+2003, 0xe2 0x80 0x83)
  60. {
  61. output: []byte(` PID COMMAND
  62. 42 foo
  63. 43 bar
  64. - -
  65. 100 baz
  66. `),
  67. pids: []uint32{42, 43},
  68. errExpected: true,
  69. },
  70. // the first space is U+2003, the second one is ascii.
  71. {
  72. output: []byte(` PID COMMAND
  73. 42 foo
  74. 43 bar
  75. 100 baz
  76. `),
  77. pids: []uint32{42, 43},
  78. errExpected: true,
  79. },
  80. }
  81. for _, tc := range tests {
  82. tc := tc
  83. t.Run(string(tc.output), func(t *testing.T) {
  84. _, err := parsePSOutput(tc.output, tc.pids)
  85. if tc.errExpected && err == nil {
  86. t.Fatalf("expected error, got %v (%q)", err, string(tc.output))
  87. }
  88. if !tc.errExpected && err != nil {
  89. t.Fatalf("expected nil, got %v (%q)", err, string(tc.output))
  90. }
  91. })
  92. }
  93. }