container_test.go 5.1 KB

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