Skip to content

eBPF-based security platform

testing: add deterministic keepalive and JSON-lines coverage

John Fastabendec8e562b59b1
diff --git a/testing/otel/t0018-status-otel.sh b/testing/otel/t0018-status-otel.sh
index 4390bbc9..de55bd34 100644
--- a/testing/otel/t0018-status-otel.sh
+++ b/testing/otel/t0018-status-otel.sh
@@ -6,6 +6,7 @@ cd "$(dirname "$0")"
. ../test-lib.sh
S=../../shirudo
+H=../helper/test-otel-map
test_expect_success 'status --otel sends a structured keepalive log' '
work=$(mktemp -d)
@@ -53,6 +54,33 @@ assert "shirudo.version_id" in attrs
PY
'
+test_expect_success 'keepalive status severities and structured body are deterministic' '
+ for status in ok degraded init-pending lsm-off tampered
+ do
+ rm -f otel.port otel.body otel.meta
+ python3 ../helper/otel-mock.py success otel.port otel.body otel.meta &
+ server=$!
+ i=0; while test ! -s otel.port && test "$i" -lt 100; do i=$((i+1)); sleep .02; done
+ port=$(cat otel.port) && "$H" send-status "http://127.0.0.1:$port" "$status" && wait "$server" &&
+ python3 - "$status" otel.body <<'\''PY'\'' || return 1
+import json, sys
+status = sys.argv[1]
+record = json.load(open(sys.argv[2]))["resourceLogs"][0]["scopeLogs"][0]["logRecords"][0]
+expected = {"ok": (9, "INFO"), "degraded": (13, "WARN"),
+ "init-pending": (13, "WARN"), "lsm-off": (17, "ERROR"),
+ "tampered": (17, "ERROR")}[status]
+assert (record["severityNumber"], record["severityText"]) == expected
+assert record["eventName"] == "shirudo.keepalive"
+body = {item["key"]: item["value"] for item in record["body"]["kvlistValue"]["values"]}
+assert body["status"]["stringValue"] == status
+assert body["schema"]["intValue"] == "1"
+attrs = {item["key"]: item["value"] for item in record["attributes"]}
+assert attrs["shirudo.status"]["stringValue"] == status
+assert attrs["shirudo.tamper_attempts"]["intValue"] == "2"
+PY
+ done
+'
+
test_expect_success 'status --json --otel keeps JSON output available' '
work=$(mktemp -d)
python3 ../helper/otel-mock.py success "$work/port" "$work/body" \
diff --git a/testing/otel/t1115-map-export-all.sh b/testing/otel/t1115-map-export-all.sh
index c101893b..03b52a85 100644
--- a/testing/otel/t1115-map-export-all.sh
+++ b/testing/otel/t1115-map-export-all.sh
@@ -69,13 +69,33 @@ test_expect_success '--all exports one detailed JSON record per loaded map' '
python3 "$WORK/check-maps.py" "$WORK/maps.ndjson" "$NAME"
'
+test_expect_success 'configured JSON-lines output appends one valid record per map' '
+ printf "map_export.json_path = %s\n" "$WORK/configured.ndjson" >"$CONFIG" &&
+ "$SHIRUDO_BIN" upatch map --all &&
+ "$SHIRUDO_BIN" upatch map --all &&
+ python3 - "$WORK/configured.ndjson" <<'\''PY'\''
+import json, sys
+records = [json.loads(line) for line in open(sys.argv[1], encoding="utf-8")]
+assert len(records) == 4
+assert sum(isinstance(record, dict) and record.get("map") == "first"
+ for record in records) == 2
+assert sum(isinstance(record, list) for record in records) == 2
+PY
+'
+
+test_expect_success 'configured JSON-lines output reports an unwritable destination' '
+ printf "map_export.json_path = %s\n" "$WORK/missing/maps.ndjson" >"$CONFIG" &&
+ ! "$SHIRUDO_BIN" upatch map --all >/dev/null 2>&1
+'
+
test_expect_success '--all auto exports mixed metric and log maps' '
python3 "$WORK/otel.py" "$WORK/otel.port" "$WORK/otel.meta" &
SERVER=$!;
i=0; while test ! -s "$WORK/otel.port" && test "$i" -lt 100; do i=$((i+1)); sleep .02; done;
test -s "$WORK/otel.port" &&
port=$(cat "$WORK/otel.port") &&
- printf "%s\n" "map_export.tenant_id = tenant-config-0000" >"$CONFIG" &&
+ printf "%s\n" "map_export.tenant_id = tenant-config-0000" \
+ "map_export.otel_endpoint = http://127.0.0.1:1/ignored" >"$CONFIG" &&
"$SHIRUDO_BIN" upatch map --all --otel --otel-endpoint="http://127.0.0.1:$port/base" &&
"$SHIRUDO_BIN" upatch map --all --otel --tenant-id=tenant-uuid-1234 --otel-endpoint="http://127.0.0.1:$port/base";
kill "$SERVER" >/dev/null 2>&1 || true; wait "$SERVER" 2>/dev/null || true; SERVER="";