fix: volume driver options are not used if driver if not specified [bug #210] (#211)

This commit is contained in:
Justin Bradford
2025-12-08 15:58:43 +10:00
committed by GitHub
parent dfe12ca644
commit 6373d4097c
2 changed files with 109 additions and 1 deletions
+1 -1
View File
@@ -249,7 +249,7 @@ func dockerVolumeSpecFromCompose(serviceVolume types.ServiceVolumeConfig, volume
} }
if !volume.External { if !volume.External {
if volume.Driver != "" { if volume.Driver != "" || len(volume.DriverOpts) > 0 {
spec.VolumeOptions.Driver = &mount.Driver{ spec.VolumeOptions.Driver = &mount.Driver{
Name: volume.Driver, Name: volume.Driver,
Options: volume.DriverOpts, Options: volume.DriverOpts,
+108
View File
@@ -586,6 +586,114 @@ services:
} }
} }
func TestServiceSpecFromCompose_VolumeDriverOpts(t *testing.T) {
tests := []struct {
name string
composeYAML string
expectedVolume api.VolumeSpec
}{
{
name: "volume with driver_opts only (no driver specified)",
composeYAML: `
services:
test:
image: nginx
volumes:
- nfsmount:/data/nfs
volumes:
nfsmount:
driver_opts:
type: "nfs"
o: "addr=192.168.1.100,nolock,soft"
device: ":/mnt/share"
`,
expectedVolume: api.VolumeSpec{
Name: "nfsmount",
Type: api.VolumeTypeVolume,
VolumeOptions: &api.VolumeOptions{
Name: "nfsmount",
Driver: &mount.Driver{
Options: map[string]string{
"type": "nfs",
"o": "addr=192.168.1.100,nolock,soft",
"device": ":/mnt/share",
},
},
},
},
},
{
name: "volume with driver and driver_opts",
composeYAML: `
services:
test:
image: nginx
volumes:
- nfsmount:/data/nfs
volumes:
nfsmount:
driver: local
driver_opts:
type: "nfs"
o: "addr=192.168.1.100,nolock,soft"
device: ":/mnt/share"
`,
expectedVolume: api.VolumeSpec{
Name: "nfsmount",
Type: api.VolumeTypeVolume,
VolumeOptions: &api.VolumeOptions{
Name: "nfsmount",
Driver: &mount.Driver{
Name: "local",
Options: map[string]string{
"type": "nfs",
"o": "addr=192.168.1.100,nolock,soft",
"device": ":/mnt/share",
},
},
},
},
},
{
name: "external volume ignores driver_opts",
composeYAML: `
services:
test:
image: nginx
volumes:
- extvolume:/data
volumes:
extvolume:
external: true
`,
expectedVolume: api.VolumeSpec{
Name: "extvolume",
Type: api.VolumeTypeVolume,
VolumeOptions: &api.VolumeOptions{
Name: "extvolume",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
project, err := LoadProjectFromContent(context.Background(), tt.composeYAML)
require.NoError(t, err)
spec, err := ServiceSpecFromCompose(project, "test")
require.NoError(t, err)
require.Len(t, spec.Volumes, 1, "expected exactly one volume")
actualVolume := spec.Volumes[0]
cmpOpts := cmp.Options{cmpopts.EquateEmpty()}
assert.True(t, cmp.Equal(actualVolume, tt.expectedVolume, cmpOpts...),
"Volume mismatch:\n%s", cmp.Diff(actualVolume, tt.expectedVolume, cmpOpts...))
})
}
}
func TestServiceSpecFromCompose_XMachinesPlacement(t *testing.T) { func TestServiceSpecFromCompose_XMachinesPlacement(t *testing.T) {
tests := []struct { tests := []struct {
name string name string