Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions cli/compose/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,17 @@ func LoadVolumes(source map[string]interface{}, version string) (map[string]type
if !volume.External.External {
continue
}
if versions.LessThan(version, "3.4") {
switch {
case volume.Driver != "":
return nil, externalVolumeError(name, "driver")
case len(volume.DriverOpts) > 0:
return nil, externalVolumeError(name, "driver_opts")
case len(volume.Labels) > 0:
return nil, externalVolumeError(name, "labels")
}
}
switch {
case volume.Driver != "":
return nil, externalVolumeError(name, "driver")
case len(volume.DriverOpts) > 0:
return nil, externalVolumeError(name, "driver_opts")
case len(volume.Labels) > 0:
return nil, externalVolumeError(name, "labels")
case volume.External.Name != "":
if volume.Name != "" {
return nil, errors.Errorf("volume %s: volume.external.name and volume.name conflict; only use volume.name", name)
Expand Down
101 changes: 101 additions & 0 deletions cli/compose/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,107 @@ volumes:
assert.ErrorContains(t, err, "external_volume")
}

func TestValidNotExternalVolumeDriverOpsLabelCombination(t *testing.T) {
config, err := loadYAML(`
version: "3"
volumes:
my_volume:
driver: foobar
driver_opts:
beep: boop
click: clack
labels:
- tick=tock
- ping=pong
`)

assert.NilError(t, err)
expected := map[string]types.VolumeConfig{
"my_volume": {
Driver: "foobar",
DriverOpts: map[string]string{
"beep": "boop",
"click": "clack",
},
Labels: types.Labels{
"tick": "tock",
"ping": "pong",
},
},
}
assert.Assert(t, is.Len(config.Volumes, 1))
assert.Check(t, is.DeepEqual(expected, config.Volumes))
}

func TestValidExternalAndDriverCombination34(t *testing.T) {
config, err := loadYAML(`
version: "3.4"
volumes:
external_volume:
external: true
driver: foobar
`)

assert.NilError(t, err)
expected := map[string]types.VolumeConfig{
"external_volume": {
Name: "external_volume",
External: types.External{External: true},
Driver: "foobar",
},
}
assert.Assert(t, is.Len(config.Volumes, 1))
assert.Check(t, is.DeepEqual(expected, config.Volumes))
}

func TestValidExternalAndDirverOptsCombination35(t *testing.T) {
config, err := loadYAML(`
version: "3.5"
volumes:
external_volume:
external: true
driver_opts:
beep: boop
`)

assert.NilError(t, err)
expected := map[string]types.VolumeConfig{
"external_volume": {
Name: "external_volume",
External: types.External{External: true},
DriverOpts: map[string]string{
"beep": "boop",
},
},
}
assert.Assert(t, is.Len(config.Volumes, 1))
assert.Check(t, is.DeepEqual(expected, config.Volumes))
}

func TestValidExternalAndLabelsCombination36(t *testing.T) {
config, err := loadYAML(`
version: "3.6"
volumes:
external_volume:
external: true
labels:
- beep=boop
`)

assert.NilError(t, err)
expected := map[string]types.VolumeConfig{
"external_volume": {
Name: "external_volume",
External: types.External{External: true},
Labels: types.Labels{
"beep": "boop",
},
},
}
assert.Assert(t, is.Len(config.Volumes, 1))
assert.Check(t, is.DeepEqual(expected, config.Volumes))
}

func TestLoadVolumeInvalidExternalNameAndNameCombination(t *testing.T) {
_, err := loadYAML(`
version: "3.4"
Expand Down