From 8fb11635778fa4d03e8f1527623eb8252dbf3206 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Fri, 5 Jan 2024 16:24:18 +0100 Subject: [PATCH] driver(container): fix conditional statement for error handling Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> (cherry picked from commit 57d737a13c9e7484753d796407582a9a3997d7d2) --- driver/docker-container/factory.go | 4 +-- tests/create.go | 39 ++++++++++++++++++++++++++++++ tests/integration_test.go | 1 + tests/rm.go | 12 +++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 tests/create.go create mode 100644 tests/rm.go diff --git a/driver/docker-container/factory.go b/driver/docker-container/factory.go index 8fa37731529..00564be7ab4 100644 --- a/driver/docker-container/factory.go +++ b/driver/docker-container/factory.go @@ -51,11 +51,11 @@ func (f *factory) New(ctx context.Context, cfg driver.InitConfig) (driver.Driver case k == "image": d.image = v case k == "memory": - if err := d.memory.Set(v); err == nil { + if err := d.memory.Set(v); err != nil { return nil, err } case k == "memory-swap": - if err := d.memorySwap.Set(v); err == nil { + if err := d.memorySwap.Set(v); err != nil { return nil, err } case k == "cpu-period": diff --git a/tests/create.go b/tests/create.go new file mode 100644 index 00000000000..65476c24782 --- /dev/null +++ b/tests/create.go @@ -0,0 +1,39 @@ +package tests + +import ( + "strings" + "testing" + + "github.com/moby/buildkit/util/testutil/integration" + "github.com/stretchr/testify/require" +) + +func createCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) { + opts = append([]cmdOpt{withArgs("create")}, opts...) + cmd := buildxCmd(sb, opts...) + out, err := cmd.CombinedOutput() + return string(out), err +} + +var createTests = []func(t *testing.T, sb integration.Sandbox){ + testCreateMemoryLimit, +} + +func testCreateMemoryLimit(t *testing.T, sb integration.Sandbox) { + if sb.Name() != "docker-container" { + t.Skip("only testing for docker-container driver") + } + + var builderName string + t.Cleanup(func() { + if builderName == "" { + return + } + out, err := rmCmd(sb, withArgs(builderName)) + require.NoError(t, err, out) + }) + + out, err := createCmd(sb, withArgs("--driver", "docker-container", "--driver-opt", "network=host", "--driver-opt", "memory=1g")) + require.NoError(t, err, out) + builderName = strings.TrimSpace(out) +} diff --git a/tests/integration_test.go b/tests/integration_test.go index db2e5d447b6..6b48f171c31 100644 --- a/tests/integration_test.go +++ b/tests/integration_test.go @@ -27,6 +27,7 @@ func TestIntegration(t *testing.T) { tests = append(tests, lsTests...) tests = append(tests, imagetoolsTests...) tests = append(tests, versionTests...) + tests = append(tests, createTests...) testIntegration(t, tests...) } diff --git a/tests/rm.go b/tests/rm.go new file mode 100644 index 00000000000..6c412f15e25 --- /dev/null +++ b/tests/rm.go @@ -0,0 +1,12 @@ +package tests + +import ( + "github.com/moby/buildkit/util/testutil/integration" +) + +func rmCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) { + opts = append([]cmdOpt{withArgs("rm")}, opts...) + cmd := buildxCmd(sb, opts...) + out, err := cmd.CombinedOutput() + return string(out), err +}