Skip to content

Commit f9be714

Browse files
authored
Merge pull request #3333 from crazy-max/compose-tests
compose integration tests
2 parents a1ca46e + 03000cc commit f9be714

6 files changed

Lines changed: 209 additions & 8 deletions

File tree

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ARG DOCKER_CLI_VERSION=${DOCKER_VERSION}
1212
ARG GOTESTSUM_VERSION=v1.12.0
1313
ARG REGISTRY_VERSION=3.0.0
1414
ARG BUILDKIT_VERSION=v0.23.2
15+
ARG COMPOSE_VERSION=v2.39.0
1516
ARG UNDOCK_VERSION=0.9.0
1617

1718
FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
@@ -24,6 +25,7 @@ FROM dockereng/cli-bin:$DOCKER_VERSION_ALT_27 AS docker-cli-alt27
2425
FROM dockereng/cli-bin:$DOCKER_VERSION_ALT_26 AS docker-cli-alt26
2526
FROM registry:$REGISTRY_VERSION AS registry
2627
FROM moby/buildkit:$BUILDKIT_VERSION AS buildkit
28+
FROM docker/compose-bin:$COMPOSE_VERSION AS compose
2729
FROM crazymax/undock:$UNDOCK_VERSION AS undock
2830

2931
FROM golatest AS gobase
@@ -137,8 +139,10 @@ COPY --link --from=docker-cli-alt27 / /opt/docker-alt-27/
137139
COPY --link --from=docker-cli-alt26 / /opt/docker-alt-26/
138140
COPY --link --from=buildkit /usr/bin/buildkitd /usr/bin/
139141
COPY --link --from=buildkit /usr/bin/buildctl /usr/bin/
142+
COPY --link --from=compose /docker-compose /usr/bin/compose
140143
COPY --link --from=undock /usr/local/bin/undock /usr/bin/
141144
COPY --link --from=binaries /buildx /usr/bin/
145+
RUN mkdir -p /usr/local/lib/docker/cli-plugins && ln -s /usr/bin/buildx /usr/local/lib/docker/cli-plugins/docker-buildx
142146
ENV TEST_DOCKER_EXTRA="docker@27.5=/opt/docker-alt-27,docker@26.1=/opt/docker-alt-26"
143147

144148
FROM integration-test-base AS integration-test

tests/bake.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
7272
testBakeMetadataWarningsDedup,
7373
testBakeMultiExporters,
7474
testBakeLoadPush,
75-
testListTargets,
76-
testListVariables,
77-
testListTypedVariables,
75+
testBakeListTargets,
76+
testBakeListVariables,
77+
testBakeListTypedVariables,
7878
testBakeCallCheck,
7979
testBakeCallCheckFlag,
8080
testBakeCallMetadata,
@@ -1691,7 +1691,7 @@ target "default" {
16911691
// TODO: test metadata file when supported by multi exporters https://github.com/docker/buildx/issues/2181
16921692
}
16931693

