Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:added tests for pre-exit hook #10

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions hooks/pre-exit
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ set -euo pipefail

# Allow to pass TMP_DIR for testing purposes
if [[ -z "${TMP_DIR:-}" ]]; then

TMP_DIR="${BUILDKITE_PLUGIN_LAMBDATEST_TMP_DIR:-}"
function cleanup {

echo "Removing ${TMP_DIR}"
echo "Removing TMP_DIR ${TMP_DIR}"
if [[ -d "${TMP_DIR}" ]]; then
rm -rf "${TMP_DIR}"
rm -rf "${TMP_DIR}"
fi
}
else
Expand All @@ -25,9 +23,9 @@ is_pid_alive() {
}

stop_tunnel() {

echo "Stopping LambdaTest Tunnel"
pushd "${TMP_DIR}" >/dev/null

local find_pidfiles
find_pidfiles=$(find . -maxdepth 1 -name 'pid.*' -type f)

Expand All @@ -37,21 +35,21 @@ stop_tunnel() {
local pid
pid=$(cat "${pidfile}")
kill -SIGINT "${pid}" || true

local counter=0
while is_pid_alive "${pid}"; do
sleep 1
counter=$((counter+1))
counter=$((counter + 1))
if [[ "${counter}" -gt 60 ]]; then
# try to kill again after a minute
kill -SIGINT "${pid}" 2>/dev/null || true
fi
echo "Tunnel Successfully Stopped"
done
echo
echo "Tunnel Successfully Stopped"
fi
done <<< "${find_pidfiles}"

popd >/dev/null

}

stop_tunnel
36 changes: 36 additions & 0 deletions tests/pre-exit.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bats

load "$BATS_PLUGIN_PATH/load.bash"

@test "Ensure TMP_DIR is not removed if it existed before" {
export TMP_DIR=$(mktemp -d)

run "${PWD}/hooks/pre-exit"

assert_success
assert_output --partial "Not removing given TMP_DIR (${TMP_DIR})"

rm -rf "$TMP_DIR"
unset TMP_DIR
}

@test "Ensure TMP_DIR is removed if didnt exist" {
run "${PWD}/hooks/pre-exit"
assert_success
assert_output --partial "Removing TMP_DIR"
}

@test "Ensure tunnel is stopped" {
export TMP_DIR=$(mktemp -d)
touch "${TMP_DIR}/pid.$$"

run "${PWD}/hooks/pre-exit"

assert_success
assert_output --partial "Stopping LambdaTest Tunnel"
assert_output --partial "Tunnel Successfully Stopped"


unset TMP_DIR
rm -rf "$TMP_DIR"
}