Skip to content

Commit 9f06a02

Browse files
tymonxndeloof
authored andcommitted
Moved bind mode creation to getBindMode function
Added unit tests for all uses cases. Signed-off-by: Tymoteusz Blazejczyk <[email protected]>
1 parent 9d0421a commit 9f06a02

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

pkg/compose/create.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -719,14 +719,7 @@ MOUNTS:
719719
if m.Type == mount.TypeBind || m.Type == mount.TypeNamedPipe {
720720
for _, v := range service.Volumes {
721721
if v.Target == m.Target && v.Bind != nil && v.Bind.CreateHostPath {
722-
mode := "rw"
723-
if m.ReadOnly {
724-
mode = "ro"
725-
}
726-
if v.Bind.SELinux != "" {
727-
mode += "," + v.Bind.SELinux
728-
}
729-
binds = append(binds, fmt.Sprintf("%s:%s:%s", m.Source, m.Target, mode))
722+
binds = append(binds, fmt.Sprintf("%s:%s:%s", m.Source, m.Target, getBindMode(v.Bind, m.ReadOnly)))
730723
continue MOUNTS
731724
}
732725
}
@@ -736,6 +729,23 @@ MOUNTS:
736729
return volumeMounts, binds, mounts, nil
737730
}
738731

732+
func getBindMode(bind *types.ServiceVolumeBind, readOnly bool) string {
733+
mode := "rw"
734+
735+
if readOnly {
736+
mode = "ro"
737+
}
738+
739+
switch bind.SELinux {
740+
case types.SELinuxShared:
741+
mode += ",z"
742+
case types.SELinuxPrivate:
743+
mode += ",Z"
744+
}
745+
746+
return mode
747+
}
748+
739749
func buildContainerMountOptions(p types.Project, s types.ServiceConfig, img moby.ImageInspect, inherit *moby.Container) ([]mount.Mount, error) {
740750
var mounts = map[string]mount.Mount{}
741751
if inherit != nil {

pkg/compose/create_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,12 @@ func TestBuildContainerMountOptions(t *testing.T) {
142142
assert.Equal(t, mounts[0].Target, "/var/myvolume1")
143143
assert.Equal(t, mounts[1].Target, "/var/myvolume2")
144144
}
145+
146+
func TestGetBindMode(t *testing.T) {
147+
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{}, false), "rw")
148+
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{}, true), "ro")
149+
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxShared}, false), "rw,z")
150+
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxPrivate}, false), "rw,Z")
151+
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxShared}, true), "ro,z")
152+
assert.Equal(t, getBindMode(&composetypes.ServiceVolumeBind{SELinux: composetypes.SELinuxPrivate}, true), "ro,Z")
153+
}

0 commit comments

Comments
 (0)