buildfile_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 := mkRuntime(t)
  98. defer nuke(runtime)
  99. srv := &Server{
  100. runtime: runtime,
  101. pullingPool: make(map[string]struct{}),
  102. pushingPool: make(map[string]struct{}),
  103. }
  104. buildfile := NewBuildFile(srv, ioutil.Discard)
  105. if _, err := buildfile.Build(mkTestContext(ctx.dockerfile, ctx.files, t)); err != nil {
  106. t.Fatal(err)
  107. }
  108. }
  109. }
  110. func TestVolume(t *testing.T) {
  111. runtime, err := newTestRuntime()
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. defer nuke(runtime)
  116. srv := &Server{
  117. runtime: runtime,
  118. pullingPool: make(map[string]struct{}),
  119. pushingPool: make(map[string]struct{}),
  120. }
  121. buildfile := NewBuildFile(srv, ioutil.Discard)
  122. imgId, err := buildfile.Build(mkTestContext(`
  123. from %s
  124. VOLUME /test
  125. CMD Hello world
  126. `, nil, t))
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. img, err := srv.ImageInspect(imgId)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. if len(img.Config.Volumes) == 0 {
  135. t.Fail()
  136. }
  137. for key, _ := range img.Config.Volumes {
  138. if key != "/test" {
  139. t.Fail()
  140. }
  141. }
  142. }