buildfile_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 = []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 docker-ut
  80. ENTRYPOINT /bin/echo
  81. CMD Hello world
  82. `,
  83. nil,
  84. },
  85. }
  86. // FIXME: test building with 2 successive overlapping ADD commands
  87. func TestBuild(t *testing.T) {
  88. for _, ctx := range testContexts {
  89. runtime, err := newTestRuntime()
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. defer nuke(runtime)
  94. srv := &Server{
  95. runtime: runtime,
  96. pullingPool: make(map[string]struct{}),
  97. pushingPool: make(map[string]struct{}),
  98. }
  99. buildfile := NewBuildFile(srv, ioutil.Discard)
  100. if _, err := buildfile.Build(mkTestContext(ctx.dockerfile, ctx.files, t)); err != nil {
  101. t.Fatal(err)
  102. }
  103. }
  104. }