Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/panel-scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ jobs:
- run: bash tests/scripts/gpu_nodes_test.sh
- run: bash tests/scripts/service_test.sh
- run: bash tests/scripts/postgres_backup_test.sh
- run: bash tests/scripts/timescaledb_pod_test.sh
- run: bash tests/scripts/choose_node_test.sh
- run: bash tests/scripts/nvidia_fallback_test.sh
- run: bash tests/scripts/image_updates_test.sh
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/timescaledb-upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ jobs:
steps:
- uses: actions/checkout@v4
- run: bash tests/timescaledb/docker_test.sh
- run: bash tests/timescaledb/orphan_reaper_test.sh
4 changes: 4 additions & 0 deletions base/timescaledb/stateful-set.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ spec:
labels:
app: timescaledb
spec:
# The pod's pause container is PID 1 and reaps what a cut-off kubectl
# exec leaves behind. With postgres as PID 1, such an orphan dying by a
# signal is an "untracked child" crash that restarts the whole database.
shareProcessNamespace: true
dnsConfig:
options:
- name: ndots
Expand Down
53 changes: 53 additions & 0 deletions tests/scripts/timescaledb_pod_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# shellcheck source=tests/scripts/lib.sh
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"

OVERLAYS="vault-http vault-https local-secrets-http local-secrets-https"

# A copy of the checkout with its env files made from the examples, and every
# overlay update.sh can apply written.
make_panel() {
PANEL="$WORK_DIR/panel"
mkdir -p "$PANEL"
cp -R "$TEST_ROOT/base" "$TEST_ROOT/overlays" "$TEST_ROOT/utils" "$PANEL/"
find "$PANEL/overlays" -name '*.env.example' | while read -r example; do
cp "$example" "${example%.example}"
done
(
export PANEL_DIR="$PANEL"
# shellcheck source=utils/write_overlays.sh
source "$PANEL/utils/write_overlays.sh"
write_overlays
) >"$WORK_DIR/write-output" 2>&1 || fail "write_overlays failed: $(cat "$WORK_DIR/write-output")"
}

# share_process_namespace OVERLAY: the TimescaleDB pod's setting, as rendered.
share_process_namespace() {
kubectl kustomize "$PANEL/overlays/$1" >"$WORK_DIR/$1.yaml" || fail "kubectl kustomize $1 failed"
KUBECONFIG=/dev/null kubectl patch --local -o json --type merge -p '{}' -f "$WORK_DIR/$1.yaml" >"$WORK_DIR/$1.json" ||
fail "could not read $1 as JSON"
python3 -I -c '
import json, sys
text = open(sys.argv[1]).read()
decoder = json.JSONDecoder()
at = 0
while at < len(text):
if text[at].isspace():
at += 1
continue
item, at = decoder.raw_decode(text, at)
if item.get("kind") == "StatefulSet" and item["metadata"]["name"] == "timescaledb":
print(item["spec"]["template"]["spec"].get("shareProcessNamespace"))
' "$WORK_DIR/$1.json"
}

test_every_overlay_gives_timescaledb_a_reaper_as_pid_1() {
make_panel
local overlay
for overlay in $OVERLAYS; do
assert_eq "$(share_process_namespace "$overlay")" "True"
done
}

run_tests
100 changes: 100 additions & 0 deletions tests/timescaledb/orphan_reaper_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

# Why the TimescaleDB pod shares its process namespace: an exec that gets cut
# off leaves its children to PID 1. When PID 1 is the postmaster, one of them
# dying by a signal is an "untracked child" crash and the whole database
# restarts. With a reaper as PID 1 (docker's --init standing in for the pod's
# pause container) nothing happens. Runs the pinned image, one container at a
# time.

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_ROOT="${TEST_ROOT:-$(cd "$HERE/../.." && pwd)}"
PREFIX=timescaledb-orphan-test
IMAGE="$(sed -n 's/^ *image: *\(timescale\/timescaledb:.*\)$/\1/p' "$TEST_ROOT/base/timescaledb/stateful-set.yaml" | head -n 1)"
TESTS_PASSED=0
TESTS_FAILED=0

