compose_run_build_once_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "regexp"
  19. "strings"
  20. "testing"
  21. "gotest.tools/v3/assert"
  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. _ = c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once.yaml", "down", "--rmi", "local", "--remove-orphans", "-v")
  32. res := c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once.yaml", "--verbose", "run", "--build", "--rm", "curl")
  33. output := res.Stdout()
  34. nginxBuilds := countServiceBuilds(output, projectName, "nginx")
  35. assert.Equal(t, nginxBuilds, 1, "nginx should build once, built %d times\nOutput:\n%s", nginxBuilds, output)
  36. assert.Assert(t, strings.Contains(res.Stdout(), "curl service"))
  37. c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once.yaml", "down", "--remove-orphans")
  38. })
  39. t.Run("nested dependencies build only once each", func(t *testing.T) {
  40. projectName := randomProjectName("build-nested")
  41. _ = c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-nested.yaml", "down", "--rmi", "local", "--remove-orphans", "-v")
  42. res := c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-nested.yaml", "--verbose", "run", "--build", "--rm", "app")
  43. output := res.Stdout()
  44. dbBuilds := countServiceBuilds(output, projectName, "db")
  45. apiBuilds := countServiceBuilds(output, projectName, "api")
  46. appBuilds := countServiceBuilds(output, projectName, "app")
  47. assert.Equal(t, dbBuilds, 1, "db should build once, built %d times\nOutput:\n%s", dbBuilds, output)
  48. assert.Equal(t, apiBuilds, 1, "api should build once, built %d times\nOutput:\n%s", apiBuilds, output)
  49. assert.Equal(t, appBuilds, 1, "app should build once, built %d times\nOutput:\n%s", appBuilds, output)
  50. assert.Assert(t, strings.Contains(output, "App running"))
  51. c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-nested.yaml", "down", "--rmi", "local", "--remove-orphans", "-v")
  52. })
  53. t.Run("service with no dependencies builds once", func(t *testing.T) {
  54. projectName := randomProjectName("build-simple")
  55. _ = c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-no-deps.yaml", "down", "--rmi", "local", "--remove-orphans")
  56. res := c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-no-deps.yaml", "run", "--build", "--rm", "simple")
  57. output := res.Stdout()
  58. simpleBuilds := countServiceBuilds(output, projectName, "simple")
  59. assert.Equal(t, simpleBuilds, 1, "simple should build once, built %d times\nOutput:\n%s", simpleBuilds, output)
  60. assert.Assert(t, strings.Contains(res.Stdout(), "Simple service"))
  61. c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-no-deps.yaml", "down", "--remove-orphans")
  62. })
  63. }
  64. // countServiceBuilds counts how many times a service was built by matching
  65. // the "naming to *{projectName}-{serviceName}* done" pattern in the output
  66. func countServiceBuilds(output, projectName, serviceName string) int {
  67. pattern := regexp.MustCompile(`naming to .*` + regexp.QuoteMeta(projectName) + `-` + regexp.QuoteMeta(serviceName) + `.* done`)
  68. return len(pattern.FindAllString(output, -1))
  69. }
  70. // randomProjectName generates a unique project name for parallel test execution
  71. // Format: prefix-<8 random hex chars> (e.g., "build-once-3f4a9b2c")
  72. func randomProjectName(prefix string) string {
  73. b := make([]byte, 4) // 4 bytes = 8 hex chars
  74. rand.Read(b) //nolint:errcheck
  75. return fmt.Sprintf("%s-%s", prefix, hex.EncodeToString(b))
  76. }