-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_test.go
More file actions
140 lines (122 loc) · 4.83 KB
/
query_test.go
File metadata and controls
140 lines (122 loc) · 4.83 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
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
package prometheus
import (
"bytes"
"encoding/json"
"testing"
"time"
)
func TestQueryReachable(t *testing.T) {
client := Client{Endpoint: "http://demo.robustperception.io:9090"}
result, _ := client.Query(InstantQuery{QueryExpression: "up"})
if result.Status != "success" {
t.Error("Query not successful")
}
}
func TestQueryNoEndpoint(t *testing.T) {
client := Client{}
result, queryError := client.Query(InstantQuery{QueryExpression: "up"})
if queryError == nil {
t.Error("Query should not be successful")
}
if result != nil {
t.Error("result should be nil")
}
}
func TestInstantResultTime(t *testing.T) {
client := Client{Endpoint: "http://demo.robustperception.io:9090"}
result, _ := client.Query(InstantQuery{QueryExpression: "up"})
if result.Result()[0].Values[0].Timestamp.IsZero() {
t.Error("Result time is zero")
}
}
func TestRangeResultTime(t *testing.T) {
client := Client{Endpoint: "http://demo.robustperception.io:9090"}
result, _ := client.Query(RangeQuery{QueryExpression: "up", Start: time.Now().Add(-1 * time.Hour), End: time.Now(), Step: 1 * time.Minute})
if result.Result()[0].Values[0].Timestamp.IsZero() {
t.Error("Result time is zero")
}
}
func TestRangeQuery(t *testing.T) {
client := Client{Endpoint: "http://demo.robustperception.io:9090"}
result, _ := client.Query(RangeQuery{QueryExpression: "node_load5", Start: time.Now().Add(-1 * time.Hour), End: time.Now(), Step: 1 * time.Minute})
if len(result.Result()) < 1 {
t.Error("Not enough query results")
}
}
func TestRangeResultTimeZero(t *testing.T) {
client := Client{Endpoint: "http://demo.robustperception.io:9090"}
result, _ := client.Query(RangeQuery{QueryExpression: "up", Start: time.Now().Add(-1 * time.Hour), End: time.Now(), Step: 1 * time.Minute})
for _, res := range result.Result() {
for _, val := range res.Values {
if val.Timestamp.IsZero() {
t.Error("Result time is zero")
}
}
}
}
func TestQueryResponseTypes(t *testing.T) {
client := Client{Endpoint: "http://demo.robustperception.io:9090"}
for _, testCase := range []struct {
query Query
expectedType string
expectSuccess bool
}{
{expectSuccess: true, query: InstantQuery{QueryExpression: "up"}, expectedType: "vector"},
{expectSuccess: true, query: InstantQuery{QueryExpression: "sum(up)"}, expectedType: "vector"},
{expectSuccess: true, query: InstantQuery{QueryExpression: "up{job=\"prometheus\"}"}, expectedType: "vector"},
{expectSuccess: true, query: InstantQuery{QueryExpression: "up{job=\"prometheus\"}[5m]"}, expectedType: "matrix"},
{expectSuccess: true, query: InstantQuery{QueryExpression: "rate(up[5m])"}, expectedType: "vector"},
{expectSuccess: true, query: RangeQuery{QueryExpression: "up", Start: time.Now().Add(-1 * time.Hour), End: time.Now(), Step: 20 * time.Minute}, expectedType: "matrix"},
{expectSuccess: true, query: RangeQuery{QueryExpression: "sum(up)", Start: time.Now().Add(-1 * time.Hour), End: time.Now(), Step: 20 * time.Minute}, expectedType: "matrix"},
{expectSuccess: true, query: RangeQuery{QueryExpression: "up{job=\"prometheus\"}", Start: time.Now().Add(-1 * time.Hour), End: time.Now(), Step: 20 * time.Minute}, expectedType: "matrix"},
{expectSuccess: false, query: RangeQuery{QueryExpression: "up{job=\"prometheus\"}[5m]", Start: time.Now().Add(-1 * time.Hour), End: time.Now(), Step: 20 * time.Minute}},
{expectSuccess: true, query: RangeQuery{QueryExpression: "rate(up[5m])", Start: time.Now().Add(-1 * time.Hour), End: time.Now(), Step: 20 * time.Minute}, expectedType: "matrix"},
} {
t.Run("", func(t *testing.T) {
response, err := client.Query(testCase.query)
if err != nil && testCase.expectSuccess {
t.Log(testCase.query)
t.Fatal(err)
}
if !testCase.expectSuccess && response == nil {
t.Log(err)
return
}
if testCase.expectSuccess && response.Status != "success" {
t.Errorf("Expected success, got %s", response.Status)
}
if !testCase.expectSuccess && response.Status == "success" {
t.Errorf("Expected failure, got %s", response.Status)
}
buf := new(bytes.Buffer)
encoder := json.NewEncoder(buf)
encoder.SetIndent("", " ")
_ = encoder.Encode(response)
t.Log(buf.String())
if !testCase.expectSuccess {
return
}
if response.Data.ResultType != testCase.expectedType {
t.Errorf("Expected result type %s, got %s", testCase.expectedType, response.Data.ResultType)
}
// check if the result is not empty
if len(response.Data.Result) == 0 {
t.Error("Result is empty")
}
// check if result data is not empty
for _, res := range response.Data.Result {
if len(res.Values) == 0 {
t.Error("Result data is empty")
}
}
// check if the timestamp is not zero
for _, res := range response.Data.Result {
for _, val := range res.Values {
if val.Timestamp.IsZero() {
t.Error("Result time is zero")
}
}
}
})
}
}