11#! /usr/bin/env bats
2+ # shellcheck disable=SC2030,SC2031
23bats_require_minimum_version 1.5.0
34
45setup () {
@@ -15,16 +16,17 @@ setup() {
1516 run-prometheus.sh \
1617 run-tempo.sh \
1718 run-pyroscope.sh; do
18- cat > " $TESTDIR /$script " << 'EOF '
19+ cat > " $TESTDIR /$script " << 'SCRIPT '
1920#!/usr/bin/env bash
2021sleep 60
21- EOF
22+ SCRIPT
2223 chmod +x " $TESTDIR /$script "
2324 done
2425
25- cat > " $TESTDIR /curl" << 'EOF '
26+ cat > " $TESTDIR /curl" << 'SCRIPT '
2627#!/usr/bin/env bash
2728args="$*"
29+ mode="${STUB_SA_MODE:-success}"
2830
2931if [[ "$args" == *"/ready"* ||
3032 "$args" == *"/api/health"* ||
@@ -33,23 +35,45 @@ if [[ "$args" == *"/ready"* ||
3335 exit 0
3436fi
3537
38+ if [[ "$args" == *"/api/serviceaccounts/1/tokens"* && "$args" == *"-X DELETE"* ]]; then
39+ printf '{}'
40+ exit 0
41+ fi
42+
3643if [[ "$args" == *"/api/serviceaccounts/1/tokens"* && "$args" == *"-d"* ]]; then
37- printf '{"key":"token123"}'
44+ if [[ "$mode" == "success" || "$mode" == "with_existing_token" ]]; then
45+ printf '{"key":"token123"}'
46+ fi
3847 exit 0
3948fi
4049
4150if [[ "$args" == *"/api/serviceaccounts/1/tokens"* ]]; then
42- printf '[]'
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
4365 exit 0
4466fi
4567
4668if [[ "$args" == *"/api/serviceaccounts"* && "$args" == *"-d"* ]]; then
47- printf '{"id":1}'
69+ if [[ "$mode" == "success" || "$mode" == "with_existing_token" ]]; then
70+ printf '{"id":1}'
71+ fi
4872 exit 0
4973fi
5074
5175printf '{}'
52- EOF
76+ SCRIPT
5377 chmod +x " $TESTDIR /curl"
5478}
5579
@@ -58,7 +82,7 @@ teardown() {
5882}
5983
6084run_run_all () {
61- local version=$1
85+ local version=${1 :- latest}
6286 cd " $TESTDIR " || return 1
6387 PATH=" $TESTDIR :$PATH " \
6488 LGTM_CONFIG_DIR=" $CONFIGDIR " \
@@ -71,7 +95,7 @@ run_run_all() {
7195@test " docs URL uses main for latest" {
7296 local expected=" https://github.com/grafana/docker-otel-lgtm/blob/main/docs/mcp-integration.md"
7397 local expected_line=" Docs: $expected "
74- run run_run_all " latest"
98+ run run_run_all latest
7599 [[ " $output " == * " $expected_line " * ]]
76100 [[ " $output " != * " /blob/vlatest/" * ]]
77101}
@@ -83,19 +107,18 @@ run_run_all() {
83107 [[ " $output " == * " $expected_line " * ]]
84108}
85109
86-
87110@test " docs URL uses main for main tag" {
88111 local expected=" https://github.com/grafana/docker-otel-lgtm/blob/main/docs/mcp-integration.md"
89112 local expected_line=" Docs: $expected "
90- run run_run_all " main"
113+ run run_run_all main
91114 [[ " $output " == * " $expected_line " * ]]
92115 [[ " $output " != * " /blob/vmain/" * ]]
93116}
94117
95118@test " printed MCP commands escape configurable paths" {
96119 local configdir=" $TESTDIR /etc/lgtm with spaces"
97120 local escaped_configdir=${configdir// / \\ }
98- CONFIGDIR=" $configdir " run run_run_all " latest"
121+ CONFIGDIR=" $configdir " run run_run_all latest
99122 [[ " $output " == * " bash <(docker exec lgtm cat ${escaped_configdir} /claude-mcp-setup.sh)" * ]]
100123 [[ " $output " == * " docker exec lgtm cat ${escaped_configdir} /mcp.json" * ]]
101124}
@@ -104,21 +127,24 @@ run_run_all() {
104127 local expected
105128 expected=" https://github.com/grafana/docker-otel-lgtm/blob/v1.2.3-test/docs/mcp-integration.md"
106129 local expected_line=" Docs: $expected "
107- run run_run_all " 1.2.3-test"
130+ run run_run_all 1.2.3-test
108131 [[ " $output " == * " $expected_line " * ]]
109132}
110133
111134@test " docs URL does not double-prefix version that already starts with v" {
112135 local expected
113136 expected=" https://github.com/grafana/docker-otel-lgtm/blob/v1.2.3-test/docs/mcp-integration.md"
114137 local expected_line=" Docs: $expected "
115- run run_run_all " v1.2.3-test"
138+ run run_run_all v1.2.3-test
116139 [[ " $output " == * " $expected_line " * ]]
117140 [[ " $output " != * " /blob/vv1.2.3-test/" * ]]
118141}
119142
120143@test " MCP bootstrap writes helper artifacts with expected contents" {
121- run run_run_all " latest"
144+ export TEMPO_EXTRA_ARGS=" --query-frontend.mcp-server.enabled=true"
145+ export STUB_SA_MODE=success
146+
147+ run run_run_all latest
122148 [ -f " $CONFIGDIR /mcp.json" ]
123149 [ -f " $CONFIGDIR /claude-mcp-setup.sh" ]
124150 [ -f " $TOKENFILE " ]
@@ -134,12 +160,77 @@ run_run_all() {
134160 grep -Fq \
135161 ' claude mcp add grafana -e "GRAFANA_URL=http://localhost:3000"' \
136162 " $CONFIGDIR /claude-mcp-setup.sh"
163+ grep -Fq ' GRAFANA_SERVICE_ACCOUNT_TOKEN=token123' " $CONFIGDIR /claude-mcp-setup.sh"
137164 grep -Fq \
138- ' GRAFANA_SERVICE_ACCOUNT_TOKEN=token123 ' \
165+ ' claude mcp add --transport http tempo "http://localhost:3200/api/mcp" ' \
139166 " $CONFIGDIR /claude-mcp-setup.sh"
167+
168+ grep -Fqx ' token123' " $TOKENFILE "
169+ }
170+
171+ @test " enabled tempo with service account writes both MCP servers" {
172+ export TEMPO_EXTRA_ARGS=" --query-frontend.mcp-server.enabled=true"
173+ export STUB_SA_MODE=success
174+
175+ run run_run_all latest
176+ [[ " $output " == * " Tempo MCP: server enabled at http://localhost:3200/api/mcp" * ]]
177+ [[ " $output " == * " Grafana MCP: server enabled with service account token" * ]]
178+ [[ " $output " == * " - 3200: Tempo endpoint (MCP at http://localhost:3200/api/mcp)" * ]]
179+
180+ grep -Fq ' "grafana"' " $CONFIGDIR /mcp.json"
181+ grep -Fq ' "tempo"' " $CONFIGDIR /mcp.json"
182+ grep -Fq ' GRAFANA_SERVICE_ACCOUNT_TOKEN": "token123"' " $CONFIGDIR /mcp.json"
183+ grep -Fq ' claude mcp add grafana' " $CONFIGDIR /claude-mcp-setup.sh"
140184 grep -Fq \
141185 ' claude mcp add --transport http tempo "http://localhost:3200/api/mcp"' \
142186 " $CONFIGDIR /claude-mcp-setup.sh"
143-
144187 grep -Fqx ' token123' " $TOKENFILE "
145188}
189+
190+ @test " disabled tempo with service account writes grafana-only MCP config" {
191+ unset TEMPO_EXTRA_ARGS
192+ export STUB_SA_MODE=success
193+
194+ run run_run_all latest
195+ [[ " $output " == * " Tempo MCP: server disabled;" * ]]
196+ [[ " $output " == * " TEMPO_EXTRA_ARGS=--query-frontend.mcp-server.enabled=true" * ]]
197+ [[ " $output " == * " Grafana MCP: server enabled with service account token" * ]]
198+ [[ " $output " == * " - 3200: Tempo endpoint" * ]]
199+ [[ " $output " != * " - 3200: Tempo endpoint (MCP at http://localhost:3200/api/mcp)" * ]]
200+
201+ grep -Fq ' "grafana"' " $CONFIGDIR /mcp.json"
202+ run ! grep -Fq ' "tempo"' " $CONFIGDIR /mcp.json"
203+ grep -Fq ' claude mcp add grafana' " $CONFIGDIR /claude-mcp-setup.sh"
204+ run ! grep -Fq ' claude mcp add --transport http tempo' " $CONFIGDIR /claude-mcp-setup.sh"
205+ }
206+
207+ @test " enabled tempo without service account writes tempo-only MCP config" {
208+ export TEMPO_EXTRA_ARGS=" --query-frontend.mcp-server.enabled=true"
209+ export STUB_SA_MODE=missing
210+
211+ run run_run_all latest
212+ [[ " $output " == * " Tempo MCP: server enabled at http://localhost:3200/api/mcp" * ]]
213+ [[ " $output " == * " Grafana MCP: server unavailable; could not create service account token" * ]]
214+
215+ run ! grep -Fq ' "grafana"' " $CONFIGDIR /mcp.json"
216+ grep -Fq ' "tempo"' " $CONFIGDIR /mcp.json"
217+ run ! grep -Fq ' claude mcp add grafana' " $CONFIGDIR /claude-mcp-setup.sh"
218+ grep -Fq \
219+ ' claude mcp add --transport http tempo "http://localhost:3200/api/mcp"' \
220+ " $CONFIGDIR /claude-mcp-setup.sh"
221+ [ ! -f " $TOKENFILE " ]
222+ }
223+
224+ @test " disabled tempo without service account writes empty MCP config" {
225+ unset TEMPO_EXTRA_ARGS
226+ export STUB_SA_MODE=missing
227+
228+ run run_run_all latest
229+ [[ " $output " == * " Tempo MCP: server disabled;" * ]]
230+ [[ " $output " == * " TEMPO_EXTRA_ARGS=--query-frontend.mcp-server.enabled=true" * ]]
231+ [[ " $output " == * " Grafana MCP: server unavailable; could not create service account token" * ]]
232+
233+ grep -Fq ' "mcpServers": {}' " $CONFIGDIR /mcp.json"
234+ run ! grep -Fq ' claude mcp add ' " $CONFIGDIR /claude-mcp-setup.sh"
235+ [ ! -f " $TOKENFILE " ]
236+ }
0 commit comments