compose_run_build_once_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "crypto/rand"
  16. "encoding/hex"
  17. "fmt"
  18. "strings"
  19. "testing"
  20. "gotest.tools/v3/assert"
  21. "gotest.tools/v3/icmd"
  22. )
  23. // TestRunBuildOnce tests that services with pull_policy: build are only built once
  24. // when using 'docker compose run', even when they are dependencies.
  25. // This addresses a bug where dependencies were built twice: once in startDependencies
  26. // and once in ensureImagesExists.
  27. func TestRunBuildOnce(t *testing.T) {
  28. c := NewParallelCLI(t)
  29. t.Run("dependency with pull_policy build is built only once", func(t *testing.T) {
  30. projectName := randomProjectName("build-once")
  31. res := c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once.yaml", "down", "--rmi", "local", "--remove-orphans")
  32. res.Assert(t, icmd.Success)
  33. res = c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once.yaml", "run", "--build", "--rm", "curl")
  34. res.Assert(t, icmd.Success)
  35. // Count how many times nginx was built by looking for its unique RUN command output
  36. nginxBuilds := strings.Count(res.Combined(), "Building nginx at")
  37. // nginx should build exactly once, not twice
  38. assert.Equal(t, nginxBuilds, 1, "nginx dependency should build once, but built %d times", nginxBuilds)
  39. assert.Assert(t, strings.Contains(res.Combined(), "curl service"))
  40. c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once.yaml", "down", "--remove-orphans")
  41. })
  42. t.Run("nested dependencies build only once each", func(t *testing.T) {
  43. projectName := randomProjectName("build-nested")
  44. res := c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-nested.yaml", "down", "--rmi", "local", "--remove-orphans")
  45. res.Assert(t, icmd.Success)
  46. res = c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-nested.yaml", "run", "--build", "--rm", "app")
  47. res.Assert(t, icmd.Success)
  48. output := res.Combined()
  49. // Each service should build exactly once
  50. dbBuilds := strings.Count(output, "DB built at")
  51. apiBuilds := strings.Count(output, "API built at")
  52. appBuilds := strings.Count(output, "App built at")
  53. assert.Equal(t, dbBuilds, 1, "db should build once, built %d times", dbBuilds)
  54. assert.Equal(t, apiBuilds, 1, "api should build once, built %d times", apiBuilds)
  55. assert.Equal(t, appBuilds, 1, "app should build once, built %d times", appBuilds)
  56. assert.Assert(t, strings.Contains(output, "App running"))
  57. c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-nested.yaml", "down", "--remove-orphans")
  58. })
  59. t.Run("service with no dependencies builds once", func(t *testing.T) {
  60. projectName := randomProjectName("build-simple")
  61. res := c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-no-deps.yaml", "down", "--rmi", "local", "--remove-orphans")
  62. res.Assert(t, icmd.Success)
  63. res = c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-no-deps.yaml", "run", "--build", "--rm", "simple")
  64. res.Assert(t, icmd.Success)
  65. // Should build exactly once
  66. simpleBuilds := strings.Count(res.Combined(), "Simple service built at")
  67. assert.Equal(t, simpleBuilds, 1, "simple should build once, built %d times", simpleBuilds)
  68. assert.Assert(t, strings.Contains(res.Combined(), "Simple service"))
  69. c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-no-deps.yaml", "down", "--remove-orphans")
  70. })
  71. }
  72. // randomProjectName generates a unique project name for parallel test execution
  73. // Format: prefix-<8 random hex chars> (e.g., "build-once-3f4a9b2c")
  74. func randomProjectName(prefix string) string {
  75. b := make([]byte, 4) // 4 bytes = 8 hex chars
  76. rand.Read(b) //nolint:errcheck
  77. return fmt.Sprintf("%s-%s", prefix, hex.EncodeToString(b))
  78. }