-
Notifications
You must be signed in to change notification settings - Fork 13
/
query_test.go
233 lines (198 loc) · 5.73 KB
/
query_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package sqlds
import (
"context"
"database/sql"
"errors"
"fmt"
"testing"
"time"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
errorPingCompleted = errors.New("ping completed")
errorQueryCompleted = errors.New("query completed")
)
type testConnection struct {
PingWait time.Duration
QueryWait time.Duration
QueryRunCount int
}
func (t *testConnection) Close() error {
t.QueryRunCount = 0
return nil
}
func (t *testConnection) Ping() error {
return errorPingCompleted
}
func (t *testConnection) PingContext(ctx context.Context) error {
done := make(chan bool)
go func() {
time.Sleep(t.QueryWait)
done <- true
}()
select {
case <-ctx.Done():
return context.Canceled
case <-done:
return errorPingCompleted
}
}
func (t *testConnection) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
t.QueryRunCount++
done := make(chan bool)
go func() {
time.Sleep(t.QueryWait)
done <- true
}()
select {
case <-ctx.Done():
return nil, context.Canceled
case <-done:
return nil, errorQueryCompleted
}
}
func TestQuery_Timeout(t *testing.T) {
t.Run("it should return context.Canceled if the query timeout is exceeded", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
conn := &testConnection{
PingWait: time.Second * 5,
QueryWait: time.Second * 5,
}
defer conn.Close()
settings := backend.DataSourceInstanceSettings{
Name: "foo",
}
sqlQuery := NewQuery(conn, settings, []sqlutil.Converter{}, nil)
_, err := sqlQuery.Run(ctx, &Query{})
if !errors.Is(err, context.Canceled) {
t.Fatal("expected error to be context.Canceled, received", err)
}
if conn.QueryRunCount != 1 {
t.Fatal("expected the querycontext function to run only once, but ran", conn.QueryRunCount, "times")
}
})
t.Run("it should run to completion and not return a query timeout error", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
conn := &testConnection{
PingWait: time.Second,
QueryWait: time.Second,
}
defer conn.Close()
settings := backend.DataSourceInstanceSettings{
Name: "foo",
}
sqlQuery := NewQuery(conn, settings, []sqlutil.Converter{}, nil)
_, err := sqlQuery.Run(ctx, &Query{})
if !errors.Is(err, ErrorQuery) {
t.Fatal("expected function to complete, received error: ", err)
}
})
}
func TestFixFrameForLongToMulti(t *testing.T) {
t.Run("fix time", func(t *testing.T) {
time1 := time.UnixMilli(1)
time2 := time.UnixMilli(2)
frame := data.NewFrame("",
data.NewField("time", nil, []*time.Time{&time1, &time2}),
data.NewField("host", nil, []string{"a", "b"}),
data.NewField("iface", nil, []string{"eth0", "eth0"}),
data.NewField("in_bytes", nil, []float64{1, 2}),
data.NewField("out_bytes", nil, []int64{3, 4}),
)
err := fixFrameForLongToMulti(frame)
require.NoError(t, err)
require.Equal(t, frame.Fields[0].Type(), data.FieldTypeTime)
require.Equal(t, frame.Fields[0].Len(), 2)
require.Equal(t, frame.Fields[0].At(0).(time.Time), time1)
require.Equal(t, frame.Fields[0].At(1).(time.Time), time2)
require.Equal(t, frame.Meta.Type, data.FrameTypeTimeSeriesLong)
require.Equal(t, frame.Meta.TypeVersion, data.FrameTypeVersion{0, 1})
})
t.Run("errors for null time", func(t *testing.T) {
time1 := time.UnixMilli(1)
frame := data.NewFrame("",
data.NewField("time", nil, []*time.Time{&time1, nil}),
data.NewField("host", nil, []string{"a", "b"}),
data.NewField("in_bytes", nil, []float64{1, 2}),
)
err := fixFrameForLongToMulti(frame)
require.Equal(t, err, fmt.Errorf("can not convert to wide series, input has null time values"))
})
t.Run("error for no time", func(t *testing.T) {
frame := data.NewFrame("",
data.NewField("host", nil, []string{"a", "b"}),
data.NewField("in_bytes", nil, []float64{1, 2}),
)
err := fixFrameForLongToMulti(frame)
require.Equal(t, err, fmt.Errorf("can not convert to wide series, input is missing a time field"))
})
}
func TestLabelNameSanitization(t *testing.T) {
testcases := []struct {
input string
expected string
err bool
}{
{input: "job", expected: "job"},
{input: "job._loal['", expected: "job_loal"},
{input: "", expected: "", err: true},
{input: ";;;", expected: "", err: true},
{input: "Data source", expected: "Data_source"},
}
for _, tc := range testcases {
got, ok := sanitizeLabelName(tc.input)
if tc.err {
assert.Equal(t, false, ok)
} else {
assert.Equal(t, true, ok)
assert.Equal(t, tc.expected, got)
}
}
}
func TestIsProcessingDownstreamError(t *testing.T) {
tests := []struct {
name string
err error
expected bool
}{
{
name: "ErrorInputFieldsWithoutRows returns true",
err: data.ErrorInputFieldsWithoutRows,
expected: true,
},
{
name: "ErrorSeriesUnsorted returns true",
err: data.ErrorSeriesUnsorted,
expected: true,
},
{
name: "ErrorNullTimeValues returns true",
err: data.ErrorNullTimeValues,
expected: true,
},
{
name: "Different error returns false",
err: errors.New("some other error"),
expected: false,
},
{
name: "Wrapped downstream error returns true",
err: fmt.Errorf("wrapped: %w", data.ErrorInputFieldsWithoutRows),
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isProcessingDownstreamError(tt.err)
if result != tt.expected {
t.Errorf("isProcessingDownstreamError(%v) = %v; want %v", tt.err, result, tt.expected)
}
})
}
}