Skip to content

Commit 2011c48

Browse files
authored
Refactor docker compose command handling in script
1 parent 9751aa2 commit 2011c48

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

docker_compose_test/docker_compose_test.sh

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ fi
4747
# if we use the file from inside the sandbox, symlinks will be used for volume mounted files.
4848
ABSOLUTE_COMPOSE_FILE_PATH=$WORKSPACE_PATH/$DOCKER_COMPOSE_FILE
4949

50-
docker_compose_bin="docker compose"
51-
docker-compose --version &>/dev/null
52-
if [ $? -eq 0 ]; then
53-
docker_compose_bin="docker-compose"
50+
docker_compose_bin=(docker compose)
51+
if command -v docker-compose &>/dev/null; then
52+
docker_compose_bin=(docker-compose)
5453
fi
5554

5655
cleanup() {
5756
echo "Cleaning up docker-compose resources..."
58-
docker_compose_down_cmd="$docker_compose_bin -f $ABSOLUTE_COMPOSE_FILE_PATH down --volumes --remove-orphans"
57+
docker_compose_down_cmd="${docker_compose_bin[@]} -f $ABSOLUTE_COMPOSE_FILE_PATH down --volumes --remove-orphans"
5958
echo "running: $docker_compose_down_cmd"
6059
echo "$docker_compose_down_cmd" | bash
6160
}
@@ -66,18 +65,13 @@ cleanup() {
6665
trap cleanup EXIT
6766

6867
# bring up compose file & get exit status-code from the integration test container.
69-
docker_compose_up_cmd="$docker_compose_bin -f $ABSOLUTE_COMPOSE_FILE_PATH up --exit-code-from $DOCKER_COMPOSE_TEST_CONTAINER $EXTRA_DOCKER_COMPOSE_UP_ARGS"
68+
docker_compose_up_cmd="${docker_compose_bin[@]} -f $ABSOLUTE_COMPOSE_FILE_PATH up --exit-code-from $DOCKER_COMPOSE_TEST_CONTAINER $EXTRA_DOCKER_COMPOSE_UP_ARGS"
7069
echo "running: $docker_compose_up_cmd"
7170
echo "$docker_compose_up_cmd" | bash
7271
result=$?
7372

7473
# Figure out the exit code of the test container incase it never actually started.
75-
docker_compose_ps_cmd="$docker_compose_bin -f $ABSOLUTE_COMPOSE_FILE_PATH ps -qa $DOCKER_COMPOSE_TEST_CONTAINER"
76-
echo "running: $docker_compose_ps_cmd"
77-
container_id="$docker_compose_ps_cmd" | bash
78-
echo "container id: $container_id"
79-
80-
EXIT_CODE=$(docker inspect $container_id --format='{{.State.ExitCode}}' 2>/dev/null)
74+
EXIT_CODE=$(docker inspect $("${docker_compose_bin[@]}" -f $ABSOLUTE_COMPOSE_FILE_PATH ps -qa $DOCKER_COMPOSE_TEST_CONTAINER) --format='{{.State.ExitCode}}' 2>/dev/null)
8175
if [ "$EXIT_CODE" != "0" ] || [ -z "$EXIT_CODE" ]; then
8276
echo "Error: $DOCKER_COMPOSE_TEST_CONTAINER container failed or never started!"
8377
exit 1

0 commit comments

Comments
 (0)