-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathdatasources_test.go
More file actions
68 lines (58 loc) · 2.05 KB
/
datasources_test.go
File metadata and controls
68 lines (58 loc) · 2.05 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
// 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 (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDatasourcesTools(t *testing.T) {
t.Run("list datasources", func(t *testing.T) {
ctx := newTestContext()
result, err := listDatasources(ctx, ListDatasourcesParams{})
require.NoError(t, err)
// Eleven datasources are provisioned in the test environment (Prometheus, Prometheus Demo, Loki, Pyroscope, Tempo, Tempo Secondary, Alertmanager, ClickHouse, Elasticsearch, OpenSearch, and CloudWatch).
assert.Len(t, result.Datasources, 11)
})
t.Run("list datasources for type", func(t *testing.T) {
ctx := newTestContext()
result, err := listDatasources(ctx, ListDatasourcesParams{Type: "Prometheus"})
require.NoError(t, err)
// Only two Prometheus datasources are provisioned in the test environment.
assert.Len(t, result.Datasources, 2)
})
t.Run("get datasource by uid", func(t *testing.T) {
ctx := newTestContext()
result, err := getDatasource(ctx, GetDatasourceParams{
UID: "prometheus",
})
require.NoError(t, err)
assert.Equal(t, "Prometheus", result.Name)
})
t.Run("get datasource by uid - not found", func(t *testing.T) {
ctx := newTestContext()
result, err := getDatasource(ctx, GetDatasourceParams{
UID: "non-existent-datasource",
})
require.Error(t, err)
require.Nil(t, result)
assert.Contains(t, err.Error(), "not found")
})
t.Run("get datasource by name", func(t *testing.T) {
ctx := newTestContext()
result, err := getDatasource(ctx, GetDatasourceParams{
Name: "Prometheus",
})
require.NoError(t, err)
assert.Equal(t, "Prometheus", result.Name)
})
t.Run("get datasource - neither provided", func(t *testing.T) {
ctx := newTestContext()
result, err := getDatasource(ctx, GetDatasourceParams{})
require.Error(t, err)
require.Nil(t, result)
assert.Contains(t, err.Error(), "either uid or name must be provided")
})
}