1694-
func testListTargets(t *testing.T, sb integration.Sandbox) {
1694+
func testBakeListTargets(t *testing.T, sb integration.Sandbox) {
16951695
bakefile := []byte(`
16961696
target "foo" {
16971697
description = "This builds foo"
@@ -1714,7 +1714,7 @@ target "abc" {
17141714
require.Equal(t, "TARGET\tDESCRIPTION\nabc\t\nfoo\tThis builds foo", strings.TrimSpace(out))
17151715
}
17161716

1717-
func testListVariables(t *testing.T, sb integration.Sandbox) {
1717+
func testBakeListVariables(t *testing.T, sb integration.Sandbox) {
17181718
bakefile := []byte(`
17191719
variable "foo" {
17201720
default = "bar"
@@ -1743,7 +1743,7 @@ target "default" {
17431743
require.Equal(t, "VARIABLE\tTYPE\tVALUE\tDESCRIPTION\nabc\t\t\t<null>\t\ndef\t\t\t\t\nfoo\t\t\tbar\tThis is foo", strings.TrimSpace(out))
17441744
}
17451745

1746-
func testListTypedVariables(t *testing.T, sb integration.Sandbox) {
1746+
func testBakeListTypedVariables(t *testing.T, sb integration.Sandbox) {
17471747
bakefile := []byte(`
17481748
variable "abc" {
17491749
type = string

tests/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
7676
testBuildSecret,
7777
testBuildDefaultLoad,
7878
testBuildCall,
79-
testCheckCallOutput,
79+
testBuildCheckCallOutput,
8080
testBuildExtraHosts,
8181
}
8282

@@ -1241,7 +1241,7 @@ COPy --from=base \
12411241
})
12421242
}
12431243

1244-
func testCheckCallOutput(t *testing.T, sb integration.Sandbox) {
1244+
func testBuildCheckCallOutput(t *testing.T, sb integration.Sandbox) {
12451245
t.Run("check for warning count msg in check without warnings", func(t *testing.T) {
12461246
dockerfile := []byte(`
12471247
FROM busybox AS base

tests/compose.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/containerd/continuity/fs/fstest"
9+
"github.com/moby/buildkit/identity"
10+
"github.com/moby/buildkit/util/contentutil"
11+
"github.com/moby/buildkit/util/testutil"
12+
"github.com/moby/buildkit/util/testutil/integration"
13+
"github.com/pkg/errors"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
var composeTests = []func(t *testing.T, sb integration.Sandbox){
18+
testComposeBuildLocalStore,
19+
testComposeBuildRegistry,
20+
testComposeBuildMultiPlatform,
21+
testComposeBuildCheck,
22+
}
23+
24+
func testComposeBuildLocalStore(t *testing.T, sb integration.Sandbox) {
25+
if !isDockerWorker(sb) && !isDockerContainerWorker(sb) {
26+
t.Skip("only testing with docker and docker-container worker")
27+
}
28+
29+
target := "buildx:local-" + identity.NewID()
30+
dir := composeTestProject(target, t)
31+
32+
t.Cleanup(func() {
33+
cmd := dockerCmd(sb, withArgs("image", "rm", target))
34+
cmd.Stderr = os.Stderr
35+
require.NoError(t, cmd.Run())
36+
})
37+
38+
cmd := composeCmd(sb, withDir(dir), withArgs("build"))
39+
out, err := cmd.CombinedOutput()
40+
require.NoError(t, err, string(out))
41+
42+
cmd = dockerCmd(sb, withArgs("image", "inspect", target))
43+
cmd.Stderr = os.Stderr
44+
require.NoError(t, cmd.Run())
45+
}
46+
47+
func testComposeBuildRegistry(t *testing.T, sb integration.Sandbox) {
48+
registry, err := sb.NewRegistry()
49+
if errors.Is(err, integration.ErrRequirements) {
50+
t.Skip(err.Error())
51+
}
52+
require.NoError(t, err)
53+
54+
target := registry + "/buildx/registry:latest"
55+
dir := composeTestProject(target, t)
56+
57+
cmd := composeCmd(sb, withDir(dir), withArgs("build", "--push"))
58+
out, err := cmd.CombinedOutput()
59+
require.NoError(t, err, string(out))
60+
61+
desc, provider, err := contentutil.ProviderFromRef(target)
62+
require.NoError(t, err)
63+
_, err = testutil.ReadImages(sb.Context(), provider, desc)
64+
require.NoError(t, err)
65+
}
66+
67+
func testComposeBuildMultiPlatform(t *testing.T, sb integration.Sandbox) {
68+
registry, err := sb.NewRegistry()
69+
if errors.Is(err, integration.ErrRequirements) {
70+
t.Skip(err.Error())
71+
}
72+
require.NoError(t, err)
73+
74+
target := registry + "/buildx/registry:latest"
75+
76+
dockerfile := []byte(`
77+
FROM busybox:latest
78+
COPY foo /etc/foo
79+
`)
80+
composefile := fmt.Appendf([]byte{}, `
81+
services:
82+
bar:
83+
build:
84+
context: .
85+
platforms:
86+
- linux/amd64
87+
- linux/arm64
88+
image: %s
89+
`, target)
90+
91+
dir := tmpdir(
92+
t,
93+
fstest.CreateFile("compose.yml", composefile, 0600),
94+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
95+
fstest.CreateFile("foo", []byte("foo"), 0600),
96+
)
97+
98+
cmd := composeCmd(sb, withDir(dir), withArgs("build", "--push"))
99+
out, err := cmd.CombinedOutput()
100+
101+
if !isMobyWorker(sb) {
102+
require.NoError(t, err, string(out))
103+
104+
desc, provider, err := contentutil.ProviderFromRef(target)
105+
require.NoError(t, err)
106+
imgs, err := testutil.ReadImages(sb.Context(), provider, desc)
107+
require.NoError(t, err)
108+
109+
img := imgs.Find("linux/amd64")
110+
require.NotNil(t, img)
111+
img = imgs.Find("linux/arm64")
112+
require.NotNil(t, img)
113+
} else {
114+
require.Error(t, err, string(out))
115+
require.Contains(t, string(out), "Multi-platform build is not supported")
116+
}
117+
}
118+
119+
func testComposeBuildCheck(t *testing.T, sb integration.Sandbox) {
120+
dockerfile := []byte(`
121+
frOM busybox as base
122+
cOpy Dockerfile .
123+
from scratch
124+
COPy --from=base \
125+
/Dockerfile \
126+
/
127+
`)
128+
129+
composefile := []byte(`
130+
services:
131+
bar:
132+
build:
133+
context: .
134+
`)
135+
136+
dir := tmpdir(
137+
t,
138+
fstest.CreateFile("compose.yml", composefile, 0600),
139+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
140+
)
141+
142+
cmd := composeCmd(sb, withDir(dir), withArgs("build", "--check"))
143+
out, err := cmd.CombinedOutput()
144+
require.Error(t, err, string(out))
145+
require.Contains(t, string(out), "Check complete, 3 warnings have been found!")
146+
}
147+
148+
func composeTestProject(imageName string, t *testing.T) string {
149+
dockerfile := []byte(`
150+
FROM busybox:latest AS base
151+
COPY foo /etc/foo
152+
RUN cp /etc/foo /etc/bar
153+
154+
FROM scratch
155+
COPY --from=base /etc/bar /bar
156+
`)
157+
158+
composefile := fmt.Appendf([]byte{}, `
159+
services:
160+
bar:
161+
build:
162+
context: .
163+
image: %s
164+
`, imageName)
165+
166+
return tmpdir(
167+
t,
168+
fstest.CreateFile("compose.yml", composefile, 0600),
169+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
170+
fstest.CreateFile("foo", []byte("foo"), 0600),
171+
)
172+
}

tests/integration.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ func buildxCmd(sb integration.Sandbox, opts ...cmdOpt) *exec.Cmd {
7575
return cmd
7676
}
7777

78+
func composeCmd(sb integration.Sandbox, opts ...cmdOpt) *exec.Cmd {
79+
cmd := exec.Command("compose")
80+
cmd.Env = os.Environ()
81+
for _, opt := range opts {
82+
opt(cmd)
83+
}
84+
85+
if builder := sb.Address(); builder != "" {
86+
cmd.Env = append(cmd.Env,
87+
"BUILDX_CONFIG="+buildxConfig(sb),
88+
"BUILDX_BUILDER="+builder,
89+
)
90+
}
91+
if context := sb.DockerAddress(); context != "" {
92+
cmd.Env = append(cmd.Env, "DOCKER_CONTEXT="+context)
93+
}
94+
if v := os.Getenv("GO_TEST_COVERPROFILE"); v != "" {
95+
coverDir := filepath.Join(filepath.Dir(v), "helpers")
96+
cmd.Env = append(cmd.Env, "GOCOVERDIR="+coverDir)
97+
}
98+
cmd.Env = append(cmd.Env, "COMPOSE_BAKE=true")
99+
return cmd
100+
}
101+
78102
func dockerCmd(sb integration.Sandbox, opts ...cmdOpt) *exec.Cmd {
79103
cmd := exec.Command("docker")
80104
cmd.Env = os.Environ()

tests/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestIntegration(t *testing.T) {
3232
tests = append(tests, createTests...)
3333
tests = append(tests, rmTests...)
3434
tests = append(tests, dialstdioTests...)
35+
tests = append(tests, composeTests...)
3536
testIntegration(t, tests...)
3637
}
3738

0 commit comments

Comments
 (0)