compose_build_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "net/http"
  16. "strings"
  17. "testing"
  18. "time"
  19. "gotest.tools/v3/assert"
  20. "gotest.tools/v3/icmd"
  21. )
  22. func TestLocalComposeBuild(t *testing.T) {
  23. c := NewParallelE2eCLI(t, binDir)
  24. t.Run("build named and unnamed images", func(t *testing.T) {
  25. // ensure local test run does not reuse previously build image
  26. c.RunDockerOrExitError("rmi", "build-test_nginx")
  27. c.RunDockerOrExitError("rmi", "custom-nginx")
  28. res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "build")
  29. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  30. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  31. c.RunDockerCmd("image", "inspect", "custom-nginx")
  32. })
  33. t.Run("build with build-arg", func(t *testing.T) {
  34. // ensure local test run does not reuse previously build image
  35. c.RunDockerOrExitError("rmi", "build-test_nginx")
  36. c.RunDockerOrExitError("rmi", "custom-nginx")
  37. c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO=BAR")
  38. res := c.RunDockerCmd("image", "inspect", "build-test_nginx")
  39. res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
  40. })
  41. t.Run("build with build-arg set by env", func(t *testing.T) {
  42. // ensure local test run does not reuse previously build image
  43. c.RunDockerOrExitError("rmi", "build-test_nginx")
  44. c.RunDockerOrExitError("rmi", "custom-nginx")
  45. icmd.RunCmd(c.NewDockerCmd("compose", "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO"),
  46. func(cmd *icmd.Cmd) {
  47. cmd.Env = append(cmd.Env, "FOO=BAR")
  48. })
  49. res := c.RunDockerCmd("image", "inspect", "build-test_nginx")
  50. res.Assert(t, icmd.Expected{Out: `"FOO": "BAR"`})
  51. })
  52. t.Run("build as part of up", func(t *testing.T) {
  53. c.RunDockerOrExitError("rmi", "build-test_nginx")
  54. c.RunDockerOrExitError("rmi", "custom-nginx")
  55. res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "up", "-d")
  56. t.Cleanup(func() {
  57. c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "down")
  58. })
  59. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  60. res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
  61. output := HTTPGetWithRetry(t, "http://localhost:8070", http.StatusOK, 2*time.Second, 20*time.Second)
  62. assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
  63. c.RunDockerCmd("image", "inspect", "build-test_nginx")
  64. c.RunDockerCmd("image", "inspect", "custom-nginx")
  65. })
  66. t.Run("no rebuild when up again", func(t *testing.T) {
  67. res := c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "up", "-d")
  68. assert.Assert(t, !strings.Contains(res.Stdout(), "COPY static"), res.Stdout())
  69. })
  70. t.Run("rebuild when up --build", func(t *testing.T) {
  71. res := c.RunDockerCmd("compose", "--workdir", "fixtures/build-test", "up", "-d", "--build")
  72. res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
  73. res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})
  74. })
  75. t.Run("cleanup build project", func(t *testing.T) {
  76. c.RunDockerCmd("compose", "--project-directory", "fixtures/build-test", "down")
  77. c.RunDockerCmd("rmi", "build-test_nginx")
  78. c.RunDockerCmd("rmi", "custom-nginx")
  79. })
  80. }