cleanup() {
docker ps -aq --filter "name=^$PREFIX" | xargs docker rm -f >/dev/null 2>&1
}
trap cleanup EXIT

fail() {
echo " $*"
exit 1
}

# start NAME [DOCKER_ARGS...]: postgres up and past the image's first-run setup.
start() {
local name="$PREFIX-$1"
shift
docker run -d --name "$name" "$@" -e POSTGRES_PASSWORD=test "$IMAGE" >/dev/null ||
fail "could not start $IMAGE"
for _ in $(seq 1 120); do
if docker logs "$name" 2>&1 | grep -q "PostgreSQL init process complete" &&
docker exec "$name" pg_isready -q -U postgres -h 127.0.0.1; then
return 0
fi
sleep 1
done
fail "postgres never became ready: $(docker logs "$name" 2>&1 | tail -5)"
}

# orphan_dies_by_a_signal NAME: what a cut-off exec leaves behind, killed.
orphan_dies_by_a_signal() {
local name="$PREFIX-$1" pid
pid="$(docker exec "$name" sh -c 'sleep 300 >/dev/null 2>&1 & echo $!')" || fail "could not start the orphan"
sleep 1
[ "$(docker exec "$name" sh -c "sed -n 's/^PPid:[[:space:]]*//p' /proc/$pid/status")" = 1 ] ||
fail "the orphan $pid was not handed to PID 1"
docker exec "$name" kill -9 "$pid" || fail "could not kill the orphan $pid"
sleep 3
}

crashed() {
docker logs "$PREFIX-$1" 2>&1 | grep -q "untracked child process .* was terminated by signal 9" &&
docker logs "$PREFIX-$1" 2>&1 | grep -q "all server processes terminated; reinitializing"
}

test_postgres_as_pid_1_restarts_the_database_when_an_orphan_is_killed() {
start postgres-pid-1
[ "$(docker exec "$PREFIX-postgres-pid-1" cat /proc/1/comm)" = postgres ] || fail "postgres is not PID 1"
orphan_dies_by_a_signal postgres-pid-1
crashed postgres-pid-1 || fail "postgres did not restart: $(docker logs "$PREFIX-postgres-pid-1" 2>&1 | tail -5)"
docker rm -f "$PREFIX-postgres-pid-1" >/dev/null
}

test_a_reaper_as_pid_1_takes_the_orphan_and_postgres_stays_up() {
start reaper --init
[ "$(docker exec "$PREFIX-reaper" cat /proc/1/comm)" != postgres ] || fail "postgres is still PID 1"
orphan_dies_by_a_signal reaper
if docker logs "$PREFIX-reaper" 2>&1 | grep -E -q "untracked child process|reinitializing"; then
fail "postgres restarted: $(docker logs "$PREFIX-reaper" 2>&1 | tail -5)"
fi
docker exec "$PREFIX-reaper" pg_isready -q -U postgres -h 127.0.0.1 || fail "postgres is down"
docker rm -f "$PREFIX-reaper" >/dev/null
}

run_test() {
if ("$1"); then
TESTS_PASSED=$((TESTS_PASSED + 1))
echo "ok $1"
else
TESTS_FAILED=$((TESTS_FAILED + 1))
echo "FAIL $1"
fi
}

cleanup
docker pull -q "$IMAGE" >/dev/null || exit 1
if [ "$#" -eq 0 ]; then
while IFS= read -r name; do
set -- "$@" "$name"
done < <(declare -F | awk '{ print $3 }' | grep '^test_')
fi
for name in "$@"; do
run_test "$name"
done
echo
echo "$TESTS_PASSED passed, $TESTS_FAILED failed"
[ "$TESTS_FAILED" -eq 0 ]
Loading