From 44c5e5d9b1ece0159e1d85258a93c13d281cf51f Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Tue, 5 Oct 2021 09:16:44 +0200 Subject: [PATCH] Fix index out of range on compose.buildContainerMountOptions Signed-off-by: Ulysses Souza --- pkg/compose/create.go | 25 ++++++++-------- pkg/compose/create_test.go | 58 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 67 insertions(+), 16 deletions(-) diff --git a/pkg/compose/create.go b/pkg/compose/create.go index 7fecf52738b..08b6b76aac2 100644 --- a/pkg/compose/create.go +++ b/pkg/compose/create.go @@ -750,24 +750,21 @@ func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby } } } - for i, v := range s.Volumes { - if v.Target != m.Destination { + volumes := []types.ServiceVolumeConfig{} + for _, v := range s.Volumes { + if v.Target != m.Destination || v.Source != "" { + volumes = append(volumes, v) continue } - if v.Source == "" { - // inherit previous container's anonymous volume - mounts[m.Destination] = mount.Mount{ - Type: m.Type, - Source: src, - Target: m.Destination, - ReadOnly: !m.RW, - } - // Avoid mount to be later re-defined - l := len(s.Volumes) - 1 - s.Volumes[i] = s.Volumes[l] - s.Volumes = s.Volumes[:l] + // inherit previous container's anonymous volume + mounts[m.Destination] = mount.Mount{ + Type: m.Type, + Source: src, + Target: m.Destination, + ReadOnly: !m.RW, } } + s.Volumes = volumes } } diff --git a/pkg/compose/create_test.go b/pkg/compose/create_test.go index ecca0ab43c7..22fb1efe26f 100644 --- a/pkg/compose/create_test.go +++ b/pkg/compose/create_test.go @@ -21,10 +21,10 @@ import ( "path/filepath" "testing" - "github.com/docker/compose/v2/pkg/api" - "github.com/compose-spec/compose-go/types" composetypes "github.com/compose-spec/compose-go/types" + "github.com/docker/compose/v2/pkg/api" + moby "github.com/docker/docker/api/types" mountTypes "github.com/docker/docker/api/types/mount" "gotest.tools/v3/assert" ) @@ -81,3 +81,57 @@ func TestPrepareNetworkLabels(t *testing.T) { "com.docker.compose.version": api.ComposeVersion, })) } + +func TestBuildContainerMountOptions(t *testing.T) { + project := composetypes.Project{ + Name: "myProject", + Services: []composetypes.ServiceConfig{ + { + Name: "myService", + Volumes: []composetypes.ServiceVolumeConfig{ + { + Type: composetypes.VolumeTypeVolume, + Target: "/var/myvolume1", + }, + { + Type: composetypes.VolumeTypeVolume, + Target: "/var/myvolume2", + }, + }, + }, + }, + Volumes: composetypes.Volumes(map[string]composetypes.VolumeConfig{ + "myVolume1": { + Name: "myProject_myVolume1", + }, + "myVolume2": { + Name: "myProject_myVolume2", + }, + }), + } + + inherit := &moby.Container{ + Mounts: []moby.MountPoint{ + { + Type: composetypes.VolumeTypeVolume, + Destination: "/var/myvolume1", + }, + { + Type: composetypes.VolumeTypeVolume, + Destination: "/var/myvolume2", + }, + }, + } + + mounts, err := buildContainerMountOptions(project, project.Services[0], moby.ImageInspect{}, inherit) + assert.NilError(t, err) + assert.Assert(t, len(mounts) == 2) + assert.Assert(t, mounts[0].Target == "/var/myvolume1") + assert.Assert(t, mounts[1].Target == "/var/myvolume2") + + mounts, err = buildContainerMountOptions(project, project.Services[0], moby.ImageInspect{}, inherit) + assert.NilError(t, err) + assert.Assert(t, len(mounts) == 2) + assert.Assert(t, mounts[0].Target == "/var/myvolume1") + assert.Assert(t, mounts[1].Target == "/var/myvolume2") +}