Skip to content

Commit

Permalink
test: multi exporters
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Mar 5, 2024
1 parent a03263a commit 9a2536d
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 1 deletion.
160 changes: 160 additions & 0 deletions tests/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package tests

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/containerd/continuity/fs/fstest"
"github.com/docker/buildx/util/gitutil"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/util/contentutil"
"github.com/moby/buildkit/util/testutil"
"github.com/moby/buildkit/util/testutil/integration"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -36,6 +42,8 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
testBakeShmSize,
testBakeUlimits,
testBakeRefs,
testBakeMultiExporters,
testBakeLoadPush,
}

func testBakeLocal(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -631,3 +639,155 @@ target "default" {

require.NotEmpty(t, md.Default.BuildRef)
}

func testBakeMultiExporters(t *testing.T, sb integration.Sandbox) {
if sb.Name() != "docker" {
t.Skip("skipping test for non-docker workers")
}

registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) {
t.Skip(err.Error())
}
require.NoError(t, err)

targetReg := registry + "/buildx/registry:latest"
targetStore := "buildx:local-" + identity.NewID()

var builderName string
t.Cleanup(func() {
if builderName == "" {
return
}

cmd := dockerCmd(sb, withArgs("image", "rm", targetStore))
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())

out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
})

// TODO: use stable buildkit image when v0.13.0 released
out, err := createCmd(sb, withArgs(
"--driver", "docker-container",
"--buildkitd-flags=--allow-insecure-entitlement=network.host",
"--driver-opt", "network=host",
"--driver-opt", "image=moby/buildkit:v0.13.0-rc3",
))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)

dockerfile := []byte(`
FROM scratch
COPY foo /foo
`)
bakefile := []byte(`
target "default" {
}
`)
dir := tmpdir(
t,
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
fstest.CreateFile("Dockerfile", dockerfile, 0600),
fstest.CreateFile("foo", []byte("foo"), 0600),
)

outputs := []string{
"--set", fmt.Sprintf("*.output=type=image,name=%s,push=true", targetReg),
"--set", fmt.Sprintf("*.output=type=docker,name=%s", targetStore),
"--set", fmt.Sprintf("*.output=type=oci,dest=%s/result", dir),
}
cmd := buildxCmd(sb, withDir(dir), withArgs("bake"), withArgs(outputs...))
cmd.Env = append(cmd.Env, "BUILDX_BUILDER="+builderName)
outb, err := cmd.CombinedOutput()
require.NoError(t, err, string(outb))

// test registry
desc, provider, err := contentutil.ProviderFromRef(targetReg)
require.NoError(t, err)
_, err = testutil.ReadImages(sb.Context(), provider, desc)
require.NoError(t, err)

// test docker store
cmd = dockerCmd(sb, withArgs("image", "inspect", targetStore))
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())

// test oci
_, err = os.ReadFile(fmt.Sprintf("%s/result", dir))
require.NoError(t, err)

// TODO: test metadata file when supported by multi exporters https://github.com/docker/buildx/issues/2181
}

func testBakeLoadPush(t *testing.T, sb integration.Sandbox) {
if sb.Name() != "docker" {
t.Skip("skipping test for non-docker workers")
}

registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) {
t.Skip(err.Error())
}
require.NoError(t, err)

target := registry + "/buildx/registry:" + identity.NewID()

var builderName string
t.Cleanup(func() {
if builderName == "" {
return
}

cmd := dockerCmd(sb, withArgs("image", "rm", target))
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())

out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
})

// TODO: use stable buildkit image when v0.13.0 released
out, err := createCmd(sb, withArgs(
"--driver", "docker-container",
"--buildkitd-flags=--allow-insecure-entitlement=network.host",
"--driver-opt", "network=host",
"--driver-opt", "image=moby/buildkit:v0.13.0-rc3",
))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)

dockerfile := []byte(`
FROM scratch
COPY foo /foo
`)
bakefile := []byte(`
target "default" {
}
`)
dir := tmpdir(
t,
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
fstest.CreateFile("Dockerfile", dockerfile, 0600),
fstest.CreateFile("foo", []byte("foo"), 0600),
)

cmd := buildxCmd(sb, withDir(dir), withArgs("bake", "--push", "--load", fmt.Sprintf("--set=*.tags=%s", target)))
cmd.Env = append(cmd.Env, "BUILDX_BUILDER="+builderName)
outb, err := cmd.CombinedOutput()
require.NoError(t, err, string(outb))

// test registry
desc, provider, err := contentutil.ProviderFromRef(target)
require.NoError(t, err)
_, err = testutil.ReadImages(sb.Context(), provider, desc)
require.NoError(t, err)

// test docker store
cmd = dockerCmd(sb, withArgs("image", "inspect", target))
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())

// TODO: test metadata file when supported by multi exporters https://github.com/docker/buildx/issues/2181
}
135 changes: 134 additions & 1 deletion tests/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/containerd/containerd/platforms"
"github.com/containerd/continuity/fs/fstest"
"github.com/creack/pty"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/contentutil"
"github.com/moby/buildkit/util/testutil"
Expand Down Expand Up @@ -54,6 +55,8 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){
testBuildShmSize,
testBuildUlimit,
testBuildRef,
testBuildMultiExporters,
testBuildLoadPush,
}

