buildfile_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package docker
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net"
  6. "net/http"
  7. "net/http/httptest"
  8. "strings"
  9. "testing"
  10. )
  11. // mkTestContext generates a build context from the contents of the provided dockerfile.
  12. // This context is suitable for use as an argument to BuildFile.Build()
  13. func mkTestContext(dockerfile string, files [][2]string, t *testing.T) Archive {
  14. context, err := mkBuildContext(dockerfile, files)
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. return context
  19. }
  20. // A testContextTemplate describes a build context and how to test it
  21. type testContextTemplate struct {
  22. // Contents of the Dockerfile
  23. dockerfile string
  24. // Additional files in the context, eg [][2]string{"./passwd", "gordon"}
  25. files [][2]string
  26. // Additional remote files to host on a local HTTP server.
  27. remoteFiles [][2]string
  28. }
  29. // A table of all the contexts to build and test.
  30. // A new docker runtime will be created and torn down for each context.
  31. var testContexts = []testContextTemplate{
  32. {
  33. `
  34. from {IMAGE}
  35. run sh -c 'echo root:testpass > /tmp/passwd'
  36. run mkdir -p /var/run/sshd
  37. run [ "$(cat /tmp/passwd)" = "root:testpass" ]
  38. run [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]
  39. `,
  40. nil,
  41. nil,
  42. },
  43. {
  44. `
  45. from {IMAGE}
  46. add foo /usr/lib/bla/bar
  47. run [ "$(cat /usr/lib/bla/bar)" = 'hello' ]
  48. add http://{SERVERADDR}/baz /usr/lib/baz/quux
  49. run [ "$(cat /usr/lib/baz/quux)" = 'world!' ]
  50. `,
  51. [][2]string{{"foo", "hello"}},
  52. [][2]string{{"/baz", "world!"}},
  53. },
  54. {
  55. `
  56. from {IMAGE}
  57. add f /
  58. run [ "$(cat /f)" = "hello" ]
  59. add f /abc
  60. run [ "$(cat /abc)" = "hello" ]
  61. add f /x/y/z
  62. run [ "$(cat /x/y/z)" = "hello" ]
  63. add f /x/y/d/
  64. run [ "$(cat /x/y/d/f)" = "hello" ]
  65. add d /
  66. run [ "$(cat /ga)" = "bu" ]
  67. add d /somewhere
  68. run [ "$(cat /somewhere/ga)" = "bu" ]
  69. add d /anotherplace/
  70. run [ "$(cat /anotherplace/ga)" = "bu" ]
  71. add d /somewheeeere/over/the/rainbooow
  72. run [ "$(cat /somewheeeere/over/the/rainbooow/ga)" = "bu" ]
  73. `,
  74. [][2]string{
  75. {"f", "hello"},
  76. {"d/ga", "bu"},
  77. },
  78. nil,
  79. },
  80. {
  81. `
  82. from {IMAGE}
  83. add http://{SERVERADDR}/x /a/b/c
  84. run [ "$(cat /a/b/c)" = "hello" ]
  85. add http://{SERVERADDR}/x?foo=bar /
  86. run [ "$(cat /x)" = "hello" ]
  87. add http://{SERVERADDR}/x /d/
  88. run [ "$(cat /d/x)" = "hello" ]
  89. add http://{SERVERADDR} /e
  90. run [ "$(cat /e)" = "blah" ]
  91. `,
  92. nil,
  93. [][2]string{{"/x", "hello"}, {"/", "blah"}},
  94. },
  95. {
  96. `
  97. from {IMAGE}
  98. env FOO BAR
  99. run [ "$FOO" = "BAR" ]
  100. `,
  101. nil,
  102. nil,
  103. },
  104. {
  105. `
  106. from {IMAGE}
  107. ENTRYPOINT /bin/echo
  108. CMD Hello world
  109. `,
  110. nil,
  111. nil,
  112. },
  113. {
  114. `
  115. from {IMAGE}
  116. VOLUME /test
  117. CMD Hello world
  118. `,
  119. nil,
  120. nil,
  121. },
  122. {
  123. `
  124. from {IMAGE}
  125. env FOO /foo/baz
  126. env BAR /bar
  127. env BAZ $BAR
  128. env FOOPATH $PATH:$FOO
  129. run [ "$BAR" = "$BAZ" ]
  130. run [ "$FOOPATH" = "$PATH:/foo/baz" ]
  131. `,
  132. nil,
  133. nil,
  134. },
  135. {
  136. `
  137. from {IMAGE}
  138. env FOO /bar
  139. env TEST testdir
  140. env BAZ /foobar
  141. add testfile $BAZ/
  142. add $TEST $FOO
  143. run [ "$(cat /foobar/testfile)" = "test1" ]
  144. run [ "$(cat /bar/withfile)" = "test2" ]
  145. `,
  146. [][2]string{
  147. {"testfile", "test1"},
  148. {"testdir/withfile", "test2"},
  149. },
  150. nil,
  151. },
  152. }
  153. // FIXME: test building with 2 successive overlapping ADD commands
  154. func constructDockerfile(template string, ip net.IP, port string) string {
  155. serverAddr := fmt.Sprintf("%s:%s", ip, port)
  156. replacer := strings.NewReplacer("{IMAGE}", unitTestImageID, "{SERVERADDR}", serverAddr)
  157. return replacer.Replace(template)
  158. }
  159. func mkTestingFileServer(files [][2]string) (*httptest.Server, error) {
  160. mux := http.NewServeMux()
  161. for _, file := range files {
  162. name, contents := file[0], file[1]
  163. mux.HandleFunc(name, func(w http.ResponseWriter, r *http.Request) {
  164. w.Write([]byte(contents))
  165. })
  166. }
  167. // This is how httptest.NewServer sets up a net.Listener, except that our listener must accept remote
  168. // connections (from the container).
  169. listener, err := net.Listen("tcp", ":0")
  170. if err != nil {
  171. return nil, err
  172. }
  173. s := httptest.NewUnstartedServer(mux)
  174. s.Listener = listener
  175. s.Start()
  176. return s, nil
  177. }
  178. func TestBuild(t *testing.T) {
  179. for _, ctx := range testContexts {
  180. buildImage(ctx, t)
  181. }
  182. }
  183. func buildImage(context testContextTemplate, t *testing.T) *Image {
  184. runtime, err := newTestRuntime()
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. defer nuke(runtime)
  189. srv := &Server{
  190. runtime: runtime,
  191. pullingPool: make(map[string]struct{}),
  192. pushingPool: make(map[string]struct{}),
  193. }
  194. httpServer, err := mkTestingFileServer(context.remoteFiles)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. defer httpServer.Close()
  199. idx := strings.LastIndex(httpServer.URL, ":")
  200. if idx < 0 {
  201. t.Fatalf("could not get port from test http server address %s", httpServer.URL)
  202. }
  203. port := httpServer.URL[idx+1:]
  204. ip := runtime.networkManager.bridgeNetwork.IP
  205. dockerfile := constructDockerfile(context.dockerfile, ip, port)
  206. buildfile := NewBuildFile(srv, ioutil.Discard, false)
  207. id, err := buildfile.Build(mkTestContext(dockerfile, context.files, t))
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. img, err := srv.ImageInspect(id)
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. return img
  216. }
  217. func TestVolume(t *testing.T) {
  218. img := buildImage(testContextTemplate{`
  219. from {IMAGE}
  220. volume /test
  221. cmd Hello world
  222. `, nil, nil}, t)
  223. if len(img.Config.Volumes) == 0 {
  224. t.Fail()
  225. }
  226. for key := range img.Config.Volumes {
  227. if key != "/test" {
  228. t.Fail()
  229. }
  230. }
  231. }
  232. func TestBuildMaintainer(t *testing.T) {
  233. img := buildImage(testContextTemplate{`
  234. from {IMAGE}
  235. maintainer dockerio
  236. `, nil, nil}, t)
  237. if img.Author != "dockerio" {
  238. t.Fail()
  239. }
  240. }
  241. func TestBuildEnv(t *testing.T) {
  242. img := buildImage(testContextTemplate{`
  243. from {IMAGE}
  244. env port 4243
  245. `,
  246. nil, nil}, t)
  247. hasEnv := false
  248. for _, envVar := range img.Config.Env {
  249. if envVar == "port=4243" {
  250. hasEnv = true
  251. break
  252. }
  253. }
  254. if !hasEnv {
  255. t.Fail()
  256. }
  257. }
  258. func TestBuildCmd(t *testing.T) {
  259. img := buildImage(testContextTemplate{`
  260. from {IMAGE}
  261. cmd ["/bin/echo", "Hello World"]
  262. `,
  263. nil, nil}, t)
  264. if img.Config.Cmd[0] != "/bin/echo" {
  265. t.Log(img.Config.Cmd[0])
  266. t.Fail()
  267. }
  268. if img.Config.Cmd[1] != "Hello World" {
  269. t.Log(img.Config.Cmd[1])
  270. t.Fail()
  271. }
  272. }
  273. func TestBuildExpose(t *testing.T) {
  274. img := buildImage(testContextTemplate{`
  275. from {IMAGE}
  276. expose 4243
  277. `,
  278. nil, nil}, t)
  279. if img.Config.PortSpecs[0] != "4243" {
  280. t.Fail()
  281. }
  282. }
  283. func TestBuildEntrypoint(t *testing.T) {
  284. img := buildImage(testContextTemplate{`
  285. from {IMAGE}
  286. entrypoint ["/bin/echo"]
  287. `,
  288. nil, nil}, t)
  289. if img.Config.Entrypoint[0] != "/bin/echo" {
  290. }
  291. }