-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmcpgrafana_test.go
75 lines (64 loc) · 2.59 KB
/
mcpgrafana_test.go
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
//go:build unit
// +build unit
package mcpgrafana
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExtractIncidentClientFromEnv(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com/")
ctx := ExtractIncidentClientFromEnv(context.Background())
client := IncidentClientFromContext(ctx)
require.NotNil(t, client)
assert.Equal(t, "http://my-test-url.grafana.com/api/plugins/grafana-incident-app/resources/api/v1/", client.RemoteHost)
}
func TestExtractGrafanaInfoFromHeaders(t *testing.T) {
t.Run("no headers, no env", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
url := GrafanaURLFromContext(ctx)
assert.Equal(t, defaultGrafanaURL, url)
apiKey := GrafanaAPIKeyFromContext(ctx)
assert.Equal(t, "", apiKey)
})
t.Run("no headers, with env", func(t *testing.T) {
t.Setenv("GRAFANA_URL", "http://my-test-url.grafana.com")
t.Setenv("GRAFANA_API_KEY", "my-test-api-key")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
url := GrafanaURLFromContext(ctx)
assert.Equal(t, "http://my-test-url.grafana.com", url)
apiKey := GrafanaAPIKeyFromContext(ctx)
assert.Equal(t, "my-test-api-key", apiKey)
})
t.Run("with headers, no env", func(t *testing.T) {
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
req.Header.Set(grafanaURLHeader, "http://my-test-url.grafana.com")
req.Header.Set(grafanaAPIKeyHeader, "my-test-api-key")
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
url := GrafanaURLFromContext(ctx)
assert.Equal(t, "http://my-test-url.grafana.com", url)
apiKey := GrafanaAPIKeyFromContext(ctx)
assert.Equal(t, "my-test-api-key", apiKey)
})
t.Run("with headers, with env", func(t *testing.T) {
// Env vars should be ignored if headers are present.
t.Setenv("GRAFANA_URL", "will-not-be-used")
t.Setenv("GRAFANA_API_KEY", "will-not-be-used")
req, err := http.NewRequest("GET", "http://example.com", nil)
require.NoError(t, err)
req.Header.Set(grafanaURLHeader, "http://my-test-url.grafana.com")
req.Header.Set(grafanaAPIKeyHeader, "my-test-api-key")
ctx := ExtractGrafanaInfoFromHeaders(context.Background(), req)
url := GrafanaURLFromContext(ctx)
assert.Equal(t, "http://my-test-url.grafana.com", url)
apiKey := GrafanaAPIKeyFromContext(ctx)
assert.Equal(t, "my-test-api-key", apiKey)
})
}