Skip to content

Commit ca961cc

Browse files
ndeloofglours
authored andcommitted
detect conflict with multiple mounts
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 409f451 commit ca961cc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

loader/validate.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,24 @@ func checkConsistency(project *types.Project) error { //nolint:gocyclo
172172
}
173173
}
174174
}
175+
176+
mounts := map[string]string{}
177+
for i, tmpfs := range s.Tmpfs {
178+
loc := fmt.Sprintf("services.%s.tmpfs[%d]", s.Name, i)
179+
path, _, _ := strings.Cut(tmpfs, ":")
180+
if p, ok := mounts[path]; ok {
181+
return fmt.Errorf("%s: target %s already mounted as %s", loc, path, p)
182+
}
183+
mounts[path] = loc
184+
}
185+
for i, volume := range s.Volumes {
186+
loc := fmt.Sprintf("services.%s.volumes[%d]", s.Name, i)
187+
if p, ok := mounts[volume.Target]; ok {
188+
return fmt.Errorf("%s: target %s already mounted as %s", loc, volume.Target, p)
189+
}
190+
mounts[volume.Target] = loc
191+
}
192+
175193
}
176194

177195
for name, secret := range project.Secrets {

loader/validate_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,32 @@ func TestValidateWatch(t *testing.T) {
407407
assert.ErrorContains(t, err, "depends on undefined service")
408408
})
409409
}
410+
411+
func TestValidateMountConflict(t *testing.T) {
412+
project := &types.Project{
413+
Services: types.Services{
414+
"myservice": {
415+
Name: "myservice",
416+
Image: "scratch",
417+
Tmpfs: []string{
418+
"/foo",
419+
"/conflict:size=64m",
420+
},
421+
Volumes: []types.ServiceVolumeConfig{
422+
{
423+
Type: "bind",
424+
Target: "/bar",
425+
Source: ".",
426+
},
427+
{
428+
Type: "bind",
429+
Target: "/conflict",
430+
Source: ".",
431+
},
432+
},
433+
},
434+
},
435+
}
436+
err := checkConsistency(project)
437+
assert.Error(t, err, "services.myservice.volumes[1]: target /conflict already mounted as services.myservice.tmpfs[1]")
438+
}

0 commit comments

Comments
 (0)