Fixed incorrect docker mount arguments generation

The previous implementation combined --mount and -v notation
of argument [1] adding :ro to make the read-only mount point.
E.g. the command [2] called docker with the following
params: [3]. As a result instead of the read-only
folder /tmp/source, the read-write folder /tmp/source/:ro/'
is created.

This PR:
1. substitutes ':ro' with correct ',readonly'.
2. changes 'src=' and 'dst=' with 'source=' and 'target=' as
   it is stated in the documentation [1]
3. introduces the ability to EXPLICITLY create a mountpoint
   with read-write access. To do so it's necessary to add
   ',rw=true' to the --mount argument
4. corrects UTs adds some additional coverage for added
   functionality

[1]
https://docs.docker.com/storage/bind-mounts/#use-a-read-only-bind-mount

[2]
kustomize fn run ./d --mount type=bind,src=$(pwd)/test/,dst=/tmp/source/

[3]
--mount type=bind,src=/home/ubuntu/kpt-functions-catalog/functions/ts/test/,dst=/tmp/source/:ro
This commit is contained in:
Alexey Odinokov
2020-07-11 05:21:42 +00:00
parent 556eb48651
commit 63f9f79fc0
3 changed files with 60 additions and 5 deletions

View File

@@ -1379,3 +1379,44 @@ metadata:
assert.Equal(t, tc.required, fn.Container.Network.Required)
}
}
func Test_StringToStorageMount(t *testing.T) {
tests := []struct {
in string
expectedOut string
}{
{
in: "type=bind,src=/tmp/test/,dst=/tmp/source/",
expectedOut: "type=bind,source=/tmp/test/,target=/tmp/source/,readonly",
},
{
in: "type=bind,src=/tmp/test/,dst=/tmp/source/,rw=true",
expectedOut: "type=bind,source=/tmp/test/,target=/tmp/source/",
},
{
in: "type=bind,src=/tmp/test/,dst=/tmp/source/,rw=false",
expectedOut: "type=bind,source=/tmp/test/,target=/tmp/source/,readonly",
},
{
in: "type=bind,src=/tmp/test/,dst=/tmp/source/,rw=",
expectedOut: "type=bind,source=/tmp/test/,target=/tmp/source/,readonly",
},
{
in: "type=tmpfs,src=/tmp/test/,dst=/tmp/source/,rw=invalid",
expectedOut: "type=tmpfs,source=/tmp/test/,target=/tmp/source/,readonly",
},
{
in: "type=tmpfs,src=/tmp/test/,dst=/tmp/source/,rwe=invalid",
expectedOut: "type=tmpfs,source=/tmp/test/,target=/tmp/source/,readonly",
},
{
in: "type=tmpfs,src=/tmp/test/,dst",
expectedOut: "type=tmpfs,source=/tmp/test/,target=,readonly",
},
}
for _, tc := range tests {
s := StringToStorageMount(tc.in)
assert.Equal(t, tc.expectedOut, (&s).String())
}
}