buildfile_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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, nil, true)
  181. }
  182. }
  183. func buildImage(context testContextTemplate, t *testing.T, srv *Server, useCache bool) *Image {
  184. if srv == nil {
  185. runtime, err := newTestRuntime()
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. defer nuke(runtime)
  190. srv = &Server{
  191. runtime: runtime,
  192. pullingPool: make(map[string]struct{}),
  193. pushingPool: make(map[string]struct{}),
  194. }
  195. }
  196. httpServer, err := mkTestingFileServer(context.remoteFiles)
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. defer httpServer.Close()
  201. idx := strings.LastIndex(httpServer.URL, ":")
  202. if idx < 0 {
  203. t.Fatalf("could not get port from test http server address %s", httpServer.URL)
  204. }
  205. port := httpServer.URL[idx+1:]
  206. ip := srv.runtime.networkManager.bridgeNetwork.IP
  207. dockerfile := constructDockerfile(context.dockerfile, ip, port)
  208. buildfile := NewBuildFile(srv, ioutil.Discard, false, useCache)
  209. id, err := buildfile.Build(mkTestContext(dockerfile, context.files, t))
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. img, err := srv.ImageInspect(id)
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. return img
  218. }
  219. func TestVolume(t *testing.T) {
  220. img := buildImage(testContextTemplate{`
  221. from {IMAGE}
  222. volume /test
  223. cmd Hello world
  224. `, nil, nil}, t, nil, true)
  225. if len(img.Config.Volumes) == 0 {
  226. t.Fail()
  227. }
  228. for key := range img.Config.Volumes {
  229. if key != "/test" {
  230. t.Fail()
  231. }
  232. }
  233. }
  234. func TestBuildMaintainer(t *testing.T) {
  235. img := buildImage(testContextTemplate{`
  236. from {IMAGE}
  237. maintainer dockerio
  238. `, nil, nil}, t, nil, true)
  239. if img.Author != "dockerio" {
  240. t.Fail()
  241. }
  242. }
  243. func TestBuildEnv(t *testing.T) {
  244. img := buildImage(testContextTemplate{`
  245. from {IMAGE}
  246. env port 4243
  247. `,
  248. nil, nil}, t, nil, true)
  249. hasEnv := false
  250. for _, envVar := range img.Config.Env {
  251. if envVar == "port=4243" {
  252. hasEnv = true
  253. break
  254. }
  255. }
  256. if !hasEnv {
  257. t.Fail()
  258. }
  259. }
  260. func TestBuildCmd(t *testing.T) {
  261. img := buildImage(testContextTemplate{`
  262. from {IMAGE}
  263. cmd ["/bin/echo", "Hello World"]
  264. `,
  265. nil, nil}, t, nil, true)
  266. if img.Config.Cmd[0] != "/bin/echo" {
  267. t.Log(img.Config.Cmd[0])
  268. t.Fail()
  269. }
  270. if img.Config.Cmd[1] != "Hello World" {
  271. t.Log(img.Config.Cmd[1])
  272. t.Fail()
  273. }
  274. }
  275. func TestBuildExpose(t *testing.T) {
  276. img := buildImage(testContextTemplate{`
  277. from {IMAGE}
  278. expose 4243
  279. `,
  280. nil, nil}, t, nil, true)
  281. if img.Config.PortSpecs[0] != "4243" {
  282. t.Fail()
  283. }
  284. }
  285. func TestBuildEntrypoint(t *testing.T) {
  286. img := buildImage(testContextTemplate{`
  287. from {IMAGE}
  288. entrypoint ["/bin/echo"]
  289. `,
  290. nil, nil}, t, nil, true)
  291. if img.Config.Entrypoint[0] != "/bin/echo" {
  292. }
  293. }
  294. func TestBuildImageWithCache(t *testing.T) {
  295. runtime, err := newTestRuntime()
  296. if err != nil {
  297. t.Fatal(err)
  298. }
  299. defer nuke(runtime)
  300. srv := &Server{
  301. runtime: runtime,
  302. pullingPool: make(map[string]struct{}),
  303. pushingPool: make(map[string]struct{}),
  304. }
  305. template := testContextTemplate{`
  306. from {IMAGE}
  307. maintainer dockerio
  308. `,
  309. nil, nil}
  310. img := buildImage(template, t, srv, true)
  311. imageId := img.ID
  312. img = nil
  313. img = buildImage(template, t, srv, true)
  314. if imageId != img.ID {
  315. t.Logf("Image ids should match: %s != %s", imageId, img.ID)
  316. t.Fail()
  317. }
  318. }
  319. func TestBuildImageWithoutCache(t *testing.T) {
  320. runtime, err := newTestRuntime()
  321. if err != nil {
  322. t.Fatal(err)
  323. }
  324. defer nuke(runtime)
  325. srv := &Server{
  326. runtime: runtime,
  327. pullingPool: make(map[string]struct{}),
  328. pushingPool: make(map[string]struct{}),
  329. }
  330. template := testContextTemplate{`
  331. from {IMAGE}
  332. maintainer dockerio
  333. `,
  334. nil, nil}
  335. img := buildImage(template, t, srv, true)
  336. imageId := img.ID
  337. img = nil
  338. img = buildImage(template, t, srv, false)
  339. if imageId == img.ID {
  340. t.Logf("Image ids should not match: %s == %s", imageId, img.ID)
  341. t.Fail()
  342. }
  343. }