mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
Adding unit test for bind mount creation
Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
parent
814536c0bd
commit
0b2eaede8c
@ -873,30 +873,38 @@ func buildContainerMountOptions(p *types.Project, s types.ServiceConfig, inherit
|
|||||||
if contains(inherited, v.Target) {
|
if contains(inherited, v.Target) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
source := v.Source
|
mount, err := buildMount(v)
|
||||||
if v.Type == "bind" && !filepath.IsAbs(source) {
|
if err != nil {
|
||||||
// volume source has already been prefixed with workdir if required, by compose-go project loader
|
return nil, err
|
||||||
var err error
|
|
||||||
source, err = filepath.Abs(source)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
mounts = append(mounts, mount)
|
||||||
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),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return mounts, nil
|
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 {
|
func buildBindOption(bind *types.ServiceVolumeBind) *mount.BindOptions {
|
||||||
if bind == nil {
|
if bind == nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -19,9 +19,13 @@
|
|||||||
package local
|
package local
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
composetypes "github.com/compose-spec/compose-go/types"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
mountTypes "github.com/docker/docker/api/types/mount"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"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", "running", "running"}), "running(3)")
|
||||||
assert.Equal(t, combinedStatus([]string{"running", "exited", "running"}), "exited(1), running(2)")
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user