container_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package docker
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/runconfig"
  8. )
  9. func TestRestartStdin(t *testing.T) {
  10. daemon := mkDaemon(t)
  11. defer nuke(daemon)
  12. container, _, err := daemon.Create(&runconfig.Config{
  13. Image: GetTestImage(daemon).ID,
  14. Cmd: []string{"cat"},
  15. OpenStdin: true,
  16. },
  17. &runconfig.HostConfig{},
  18. "",
  19. )
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. defer daemon.Destroy(container)
  24. stdin := container.StdinPipe()
  25. stdout := container.StdoutPipe()
  26. if err := container.Start(); err != nil {
  27. t.Fatal(err)
  28. }
  29. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  30. t.Fatal(err)
  31. }
  32. if err := stdin.Close(); err != nil {
  33. t.Fatal(err)
  34. }
  35. container.WaitStop(-1 * time.Second)
  36. output, err := ioutil.ReadAll(stdout)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if err := stdout.Close(); err != nil {
  41. t.Fatal(err)
  42. }
  43. if string(output) != "hello world" {
  44. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  45. }
  46. // Restart and try again
  47. stdin = container.StdinPipe()
  48. stdout = container.StdoutPipe()
  49. if err := container.Start(); err != nil {
  50. t.Fatal(err)
  51. }
  52. if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
  53. t.Fatal(err)
  54. }
  55. if err := stdin.Close(); err != nil {
  56. t.Fatal(err)
  57. }
  58. container.WaitStop(-1 * time.Second)
  59. output, err = ioutil.ReadAll(stdout)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. if err := stdout.Close(); err != nil {
  64. t.Fatal(err)
  65. }
  66. if string(output) != "hello world #2" {
  67. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
  68. }
  69. }
  70. func TestStdin(t *testing.T) {
  71. daemon := mkDaemon(t)
  72. defer nuke(daemon)
  73. container, _, err := daemon.Create(&runconfig.Config{
  74. Image: GetTestImage(daemon).ID,
  75. Cmd: []string{"cat"},
  76. OpenStdin: true,
  77. },
  78. &runconfig.HostConfig{},
  79. "",
  80. )
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. defer daemon.Destroy(container)
  85. stdin := container.StdinPipe()
  86. stdout := container.StdoutPipe()
  87. if err := container.Start(); err != nil {
  88. t.Fatal(err)
  89. }
  90. defer stdin.Close()
  91. defer stdout.Close()
  92. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  93. t.Fatal(err)
  94. }
  95. if err := stdin.Close(); err != nil {
  96. t.Fatal(err)
  97. }
  98. container.WaitStop(-1 * time.Second)
  99. output, err := ioutil.ReadAll(stdout)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. if string(output) != "hello world" {
  104. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  105. }
  106. }
  107. func TestTty(t *testing.T) {
  108. daemon := mkDaemon(t)
  109. defer nuke(daemon)
  110. container, _, err := daemon.Create(&runconfig.Config{
  111. Image: GetTestImage(daemon).ID,
  112. Cmd: []string{"cat"},
  113. OpenStdin: true,
  114. },
  115. &runconfig.HostConfig{},
  116. "",
  117. )
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. defer daemon.Destroy(container)
  122. stdin := container.StdinPipe()
  123. stdout := container.StdoutPipe()
  124. if err := container.Start(); err != nil {
  125. t.Fatal(err)
  126. }
  127. defer stdin.Close()
  128. defer stdout.Close()
  129. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  130. t.Fatal(err)
  131. }
  132. if err := stdin.Close(); err != nil {
  133. t.Fatal(err)
  134. }
  135. container.WaitStop(-1 * time.Second)
  136. output, err := ioutil.ReadAll(stdout)
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. if string(output) != "hello world" {
  141. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  142. }
  143. }
  144. func BenchmarkRunSequential(b *testing.B) {
  145. daemon := mkDaemon(b)
  146. defer nuke(daemon)
  147. for i := 0; i < b.N; i++ {
  148. container, _, err := daemon.Create(&runconfig.Config{
  149. Image: GetTestImage(daemon).ID,
  150. Cmd: []string{"echo", "-n", "foo"},
  151. },
  152. &runconfig.HostConfig{},
  153. "",
  154. )
  155. if err != nil {
  156. b.Fatal(err)
  157. }
  158. defer daemon.Destroy(container)
  159. output, err := container.Output()
  160. if err != nil {
  161. b.Fatal(err)
  162. }
  163. if string(output) != "foo" {
  164. b.Fatalf("Unexpected output: %s", output)
  165. }
  166. if err := daemon.Destroy(container); err != nil {
  167. b.Fatal(err)
  168. }
  169. }
  170. }
  171. func BenchmarkRunParallel(b *testing.B) {
  172. daemon := mkDaemon(b)
  173. defer nuke(daemon)
  174. var tasks []chan error
  175. for i := 0; i < b.N; i++ {
  176. complete := make(chan error)
  177. tasks = append(tasks, complete)
  178. go func(i int, complete chan error) {
  179. container, _, err := daemon.Create(&runconfig.Config{
  180. Image: GetTestImage(daemon).ID,
  181. Cmd: []string{"echo", "-n", "foo"},
  182. },
  183. &runconfig.HostConfig{},
  184. "",
  185. )
  186. if err != nil {
  187. complete <- err
  188. return
  189. }
  190. defer daemon.Destroy(container)
  191. if err := container.Start(); err != nil {
  192. complete <- err
  193. return
  194. }
  195. if _, err := container.WaitStop(15 * time.Second); err != nil {
  196. complete <- err
  197. return
  198. }
  199. // if string(output) != "foo" {
  200. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  201. // }
  202. if err := daemon.Destroy(container); err != nil {
  203. complete <- err
  204. return
  205. }
  206. complete <- nil
  207. }(i, complete)
  208. }
  209. var errors []error
  210. for _, task := range tasks {
  211. err := <-task
  212. if err != nil {
  213. errors = append(errors, err)
  214. }
  215. }
  216. if len(errors) > 0 {
  217. b.Fatal(errors)
  218. }
  219. }