From 0b2eaede8cd50131689a05309e178f3b8d60cd58 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Fri, 4 Dec 2020 10:55:37 +0100 Subject: [PATCH] Adding unit test for bind mount creation Signed-off-by: Guillaume Tardif --- local/compose.go | 46 +++++++++++++++++++++++++------------------ local/compose_test.go | 18 +++++++++++++++++ 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/local/compose.go b/local/compose.go index e73c32ad7..2fb7cc5e2 100644 --- a/local/compose.go +++ b/local/compose.go @@ -873,30 +873,38 @@ func buildContainerMountOptions(p *types.Project, s types.ServiceConfig, inherit if contains(inherited, v.Target) { continue } - source := v.Source - if v.Type == "bind" && !filepath.IsAbs(source) { - // volume source has already been prefixed with workdir if required, by compose-go project loader - var err error - source, err = filepath.Abs(source) - if err != nil { - return nil, err - } + mount, err := buildMount(v) + if err != nil { + return nil, err } - - mounts = append(mounts, mount.Mount{ - Type: mount.Type(v.Type), - Source: source, - Target: v.Target, - ReadOnly: v.ReadOnly, - Consistency: mount.Consistency(v.Consistency), - BindOptions: buildBindOption(v.Bind), - VolumeOptions: buildVolumeOptions(v.Volume), - TmpfsOptions: buildTmpfsOptions(v.Tmpfs), - }) + mounts = append(mounts, mount) } return mounts, nil } +func buildMount(volume types.ServiceVolumeConfig) (mount.Mount, error) { + source := volume.Source + if volume.Type == "bind" && !filepath.IsAbs(source) { + // volume source has already been prefixed with workdir if required, by compose-go project loader + var err error + source, err = filepath.Abs(source) + if err != nil { + return mount.Mount{}, err + } + } + + return mount.Mount{ + Type: mount.Type(volume.Type), + Source: source, + Target: volume.Target, + ReadOnly: volume.ReadOnly, + Consistency: mount.Consistency(volume.Consistency), + BindOptions: buildBindOption(volume.Bind), + VolumeOptions: buildVolumeOptions(volume.Volume), + TmpfsOptions: buildTmpfsOptions(volume.Tmpfs), + }, nil +} + func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions { if bind == nil { return nil diff --git a/local/compose_test.go b/local/compose_test.go index 74de0745b..fa406709c 100644 --- a/local/compose_test.go +++ b/local/compose_test.go @@ -19,9 +19,13 @@ package local import ( + "os" + "path/filepath" "testing" + composetypes "github.com/compose-spec/compose-go/types" "github.com/docker/docker/api/types" + mountTypes "github.com/docker/docker/api/types/mount" "gotest.tools/v3/assert" "github.com/docker/compose-cli/api/compose" @@ -107,3 +111,17 @@ func TestStacksMixedStatus(t *testing.T) { assert.Equal(t, combinedStatus([]string{"running", "running", "running"}), "running(3)") assert.Equal(t, combinedStatus([]string{"running", "exited", "running"}), "exited(1), running(2)") } + +func TestBuildBindMount(t *testing.T) { + volume := composetypes.ServiceVolumeConfig{ + Type: composetypes.VolumeTypeBind, + Source: "e2e/volume-test", + Target: "/data", + } + mount, err := buildMount(volume) + assert.NilError(t, err) + assert.Assert(t, filepath.IsAbs(mount.Source)) + _, err = os.Stat(mount.Source) + assert.NilError(t, err) + assert.Equal(t, mount.Type, mountTypes.TypeBind) +}