Skip to content

Commit

Permalink
Add support for --mount aliases like src, target, dst
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed Jun 16, 2023
1 parent c2b234b commit 8e45ff2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
18 changes: 10 additions & 8 deletions docker
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ function fix_volume_arg() {
else
local mode_suffix=""
fi
elif [[ "${volume_arg}" =~ ,?type\=bind,? ]]; then
elif [[ "${volume_arg}" =~ (^|,)type\=bind(,|$) ]]; then
# There must be a better way of doing this
if [[ "${volume_arg}" =~ ,?source\=(/[^,]+),? ]]; then
local source="${BASH_REMATCH[1]}"
if [[ "${volume_arg}" =~ ,?destination\=(/[^,]+),? ]]; then
if [[ "${volume_arg}" =~ (^|,)((source|src)\=(/[^,]+))(,|$) ]]; then
local source_key="${BASH_REMATCH[3]}"
local source="${BASH_REMATCH[4]}"
if [[ "${volume_arg}" =~ (^|,)((destination|dst|target)\=(/[^,]+))(,|$) ]]; then
local arg_type="mount"
local destination="${BASH_REMATCH[1]}"
local destination_key="${BASH_REMATCH[3]}"
local destination="${BASH_REMATCH[4]}"
else
# Leave it as it is if it does not match the pattern
return
Expand Down Expand Up @@ -100,8 +102,8 @@ function fix_volume_arg() {
if [[ "${arg_type}" == "volume" ]]; then
extra_args+=(--volume "${container_volume_source}:${destination}/${container_volume_destination#"${source}/"}${mode_suffix}")
elif [[ "${arg_type}" == "mount" ]]; then
local fixed_arg="${volume_arg//"source=${source}"/"source=${container_volume_source}"}"
fixed_arg="${fixed_arg//"destination=${destination}"/"destination=${destination}/${container_volume_destination#"${source}/"}"}"
local fixed_arg="${volume_arg//"${source_key}=${source}"/"${source_key}=${container_volume_source}"}"
fixed_arg="${fixed_arg//"${destination_key}=${destination}"/"${destination_key}=${destination}/${container_volume_destination#"${source}/"}"}"
extra_args+=(--mount "${fixed_arg}")
else
echo "Unexpected arg_type: ${arg_type}" >&2
Expand All @@ -116,7 +118,7 @@ function fix_volume_arg() {
if [[ "${arg_type}" == "volume" ]]; then
volume_arg="${fixed_source}:${destination}${mode_suffix}"
elif [[ "${arg_type}" == "mount" ]]; then
volume_arg="${volume_arg//"source=${source}"/"source=${fixed_source}"}"
volume_arg="${volume_arg//"${source_key}=${source}"/"${source_key}=${fixed_source}"}"
else
echo "Unexpected arg_type: ${arg_type}" >&2
exit 1
Expand Down
6 changes: 3 additions & 3 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ for docker_version in "${docker_versions[@]}"; do
docker --host test run --volume /wd:/wd:ro --volume /test:/test:ro alpine --volume /wd:/wd |
grep --quiet "^docker.orig --host test run --volume ${PWD}:/wd:ro --volume /container-root/test:/test:ro --volume ${PWD}/testfile:/test/testfile:ro alpine --volume /wd:/wd$"

echo "Same as above (with --mount), but retaining read only mode on auto added volume"
echo "Same as above (with --mount src and target, dst), but retaining read only mode on auto added volume"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --env DOND_SHIM_MOCK_CONTAINER_ROOT_DIR=/container-root --volume "${PWD}:/wd" --volume "${PWD}/testfile:/test/testfile" "${image_id}" \
docker --host test run --mount type=bind,source=/wd,destination=/wd,readonly --mount type=bind,source=/test,destination=/test,readonly alpine --mount type=bind,source=/wd,destination=/wd,readonly |
grep --quiet "^docker.orig --host test run --mount type=bind,source=${PWD},destination=/wd,readonly --mount type=bind,source=/container-root/test,destination=/test,readonly --mount type=bind,source=${PWD}/testfile,destination=/test/testfile,readonly alpine --mount type=bind,source=/wd,destination=/wd,readonly$"
docker --host test run --mount type=bind,src=/wd,target=/wd,readonly --mount type=bind,source=/test,dst=/test,readonly alpine --mount type=bind,source=/wd,destination=/wd,readonly |
grep --quiet "^docker.orig --host test run --mount type=bind,src=${PWD},target=/wd,readonly --mount type=bind,source=/container-root/test,dst=/test,readonly --mount type=bind,source=${PWD}/testfile,dst=/test/testfile,readonly alpine --mount type=bind,source=/wd,destination=/wd,readonly$"

echo "Same but for container run"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${PWD}:/wd" "${image_id}" \
Expand Down

0 comments on commit 8e45ff2

Please sign in to comment.