compose_build_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package e2e
  14. import (
  15. "io/ioutil"
  16. "net/http"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "testing"
  21. "time"
  22. "gotest.tools/v3/assert"
  23. "gotest.tools/v3/icmd"
  24. . "github.com/docker/compose-cli/utils/e2e"
  25. )
  26. func TestLocalComposeBuild(t *testing.T) {
  27. c := NewParallelE2eCLI(t, binDir)
  28. t.Run("build named and unnamed images", func(t *testing.T) {
  29. // ensure local test run does not reuse previously build image
  30. c.RunDockerOrExitError("rmi", "build-test_nginx")
  31. c.RunDockerOrExitError("rmi", "custom-nginx")
  32. res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "build")
  33. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  34. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  35. c.RunDockerCmd("image", "inspect", "custom-nginx")
  36. })
  37. t.Run("build with build-arg", func(t *testing.T) {
  38. // ensure local test run does not reuse previously build image
  39. c.RunDockerOrExitError("rmi", "build-test_nginx")
  40. c.RunDockerOrExitError("rmi", "custom-nginx")
  41. c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO=BAR")
  42. res := c.RunDockerCmd("image", "inspect", "build-test_nginx")
  43. res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
  44. })
  45. t.Run("build with build-arg set by env", func(t *testing.T) {
  46. // ensure local test run does not reuse previously build image
  47. c.RunDockerOrExitError("rmi", "build-test_nginx")
  48. c.RunDockerOrExitError("rmi", "custom-nginx")
  49. icmd.RunCmd(c.NewDockerCmd("compose", "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO"),
  50. func(cmd *icmd.Cmd) {
  51. cmd.Env = append(cmd.Env, "FOO=BAR")
  52. })
  53. res := c.RunDockerCmd("image", "inspect", "build-test_nginx")
  54. res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
  55. })
  56. t.Run("build as part of up", func(t *testing.T) {
  57. c.RunDockerOrExitError("rmi", "build-test_nginx")
  58. c.RunDockerOrExitError("rmi", "custom-nginx")
  59. res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "up", "-d")
  60. t.Cleanup(func() {
  61. c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "down")
  62. })
  63. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  64. res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
  65. output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
  66. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  67. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  68. c.RunDockerCmd("image", "inspect", "custom-nginx")
  69. })
  70. t.Run("no rebuild when up again", func(t *testing.T) {
  71. res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "up", "-d")
  72. assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static"), res.Stdout())
  73. })
  74. t.Run("rebuild when up --build", func(t *testing.T) {
  75. res := c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "up", "-d", "--build")
  76. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  77. res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
  78. })
  79. t.Run("cleanup build project", func(t *testing.T) {
  80. c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "down")
  81. c.RunDockerCmd("rmi", "build-test_nginx")
  82. c.RunDockerCmd("rmi", "custom-nginx")
  83. })
  84. }
  85. func TestLocalComposeBuildStaticDockerfilePath(t *testing.T) {
  86. c := NewParallelE2eCLI(t, binDir)
  87. t.Run("build ddev-style compose files", func(t *testing.T) {
  88. dir, err := ioutil.TempDir("", "ddev")
  89. assert.NilError(t, err)
  90. defer os.RemoveAll(dir) //nolint:errcheck
  91. assert.NilError(t, ioutil.WriteFile(filepath.Join(dir, "compose.yaml"), []byte(`services:
  92. service1:
  93. build:
  94. context: `+dir+`/service1
  95. dockerfile: Dockerfile
  96. service2:
  97. build:
  98. context: `+dir+`/service2
  99. dockerfile: `+dir+`/service2/Dockerfile
  100. `), 0644))
  101. assert.NilError(t, os.Mkdir(filepath.Join(dir, "service1"), 0700))
  102. assert.NilError(t, ioutil.WriteFile(filepath.Join(dir, "service1", "Dockerfile"), []byte(`FROM alpine
  103. RUN echo "hello"
  104. `), 0644))
  105. assert.NilError(t, os.Mkdir(filepath.Join(dir, "service2"), 0700))
  106. assert.NilError(t, ioutil.WriteFile(filepath.Join(dir, "service2", "Dockerfile"), []byte(`FROM alpine
  107. RUN echo "world"
  108. `), 0644))
  109. res := c.RunDockerCmd("compose", "-f", filepath.Join(dir, "compose.yaml"), "build")
  110. res.Assert(t, icmd.Expected{Out: `RUN echo "hello"`})
  111. res.Assert(t, icmd.Expected{Out: `RUN echo "world"`})
  112. c.RunDockerCmd("compose", "-f", filepath.Join(dir, "compose.yaml"), "down", "--rmi", "all")
  113. })
  114. }