Skip to content

Commit dad6b9b

Browse files
committed
fixes
Signed-off-by: Suleiman Dibirov <[email protected]>
1 parent 79bbaa9 commit dad6b9b

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

pkg/e2e/compose_run_build_once_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/rand"
2121
"encoding/hex"
2222
"fmt"
23+
"regexp"
2324
"strings"
2425
"testing"
2526

@@ -38,11 +39,11 @@ func TestRunBuildOnce(t *testing.T) {
3839
_ = c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once.yaml", "down", "--rmi", "local", "--remove-orphans", "-v")
3940
res := c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once.yaml", "--verbose", "run", "--build", "--rm", "curl")
4041

41-
// Count how many times nginx was built by looking for its unique RUN command output
42-
nginxBuilds := strings.Count(res.Stdout(), "Building nginx at")
42+
output := res.Stdout()
43+
44+
nginxBuilds := countServiceBuilds(output, projectName, "nginx")
4345

44-
// nginx should build exactly once, not twice
45-
assert.Equal(t, nginxBuilds, 1, "nginx dependency should build once, but built %d times", nginxBuilds)
46+
assert.Equal(t, nginxBuilds, 1, "nginx should build once, built %d times\nOutput:\n%s", nginxBuilds, output)
4647
assert.Assert(t, strings.Contains(res.Stdout(), "curl service"))
4748

4849
c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once.yaml", "down", "--remove-orphans")
@@ -55,13 +56,9 @@ func TestRunBuildOnce(t *testing.T) {
5556

5657
output := res.Stdout()
5758

58-
dbBuildMarker := fmt.Sprintf("naming to docker.io/library/%s-db", projectName)
59-
apiBuildMarker := fmt.Sprintf("naming to docker.io/library/%s-api", projectName)
60-
appBuildMarker := fmt.Sprintf("naming to docker.io/library/%s-app", projectName)
61-
62-
dbBuilds := strings.Count(output, dbBuildMarker)
63-
apiBuilds := strings.Count(output, apiBuildMarker)
64-
appBuilds := strings.Count(output, appBuildMarker)
59+
dbBuilds := countServiceBuilds(output, projectName, "db")
60+
apiBuilds := countServiceBuilds(output, projectName, "api")
61+
appBuilds := countServiceBuilds(output, projectName, "app")
6562

6663
assert.Equal(t, dbBuilds, 1, "db should build once, built %d times\nOutput:\n%s", dbBuilds, output)
6764
assert.Equal(t, apiBuilds, 1, "api should build once, built %d times\nOutput:\n%s", apiBuilds, output)
@@ -76,15 +73,24 @@ func TestRunBuildOnce(t *testing.T) {
7673
_ = c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-no-deps.yaml", "down", "--rmi", "local", "--remove-orphans")
7774
res := c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-no-deps.yaml", "run", "--build", "--rm", "simple")
7875

79-
// Should build exactly once
80-
simpleBuilds := strings.Count(res.Stdout(), "Simple service built at")
81-
assert.Equal(t, simpleBuilds, 1, "simple should build once, built %d times", simpleBuilds)
76+
output := res.Stdout()
77+
78+
simpleBuilds := countServiceBuilds(output, projectName, "simple")
79+
80+
assert.Equal(t, simpleBuilds, 1, "simple should build once, built %d times\nOutput:\n%s", simpleBuilds, output)
8281
assert.Assert(t, strings.Contains(res.Stdout(), "Simple service"))
8382

8483
c.RunDockerComposeCmd(t, "-p", projectName, "-f", "./fixtures/run-test/build-once-no-deps.yaml", "down", "--remove-orphans")
8584
})
8685
}
8786

87+
// countServiceBuilds counts how many times a service was built by matching
88+
// the "naming to *{projectName}-{serviceName}* done" pattern in the output
89+
func countServiceBuilds(output, projectName, serviceName string) int {
90+
pattern := regexp.MustCompile(`naming to .*` + regexp.QuoteMeta(projectName) + `-` + regexp.QuoteMeta(serviceName) + `.* done`)
91+
return len(pattern.FindAllString(output, -1))
92+
}
93+
8894
// randomProjectName generates a unique project name for parallel test execution
8995
// Format: prefix-<8 random hex chars> (e.g., "build-once-3f4a9b2c")
9096
func randomProjectName(prefix string) string {

pkg/e2e/fixtures/run-test/build-once-no-deps.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ services:
55
build:
66
dockerfile_inline: |
77
FROM alpine
8-
RUN echo "Simple service built at $(date)" > /build.txt
8+
RUN echo "Simple built at $(date)" > /build.txt
99
CMD echo "Simple service"
1010

pkg/e2e/fixtures/run-test/build-once.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services:
66
build:
77
dockerfile_inline: |
88
FROM alpine
9-
RUN echo "Building nginx at $(date)" > /build-time.txt
9+
RUN echo "Nginx built at $(date)" > /build-time.txt
1010
CMD sleep 3600
1111
1212
# Service that depends on nginx

0 commit comments

Comments
 (0)