|
1 | 1 | package plugin |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "errors" |
6 | 7 | "fmt" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/ClickHouse/clickhouse-go/v2" |
| 11 | + "github.com/grafana/grafana-plugin-sdk-go/backend" |
10 | 12 | "github.com/grafana/grafana-plugin-sdk-go/data" |
11 | 13 | "github.com/stretchr/testify/assert" |
12 | 14 | ) |
@@ -317,3 +319,120 @@ func TestContainsClickHouseException(t *testing.T) { |
317 | 319 | assert.True(t, result) |
318 | 320 | }) |
319 | 321 | } |
| 322 | + |
| 323 | +func TestMutateQueryData(t *testing.T) { |
| 324 | + h := &Clickhouse{} |
| 325 | + |
| 326 | + t.Run("extracts dashboard and panel headers", func(t *testing.T) { |
| 327 | + ctx := context.Background() |
| 328 | + req := &backend.QueryDataRequest{ |
| 329 | + Headers: map[string]string{ |
| 330 | + "http_X-Dashboard-Uid": "dash-abc123", |
| 331 | + "http_X-Panel-Id": "42", |
| 332 | + }, |
| 333 | + } |
| 334 | + |
| 335 | + newCtx, _ := h.MutateQueryData(ctx, req) |
| 336 | + |
| 337 | + gh, ok := newCtx.Value(grafanaHeadersKey).(grafanaHeaders) |
| 338 | + assert.True(t, ok) |
| 339 | + assert.Equal(t, "dash-abc123", gh.DashboardUID) |
| 340 | + assert.Equal(t, "42", gh.PanelID) |
| 341 | + }) |
| 342 | + |
| 343 | + t.Run("no headers set leaves context unchanged", func(t *testing.T) { |
| 344 | + ctx := context.Background() |
| 345 | + req := &backend.QueryDataRequest{ |
| 346 | + Headers: map[string]string{}, |
| 347 | + } |
| 348 | + |
| 349 | + newCtx, _ := h.MutateQueryData(ctx, req) |
| 350 | + |
| 351 | + _, ok := newCtx.Value(grafanaHeadersKey).(grafanaHeaders) |
| 352 | + assert.False(t, ok) |
| 353 | + }) |
| 354 | + |
| 355 | + t.Run("only dashboard header set", func(t *testing.T) { |
| 356 | + ctx := context.Background() |
| 357 | + req := &backend.QueryDataRequest{ |
| 358 | + Headers: map[string]string{ |
| 359 | + "http_X-Dashboard-Uid": "dash-only", |
| 360 | + }, |
| 361 | + } |
| 362 | + |
| 363 | + newCtx, _ := h.MutateQueryData(ctx, req) |
| 364 | + |
| 365 | + gh, ok := newCtx.Value(grafanaHeadersKey).(grafanaHeaders) |
| 366 | + assert.True(t, ok) |
| 367 | + assert.Equal(t, "dash-only", gh.DashboardUID) |
| 368 | + assert.Equal(t, "", gh.PanelID) |
| 369 | + }) |
| 370 | + |
| 371 | + t.Run("only panel header set", func(t *testing.T) { |
| 372 | + ctx := context.Background() |
| 373 | + req := &backend.QueryDataRequest{ |
| 374 | + Headers: map[string]string{ |
| 375 | + "http_X-Panel-Id": "99", |
| 376 | + }, |
| 377 | + } |
| 378 | + |
| 379 | + newCtx, _ := h.MutateQueryData(ctx, req) |
| 380 | + |
| 381 | + gh, ok := newCtx.Value(grafanaHeadersKey).(grafanaHeaders) |
| 382 | + assert.True(t, ok) |
| 383 | + assert.Equal(t, "", gh.DashboardUID) |
| 384 | + assert.Equal(t, "99", gh.PanelID) |
| 385 | + }) |
| 386 | + |
| 387 | + t.Run("nil headers does not panic", func(t *testing.T) { |
| 388 | + ctx := context.Background() |
| 389 | + req := &backend.QueryDataRequest{} |
| 390 | + |
| 391 | + newCtx, newReq := h.MutateQueryData(ctx, req) |
| 392 | + |
| 393 | + assert.NotNil(t, newCtx) |
| 394 | + assert.NotNil(t, newReq) |
| 395 | + }) |
| 396 | +} |
| 397 | + |
| 398 | +func TestMutateQuery_GrafanaMetadata(t *testing.T) { |
| 399 | + h := &Clickhouse{} |
| 400 | + |
| 401 | + t.Run("includes dashboard and panel from context", func(t *testing.T) { |
| 402 | + ctx := context.WithValue(context.Background(), grafanaHeadersKey, grafanaHeaders{ |
| 403 | + DashboardUID: "my-dashboard", |
| 404 | + PanelID: "7", |
| 405 | + }) |
| 406 | + |
| 407 | + newCtx, _ := h.MutateQuery(ctx, backend.DataQuery{ |
| 408 | + JSON: []byte(`{}`), |
| 409 | + }) |
| 410 | + |
| 411 | + // Verify the clickhouse context was set by checking the context is different |
| 412 | + assert.NotEqual(t, ctx, newCtx) |
| 413 | + }) |
| 414 | + |
| 415 | + t.Run("no grafana headers in context still works", func(t *testing.T) { |
| 416 | + ctx := context.Background() |
| 417 | + |
| 418 | + newCtx, _ := h.MutateQuery(ctx, backend.DataQuery{ |
| 419 | + JSON: []byte(`{}`), |
| 420 | + }) |
| 421 | + |
| 422 | + // Without user or headers, context should be unchanged (no comments to add) |
| 423 | + assert.Equal(t, ctx, newCtx) |
| 424 | + }) |
| 425 | + |
| 426 | + t.Run("handles invalid JSON gracefully", func(t *testing.T) { |
| 427 | + ctx := context.WithValue(context.Background(), grafanaHeadersKey, grafanaHeaders{ |
| 428 | + DashboardUID: "dash1", |
| 429 | + }) |
| 430 | + |
| 431 | + newCtx, _ := h.MutateQuery(ctx, backend.DataQuery{ |
| 432 | + JSON: []byte(`invalid json`), |
| 433 | + }) |
| 434 | + |
| 435 | + // Should still set clickhouse context with dashboard info even if JSON is bad |
| 436 | + assert.NotEqual(t, ctx, newCtx) |
| 437 | + }) |
| 438 | +} |
0 commit comments