Bläddra i källkod

Merge pull request #29513 from thaJeztah/fix-anonymous-volumes

fix conversion of anonymous volumes in compose-file
Vincent Demeester 8 år sedan
förälder
incheckning
38886bbd74
2 ändrade filer med 35 tillägg och 2 borttagningar
  1. 14 2
      cli/compose/convert/volume.go
  2. 21 0
      cli/compose/convert/volume_test.go

+ 14 - 2
cli/compose/convert/volume.go

@@ -31,6 +31,12 @@ func convertVolumeToMount(volumeSpec string, stackVolumes volumes, namespace Nam
 	// 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]
@@ -41,8 +47,14 @@ func convertVolumeToMount(volumeSpec string, stackVolumes volumes, namespace Nam
 		target = parts[1]
 	case 1:
 		target = parts[0]
-	default:
-		return mount.Mount{}, fmt.Errorf("invald volume: %s", volumeSpec)
+	}
+
+	if source == "" {
+		// Anonymous volume
+		return mount.Mount{
+			Type:   mount.TypeVolume,
+			Target: target,
+		}, nil
 	}
 
 	// TODO: catch Windows paths here

+ 21 - 0
cli/compose/convert/volume_test.go

@@ -34,6 +34,27 @@ func TestGetBindOptionsNone(t *testing.T) {
 	assert.Equal(t, opts, (*mount.BindOptions)(nil))
 }
 
+func TestConvertVolumeToMountAnonymousVolume(t *testing.T) {
+	stackVolumes := volumes{}
+	namespace := NewNamespace("foo")
+	expected := mount.Mount{
+		Type:   mount.TypeVolume,
+		Target: "/foo/bar",
+	}
+	mount, err := convertVolumeToMount("/foo/bar", stackVolumes, namespace)
+	assert.NilError(t, err)
+	assert.DeepEqual(t, mount, expected)
+}
+
+func TestConvertVolumeToMountInvalidFormat(t *testing.T) {
+	namespace := NewNamespace("foo")
+	invalids := []string{"::", "::cc", ":bb:", "aa::", "aa::cc", "aa:bb:", " : : ", " : :cc", " :bb: ", "aa: : ", "aa: :cc", "aa:bb: "}
+	for _, vol := range invalids {
+		_, err := convertVolumeToMount(vol, volumes{}, namespace)
+		assert.Error(t, err, "invalid volume: "+vol)
+	}
+}
+
 func TestConvertVolumeToMountNamedVolume(t *testing.T) {
 	stackVolumes := volumes{
 		"normal": composetypes.VolumeConfig{