Skip to content

Commit

Permalink
improve support for spaces
Browse files Browse the repository at this point in the history
* arguments can have spaces
* docker.add_option can handle arguments with spaces
* docker.add_env can handle arguments with spaces
  • Loading branch information
savar committed Jan 24, 2017
1 parent 4a1347b commit ed1d359
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
4 changes: 2 additions & 2 deletions bootstrap/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ATHENA_CONTAINER_STARTED=
ATHENA_DOCKER_DAEMON_IS_RUNNING=
ATHENA_DOCKER_IP=
ATHENA_DOCKER_NO_DEFAULT_ROUTER=
ATHENA_DOCKER_OPTS=
ATHENA_DOCKER_OPTS=()
ATHENA_DOCKER_AUTO_CLEANUP=1
ATHENA_DOCKER_PRIVILEGED_MODE_ENABLED=1
ATHENA_MOCK=
Expand All @@ -20,7 +20,7 @@ ATHENA_PLG_IMAGE_NAME=
ATHENA_PLG_IMAGE_VERSION=

# SHARED-ONLY variables
ATHENA_ARGS=
ATHENA_ARGS=()
ATHENA_BASE_DIR=
ATHENA_BASE_LIB_DIR=
ATHENA_IS_DEBUG=
Expand Down
19 changes: 12 additions & 7 deletions lib/functions.docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,12 @@ function athena.docker.cleanup() {
fi
}

# This function adds the given option to the docker run option string ($ATHENA_DOCKER_OPTS).
# USAGE: athena.docker.add_option <your option>
# This function adds the given option(s) to the docker run option string ($ATHENA_DOCKER_OPTS).
# USAGE: athena.docker.add_option <option...>
# RETURN: --
function athena.docker.add_option()
{
athena.argument.argument_is_not_empty_or_fail "$1" "option"
athena.utils.add_to_array "ATHENA_DOCKER_OPTS" "$@"
}

Expand All @@ -298,8 +299,10 @@ function athena.docker.add_option()
# RETURN: --
function athena.docker.add_env()
{
athena.argument.argument_is_not_empty_or_fail "$1"
athena.argument.argument_is_not_empty_or_fail "$2"
local name="$1"
athena.docker.add_option --env $name"="$2
athena.docker.add_option --env "$name"="$2"
}

# This function adds environment variables with the given prefix to the docker run option string.
Expand All @@ -313,7 +316,7 @@ function athena.docker.add_envs_with_prefix()
local tmp=$(eval echo \${!$prefix@})
for var in $(echo $tmp | grep -v ^$ | tr ' ' '\n')
do
athena.docker.add_option --env $var=${!var}
athena.docker.add_env "$var" "${!var}"
done
return 0
}
Expand Down Expand Up @@ -535,6 +538,8 @@ function athena.docker.run_container_with_default_router()
local name="$1"
local tag_name="$2"
local athena_command="$3"
local -a athena_arguments
local -a docker_opts

athena.docker.add_env "ATHENA_PLUGIN" "$(athena.plugin.get_plugin)"
athena.docker.add_env "ATHENA_BASE_SHARED_LIB_DIR" "/opt/shared"
Expand All @@ -550,10 +555,10 @@ function athena.docker.run_container_with_default_router()
athena.docker.add_option "$tag_name"
athena.docker.add_option "$router"
athena.docker.add_option "$athena_command"
athena.docker.add_option "$(athena.argument.get_arguments)"

local docker_opts="$(athena.docker.get_options)"
athena.docker.run ${docker_opts[@]}
athena.argument.get_arguments athena_arguments
athena.docker.get_options docker_opts
athena.docker.run "${docker_opts[@]}" "${athena_arguments[@]}"
}

# This function specifies that the default router should not be used.
Expand Down
16 changes: 11 additions & 5 deletions tests/test.functions.docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function testcase_athena.docker.run_container_with_default_router()
bashunit.test.assert_exit_code.expects_fail "athena.docker.run_container_with_default_router"
bashunit.test.assert_exit_code.expects_fail "athena.docker.run_container_with_default_router" "one"
bashunit.test.assert_exit_code.expects_fail "athena.docker.run_container_with_default_router" "one" "two"
bashunit.test.mock "athena.docker.run" "_my_docker_echo"
bashunit.test.mock "athena.docker.run" "_my_fill_called_with_arguments"
bashunit.test.mock.outputs "athena.docker.get_ip" "127.0.0.1"
bashunit.test.mock.outputs "athena.os.get_host_ip" "127.0.0.1"
bashunit.test.mock.outputs "athena.plugin.get_shared_lib_dir" "/path/to/shared/dir"
Expand All @@ -27,10 +27,12 @@ function testcase_athena.docker.run_container_with_default_router()

athena.docker.set_options
athena.docker.add_option "OTHER_OPTIONS"
athena.argument.set_arguments one two three
bashunit.test.assert_output "athena.docker.run_container_with_default_router" \
"OTHER_OPTIONS --env ATHENA_PLUGIN=base --env ATHENA_BASE_SHARED_LIB_DIR=/opt/shared --env BIN_DIR=/opt/athena/bin --env CMD_DIR=/opt/athena/bin/cmd --env LIB_DIR=/opt/athena/bin/lib --env ATHENA_DOCKER_IP=127.0.0.1 --env ATHENA_DOCKER_HOST_IP=127.0.0.1 -v /path/to/shared/dir:/opt/shared -v /path/to/plugin/dir:/opt/athena -v /path/to/bootstrap/dir:/opt/bootstrap --name mycontainer mytag:version /opt/bootstrap/router.sh mycommand one two three" \
"mycontainer" "mytag:version" "mycommand"
athena.argument.set_arguments one two three "four and five"

CALLED_WITH_ARGUMENTS=()
athena.docker.run_container_with_default_router "mycontainer" "mytag:version" "mycommand"
EXPECTED_ARGUMENTS=( OTHER_OPTIONS --env ATHENA_PLUGIN=base --env ATHENA_BASE_SHARED_LIB_DIR=/opt/shared --env BIN_DIR=/opt/athena/bin --env CMD_DIR=/opt/athena/bin/cmd --env LIB_DIR=/opt/athena/bin/lib --env ATHENA_DOCKER_IP=127.0.0.1 --env ATHENA_DOCKER_HOST_IP=127.0.0.1 -v /path/to/shared/dir:/opt/shared -v /path/to/plugin/dir:/opt/athena -v /path/to/bootstrap/dir:/opt/bootstrap --name mycontainer mytag:version /opt/bootstrap/router.sh mycommand one two three "four and five" )
bashunit.test.assert_array EXPECTED_ARGUMENTS CALLED_WITH_ARGUMENTS
}

function testcase_athena.docker.add_option()
Expand Down Expand Up @@ -670,6 +672,10 @@ function _echo_all_arguments_in_newline()
{
echo "$@"
}
function _my_fill_called_with_arguments()
{
CALLED_WITH_ARGUMENTS=( "$@" )
}
function _my_docker_echo()
{
echo -n "$@"
Expand Down

0 comments on commit ed1d359

Please sign in to comment.