From 05b322906dc5570617a0a64b526e5b76e0bd33ec Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Sat, 9 Mar 2024 17:02:52 +0100 Subject: [PATCH] tests: create remote with container helper Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- builder/node.go | 2 +- driver/driver.go | 2 ++ driver/kubernetes/factory.go | 6 ++--- driver/kubernetes/factory_test.go | 2 +- tests/create.go | 43 +++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/builder/node.go b/builder/node.go index 14f6ec1d2344..01caad3755d7 100644 --- a/builder/node.go +++ b/builder/node.go @@ -142,7 +142,7 @@ func (b *Builder) LoadNodes(ctx context.Context, opts ...LoadNodesOption) (_ []N } } - d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, factory, n.Endpoint, dockerapi, imageopt.Auth, kcc, n.BuildkitdFlags, n.Files, n.DriverOpts, n.Platforms, b.opts.contextPathHash, lno.dialMeta) + d, err := driver.GetDriver(ctx, driver.BuilderNamePrefix+n.Name, factory, n.Endpoint, dockerapi, imageopt.Auth, kcc, n.BuildkitdFlags, n.Files, n.DriverOpts, n.Platforms, b.opts.contextPathHash, lno.dialMeta) if err != nil { node.Err = err return nil diff --git a/driver/driver.go b/driver/driver.go index 6d3c546737c9..1ac64a2bd4c0 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -24,6 +24,8 @@ const ( Running Stopping Stopped + + BuilderNamePrefix = "buildx_buildkit_" ) func (s Status) String() string { diff --git a/driver/kubernetes/factory.go b/driver/kubernetes/factory.go index 56384dbfbf47..3312d6888e21 100644 --- a/driver/kubernetes/factory.go +++ b/driver/kubernetes/factory.go @@ -244,10 +244,10 @@ func (f *factory) AllowsInstances() bool { // eg. "buildx_buildkit_loving_mendeleev0" -> "loving-mendeleev0" func buildxNameToDeploymentName(bx string) (string, error) { // TODO: commands.util.go should not pass "buildx_buildkit_" prefix to drivers - if !strings.HasPrefix(bx, "buildx_buildkit_") { - return "", errors.Errorf("expected a string with \"buildx_buildkit_\", got %q", bx) + if !strings.HasPrefix(bx, driver.BuilderNamePrefix) { + return "", errors.Errorf("expected a string with %q, got %q", driver.BuilderNamePrefix, bx) } - s := strings.TrimPrefix(bx, "buildx_buildkit_") + s := strings.TrimPrefix(bx, driver.BuilderNamePrefix) s = strings.ReplaceAll(s, "_", "-") return s, nil } diff --git a/driver/kubernetes/factory_test.go b/driver/kubernetes/factory_test.go index 605be96e1aae..dfa405031fcc 100644 --- a/driver/kubernetes/factory_test.go +++ b/driver/kubernetes/factory_test.go @@ -29,7 +29,7 @@ func TestFactory_processDriverOpts(t *testing.T) { } cfg := driver.InitConfig{ - Name: "buildx_buildkit_test", + Name: driver.BuilderNamePrefix + "test", KubeClientConfig: &kcc, } f := factory{} diff --git a/tests/create.go b/tests/create.go index a37fa3da9029..6c7f73f96ac4 100644 --- a/tests/create.go +++ b/tests/create.go @@ -1,9 +1,13 @@ package tests import ( + "fmt" + "os" "strings" "testing" + "github.com/docker/buildx/driver" + "github.com/moby/buildkit/identity" "github.com/moby/buildkit/util/testutil/integration" "github.com/stretchr/testify/require" ) @@ -18,6 +22,7 @@ func createCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) { var createTests = []func(t *testing.T, sb integration.Sandbox){ testCreateMemoryLimit, testCreateRestartAlways, + testCreateRemoteContainer, } func testCreateMemoryLimit(t *testing.T, sb integration.Sandbox) { @@ -57,3 +62,41 @@ func testCreateRestartAlways(t *testing.T, sb integration.Sandbox) { require.NoError(t, err, out) builderName = strings.TrimSpace(out) } + +func testCreateRemoteContainer(t *testing.T, sb integration.Sandbox) { + if sb.Name() != "docker" { + t.Skip("skipping test for non-docker workers") + } + + ctnBuilderName := "ctn-builder-" + identity.NewID() + remoteBuilderName := "remote-builder-" + identity.NewID() + var hasCtnBuilder, hasRemoteBuilder bool + t.Cleanup(func() { + if hasCtnBuilder { + out, err := rmCmd(sb, withArgs(ctnBuilderName)) + require.NoError(t, err, out) + } + if hasRemoteBuilder { + out, err := rmCmd(sb, withArgs(remoteBuilderName)) + require.NoError(t, err, out) + } + }) + + out, err := createCmd(sb, withArgs("--driver", "docker-container", "--name", ctnBuilderName)) + require.NoError(t, err, out) + hasCtnBuilder = true + + out, err = inspectCmd(sb, withArgs("--bootstrap", ctnBuilderName)) + require.NoError(t, err, out) + + cmd := dockerCmd(sb, withArgs("container", "inspect", fmt.Sprintf("%s%s0", driver.BuilderNamePrefix, ctnBuilderName))) + cmd.Stderr = os.Stderr + require.NoError(t, cmd.Run()) + + out, err = createCmd(sb, withArgs("--driver", "remote", "--name", remoteBuilderName, fmt.Sprintf("docker-container://%s%s0", driver.BuilderNamePrefix, ctnBuilderName))) + require.NoError(t, err, out) + hasRemoteBuilder = true + + out, err = inspectCmd(sb, withArgs(remoteBuilderName)) + require.NoError(t, err, out) +}