top_unix_test.go 2.2 KB

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