Skip to content

Commit

Permalink
feat:added tests for pre-exit hook (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
swayam0322 authored Nov 7, 2024
1 parent 6b7f0ec commit d83d5f8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
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"
}

0 comments on commit d83d5f8

Please sign in to comment.