exec_test.go 2.4 KB

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