func testBuild(t *testing.T, sb integration.Sandbox) {
Expand Down Expand Up @@ -437,7 +440,7 @@ func testBuildNetworkModeBridge(t *testing.T, sb integration.Sandbox) {
})

// TODO: use stable buildkit image when v0.13.0 released
out, err := createCmd(sb, withArgs("--driver", "docker-container", "--buildkitd-flags=--oci-worker-net=bridge --allow-insecure-entitlement=network.host", "--driver-opt", "image=moby/buildkit:master"))
out, err := createCmd(sb, withArgs("--driver", "docker-container", "--buildkitd-flags=--oci-worker-net=bridge --allow-insecure-entitlement=network.host", "--driver-opt", "image=moby/buildkit:v0.13.0-rc3"))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)

Expand Down Expand Up @@ -542,6 +545,136 @@ func testBuildRef(t *testing.T, sb integration.Sandbox) {
require.NotEmpty(t, md.BuildRef)
}

func testBuildMultiExporters(t *testing.T, sb integration.Sandbox) {
if sb.Name() != "docker" {
t.Skip("skipping test for non-docker workers")
}

registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) {
t.Skip(err.Error())
}
require.NoError(t, err)

targetReg := registry + "/buildx/registry:latest"
targetStore := "buildx:local-" + identity.NewID()

var builderName string
t.Cleanup(func() {
if builderName == "" {
return
}

cmd := dockerCmd(sb, withArgs("image", "rm", targetStore))
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())

out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
})

// TODO: use stable buildkit image when v0.13.0 released
out, err := createCmd(sb, withArgs(
"--driver", "docker-container",
"--buildkitd-flags=--allow-insecure-entitlement=network.host",
"--driver-opt", "network=host",
"--driver-opt", "image=moby/buildkit:v0.13.0-rc3",
))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)

dir := createTestProject(t)

outputs := []string{
"--output", fmt.Sprintf("type=image,name=%s,push=true", targetReg),
"--output", fmt.Sprintf("type=docker,name=%s", targetStore),
"--output", fmt.Sprintf("type=oci,dest=%s/result", dir),
}
cmd := buildxCmd(sb, withArgs("build"), withArgs(outputs...), withArgs(dir))
cmd.Env = append(cmd.Env, "BUILDX_BUILDER="+builderName)
outb, err := cmd.CombinedOutput()
require.NoError(t, err, string(outb))

// test registry
desc, provider, err := contentutil.ProviderFromRef(targetReg)
require.NoError(t, err)
_, err = testutil.ReadImages(sb.Context(), provider, desc)
require.NoError(t, err)

// test docker store
cmd = dockerCmd(sb, withArgs("image", "inspect", targetStore))
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())

// test oci
_, err = os.ReadFile(fmt.Sprintf("%s/result", dir))
require.NoError(t, err)

// TODO: test metadata file when supported by multi exporters https://github.com/docker/buildx/issues/2181
}

func testBuildLoadPush(t *testing.T, sb integration.Sandbox) {
if sb.Name() != "docker" {
t.Skip("skipping test for non-docker workers")
}

registry, err := sb.NewRegistry()
if errors.Is(err, integration.ErrRequirements) {
t.Skip(err.Error())
}
require.NoError(t, err)

target := registry + "/buildx/registry:" + identity.NewID()

var builderName string
t.Cleanup(func() {
if builderName == "" {
return
}

cmd := dockerCmd(sb, withArgs("image", "rm", target))
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())

out, err := rmCmd(sb, withArgs(builderName))
require.NoError(t, err, out)
})

// TODO: use stable buildkit image when v0.13.0 released
out, err := createCmd(sb, withArgs(
"--driver", "docker-container",
"--buildkitd-flags=--allow-insecure-entitlement=network.host",
"--driver-opt", "network=host",
"--driver-opt", "image=moby/buildkit:v0.13.0-rc3",
))
require.NoError(t, err, out)
builderName = strings.TrimSpace(out)

dir := createTestProject(t)

cmd := buildxCmd(sb, withArgs(
"build", "--push", "--load",
fmt.Sprintf("-t=%s", target),
dir,
))
cmd.Env = append(cmd.Env, "BUILDX_BUILDER="+builderName)
outb, err := cmd.CombinedOutput()
require.NoError(t, err, string(outb))

// test registry
desc, provider, err := contentutil.ProviderFromRef(target)
require.NoError(t, err)
_, err = testutil.ReadImages(sb.Context(), provider, desc)
require.NoError(t, err)

// test docker store
cmd = dockerCmd(sb, withArgs("image", "inspect", target))
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())

// TODO: test metadata file when supported by multi exporters https://github.com/docker/buildx/issues/2181
}

func createTestProject(t *testing.T) string {
dockerfile := []byte(`
FROM busybox:latest AS base
Expand Down

0 comments on commit 9a2536d

Please sign in to comment.