buildfile_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package docker
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "testing"
  6. )
  7. // mkTestContext generates a build context from the contents of the provided dockerfile.
  8. // This context is suitable for use as an argument to BuildFile.Build()
  9. func mkTestContext(dockerfile string, files [][2]string, t *testing.T) Archive {
  10. context, err := mkBuildContext(fmt.Sprintf(dockerfile, unitTestImageID), files)
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. return context
  15. }
  16. // A testContextTemplate describes a build context and how to test it
  17. type testContextTemplate struct {
  18. // Contents of the Dockerfile
  19. dockerfile string
  20. // Additional files in the context, eg [][2]string{"./passwd", "gordon"}
  21. files [][2]string
  22. }
  23. // A table of all the contexts to build and test.
  24. // A new docker runtime will be created and torn down for each context.
  25. var testContexts = []testContextTemplate{
  26. {
  27. `
  28. from %s
  29. run sh -c 'echo root:testpass > /tmp/passwd'
  30. run mkdir -p /var/run/sshd
  31. run [ "$(cat /tmp/passwd)" = "root:testpass" ]
  32. run [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]
  33. `,
  34. nil,
  35. },
  36. {
  37. `
  38. from %s
  39. add foo /usr/lib/bla/bar
  40. run [ "$(cat /usr/lib/bla/bar)" = 'hello world!' ]
  41. `,
  42. [][2]string{{"foo", "hello world!"}},
  43. },
  44. {
  45. `
  46. from %s
  47. add f /
  48. run [ "$(cat /f)" = "hello" ]
  49. add f /abc
  50. run [ "$(cat /abc)" = "hello" ]
  51. add f /x/y/z
  52. run [ "$(cat /x/y/z)" = "hello" ]
  53. add f /x/y/d/
  54. run [ "$(cat /x/y/d/f)" = "hello" ]
  55. add d /
  56. run [ "$(cat /ga)" = "bu" ]
  57. add d /somewhere
  58. run [ "$(cat /somewhere/ga)" = "bu" ]
  59. add d /anotherplace/
  60. run [ "$(cat /anotherplace/ga)" = "bu" ]
  61. add d /somewheeeere/over/the/rainbooow
  62. run [ "$(cat /somewheeeere/over/the/rainbooow/ga)" = "bu" ]
  63. `,
  64. [][2]string{
  65. {"f", "hello"},
  66. {"d/ga", "bu"},
  67. },
  68. },
  69. {
  70. `
  71. from %s
  72. env FOO BAR
  73. run [ "$FOO" = "BAR" ]
  74. `,
  75. nil,
  76. },
  77. {
  78. `
  79. from %s
  80. ENTRYPOINT /bin/echo
  81. CMD Hello world
  82. `,
  83. nil,
  84. },
  85. {
  86. `
  87. from %s
  88. VOLUME /test
  89. CMD Hello world
  90. `,
  91. nil,
  92. },
  93. }
  94. // FIXME: test building with 2 successive overlapping ADD commands
  95. func TestBuild(t *testing.T) {
  96. for _, ctx := range testContexts {
  97. runtime, err := newTestRuntime()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. defer nuke(runtime)
  102. srv := &Server{
  103. runtime: runtime,
  104. pullingPool: make(map[string]struct{}),
  105. pushingPool: make(map[string]struct{}),
  106. }
  107. buildfile := NewBuildFile(srv, ioutil.Discard)
  108. if _, err := buildfile.Build(mkTestContext(ctx.dockerfile, ctx.files, t)); err != nil {
  109. t.Fatal(err)
  110. }
  111. }
  112. }
  113. func TestVolume(t *testing.T) {
  114. runtime, err := newTestRuntime()
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. defer nuke(runtime)
  119. srv := &Server{
  120. runtime: runtime,
  121. pullingPool: make(map[string]struct{}),
  122. pushingPool: make(map[string]struct{}),
  123. }
  124. buildfile := NewBuildFile(srv, ioutil.Discard)
  125. imgId, err := buildfile.Build(mkTestContext(`
  126. from %s
  127. VOLUME /test
  128. CMD Hello world
  129. `, nil, t))
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. img, err := srv.ImageInspect(imgId)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. if len(img.Config.Volumes) == 0 {
  138. t.Fail()
  139. }
  140. for key, _ := range img.Config.Volumes {
  141. if key != "/test" {
  142. t.Fail()
  143. }
  144. }
  145. }