exec_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package container
  2. import (
  3. "testing"
  4. "github.com/docker/docker/api/types"
  5. )
  6. type arguments struct {
  7. options execOptions
  8. execCmd []string
  9. }
  10. func TestParseExec(t *testing.T) {
  11. valids := map[*arguments]*types.ExecConfig{
  12. &arguments{
  13. execCmd: []string{"command"},
  14. }: {
  15. Cmd: []string{"command"},
  16. AttachStdout: true,
  17. AttachStderr: true,
  18. },
  19. &arguments{
  20. execCmd: []string{"command1", "command2"},
  21. }: {
  22. Cmd: []string{"command1", "command2"},
  23. AttachStdout: true,
  24. AttachStderr: true,
  25. },
  26. &arguments{
  27. options: execOptions{
  28. interactive: true,
  29. tty: true,
  30. user: "uid",
  31. },
  32. execCmd: []string{"command"},
  33. }: {
  34. User: "uid",
  35. AttachStdin: true,
  36. AttachStdout: true,
  37. AttachStderr: true,
  38. Tty: true,
  39. Cmd: []string{"command"},
  40. },
  41. &arguments{
  42. options: execOptions{
  43. detach: true,
  44. },
  45. execCmd: []string{"command"},
  46. }: {
  47. AttachStdin: false,
  48. AttachStdout: false,
  49. AttachStderr: false,
  50. Detach: true,
  51. Cmd: []string{"command"},
  52. },
  53. &arguments{
  54. options: execOptions{
  55. tty: true,
  56. interactive: true,
  57. detach: true,
  58. },
  59. execCmd: []string{"command"},
  60. }: {
  61. AttachStdin: false,
  62. AttachStdout: false,
  63. AttachStderr: false,
  64. Detach: true,
  65. Tty: true,
  66. Cmd: []string{"command"},
  67. },
  68. }
  69. for valid, expectedExecConfig := range valids {
  70. execConfig, err := parseExec(&valid.options, valid.execCmd)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if !compareExecConfig(expectedExecConfig, execConfig) {
  75. t.Fatalf("Expected [%v] for %v, got [%v]", expectedExecConfig, valid, execConfig)
  76. }
  77. }
  78. }
  79. func compareExecConfig(config1 *types.ExecConfig, config2 *types.ExecConfig) bool {
  80. if config1.AttachStderr != config2.AttachStderr {
  81. return false
  82. }
  83. if config1.AttachStdin != config2.AttachStdin {
  84. return false
  85. }
  86. if config1.AttachStdout != config2.AttachStdout {
  87. return false
  88. }
  89. if config1.Detach != config2.Detach {
  90. return false
  91. }
  92. if config1.Privileged != config2.Privileged {
  93. return false
  94. }
  95. if config1.Tty != config2.Tty {
  96. return false
  97. }
  98. if config1.User != config2.User {
  99. return false
  100. }
  101. if len(config1.Cmd) != len(config2.Cmd) {
  102. return false
  103. }
  104. for index, value := range config1.Cmd {
  105. if value != config2.Cmd[index] {
  106. return false
  107. }
  108. }
  109. return true
  110. }