Adding unit test for bind mount creation

Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
Guillaume Tardif 2020-12-04 10:55:37 +01:00
parent 814536c0bd
commit 0b2eaede8c
2 changed files with 45 additions and 19 deletions

View File

@ -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

View File

@ -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)
}