Ver código fonte

add a test with multiple service builds using platforms in the same compose file

Signed-off-by: Guillaume Lours <[email protected]>
Guillaume Lours 3 anos atrás
pai
commit
8ed2d8ad07

+ 1 - 19
pkg/compose/build_buildkit.go

@@ -56,10 +56,7 @@ func (s *composeService) doBuildBuildkit(ctx context.Context, opts map[string]bu
 	defer cancel()
 	defer cancel()
 	w := xprogress.NewPrinter(progressCtx, s.stdout(), os.Stdout, mode)
 	w := xprogress.NewPrinter(progressCtx, s.stdout(), os.Stdout, mode)
 
 
-	// Get the DockerAPI if a "docker" export is defined (ie: up and run command), otherwise get nil and let use the default buildx builder
-	API := getDockerAPI(s.dockerCli, opts)
-
-	response, err := build.Build(ctx, dis, opts, API, filepath.Dir(s.configFile().Filename), w)
+	response, err := build.Build(ctx, dis, opts, &internalAPI{dockerCli: s.dockerCli}, filepath.Dir(s.configFile().Filename), w)
 	errW := w.Wait()
 	errW := w.Wait()
 	if err == nil {
 	if err == nil {
 		err = errW
 		err = errW
@@ -265,18 +262,3 @@ func (a *internalAPI) DockerAPI(name string) (dockerclient.APIClient, error) {
 	}
 	}
 	return clientForEndpoint(a.dockerCli, name)
 	return clientForEndpoint(a.dockerCli, name)
 }
 }
-
-func dockerAPI(dockerCli command.Cli) *internalAPI {
-	return &internalAPI{dockerCli: dockerCli}
-}
-
-func getDockerAPI(cli command.Cli, opts map[string]build.Options) *internalAPI {
-	for _, opt := range opts {
-		for _, export := range opt.Exports {
-			if export.Type == "docker" {
-				return dockerAPI(cli)
-			}
-		}
-	}
-	return nil
-}

+ 16 - 0
pkg/e2e/build_test.go

@@ -281,6 +281,22 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) {
 
 
 	})
 	})
 
 
+	t.Run("multi-arch multi service builds ok", func(t *testing.T) {
+		res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms",
+			"-f", "fixtures/build-test/platforms/compose-multiple-platform-builds.yaml", "build")
+		assert.NilError(t, res.Error, res.Stderr())
+		res = c.RunDockerCmd(t, "manifest", "inspect", "--insecure", "localhost:5001/build-test-platform-a:test")
+		res.Assert(t, icmd.Expected{Out: `"architecture": "amd64",`})
+		res.Assert(t, icmd.Expected{Out: `"architecture": "arm64",`})
+		res = c.RunDockerCmd(t, "manifest", "inspect", "--insecure", "localhost:5001/build-test-platform-b:test")
+		res.Assert(t, icmd.Expected{Out: `"architecture": "amd64",`})
+		res.Assert(t, icmd.Expected{Out: `"architecture": "arm64",`})
+		res = c.RunDockerCmd(t, "manifest", "inspect", "--insecure", "localhost:5001/build-test-platform-c:test")
+		res.Assert(t, icmd.Expected{Out: `"architecture": "amd64",`})
+		res.Assert(t, icmd.Expected{Out: `"architecture": "arm64",`})
+
+	})
+
 	t.Run("multi-arch up --build", func(t *testing.T) {
 	t.Run("multi-arch up --build", func(t *testing.T) {
 		res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms", "up", "--build")
 		res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms", "up", "--build")
 		assert.NilError(t, res.Error, res.Stderr())
 		assert.NilError(t, res.Error, res.Stderr())

+ 23 - 0
pkg/e2e/fixtures/build-test/platforms/compose-multiple-platform-builds.yaml

@@ -0,0 +1,23 @@
+services:
+  serviceA:
+    image: localhost:5001/build-test-platform-a:test
+    build:
+      context: ./contextServiceA
+      platforms:
+        - linux/amd64
+        - linux/arm64
+  serviceB:
+    image: localhost:5001/build-test-platform-b:test
+    build:
+      context: ./contextServiceB
+      platforms:
+        - linux/amd64
+        - linux/arm64
+  serviceC:
+    image: localhost:5001/build-test-platform-c:test
+    build:
+      context: ./contextServiceC
+      platforms:
+        - linux/amd64
+        - linux/arm64
+

+ 22 - 0
pkg/e2e/fixtures/build-test/platforms/contextServiceA/Dockerfile

@@ -0,0 +1,22 @@
+#   Copyright 2020 Docker Compose CLI authors
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+
+#       http://www.apache.org/licenses/LICENSE-2.0
+
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+FROM --platform=$BUILDPLATFORM golang:alpine AS build
+
+ARG TARGETPLATFORM
+ARG BUILDPLATFORM
+RUN echo "I'm Service A and I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log
+
+FROM alpine
+COPY --from=build /log /log

+ 22 - 0
pkg/e2e/fixtures/build-test/platforms/contextServiceB/Dockerfile

@@ -0,0 +1,22 @@
+#   Copyright 2020 Docker Compose CLI authors
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+
+#       http://www.apache.org/licenses/LICENSE-2.0
+
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+FROM --platform=$BUILDPLATFORM golang:alpine AS build
+
+ARG TARGETPLATFORM
+ARG BUILDPLATFORM
+RUN echo "I'm Service B and I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log
+
+FROM alpine
+COPY --from=build /log /log

+ 22 - 0
pkg/e2e/fixtures/build-test/platforms/contextServiceC/Dockerfile

@@ -0,0 +1,22 @@
+#   Copyright 2020 Docker Compose CLI authors
+
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+
+#       http://www.apache.org/licenses/LICENSE-2.0
+
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+
+FROM --platform=$BUILDPLATFORM golang:alpine AS build
+
+ARG TARGETPLATFORM
+ARG BUILDPLATFORM
+RUN echo "I'm Service C and I am running on $BUILDPLATFORM, building for $TARGETPLATFORM" > /log
+
+FROM alpine
+COPY --from=build /log /log