forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 12
/
query_test.go
178 lines (148 loc) · 4.28 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
package wavefront
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"testing"
"time"
asserts "github.com/stretchr/testify/assert"
)
type MockWavefrontClient struct {
Client
Response []byte
}
func (m MockWavefrontClient) Do(req *http.Request) (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(m.Response)), nil
}
func TestQuery(t *testing.T) {
assert := asserts.New(t)
query := "dudndun"
baseurl, _ := url.Parse("http://testing.wavefront.com")
q := &Query{
Params: NewQueryParams(query),
client: &MockWavefrontClient{
Response: []byte(`{"valid":"json"}`),
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
},
}
// check correct default timewindow applied
end, _ := strconv.Atoi(q.Params.EndTime)
start, _ := strconv.Atoi(q.Params.StartTime)
if end-start != 3600 {
t.Errorf("query window, expected 3600, got %d", end-start)
}
q.SetEndTime(time.Now())
assert.NoError(q.SetStartTime(LastDay))
end, _ = strconv.Atoi(q.Params.EndTime)
start, _ = strconv.Atoi(q.Params.StartTime)
if end-start != LastDay {
t.Errorf("query window, expected %d, got %d", LastDay, end-start)
}
resp, err := q.Execute()
if err != nil {
t.Fatal("error executing query:", err)
}
raw, err := io.ReadAll(resp.RawResponse)
if err != nil {
t.Error(err)
}
if err := json.Unmarshal(raw, new(map[string]interface{})); err != nil {
fmt.Println(string(raw))
t.Error("raw response is invalid JSON", err)
}
}
func getQueryOutputFromFixture(fixture string) (*QueryResponse, error) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
response, err := os.ReadFile(fixture)
if err != nil {
return nil, err
}
q := &Query{
Params: NewQueryParams("ts(some.query)"),
client: &MockWavefrontClient{
Response: response,
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
},
}
return q.Execute()
}
func TestQuery_SingleSeries(t *testing.T) {
resp, err := getQueryOutputFromFixture("./fixtures/single-series.json")
if err != nil {
t.Fatal("error executing query:", err)
}
ts := resp.TimeSeries[0]
if ts.Host != "server1.example.net" {
t.Errorf("timeseries host, exepected server1.example.net, got %s", ts.Host)
}
if ts.Label != "servers.load.load.longterm" {
t.Errorf("timeseries label, exepected servers.load.load.longterm, got %s", ts.Label)
}
if len(ts.DataPoints) != 60 {
t.Errorf("datapoints, expected 60, got %d", len(ts.DataPoints))
}
}
func TestQuery_MultiSeries(t *testing.T) {
resp, err := getQueryOutputFromFixture("./fixtures/multi-series.json")
if err != nil {
t.Fatal("error executing query:", err)
}
if len(resp.TimeSeries) != 2 {
t.Fatalf("expected 2 timeseries, got %d", len(resp.TimeSeries))
}
ts := resp.TimeSeries[0]
if ts.Host != "server1.example.net" {
t.Errorf("timeseries host, exepected server1.example.net, got %s", ts.Host)
}
if ts.Label != "servers.load.load.longterm" {
t.Errorf("timeseries label, exepected servers.load.load.longterm, got %s", ts.Label)
}
if len(ts.DataPoints) != 59 {
t.Errorf("datapoints, expected 59, got %d", len(ts.DataPoints))
}
ts = resp.TimeSeries[1]
if ts.Host != "server2.example.net" {
t.Errorf("timeseries host, exepected server2.example.net, got %s", ts.Host)
}
if ts.Label != "servers.load.load.longterm" {
t.Errorf("timeseries label, exepected servers.load.load.longterm, got %s", ts.Label)
}
if len(ts.DataPoints) != 59 {
t.Errorf("datapoints, expected 59, got %d", len(ts.DataPoints))
}
}
func TestQuery_Error(t *testing.T) {
resp, err := getQueryOutputFromFixture("./fixtures/failed-query.json")
if err != nil {
t.Fatal("error executing query:", err)
}
// must have the ErrType field populated if there is one
if resp.ErrType == "" {
t.Errorf("expected to have ErrType field populated.")
}
if resp.ErrType != "QueryExecutionFailed" {
t.Errorf("unexpected Error Type found.")
}
// must have the ErrType field populated if there is one
if resp.ErrMessage == "" {
t.Errorf("expected to have ErrMessage field populated.")
}
if resp.ErrMessage != "Terminating Query to save resources" {
t.Errorf("unexpected Error Message found.")
}
}