-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdatasource_test.go
147 lines (120 loc) · 4.35 KB
/
datasource_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package sqlds_test
import (
"context"
"encoding/json"
"errors"
"testing"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/sqlds/v4"
"github.com/grafana/sqlds/v4/test"
"github.com/stretchr/testify/assert"
)
func Test_health_retries(t *testing.T) {
opts := test.DriverOpts{
ConnectError: errors.New("foo"),
}
cfg := `{ "timeout": 0, "retries": 5, "retryOn": ["foo"] }`
req, handler, ds := healthRequest(t, "timeout", opts, cfg)
_, err := ds.CheckHealth(context.Background(), &req)
assert.Equal(t, nil, err)
assert.Equal(t, 5, handler.State.ConnectAttempts)
}
func Test_query_retries(t *testing.T) {
cfg := `{ "timeout": 0, "retries": 5, "retryOn": ["foo"] }`
opts := test.DriverOpts{
QueryError: errors.New("foo"),
}
req, handler, ds := queryRequest(t, "error", opts, cfg)
data, err := ds.QueryData(context.Background(), req)
assert.Nil(t, err)
assert.NotNil(t, data.Responses)
assert.Equal(t, 6, handler.State.QueryAttempts)
res := data.Responses["foo"]
assert.NotNil(t, res.Error)
assert.Equal(t, backend.ErrorSourceDownstream, res.ErrorSource)
}
func Test_query_apply_headers(t *testing.T) {
var message []byte
onConnect := func(msg []byte) {
message = msg
}
opts := test.DriverOpts{
QueryError: errors.New("missing token"),
QueryFailTimes: 1, // first check always fails since headers are not available on initial connect
OnConnect: onConnect,
}
cfg := `{ "timeout": 0, "retries": 1, "retryOn": ["missing token"], "forwardHeaders": true }`
req, handler, ds := queryRequest(t, "headers", opts, cfg)
req.SetHTTPHeader("foo", "bar")
data, err := ds.QueryData(context.Background(), req)
assert.Nil(t, err)
assert.NotNil(t, data.Responses)
assert.Equal(t, 2, handler.State.QueryAttempts)
assert.Contains(t, string(message), "bar")
}
func Test_check_health_with_headers(t *testing.T) {
var message json.RawMessage
onConnect := func(msg []byte) {
message = msg
}
opts := test.DriverOpts{
ConnectError: errors.New("missing token"),
ConnectFailTimes: 1, // first check always fails since headers are not available on initial connect
OnConnect: onConnect,
}
cfg := `{ "timeout": 0, "retries": 2, "retryOn": ["missing token"], "forwardHeaders": true }`
req, handler, ds := healthRequest(t, "health-headers", opts, cfg)
r := &req
r.SetHTTPHeader("foo", "bar")
res, err := ds.CheckHealth(context.Background(), r)
assert.Nil(t, err)
assert.Equal(t, "Data source is working", res.Message)
assert.Equal(t, 2, handler.State.ConnectAttempts)
assert.Contains(t, string(message), "bar")
}
func Test_no_errors(t *testing.T) {
req, _, ds := healthRequest(t, "pass", test.DriverOpts{}, "{}")
result, err := ds.CheckHealth(context.Background(), &req)
assert.Nil(t, err)
expected := "Data source is working"
assert.Equal(t, expected, result.Message)
}
func queryRequest(t *testing.T, name string, opts test.DriverOpts, cfg string) (*backend.QueryDataRequest, *test.SqlHandler, *sqlds.SQLDatasource) {
driver, handler := test.NewDriver(name, test.Data{}, nil, opts)
ds := sqlds.NewDatasource(driver)
req, settings := setupQueryRequest(name, cfg)
_, err := ds.NewDatasource(context.Background(), settings)
assert.Equal(t, nil, err)
return req, handler, ds
}
func setupQueryRequest(id string, cfg string) (*backend.QueryDataRequest, backend.DataSourceInstanceSettings) {
s := backend.DataSourceInstanceSettings{UID: id, JSONData: []byte(cfg)}
return &backend.QueryDataRequest{
PluginContext: backend.PluginContext{
DataSourceInstanceSettings: &s,
},
Queries: []backend.DataQuery{
{
RefID: "foo",
JSON: []byte(`{ "rawSql": "foo" }`),
},
},
}, s
}
func healthRequest(t *testing.T, name string, opts test.DriverOpts, cfg string) (backend.CheckHealthRequest, *test.SqlHandler, *sqlds.SQLDatasource) {
driver, handler := test.NewDriver(name, test.Data{}, nil, opts)
ds := sqlds.NewDatasource(driver)
req, settings := setupHealthRequest(name, cfg)
_, err := ds.NewDatasource(context.Background(), settings)
assert.Equal(t, nil, err)
return req, handler, ds
}
func setupHealthRequest(id string, cfg string) (backend.CheckHealthRequest, backend.DataSourceInstanceSettings) {
settings := backend.DataSourceInstanceSettings{UID: id, JSONData: []byte(cfg)}
req := backend.CheckHealthRequest{
PluginContext: backend.PluginContext{
DataSourceInstanceSettings: &settings,
},
}
return req, settings
}