فهرست منبع

Moved bind mode creation to getBindMode function

Added unit tests for all uses cases.

Signed-off-by: Tymoteusz Blazejczyk <[email protected]>
Tymoteusz Blazejczyk 3 سال پیش
والد
کامیت
9f06a02eb5
2فایلهای تغییر یافته به همراه27 افزوده شده و 8 حذف شده
  1. 18 8
      pkg/compose/create.go
  2. 9 0
      pkg/compose/create_test.go

+ 18 - 8
pkg/compose/create.go

@@ -719,14 +719,7 @@ MOUNTS:
 		if m.Type == mount.TypeBind || m.Type == mount.TypeNamedPipe {
 			for _, v := range service.Volumes {
 				if v.Target == m.Target && v.Bind != nil && v.Bind.CreateHostPath {
-					mode := "rw"
-					if m.ReadOnly {
-						mode = "ro"
-					}
-					if v.Bind.SELinux != "" {
-						mode += "," + v.Bind.SELinux
-					}
-					binds = append(binds, fmt.Sprintf("%s:%s:%s", m.Source, m.Target, mode))
+					binds = append(binds, fmt.Sprintf("%s:%s:%s", m.Source, m.Target, getBindMode(v.Bind, m.ReadOnly)))
 					continue MOUNTS
 				}
 			}
@@ -736,6 +729,23 @@ MOUNTS:
 	return volumeMounts, binds, mounts, nil
 }
 
+func getBindMode(bind *types.ServiceVolumeBind, readOnly bool) string {
+	mode := "rw"
+
+	if readOnly {
+		mode = "ro"
+	}
+
+	switch bind.SELinux {
+	case types.SELinuxShared:
+		mode += ",z"
+	case types.SELinuxPrivate:
+		mode += ",Z"
+	}
+
+	return mode
+}
+
 func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
 	var mounts = map[string]mount.Mount{}
 	if inherit != nil {

+ 9 - 0
pkg/compose/create_test.go

@@ -142,3 +142,12 @@ func TestBuildContainerMountOptions(t *testing.T) {
 	assert.Equal(t, mounts[0].Target, "/var/myvolume1")
 	assert.Equal(t, mounts[1].Target, "/var/myvolume2")
 }
+
+func TestGetBindMode(t *testing.T) {
+	assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{}, false), "rw")
+	assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{}, true), "ro")
+	assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxShared}, false), "rw,z")
+	assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxPrivate}, false), "rw,Z")
+	assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxShared}, true), "ro,z")
+	assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxPrivate}, true), "ro,Z")
+}