Fix container attaching to wrong volume name for compose non-external volumes. Took the opportunity to validate R/O volumes, but not related to this fix

Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
Guillaume Tardif 2020-12-08 17:43:10 +01:00
parent df9c96430c
commit e4ed2b02d7
4 changed files with 44 additions and 7 deletions

View File

@ -110,13 +110,34 @@ func TestStacksMixedStatus(t *testing.T) {
} }
func TestBuildBindMount(t *testing.T) { func TestBuildBindMount(t *testing.T) {
project := composetypes.Project{}
volume := composetypes.ServiceVolumeConfig{ volume := composetypes.ServiceVolumeConfig{
Type: composetypes.VolumeTypeBind, Type: composetypes.VolumeTypeBind,
Source: "e2e/volume-test", Source: "e2e/volume-test",
Target: "/data", Target: "/data",
} }
mount, err := buildMount(volume) mount, err := buildMount(project, volume)
assert.NilError(t, err) assert.NilError(t, err)
assert.Assert(t, filepath.IsAbs(mount.Source)) assert.Assert(t, filepath.IsAbs(mount.Source))
assert.Equal(t, mount.Type, mountTypes.TypeBind) assert.Equal(t, mount.Type, mountTypes.TypeBind)
} }
func TestBuildVolumeMount(t *testing.T) {
project := composetypes.Project{
Name: "myProject",
Volumes: composetypes.Volumes(map[string]composetypes.VolumeConfig{
"myVolume": {
Name: "myProject_myVolume",
},
}),
}
volume := composetypes.ServiceVolumeConfig{
Type: composetypes.VolumeTypeVolume,
Source: "myVolume",
Target: "/data",
}
mount, err := buildMount(project, volume)
assert.NilError(t, err)
assert.Equal(t, mount.Source, "myProject_myVolume")
assert.Equal(t, mount.Type, mountTypes.TypeVolume)
}

View File

@ -144,7 +144,7 @@ func getContainerCreateOptions(p *types.Project, s types.ServiceConfig, number i
StopTimeout: convert.ToSeconds(s.StopGracePeriod), StopTimeout: convert.ToSeconds(s.StopGracePeriod),
} }
mountOptions, err := buildContainerMountOptions(s, inherit) mountOptions, err := buildContainerMountOptions(*p, s, inherit)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }
@ -191,7 +191,7 @@ func buildContainerBindingOptions(s types.ServiceConfig) nat.PortMap {
return bindings return bindings
} }
func buildContainerMountOptions(s types.ServiceConfig, inherit *moby.Container) ([]mount.Mount, error) { func buildContainerMountOptions(p types.Project, s types.ServiceConfig, inherit *moby.Container) ([]mount.Mount, error) {
mounts := []mount.Mount{} mounts := []mount.Mount{}
var inherited []string var inherited []string
if inherit != nil { if inherit != nil {
@ -217,7 +217,7 @@ func buildContainerMountOptions(s types.ServiceConfig, inherit *moby.Container)
if contains(inherited, v.Target) { if contains(inherited, v.Target) {
continue continue
} }
mount, err := buildMount(v) mount, err := buildMount(p, v)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -226,9 +226,9 @@ func buildContainerMountOptions(s types.ServiceConfig, inherit *moby.Container)
return mounts, nil return mounts, nil
} }
func buildMount(volume types.ServiceVolumeConfig) (mount.Mount, error) { func buildMount(project types.Project, volume types.ServiceVolumeConfig) (mount.Mount, error) {
source := volume.Source source := volume.Source
if volume.Type == "bind" && !filepath.IsAbs(source) { if volume.Type == types.VolumeTypeBind && !filepath.IsAbs(source) {
// volume source has already been prefixed with workdir if required, by compose-go project loader // volume source has already been prefixed with workdir if required, by compose-go project loader
var err error var err error
source, err = filepath.Abs(source) source, err = filepath.Abs(source)
@ -236,6 +236,12 @@ func buildMount(volume types.ServiceVolumeConfig) (mount.Mount, error) {
return mount.Mount{}, err return mount.Mount{}, err
} }
} }
if volume.Type == types.VolumeTypeVolume {
pVolume, ok := project.Volumes[volume.Source]
if ok {
source = pVolume.Name
}
}
return mount.Mount{ return mount.Mount{
Type: mount.Type(volume.Type), Type: mount.Type(volume.Type),

View File

@ -120,10 +120,20 @@ func TestLocalComposeVolume(t *testing.T) {
c.RunDockerOrExitError("rmi", "compose-e2e-volume_nginx") c.RunDockerOrExitError("rmi", "compose-e2e-volume_nginx")
c.RunDockerOrExitError("volume", "rm", projectName+"_staticVol") c.RunDockerOrExitError("volume", "rm", projectName+"_staticVol")
c.RunDockerCmd("compose", "up", "-d", "--workdir", "volume-test", "--project-name", projectName) c.RunDockerCmd("compose", "up", "-d", "--workdir", "volume-test", "--project-name", projectName)
})
t.Run("access bind mount data", func(t *testing.T) {
output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second) output := HTTPGetWithRetry(t, "http://localhost:8090", http.StatusOK, 2*time.Second, 20*time.Second)
assert.Assert(t, strings.Contains(output, "Hello from Nginx container")) assert.Assert(t, strings.Contains(output, "Hello from Nginx container"))
})
t.Run("check container volume specs", func(t *testing.T) {
res := c.RunDockerCmd("inspect", "compose-e2e-volume_nginx2_1", "--format", "{{ json .HostConfig.Mounts }}")
//nolint
res.Assert(t, icmd.Expected{Out: `[{"Type":"volume","Source":"compose-e2e-volume_staticVol","Target":"/usr/share/nginx/html","ReadOnly":true},{"Type":"volume","Target":"/usr/src/app/node_modules"}]`})
})
t.Run("cleanup volume project", func(t *testing.T) {
_ = c.RunDockerCmd("compose", "down", "--project-name", projectName) _ = c.RunDockerCmd("compose", "down", "--project-name", projectName)
_ = c.RunDockerCmd("volume", "rm", projectName+"_staticVol") _ = c.RunDockerCmd("volume", "rm", projectName+"_staticVol")
}) })

View File

@ -9,7 +9,7 @@ services:
nginx2: nginx2:
build: nginx-build build: nginx-build
volumes: volumes:
- staticVol:/usr/share/nginx/html - staticVol:/usr/share/nginx/html:ro
- /usr/src/app/node_modules - /usr/src/app/node_modules
ports: ports:
- 9090:80 - 9090:80