From 94378a87b423ab5f93eb9a81c2716fd1b6246c40 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 19 Oct 2021 20:16:01 +0200 Subject: [PATCH] use `opts.MemBytes` for tmpfs size run mount instruction Signed-off-by: CrazyMax --- frontend/dockerfile/dockerfile_mount_test.go | 44 +++++++++++++++++++ .../instructions/commands_runmount.go | 7 +-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/frontend/dockerfile/dockerfile_mount_test.go b/frontend/dockerfile/dockerfile_mount_test.go index a2255f49..93666f9a 100644 --- a/frontend/dockerfile/dockerfile_mount_test.go +++ b/frontend/dockerfile/dockerfile_mount_test.go @@ -25,6 +25,7 @@ var mountTests = []integration.Test{ testMountMetaArg, testMountFromError, testMountInvalid, + testMountTmpfsSize, } func init() { @@ -452,3 +453,46 @@ RUN --mount=from=$ttt,type=cache,target=/tmp ls require.Error(t, err) require.Contains(t, err.Error(), "'from' doesn't support variable expansion, define alias stage instead") } + +func testMountTmpfsSize(t *testing.T, sb integration.Sandbox) { + f := getFrontend(t, sb) + + dockerfile := []byte(` +FROM busybox AS base +RUN --mount=type=tmpfs,target=/dev/shm,size=128m mount | grep /dev/shm > /tmpfssize +FROM scratch +COPY --from=base /tmpfssize / +`) + + dir, err := tmpdir( + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + require.NoError(t, err) + defer os.RemoveAll(dir) + + c, err := client.New(sb.Context(), sb.Address()) + require.NoError(t, err) + defer c.Close() + + destDir, err := ioutil.TempDir("", "buildkit") + require.NoError(t, err) + defer os.RemoveAll(destDir) + + _, err = f.Solve(sb.Context(), c, client.SolveOpt{ + Exports: []client.ExportEntry{ + { + Type: client.ExporterLocal, + OutputDir: destDir, + }, + }, + LocalDirs: map[string]string{ + builder.DefaultLocalNameDockerfile: dir, + builder.DefaultLocalNameContext: dir, + }, + }, nil) + require.NoError(t, err) + + dt, err := ioutil.ReadFile(filepath.Join(destDir, "tmpfssize")) + require.NoError(t, err) + require.Contains(t, string(dt), `size=131072k`) +} diff --git a/frontend/dockerfile/instructions/commands_runmount.go b/frontend/dockerfile/instructions/commands_runmount.go index 598712ea..517ded7d 100644 --- a/frontend/dockerfile/instructions/commands_runmount.go +++ b/frontend/dockerfile/instructions/commands_runmount.go @@ -6,6 +6,7 @@ import ( "strconv" "strings" + dockeropts "github.com/docker/docker/opts" "github.com/moby/buildkit/util/suggest" "github.com/pkg/errors" ) @@ -230,11 +231,11 @@ func parseMount(value string, expander SingleWordExpander) (*Mount, error) { } case "size": if m.Type == "tmpfs" { - v, err := strconv.ParseInt(value, 10, 64) - if err != nil { + tmpfsSize := new(dockeropts.MemBytes) + if err := tmpfsSize.Set(value); err != nil { return nil, errors.Errorf("invalid value for %s: %s", key, value) } - m.SizeLimit = v + m.SizeLimit = tmpfsSize.Value() } else { return nil, errors.Errorf("unexpected key '%s' for mount type '%s'", key, m.Type) }