Skip to content

Commit

Permalink
CR change
Browse files Browse the repository at this point in the history
  • Loading branch information
Omer Mizrahi committed May 4, 2021
1 parent 7dd7a67 commit 71c8bf4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
33 changes: 33 additions & 0 deletions frontend/dockerfile/dockerfile_mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var mountTests = []integration.Test{
testMountArg,
testMountEnvAcrossStages,
testMountMetaArg,
testMountFromError,
}

func init() {
Expand Down Expand Up @@ -348,3 +349,35 @@ RUN --mount=type=cache,id=mycache,target=$META_PATH [ -f /tmp/meta/foo ]
}, nil)
require.NoError(t, err)
}

func testMountFromError(t *testing.T, sb integration.Sandbox) {
f := getFrontend(t, sb)

dockerfile := []byte(`
FROM busybox as test
RUN touch /tmp/test
FROM busybox
ENV ttt=test
RUN --mount=from=$ttt,type=cache,target=/tmp ls
`)

dir, err := tmpdir(
fstest.CreateFile("Dockerfile", dockerfile, 0600),
)
require.NoError(t, err)
defer os.RemoveAll(dir)

c, err := client.New(context.TODO(), sb.Address())
require.NoError(t, err)
defer c.Close()

_, err = f.Solve(context.TODO(), c, client.SolveOpt{
LocalDirs: map[string]string{
builder.DefaultLocalNameDockerfile: dir,
builder.DefaultLocalNameContext: dir,
},
}, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "'from' doesn't support variable expansion")
}
46 changes: 33 additions & 13 deletions frontend/dockerfile/instructions/commands_runmount.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ func parseMount(value string, expander SingleWordExpander) (*Mount, error) {

m := &Mount{Type: MountTypeBind}

hasVariableRegex, err := regexp.Compile(`\$.`)
if err != nil {
return nil, err
}
roAuto := true

for _, field := range fields {
Expand Down Expand Up @@ -161,15 +157,7 @@ func parseMount(value string, expander SingleWordExpander) (*Mount, error) {

value := parts[1]
// check for potential variable
if hasVariableRegex.MatchString(value) {
if key == "from" {
return nil, errors.Errorf("'%s' doesn't support variable expansion", key)
}

if expander == nil {
// skip variable expansion for now
continue
}
if expander != nil {
processed, err := expander(value)
if err != nil {
return nil, err
Expand All @@ -179,10 +167,18 @@ func parseMount(value string, expander SingleWordExpander) (*Mount, error) {
switch key {
case "type":
if !isValidMountType(strings.ToLower(value)) {
if expander == nil {
continue
}
return nil, errors.Errorf("unsupported mount type %q", value)
}
m.Type = strings.ToLower(value)
case "from":
if matched, err := regexp.MatchString(`\$.`, value); err != nil { //nolint
return nil, err
} else if matched {
return nil, errors.Errorf("'%s' doesn't support variable expansion", key)
}
m.From = value
case "source", "src":
m.Source = value
Expand All @@ -191,12 +187,18 @@ func parseMount(value string, expander SingleWordExpander) (*Mount, error) {
case "readonly", "ro":
m.ReadOnly, err = strconv.ParseBool(value)
if err != nil {
if expander == nil {
continue
}
return nil, errors.Errorf("invalid value for %s: %s", key, value)
}
roAuto = false
case "readwrite", "rw":
rw, err := strconv.ParseBool(value)
if err != nil {
if expander == nil {
continue
}
return nil, errors.Errorf("invalid value for %s: %s", key, value)
}
m.ReadOnly = !rw
Expand All @@ -205,34 +207,52 @@ func parseMount(value string, expander SingleWordExpander) (*Mount, error) {
if m.Type == "secret" || m.Type == "ssh" {
v, err := strconv.ParseBool(value)
if err != nil {
if expander == nil {
continue
}
return nil, errors.Errorf("invalid value for %s: %s", key, value)
}
m.Required = v
} else {
if expander == nil {
continue
}
return nil, errors.Errorf("unexpected key '%s' for mount type '%s'", key, m.Type)
}
case "id":
m.CacheID = value
case "sharing":
if _, ok := allowedSharingTypes[strings.ToLower(value)]; !ok {
if expander == nil {
continue
}
return nil, errors.Errorf("unsupported sharing value %q", value)
}
m.CacheSharing = strings.ToLower(value)
case "mode":
mode, err := strconv.ParseUint(value, 8, 32)
if err != nil {
if expander == nil {
continue
}
return nil, errors.Errorf("invalid value %s for mode", value)
}
m.Mode = &mode
case "uid":
uid, err := strconv.ParseUint(value, 10, 32)
if err != nil {
if expander == nil {
continue
}
return nil, errors.Errorf("invalid value %s for uid", value)
}
m.UID = &uid
case "gid":
gid, err := strconv.ParseUint(value, 10, 32)
if err != nil {
if expander == nil {
continue
}
return nil, errors.Errorf("invalid value %s for gid", value)
}
m.GID = &gid
Expand Down

0 comments on commit 71c8bf4

Please sign in to comment.