Skip to content

Commit

Permalink
refactor: [#389] extract duplicate function wait_for_container_to_be_…
Browse files Browse the repository at this point in the history
…healthy
  • Loading branch information
josecelano committed Nov 15, 2023
1 parent d769d41 commit 1e0e416
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 54 deletions.
29 changes: 2 additions & 27 deletions contrib/dev-tools/container/e2e/mysql/run-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,6 @@ TORRUST_TRACKER_USER_UID=$CURRENT_USER_ID
export USER_ID
export TORRUST_TRACKER_USER_UID

# todo: remove duplicate funtion
wait_for_container_to_be_healthy() {
local container_name="$1"
local max_retries="$2"
local retry_interval="$3"
local retry_count=0

while [ $retry_count -lt "$max_retries" ]; do
container_health="$(docker inspect --format='{{json .State.Health}}' "$container_name")"
if [ "$container_health" != "{}" ]; then
container_status="$(echo "$container_health" | jq -r '.Status')"
if [ "$container_status" == "healthy" ]; then
echo "Container $container_name is healthy"
return 0
fi
fi

retry_count=$((retry_count + 1))
echo "Waiting for container $container_name to become healthy (attempt $retry_count of $max_retries)..."
sleep "$retry_interval"
done

echo "Timeout reached, container $container_name is not healthy"
return 1
}

# Install tool to create torrent files.
# It's needed by some tests to generate and parse test torrent files.
cargo install imdl || exit 1
Expand All @@ -50,7 +24,8 @@ echo "Running E2E tests using MySQL ..."
# Start E2E testing environment
./contrib/dev-tools/container/e2e/mysql/e2e-env-up.sh || exit 1

wait_for_container_to_be_healthy torrust-mysql-1 10 3
# Wait for conatiners to be healthy
./contrib/dev-tools/container/functions/wait_for_container_to_be_healthy.sh torrust-mysql-1 10 3
# todo: implement healthchecks for tracker and index and wait until they are healthy
#wait_for_container torrust-tracker-1 10 3
#wait_for_container torrust-idx-back-1 10 3
Expand Down
29 changes: 2 additions & 27 deletions contrib/dev-tools/container/e2e/sqlite/run-e2e-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,6 @@ TORRUST_TRACKER_USER_UID=$CURRENT_USER_ID
export USER_ID
export TORRUST_TRACKER_USER_UID

# todo: remove duplicate funtion
wait_for_container_to_be_healthy() {
local container_name="$1"
local max_retries="$2"
local retry_interval="$3"
local retry_count=0

while [ $retry_count -lt "$max_retries" ]; do
container_health="$(docker inspect --format='{{json .State.Health}}' "$container_name")"
if [ "$container_health" != "{}" ]; then
container_status="$(echo "$container_health" | jq -r '.Status')"
if [ "$container_status" == "healthy" ]; then
echo "Container $container_name is healthy"
return 0
fi
fi

retry_count=$((retry_count + 1))
echo "Waiting for container $container_name to become healthy (attempt $retry_count of $max_retries)..."
sleep "$retry_interval"
done

echo "Timeout reached, container $container_name is not healthy"
return 1
}

# Install tool to create torrent files.
# It's needed by some tests to generate and parse test torrent files.
cargo install imdl || exit 1
Expand All @@ -51,7 +25,8 @@ echo "Running E2E tests using SQLite ..."
# Start E2E testing environment
./contrib/dev-tools/container/e2e/sqlite/e2e-env-up.sh || exit 1

wait_for_container_to_be_healthy torrust-mysql-1 10 3
# Wait for conatiners to be healthy
./contrib/dev-tools/container/functions/wait_for_container_to_be_healthy.sh torrust-mysql-1 10 3
# todo: implement healthchecks for tracker and index and wait until they are healthy
#wait_for_container torrust-tracker-1 10 3
#wait_for_container torrust-idx-back-1 10 3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

wait_for_container_to_be_healthy() {
local container_name="$1"
local max_retries="$2"
local retry_interval="$3"
local retry_count=0

while [ $retry_count -lt "$max_retries" ]; do
container_health="$(docker inspect --format='{{json .State.Health}}' "$container_name")"
if [ "$container_health" != "{}" ]; then
container_status="$(echo "$container_health" | jq -r '.Status')"
if [ "$container_status" == "healthy" ]; then
echo "Container $container_name is healthy"
return 0
fi
fi

retry_count=$((retry_count + 1))
echo "Waiting for container $container_name to become healthy (attempt $retry_count of $max_retries)..."
sleep "$retry_interval"
done

echo "Timeout reached, container $container_name is not healthy"
return 1
}

0 comments on commit 1e0e416

Please sign in to comment.