diff options
Diffstat (limited to 'tests')
| -rwxr-xr-x | tests/run_cert_bootstrap_test.sh | 39 | ||||
| -rwxr-xr-x | tests/run_download_cert_test.sh | 37 | ||||
| -rwxr-xr-x | tests/run_test.sh | 40 | ||||
| -rw-r--r-- | tests/test_helpers.sh | 56 |
4 files changed, 65 insertions, 107 deletions
diff --git a/tests/run_cert_bootstrap_test.sh b/tests/run_cert_bootstrap_test.sh index 681a4aa..d0f4a3e 100755 --- a/tests/run_cert_bootstrap_test.sh +++ b/tests/run_cert_bootstrap_test.sh @@ -15,6 +15,8 @@ # --------------------------------------------------------------- set -uo pipefail +source "$(dirname "$0")/test_helpers.sh" + CONFIG_DIR="${1:?Usage: $0 <config_dir>}" LDS_PORT=14840 @@ -23,7 +25,6 @@ LDS_PID="" SR_PID="" TMPFILE="" DOWNLOADED_CERT="" -FAILURES=0 # ── cleanup ──────────────────────────────────────────────────── cleanup() { @@ -35,44 +36,10 @@ cleanup() { } trap cleanup EXIT -# ── helpers ──────────────────────────────────────────────────── -wait_for_port() { - local port="$1" pid="$2" label="$3" i=0 - while [ $i -lt 50 ]; do - if ! kill -0 "$pid" 2>/dev/null; then - echo "FAIL: $label exited prematurely" - exit 1 - fi - if ss -tlnp 2>/dev/null | grep -q ":${port} "; then - return 0 - fi - sleep 0.1 - i=$((i + 1)) - done - echo "FAIL: $label did not listen on port $port within 5 s" - exit 1 -} - -check() { - local label="$1" result="$2" - if [ "$result" -eq 0 ]; then - echo "PASS: $label" - else - echo "FAIL: $label" - FAILURES=$((FAILURES + 1)) - fi -} - # ── idempotency guard ───────────────────────────────────────── rm -f "$CONFIG_DIR/certs/trust_client/ServerRegister_cert.der" -# ── port check ───────────────────────────────────────────────── -for port in $LDS_PORT $SR_PORT; do - if ss -tlnp 2>/dev/null | grep -q ":${port} "; then - echo "FAIL: port $port is already in use" - exit 1 - fi -done +assert_ports_free "$LDS_PORT" "$SR_PORT" # ── start LDS ────────────────────────────────────────────────── build/bobink_opcua_discovery_server "$CONFIG_DIR/server_lds.conf" >/dev/null 2>&1 & diff --git a/tests/run_download_cert_test.sh b/tests/run_download_cert_test.sh index 3631c56..7dd7dfd 100755 --- a/tests/run_download_cert_test.sh +++ b/tests/run_download_cert_test.sh @@ -12,13 +12,14 @@ # --------------------------------------------------------------- set -uo pipefail +source "$(dirname "$0")/test_helpers.sh" + CONFIG_DIR="${1:?Usage: $0 <config_dir>}" LDS_PORT=14840 LDS_PID="" TMPFILE="" DOWNLOADED_CERT="" -FAILURES=0 # ── cleanup ──────────────────────────────────────────────────── cleanup() { @@ -28,39 +29,7 @@ cleanup() { } trap cleanup EXIT -# ── helpers ──────────────────────────────────────────────────── -wait_for_port() { - local port="$1" pid="$2" label="$3" i=0 - while [ $i -lt 50 ]; do - if ! kill -0 "$pid" 2>/dev/null; then - echo "FAIL: $label exited prematurely" - exit 1 - fi - if ss -tlnp 2>/dev/null | grep -q ":${port} "; then - return 0 - fi - sleep 0.1 - i=$((i + 1)) - done - echo "FAIL: $label did not listen on port $port within 5 s" - exit 1 -} - -check() { - local label="$1" result="$2" - if [ "$result" -eq 0 ]; then - echo "PASS: $label" - else - echo "FAIL: $label" - FAILURES=$((FAILURES + 1)) - fi -} - -# ── port check ───────────────────────────────────────────────── -if ss -tlnp 2>/dev/null | grep -q ":${LDS_PORT} "; then - echo "FAIL: port $LDS_PORT is already in use" - exit 1 -fi +assert_ports_free "$LDS_PORT" # ── start LDS ────────────────────────────────────────────────── build/bobink_opcua_discovery_server "$CONFIG_DIR/server_lds.conf" >/dev/null 2>&1 & diff --git a/tests/run_test.sh b/tests/run_test.sh index 6d17958..8f6c21b 100755 --- a/tests/run_test.sh +++ b/tests/run_test.sh @@ -18,6 +18,8 @@ set -uo pipefail # NOTE: we intentionally omit "set -e" so that every check runs # even if the client itself returns non-zero. +source "$(dirname "$0")/test_helpers.sh" + CONFIG_DIR="${1:?Usage: $0 <config_dir> <expected_policy>}" EXPECTED_POLICY="${2:?Usage: $0 <config_dir> <expected_policy>}" @@ -26,7 +28,6 @@ SR_PORT=14841 LDS_PID="" SR_PID="" TMPFILE="" -FAILURES=0 # ── cleanup ──────────────────────────────────────────────────── cleanup() { @@ -36,31 +37,7 @@ cleanup() { } trap cleanup EXIT -# ── helpers ──────────────────────────────────────────────────── -wait_for_port() { - local port="$1" pid="$2" label="$3" i=0 - while [ $i -lt 50 ]; do - if ! kill -0 "$pid" 2>/dev/null; then - echo "FAIL: $label exited prematurely" - exit 1 - fi - if ss -tlnp 2>/dev/null | grep -q ":${port} "; then - return 0 - fi - sleep 0.1 - i=$((i + 1)) - done - echo "FAIL: $label did not listen on port $port within 5 s" - exit 1 -} - -# ── port check ───────────────────────────────────────────────── -for port in $LDS_PORT $SR_PORT; do - if ss -tlnp 2>/dev/null | grep -q ":${port} "; then - echo "FAIL: port $port is already in use" - exit 1 - fi -done +assert_ports_free "$LDS_PORT" "$SR_PORT" # ── start LDS ────────────────────────────────────────────────── build/bobink_opcua_discovery_server "$CONFIG_DIR/server_lds.conf" >/dev/null 2>&1 & @@ -72,17 +49,6 @@ build/bobink_opcua_server "$CONFIG_DIR/server_register.conf" "$CONFIG_DIR/server SR_PID=$! wait_for_port "$SR_PORT" "$SR_PID" "bobink_opcua_server" -# ── validation helper ───────────────────────────────────────── -check() { - local label="$1" result="$2" - if [ "$result" -eq 0 ]; then - echo "PASS: $label" - else - echo "FAIL: $label" - FAILURES=$((FAILURES + 1)) - fi -} - # ── FindServers ─────────────────────────────────────────────── TMPFILE=$(mktemp) build/client "$CONFIG_DIR/client.conf" find-servers "opc.tcp://localhost:$LDS_PORT" >"$TMPFILE" 2>&1 diff --git a/tests/test_helpers.sh b/tests/test_helpers.sh new file mode 100644 index 0000000..a9d420c --- /dev/null +++ b/tests/test_helpers.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# --------------------------------------------------------------- +# Shared helpers for OPC UA discovery integration tests. +# +# Source this file from test scripts: +# source "$(dirname "$0")/test_helpers.sh" +# --------------------------------------------------------------- + +FAILURES=0 + +# Waits up to 5 seconds for a process to listen on a TCP port. +# Exits immediately if the process dies before the port opens. +# +# wait_for_port <port> <pid> <label> +wait_for_port() { + local port="$1" pid="$2" label="$3" i=0 + while [ $i -lt 50 ]; do + if ! kill -0 "$pid" 2>/dev/null; then + echo "FAIL: $label exited prematurely" + exit 1 + fi + if ss -tlnp 2>/dev/null | grep -q ":${port} "; then + return 0 + fi + sleep 0.1 + i=$((i + 1)) + done + echo "FAIL: $label did not listen on port $port within 5 s" + exit 1 +} + +# Records a PASS/FAIL result. Increments FAILURES on failure. +# +# check <label> <exit-code> +check() { + local label="$1" result="$2" + if [ "$result" -eq 0 ]; then + echo "PASS: $label" + else + echo "FAIL: $label" + FAILURES=$((FAILURES + 1)) + fi +} + +# Asserts that the given TCP ports are not already in use. +# Exits immediately if any port is occupied. +# +# assert_ports_free <port> [port...] +assert_ports_free() { + for port in "$@"; do + if ss -tlnp 2>/dev/null | grep -q ":${port} "; then + echo "FAIL: port $port is already in use" + exit 1 + fi + done +} |
