Skip to content

Commit 6eecf36

Browse files
committed
test: add run-all MCP bootstrap coverage
Signed-off-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
1 parent 9cf7188 commit 6eecf36

3 files changed

Lines changed: 172 additions & 10 deletions

File tree

docker/run-all.bats

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
}

docker/run-all.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ GRAFANA_CREDS="${GF_SECURITY_ADMIN_USER:-admin}:${GF_SECURITY_ADMIN_PASSWORD:-ad
153153
GRAFANA_URL="${GRAFANA_URL:-http://127.0.0.1:3000}"
154154
GRAFANA_PUBLIC_URL="${GRAFANA_PUBLIC_URL:-http://localhost:3000}"
155155
TEMPO_URL="${TEMPO_URL:-http://localhost:3200}"
156+
LGTM_CONFIG_DIR="${LGTM_CONFIG_DIR:-/etc/lgtm}"
157+
GRAFANA_SA_TOKEN_FILE="${GRAFANA_SA_TOKEN_FILE:-/tmp/grafana-sa-token}"
156158
SA_NAME="ai-tools"
157159
SA_TOKEN_NAME="ai-tools-token"
158160
GRAFANA_SA_URL="${GRAFANA_URL}/api/serviceaccounts"
@@ -187,12 +189,12 @@ fi
187189

188190
(
189191
umask 077
190-
mkdir -p /etc/lgtm
192+
mkdir -p "${LGTM_CONFIG_DIR}"
191193
if [ -n "$SA_TOKEN" ]; then
192-
echo "${SA_TOKEN}" >/tmp/grafana-sa-token
194+
echo "${SA_TOKEN}" >"${GRAFANA_SA_TOKEN_FILE}"
193195
fi
194196
if [ -n "$SA_TOKEN" ] && [[ ${TEMPO_MCP_ENABLED} == "true" ]]; then
195-
cat >/etc/lgtm/mcp.json <<-MCPEOF
197+
cat >"${LGTM_CONFIG_DIR}/mcp.json" <<-MCPEOF
196198
{
197199
"mcpServers": {
198200
"grafana": {
@@ -210,7 +212,7 @@ fi
210212
}
211213
MCPEOF
212214
elif [ -n "$SA_TOKEN" ]; then
213-
cat >/etc/lgtm/mcp.json <<-MCPEOF
215+
cat >"${LGTM_CONFIG_DIR}/mcp.json" <<-MCPEOF
214216
{
215217
"mcpServers": {
216218
"grafana": {
@@ -225,7 +227,7 @@ fi
225227
}
226228
MCPEOF
227229
elif [[ ${TEMPO_MCP_ENABLED} == "true" ]]; then
228-
cat >/etc/lgtm/mcp.json <<-MCPEOF
230+
cat >"${LGTM_CONFIG_DIR}/mcp.json" <<-MCPEOF
229231
{
230232
"mcpServers": {
231233
"tempo": {
@@ -235,7 +237,7 @@ fi
235237
}
236238
MCPEOF
237239
else
238-
cat >/etc/lgtm/mcp.json <<-MCPEOF
240+
cat >"${LGTM_CONFIG_DIR}/mcp.json" <<-MCPEOF
239241
{
240242
"mcpServers": {}
241243
}
@@ -250,7 +252,7 @@ fi
250252
if [[ ${TEMPO_MCP_ENABLED} == "true" ]]; then
251253
echo "claude mcp add --transport http tempo \"${TEMPO_URL}/api/mcp\""
252254
fi
253-
} >/etc/lgtm/claude-mcp-setup.sh
255+
} >"${LGTM_CONFIG_DIR}/claude-mcp-setup.sh"
254256
)
255257

256258
echo ""
@@ -265,8 +267,8 @@ if [ -n "$SA_TOKEN" ]; then
265267
else
266268
echo " Grafana MCP: server unavailable; could not create service account token"
267269
fi
268-
echo " Claude Code: bash <($EXEC cat /etc/lgtm/claude-mcp-setup.sh)"
269-
echo " Other tools: $EXEC cat /etc/lgtm/mcp.json"
270+
printf ' Claude Code: bash <(%s cat %q)\n' "$EXEC" "${LGTM_CONFIG_DIR}/claude-mcp-setup.sh"
271+
printf ' Other tools: %s cat %q\n' "$EXEC" "${LGTM_CONFIG_DIR}/mcp.json"
270272
docs_ref="main"
271273
[[ -n "${LGTM_VERSION}" ]] && docs_ref="v${LGTM_VERSION}"
272274
echo " Docs: https://github.com/grafana/docker-otel-lgtm/blob/${docs_ref}/docs/mcp-integration.md"

mise.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ FLINT_CONFIG_DIR = ".github/config"
3232

3333
[tasks."test:unit"]
3434
description = "Run unit tests"
35-
run = "bats docker/run-otelcol.bats"
35+
run = "bats docker/run-otelcol.bats docker/run-all.bats"
3636

3737
[tasks."ci"]
3838
description = "Run all checks (lint + unit tests)"

0 commit comments

Comments
 (0)