|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Shared health checks and trace verification for E2E tests. |
| 3 | +# Expects these variables to be set before sourcing: |
| 4 | +# OPENSEARCH_USER, OPENSEARCH_PASSWORD, OPENSEARCH_PORT, |
| 5 | +# OPENSEARCH_DASHBOARDS_PORT, OTEL_COLLECTOR_PORT_HTTP, PROMETHEUS_PORT |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +OPENSEARCH_URL="https://localhost:${OPENSEARCH_PORT}" |
| 9 | +CURL_OPTS=(-s -k -u "${OPENSEARCH_USER}:${OPENSEARCH_PASSWORD}") |
| 10 | + |
| 11 | +run_checks() { |
| 12 | + echo "==> Checking OpenSearch cluster health..." |
| 13 | + health=$(curl "${CURL_OPTS[@]}" "$OPENSEARCH_URL/_cluster/health" | sed -n 's/.*"status":"\([^"]*\)".*/\1/p') |
| 14 | + if [[ "$health" == "red" ]]; then |
| 15 | + echo "FAIL: OpenSearch cluster health is red" |
| 16 | + exit 1 |
| 17 | + fi |
| 18 | + echo " OpenSearch cluster health: $health" |
| 19 | + |
| 20 | + echo "==> Checking OTel Collector is accepting OTLP..." |
| 21 | + otel_status=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${OTEL_COLLECTOR_PORT_HTTP}/v1/traces" \ |
| 22 | + -H "Content-Type: application/json" \ |
| 23 | + -d '{"resourceSpans":[]}') |
| 24 | + if [[ "$otel_status" != "200" ]]; then |
| 25 | + echo "FAIL: OTel Collector OTLP HTTP endpoint returned $otel_status" |
| 26 | + exit 1 |
| 27 | + fi |
| 28 | + echo " OTel Collector OTLP HTTP: OK" |
| 29 | + |
| 30 | + echo "==> Checking Prometheus is up..." |
| 31 | + prom_status=$(curl -s -o /dev/null -w "%{http_code}" "http://localhost:${PROMETHEUS_PORT}/-/healthy") |
| 32 | + if [[ "$prom_status" != "200" ]]; then |
| 33 | + echo "FAIL: Prometheus health check returned $prom_status" |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + echo " Prometheus: OK" |
| 37 | + |
| 38 | + echo "==> Checking OpenSearch Dashboards is up..." |
| 39 | + dashboards_status=$(curl -s -o /dev/null -w "%{http_code}" -u "${OPENSEARCH_USER}:${OPENSEARCH_PASSWORD}" \ |
| 40 | + "http://localhost:${OPENSEARCH_DASHBOARDS_PORT}/api/status") |
| 41 | + if [[ "$dashboards_status" != "200" ]]; then |
| 42 | + echo "FAIL: OpenSearch Dashboards returned $dashboards_status" |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + echo " OpenSearch Dashboards: OK" |
| 46 | + |
| 47 | + echo "==> Sending test trace through OTel Collector..." |
| 48 | + trace_response=$(curl -s -w "\n%{http_code}" "http://localhost:${OTEL_COLLECTOR_PORT_HTTP}/v1/traces" \ |
| 49 | + -H "Content-Type: application/json" \ |
| 50 | + -d '{ |
| 51 | + "resourceSpans": [{ |
| 52 | + "resource": {"attributes": [{"key": "service.name", "value": {"stringValue": "e2e-test"}}]}, |
| 53 | + "scopeSpans": [{ |
| 54 | + "spans": [{ |
| 55 | + "traceId": "5b8efff798038103d269b633813fc60c", |
| 56 | + "spanId": "eee19b7ec3c1b174", |
| 57 | + "name": "e2e-test-span", |
| 58 | + "kind": 1, |
| 59 | + "startTimeUnixNano": "1000000000", |
| 60 | + "endTimeUnixNano": "2000000000", |
| 61 | + "status": {} |
| 62 | + }] |
| 63 | + }] |
| 64 | + }] |
| 65 | + }') |
| 66 | + trace_status=$(echo "$trace_response" | tail -1) |
| 67 | + if [[ "$trace_status" != "200" ]]; then |
| 68 | + echo "FAIL: Sending test trace returned $trace_status" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + echo " Test trace sent: OK" |
| 72 | + |
| 73 | + echo "==> Verifying trace landed in OpenSearch..." |
| 74 | + TRACE_ID="5b8efff798038103d269b633813fc60c" |
| 75 | + MAX_RETRIES=90 |
| 76 | + for i in $(seq 1 "$MAX_RETRIES"); do |
| 77 | + hits=$(curl "${CURL_OPTS[@]}" "$OPENSEARCH_URL/*span*,*trace*/_search" \ |
| 78 | + -H "Content-Type: application/json" \ |
| 79 | + -d "{\"query\":{\"bool\":{\"should\":[{\"term\":{\"traceId\":\"$TRACE_ID\"}},{\"term\":{\"traceID\":\"$TRACE_ID\"}}]}}}" \ |
| 80 | + | sed -n 's/.*"total":{"value":\([0-9]*\).*/\1/p') |
| 81 | + if [[ "$hits" -gt 0 ]]; then |
| 82 | + echo " Trace found in OpenSearch after ${i}s" |
| 83 | + break |
| 84 | + fi |
| 85 | + if [[ "$i" -eq "$MAX_RETRIES" ]]; then |
| 86 | + echo "FAIL: Trace not found in OpenSearch after ${MAX_RETRIES}s" |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + sleep 1 |
| 90 | + done |
| 91 | + |
| 92 | + echo "" |
| 93 | + echo "==> All E2E checks passed!" |
| 94 | +} |
0 commit comments