|
| 1 | +#!/usr/bin/env bats |
| 2 | +# shellcheck disable=SC2030,SC2031 |
| 3 | +bats_require_minimum_version 1.5.0 |
| 4 | + |
| 5 | +setup() { |
| 6 | + TESTDIR=$(mktemp -d) |
| 7 | + CONFIGDIR="$TESTDIR/etc/lgtm" |
| 8 | + TOKENFILE="$TESTDIR/tmp/grafana-sa-token" |
| 9 | + mkdir -p "$(dirname "$TOKENFILE")" |
| 10 | + cp "$BATS_TEST_DIRNAME/run-all.sh" "$TESTDIR/" |
| 11 | + |
| 12 | + for script in \ |
| 13 | + run-grafana.sh \ |
| 14 | + run-loki.sh \ |
| 15 | + run-otelcol.sh \ |
| 16 | + run-prometheus.sh \ |
| 17 | + run-tempo.sh \ |
| 18 | + run-pyroscope.sh; do |
| 19 | + cat >"$TESTDIR/$script" <<'SCRIPT' |
| 20 | +#!/usr/bin/env bash |
| 21 | +sleep 60 |
| 22 | +SCRIPT |
| 23 | + chmod +x "$TESTDIR/$script" |
| 24 | + done |
| 25 | + |
| 26 | + cat >"$TESTDIR/curl" <<'SCRIPT' |
| 27 | +#!/usr/bin/env bash |
| 28 | +args="$*" |
| 29 | +mode="${STUB_SA_MODE:-success}" |
| 30 | +
|
| 31 | +if [[ "$args" == *"/ready"* || |
| 32 | + "$args" == *"/api/health"* || |
| 33 | + "$args" == *"/api/v1/status/runtimeinfo"* ]]; then |
| 34 | + printf '200' |
| 35 | + exit 0 |
| 36 | +fi |
| 37 | +
|
| 38 | +if [[ "$args" == *"/api/serviceaccounts/1/tokens"* && "$args" == *"-X DELETE"* ]]; then |
| 39 | + printf '{}' |
| 40 | + exit 0 |
| 41 | +fi |
| 42 | +
|
| 43 | +if [[ "$args" == *"/api/serviceaccounts/1/tokens"* && "$args" == *"-d"* ]]; then |
| 44 | + if [[ "$mode" == "success" || "$mode" == "with-existing-token" ]]; then |
| 45 | + printf '{"key":"token123"}' |
| 46 | + fi |
| 47 | + exit 0 |
| 48 | +fi |
| 49 | +
|
| 50 | +if [[ "$args" == *"/api/serviceaccounts/1/tokens"* ]]; then |
| 51 | + if [[ "$mode" == "with-existing-token" ]]; then |
| 52 | + printf '[{"id":99,"name":"ai-tools-token"}]' |
| 53 | + else |
| 54 | + printf '[]' |
| 55 | + fi |
| 56 | + exit 0 |
| 57 | +fi |
| 58 | +
|
| 59 | +if [[ "$args" == *"/api/serviceaccounts/search?query=ai-tools"* ]]; then |
| 60 | + if [[ "$mode" == "lookup-existing" ]]; then |
| 61 | + printf '{"serviceAccounts":[{"id":1,"name":"ai-tools"}]}' |
| 62 | + else |
| 63 | + printf '{}' |
| 64 | + fi |
| 65 | + exit 0 |
| 66 | +fi |
| 67 | +
|
| 68 | +if [[ "$args" == *"/api/serviceaccounts"* && "$args" == *"-d"* ]]; then |
| 69 | + if [[ "$mode" == "success" || "$mode" == "with-existing-token" ]]; then |
| 70 | + printf '{"id":1}' |
| 71 | + fi |
| 72 | + exit 0 |
| 73 | +fi |
| 74 | +
|
| 75 | +printf '{}' |
| 76 | +SCRIPT |
| 77 | + chmod +x "$TESTDIR/curl" |
| 78 | +} |
| 79 | + |
| 80 | +teardown() { |
| 81 | + rm -rf "$TESTDIR" |
| 82 | +} |
| 83 | + |
| 84 | +run_run_all() { |
| 85 | + cd "$TESTDIR" || return 1 |
| 86 | + PATH="$TESTDIR:$PATH" \ |
| 87 | + LGTM_CONFIG_DIR="$CONFIGDIR" \ |
| 88 | + GRAFANA_SA_TOKEN_FILE="$TOKENFILE" \ |
| 89 | + LGTM_VERSION="latest" \ |
| 90 | + CONTAINER_RUNTIME=docker \ |
| 91 | + TIMEOUT_SECONDS=3 \ |
| 92 | + timeout 3s bash ./run-all.sh |
| 93 | +} |
| 94 | + |
| 95 | +@test "enabled tempo with service account writes both MCP servers" { |
| 96 | + export TEMPO_EXTRA_ARGS="--query-frontend.mcp-server.enabled=true" |
| 97 | + export STUB_SA_MODE=success |
| 98 | + |
| 99 | + run run_run_all |
| 100 | + [[ "$output" == *"Tempo MCP: server enabled at http://localhost:3200/api/mcp"* ]] |
| 101 | + [[ "$output" == *"Grafana MCP: server enabled with service account token"* ]] |
| 102 | + [[ "$output" == *" - 3200: Tempo endpoint (MCP at http://localhost:3200/api/mcp)"* ]] |
| 103 | + |
| 104 | + grep -Fq '"grafana"' "$CONFIGDIR/mcp.json" |
| 105 | + grep -Fq '"tempo"' "$CONFIGDIR/mcp.json" |
| 106 | + grep -Fq 'GRAFANA_SERVICE_ACCOUNT_TOKEN": "token123"' "$CONFIGDIR/mcp.json" |
| 107 | + grep -Fq 'claude mcp add grafana' "$CONFIGDIR/claude-mcp-setup.sh" |
| 108 | + grep -Fq \ |
| 109 | + 'claude mcp add --transport http tempo "http://localhost:3200/api/mcp"' \ |
| 110 | + "$CONFIGDIR/claude-mcp-setup.sh" |
| 111 | + grep -Fqx 'token123' "$TOKENFILE" |
| 112 | +} |
| 113 | + |
| 114 | +@test "disabled tempo with service account writes grafana-only MCP config" { |
| 115 | + unset TEMPO_EXTRA_ARGS |
| 116 | + export STUB_SA_MODE=success |
| 117 | + |
| 118 | + run run_run_all |
| 119 | + [[ "$output" == *"Tempo MCP: server disabled;"* ]] |
| 120 | + [[ "$output" == *"TEMPO_EXTRA_ARGS=--query-frontend.mcp-server.enabled=true"* ]] |
| 121 | + [[ "$output" == *"Grafana MCP: server enabled with service account token"* ]] |
| 122 | + [[ "$output" == *" - 3200: Tempo endpoint"* ]] |
| 123 | + [[ "$output" != *" - 3200: Tempo endpoint (MCP at http://localhost:3200/api/mcp)"* ]] |
| 124 | + |
| 125 | + grep -Fq '"grafana"' "$CONFIGDIR/mcp.json" |
| 126 | + run ! grep -Fq '"tempo"' "$CONFIGDIR/mcp.json" |
| 127 | + grep -Fq 'claude mcp add grafana' "$CONFIGDIR/claude-mcp-setup.sh" |
| 128 | + run ! grep -Fq 'claude mcp add --transport http tempo' "$CONFIGDIR/claude-mcp-setup.sh" |
| 129 | +} |
| 130 | + |
| 131 | +@test "enabled tempo without service account writes tempo-only MCP config" { |
| 132 | + export TEMPO_EXTRA_ARGS="--query-frontend.mcp-server.enabled=true" |
| 133 | + export STUB_SA_MODE=missing |
| 134 | + |
| 135 | + run run_run_all |
| 136 | + [[ "$output" == *"Tempo MCP: server enabled at http://localhost:3200/api/mcp"* ]] |
| 137 | + [[ "$output" == *"Grafana MCP: server unavailable; could not create service account token"* ]] |
| 138 | + |
| 139 | + run ! grep -Fq '"grafana"' "$CONFIGDIR/mcp.json" |
| 140 | + grep -Fq '"tempo"' "$CONFIGDIR/mcp.json" |
| 141 | + run ! grep -Fq 'claude mcp add grafana' "$CONFIGDIR/claude-mcp-setup.sh" |
| 142 | + grep -Fq \ |
| 143 | + 'claude mcp add --transport http tempo "http://localhost:3200/api/mcp"' \ |
| 144 | + "$CONFIGDIR/claude-mcp-setup.sh" |
| 145 | + [ ! -f "$TOKENFILE" ] |
| 146 | +} |
| 147 | + |
| 148 | +@test "disabled tempo without service account writes empty MCP config" { |
| 149 | + unset TEMPO_EXTRA_ARGS |
| 150 | + export STUB_SA_MODE=missing |
| 151 | + |
| 152 | + run run_run_all |
| 153 | + [[ "$output" == *"Tempo MCP: server disabled;"* ]] |
| 154 | + [[ "$output" == *"TEMPO_EXTRA_ARGS=--query-frontend.mcp-server.enabled=true"* ]] |
| 155 | + [[ "$output" == *"Grafana MCP: server unavailable; could not create service account token"* ]] |
| 156 | + |
| 157 | + grep -Fq '"mcpServers": {}' "$CONFIGDIR/mcp.json" |
| 158 | + run ! grep -Fq 'claude mcp add ' "$CONFIGDIR/claude-mcp-setup.sh" |
| 159 | + [ ! -f "$TOKENFILE" ] |
| 160 | +} |
0 commit comments