container_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package docker
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/docker/docker/runconfig"
  12. )
  13. func TestRestartStdin(t *testing.T) {
  14. daemon := mkDaemon(t)
  15. defer nuke(daemon)
  16. container, _, err := daemon.Create(&runconfig.Config{
  17. Image: GetTestImage(daemon).ID,
  18. Cmd: []string{"cat"},
  19. OpenStdin: true,
  20. },
  21. "",
  22. )
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. defer daemon.Destroy(container)
  27. stdin, err := container.StdinPipe()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. stdout, err := container.StdoutPipe()
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. if err := container.Start(); err != nil {
  36. t.Fatal(err)
  37. }
  38. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  39. t.Fatal(err)
  40. }
  41. if err := stdin.Close(); err != nil {
  42. t.Fatal(err)
  43. }
  44. container.State.WaitStop(-1 * time.Second)
  45. output, err := ioutil.ReadAll(stdout)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. if err := stdout.Close(); err != nil {
  50. t.Fatal(err)
  51. }
  52. if string(output) != "hello world" {
  53. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  54. }
  55. // Restart and try again
  56. stdin, err = container.StdinPipe()
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. stdout, err = container.StdoutPipe()
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. if err := container.Start(); err != nil {
  65. t.Fatal(err)
  66. }
  67. if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
  68. t.Fatal(err)
  69. }
  70. if err := stdin.Close(); err != nil {
  71. t.Fatal(err)
  72. }
  73. container.State.WaitStop(-1 * time.Second)
  74. output, err = ioutil.ReadAll(stdout)
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if err := stdout.Close(); err != nil {
  79. t.Fatal(err)
  80. }
  81. if string(output) != "hello world #2" {
  82. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
  83. }
  84. }
  85. func TestStdin(t *testing.T) {
  86. daemon := mkDaemon(t)
  87. defer nuke(daemon)
  88. container, _, err := daemon.Create(&runconfig.Config{
  89. Image: GetTestImage(daemon).ID,
  90. Cmd: []string{"cat"},
  91. OpenStdin: true,
  92. },
  93. "",
  94. )
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. defer daemon.Destroy(container)
  99. stdin, err := container.StdinPipe()
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. stdout, err := container.StdoutPipe()
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if err := container.Start(); err != nil {
  108. t.Fatal(err)
  109. }
  110. defer stdin.Close()
  111. defer stdout.Close()
  112. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  113. t.Fatal(err)
  114. }
  115. if err := stdin.Close(); err != nil {
  116. t.Fatal(err)
  117. }
  118. container.State.WaitStop(-1 * time.Second)
  119. output, err := ioutil.ReadAll(stdout)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. if string(output) != "hello world" {
  124. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  125. }
  126. }
  127. func TestTty(t *testing.T) {
  128. daemon := mkDaemon(t)
  129. defer nuke(daemon)
  130. container, _, err := daemon.Create(&runconfig.Config{
  131. Image: GetTestImage(daemon).ID,
  132. Cmd: []string{"cat"},
  133. OpenStdin: true,
  134. },
  135. "",
  136. )
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. defer daemon.Destroy(container)
  141. stdin, err := container.StdinPipe()
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. stdout, err := container.StdoutPipe()
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. if err := container.Start(); err != nil {
  150. t.Fatal(err)
  151. }
  152. defer stdin.Close()
  153. defer stdout.Close()
  154. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  155. t.Fatal(err)
  156. }
  157. if err := stdin.Close(); err != nil {
  158. t.Fatal(err)
  159. }
  160. container.State.WaitStop(-1 * time.Second)
  161. output, err := ioutil.ReadAll(stdout)
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. if string(output) != "hello world" {
  166. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  167. }
  168. }
  169. func TestEntrypoint(t *testing.T) {
  170. daemon := mkDaemon(t)
  171. defer nuke(daemon)
  172. container, _, err := daemon.Create(
  173. &runconfig.Config{
  174. Image: GetTestImage(daemon).ID,
  175. Entrypoint: []string{"/bin/echo"},
  176. Cmd: []string{"-n", "foobar"},
  177. },
  178. "",
  179. )
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. defer daemon.Destroy(container)
  184. output, err := container.Output()
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. if string(output) != "foobar" {
  189. t.Error(string(output))
  190. }
  191. }
  192. func TestEntrypointNoCmd(t *testing.T) {
  193. daemon := mkDaemon(t)
  194. defer nuke(daemon)
  195. container, _, err := daemon.Create(
  196. &runconfig.Config{
  197. Image: GetTestImage(daemon).ID,
  198. Entrypoint: []string{"/bin/echo", "foobar"},
  199. },
  200. "",
  201. )
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. defer daemon.Destroy(container)
  206. output, err := container.Output()
  207. if err != nil {
  208. t.Fatal(err)
  209. }
  210. if strings.Trim(string(output), "\r\n") != "foobar" {
  211. t.Error(string(output))
  212. }
  213. }
  214. func BenchmarkRunSequential(b *testing.B) {
  215. daemon := mkDaemon(b)
  216. defer nuke(daemon)
  217. for i := 0; i < b.N; i++ {
  218. container, _, err := daemon.Create(&runconfig.Config{
  219. Image: GetTestImage(daemon).ID,
  220. Cmd: []string{"echo", "-n", "foo"},
  221. },
  222. "",
  223. )
  224. if err != nil {
  225. b.Fatal(err)
  226. }
  227. defer daemon.Destroy(container)
  228. output, err := container.Output()
  229. if err != nil {
  230. b.Fatal(err)
  231. }
  232. if string(output) != "foo" {
  233. b.Fatalf("Unexpected output: %s", output)
  234. }
  235. if err := daemon.Destroy(container); err != nil {
  236. b.Fatal(err)
  237. }
  238. }
  239. }
  240. func BenchmarkRunParallel(b *testing.B) {
  241. daemon := mkDaemon(b)
  242. defer nuke(daemon)
  243. var tasks []chan error
  244. for i := 0; i < b.N; i++ {
  245. complete := make(chan error)
  246. tasks = append(tasks, complete)
  247. go func(i int, complete chan error) {
  248. container, _, err := daemon.Create(&runconfig.Config{
  249. Image: GetTestImage(daemon).ID,
  250. Cmd: []string{"echo", "-n", "foo"},
  251. },
  252. "",
  253. )
  254. if err != nil {
  255. complete <- err
  256. return
  257. }
  258. defer daemon.Destroy(container)
  259. if err := container.Start(); err != nil {
  260. complete <- err
  261. return
  262. }
  263. if _, err := container.State.WaitStop(15 * time.Second); err != nil {
  264. complete <- err
  265. return
  266. }
  267. // if string(output) != "foo" {
  268. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  269. // }
  270. if err := daemon.Destroy(container); err != nil {
  271. complete <- err
  272. return
  273. }
  274. complete <- nil
  275. }(i, complete)
  276. }
  277. var errors []error
  278. for _, task := range tasks {
  279. err := <-task
  280. if err != nil {
  281. errors = append(errors, err)
  282. }
  283. }
  284. if len(errors) > 0 {
  285. b.Fatal(errors)
  286. }
  287. }
  288. func tempDir(t *testing.T) string {
  289. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  290. if err != nil {
  291. t.Fatal(err)
  292. }
  293. return tmpDir
  294. }
  295. func TestBindMounts(t *testing.T) {
  296. eng := NewTestEngine(t)
  297. r := mkDaemonFromEngine(eng, t)
  298. defer r.Nuke()
  299. tmpDir := tempDir(t)
  300. defer os.RemoveAll(tmpDir)
  301. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  302. // Test reading from a read-only bind mount
  303. stdout, _ := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
  304. if !strings.Contains(stdout, "touch-me") {
  305. t.Fatal("Container failed to read from bind mount")
  306. }
  307. // test writing to bind mount
  308. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
  309. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  310. // test mounting to an illegal destination directory
  311. if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
  312. t.Fatal("Container bind mounted illegal directory")
  313. }
  314. // test mount a file
  315. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "_", "sh", "-c", "echo -n 'yotta' > /tmp/holla"}, t)
  316. content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  317. if content != "yotta" {
  318. t.Fatal("Container failed to write to bind mount file")
  319. }
  320. }