Improve validation for volume specs

The current validation only checked for the
number of elements in the volume-spec, however,
did not validate if the elements were empty.

Because of this, an empty volume-spec (""),
or volume spec only containing separators ("::")
would not be invalidated.

This adds a simple check for empty elements in
the volume-spec, and returns an error if
the spec is invalid.

A unit-test is also added to verify the behavior.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2017-01-03 21:26:19 +01:00
parent 36c0b59149
commit 1ca25a2e5e
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 16 additions and 3 deletions

View file

@ -348,6 +348,12 @@ func convertVolumeToMount(
// TODO: split Windows path mappings properly
parts := strings.SplitN(volumeSpec, ":", 3)
for _, part := range parts {
if strings.TrimSpace(part) == "" {
return mount.Mount{}, fmt.Errorf("invalid volume: %s", volumeSpec)
}
}
switch len(parts) {
case 3:
source = parts[0]
@ -358,8 +364,6 @@ func convertVolumeToMount(
target = parts[1]
case 1:
target = parts[0]
default:
return mount.Mount{}, fmt.Errorf("invalid volume: %s", volumeSpec)
}
if source == "" {

View file

@ -10,7 +10,7 @@ import (
func TestConvertVolumeToMountAnonymousVolume(t *testing.T) {
stackVolumes := map[string]composetypes.VolumeConfig{}
namespace := namespace{name:"foo"}
namespace := namespace{name: "foo"}
expected := mount.Mount{
Type: mount.TypeVolume,
Target: "/foo/bar",
@ -19,3 +19,12 @@ func TestConvertVolumeToMountAnonymousVolume(t *testing.T) {
assert.NilError(t, err)
assert.DeepEqual(t, mnt, expected)
}
func TestConvertVolumeToMountInvalidFormat(t *testing.T) {
namespace := namespace{name: "foo"}
invalids := []string{"::", "::cc", ":bb:", "aa::", "aa::cc", "aa:bb:", " : : ", " : :cc", " :bb: ", "aa: : ", "aa: :cc", "aa:bb: "}
for _, vol := range invalids {
_, err := convertVolumeToMount(vol, map[string]composetypes.VolumeConfig{}, namespace)
assert.Error(t, err, "invalid volume: "+vol)
}
}