@@ -13,25 +13,59 @@ setup() {
1313 bats_require_minimum_version 1.5.0
1414}
1515
16- start_exporter () {
17- jmp run --exporter-config " $EXPORTER_CONFIG " \
18- --tls-grpc-listener " $LISTENER_PORT " \
19- --tls-grpc-insecure &
16+ # Start the exporter in the background.
17+ # $1 - config file (default: $EXPORTER_CONFIG)
18+ # $2 - readiness: "grpc" waits via jmp shell (drains LogStream),
19+ # "port" waits via nc -z (preserves LogStream queue)
20+ # $3 - if set, redirect stderr to ${BATS_TEST_TMPDIR}/exporter.log
21+ _start_exporter () {
22+ local config=" ${1:- $EXPORTER_CONFIG } "
23+ local readiness=" ${2:- grpc} "
24+ local capture_logs=" ${3:- } "
25+
26+ if [ -n " $capture_logs " ]; then
27+ jmp run --exporter-config " $config " \
28+ --tls-grpc-listener " $LISTENER_PORT " \
29+ --tls-grpc-insecure 2> " ${BATS_TEST_TMPDIR} /exporter.log" &
30+ else
31+ jmp run --exporter-config " $config " \
32+ --tls-grpc-listener " $LISTENER_PORT " \
33+ --tls-grpc-insecure &
34+ fi
2035 LISTENER_PID=$!
2136 echo " $LISTENER_PID " > " ${BATS_TEST_TMPDIR} /exporter.pid"
2237
23- # Wait for the gRPC server to be ready
2438 local retries=30
25- while ! jmp shell --tls-grpc " 127.0.0.1:${LISTENER_PORT} " --tls-grpc-insecure -- j --help > /dev/null 2>&1 ; do
26- retries=$(( retries - 1 ))
27- if [ " $retries " -le 0 ]; then
28- echo " Exporter did not become ready in time" >&2
29- return 1
30- fi
31- sleep 0.5
32- done
39+ if [ " $readiness " = " port" ]; then
40+ # TCP-only check: doesn't drain the LogStream queue, so hook output
41+ # remains buffered for the test command to consume.
42+ while ! nc -z 127.0.0.1 " $LISTENER_PORT " 2> /dev/null; do
43+ retries=$(( retries - 1 ))
44+ if [ " $retries " -le 0 ]; then
45+ echo " Port $LISTENER_PORT did not become available" >&2
46+ return 1
47+ fi
48+ sleep 0.5
49+ done
50+ else
51+ # Full gRPC check: ensures exporter is ready for commands.
52+ # Drains LogStream queue (unsuitable for hook output tests).
53+ while ! jmp shell --tls-grpc " 127.0.0.1:${LISTENER_PORT} " --tls-grpc-insecure -- j --help > /dev/null 2>&1 ; do
54+ retries=$(( retries - 1 ))
55+ if [ " $retries " -le 0 ]; then
56+ echo " Exporter did not become ready in time" >&2
57+ return 1
58+ fi
59+ sleep 0.5
60+ done
61+ fi
3362}
3463
64+ start_exporter () { _start_exporter " $1 " grpc; }
65+ start_exporter_with_logs () { _start_exporter " $1 " grpc logs; }
66+ start_exporter_bg () { _start_exporter " $1 " port; }
67+ start_exporter_bg_with_logs () { _start_exporter " $1 " port logs; }
68+
3569stop_exporter () {
3670 if [ -f " ${BATS_TEST_TMPDIR} /exporter.pid" ]; then
3771 local pid
@@ -71,3 +105,32 @@ teardown() {
71105 run jmp shell --tls-grpc " 127.0.0.1:${LISTENER_PORT} " -- j power on
72106 assert_failure
73107}
108+
109+ @test " direct listener hooks: beforeLease hook executes and j commands work" {
110+ # Use start_exporter_bg (TCP-only readiness check) to avoid draining
111+ # the LogStream queue before the test command connects.
112+ start_exporter_bg " ${SCRIPT_DIR} /exporters/exporter-direct-hooks-before.yaml"
113+
114+ run jmp shell --tls-grpc " 127.0.0.1:${LISTENER_PORT} " --tls-grpc-insecure \
115+ --exporter-logs -- j power off
116+ assert_success
117+ assert_output --partial " BEFORE_HOOK_DIRECT: executed"
118+ assert_output --partial " BEFORE_HOOK_DIRECT: complete"
119+ }
120+
121+ @test " direct listener hooks: afterLease hook runs on exporter shutdown" {
122+ start_exporter_bg_with_logs " ${SCRIPT_DIR} /exporters/exporter-direct-hooks-both.yaml"
123+
124+ run jmp shell --tls-grpc " 127.0.0.1:${LISTENER_PORT} " --tls-grpc-insecure \
125+ --exporter-logs -- j power on
126+ assert_success
127+ assert_output --partial " BEFORE_HOOK_DIRECT: executed"
128+
129+ # Stop the exporter (SIGTERM triggers _cleanup_after_lease).
130+ # stop_exporter waits for the process to exit, so the log is complete.
131+ stop_exporter
132+
133+ # afterLease hook output should appear in the exporter's stderr log
134+ run cat " ${BATS_TEST_TMPDIR} /exporter.log"
135+ assert_output --partial " AFTER_HOOK_DIRECT: executed"
136+ }
0 commit comments