-
Notifications
You must be signed in to change notification settings - Fork 410
Expand file tree
/
Copy pathdatasources_test.go
More file actions
76 lines (67 loc) · 2.14 KB
/
Copy pathdatasources_test.go
File metadata and controls
76 lines (67 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Requires a Grafana instance running on localhost:3000,
// with a Prometheus datasource provisioned.
// Run with `go test -tags integration`.
//go:build integration
package tools
import (
"context"
"fmt"
"net/url"
"os"
"testing"
"github.com/go-openapi/strfmt"
"github.com/grafana/grafana-openapi-client-go/client"
mcpgrafana "github.com/grafana/mcp-grafana"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newTestContext creates a new context with the Grafana URL and API key
// from the environment variables GRAFANA_URL and GRAFANA_API_KEY.
// TODO: move this to a shared file.
func newTestContext() context.Context {
cfg := client.DefaultTransportConfig()
cfg.Host = "localhost:3000"
cfg.Schemes = []string{"http"}
// Extract transport config from env vars, and set it on the context.
if u, ok := os.LookupEnv("GRAFANA_URL"); ok {
url, err := url.Parse(u)
if err != nil {
panic(fmt.Errorf("invalid %s: %w", "GRAFANA_URL", err))
}
cfg.Host = url.Host
// The Grafana client will always prefer HTTPS even if the URL is HTTP,
// so we need to limit the schemes to HTTP if the URL is HTTP.
if url.Scheme == "http" {
cfg.Schemes = []string{"http"}
}
}
if apiKey := os.Getenv("GRAFANA_API_KEY"); apiKey != "" {
cfg.APIKey = apiKey
}
client := client.NewHTTPClientWithConfig(strfmt.Default, cfg)
return mcpgrafana.WithGrafanaClient(context.Background(), client)
}
func TestDatasourcesTools(t *testing.T) {
t.Run("list datasources", func(t *testing.T) {
ctx := newTestContext()
result, err := listDatasources(ctx, ListDatasourcesParams{})
require.NoError(t, err)
assert.Greater(t, len(result), 0)
})
t.Run("get datasource by uid", func(t *testing.T) {
ctx := newTestContext()
result, err := getDatasourceByUID(ctx, GetDatasourceByUIDParams{
UID: "prometheus",
})
require.NoError(t, err)
assert.Equal(t, "Prometheus", result.Name)
})
t.Run("get datasource by name", func(t *testing.T) {
ctx := newTestContext()
result, err := getDatasourceByName(ctx, GetDatasourceByNameParams{
Name: "Prometheus",
})
require.NoError(t, err)
assert.Equal(t, "Prometheus", result.Name)
})
}