@@ -8,24 +8,30 @@ import (
8
8
"net/http"
9
9
"strconv"
10
10
"testing"
11
+ "time"
11
12
12
13
"github.com/stretchr/testify/assert"
13
14
"github.com/stretchr/testify/require"
14
- "github.com/weaveworks/common/httpgrpc"
15
15
"github.com/weaveworks/common/user"
16
16
17
17
"github.com/cortexproject/cortex/pkg/querier/tripperware"
18
18
)
19
19
20
20
func TestRequest (t * testing.T ) {
21
+ now := time .Now ()
22
+ codec := instantQueryCodec {now : func () time.Time {
23
+ return now
24
+ }}
21
25
22
26
for _ , tc := range []struct {
23
27
url string
28
+ expectedUrl string
24
29
expected tripperware.Request
25
30
expectedErr error
26
31
}{
27
32
{
28
- url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all&time=1536673680" ,
33
+ url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all&time=1536673680" ,
34
+ expectedUrl : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all&time=1536673680" ,
29
35
expected : & PrometheusRequest {
30
36
Path : "/api/v1/query" ,
31
37
Time : 1536673680 * 1e3 ,
@@ -37,7 +43,8 @@ func TestRequest(t *testing.T) {
37
43
},
38
44
},
39
45
{
40
- url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680" ,
46
+ url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680" ,
47
+ expectedUrl : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=1536673680" ,
41
48
expected : & PrometheusRequest {
42
49
Path : "/api/v1/query" ,
43
50
Time : 1536673680 * 1e3 ,
@@ -49,8 +56,17 @@ func TestRequest(t *testing.T) {
49
56
},
50
57
},
51
58
{
52
- url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&stats=all" ,
53
- expectedErr : httpgrpc .Errorf (http .StatusBadRequest , "invalid parameter \" time\" ; cannot parse \" \" to a valid timestamp" ),
59
+ url : "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29" ,
60
+ expectedUrl : fmt .Sprintf ("%s%d" , "/api/v1/query?query=sum%28container_memory_rss%29+by+%28namespace%29&time=" , now .Unix ()),
61
+ expected : & PrometheusRequest {
62
+ Path : "/api/v1/query" ,
63
+ Time : now .Unix () * 1e3 ,
64
+ Query : "sum(container_memory_rss) by (namespace)" ,
65
+ Stats : "" ,
66
+ Headers : map [string ][]string {
67
+ "Test-Header" : {"test" },
68
+ },
69
+ },
54
70
},
55
71
} {
56
72
t .Run (tc .url , func (t * testing.T ) {
@@ -63,16 +79,16 @@ func TestRequest(t *testing.T) {
63
79
// Get a deep copy of the request with Context changed to ctx
64
80
r = r .Clone (ctx )
65
81
66
- req , err := InstantQueryCodec .DecodeRequest (ctx , r , []string {"Test-Header" })
82
+ req , err := codec .DecodeRequest (ctx , r , []string {"Test-Header" })
67
83
if err != nil {
68
84
require .EqualValues (t , tc .expectedErr , err )
69
85
return
70
86
}
71
87
require .EqualValues (t , tc .expected , req )
72
88
73
- rdash , err := InstantQueryCodec .EncodeRequest (context .Background (), req )
89
+ rdash , err := codec .EncodeRequest (context .Background (), req )
74
90
require .NoError (t , err )
75
- require .EqualValues (t , tc .url , rdash .RequestURI )
91
+ require .EqualValues (t , tc .expectedUrl , rdash .RequestURI )
76
92
})
77
93
}
78
94
}
@@ -106,7 +122,7 @@ func TestResponse(t *testing.T) {
106
122
Header : http.Header {"Content-Type" : []string {"application/json" }},
107
123
Body : io .NopCloser (bytes .NewBuffer ([]byte (tc .body ))),
108
124
}
109
- resp , err := InstantQueryCodec .DecodeResponse (context .Background (), response , nil )
125
+ resp , err := Codec .DecodeResponse (context .Background (), response , nil )
110
126
require .NoError (t , err )
111
127
112
128
// Reset response, as the above call will have consumed the body reader.
@@ -116,7 +132,7 @@ func TestResponse(t *testing.T) {
116
132
Body : io .NopCloser (bytes .NewBuffer ([]byte (tc .body ))),
117
133
ContentLength : int64 (len (tc .body )),
118
134
}
119
- resp2 , err := InstantQueryCodec .EncodeResponse (context .Background (), resp )
135
+ resp2 , err := Codec .EncodeResponse (context .Background (), resp )
120
136
require .NoError (t , err )
121
137
assert .Equal (t , response , resp2 )
122
138
})
@@ -199,16 +215,16 @@ func TestMergeResponse(t *testing.T) {
199
215
Header : http.Header {"Content-Type" : []string {"application/json" }},
200
216
Body : io .NopCloser (bytes .NewBuffer ([]byte (r ))),
201
217
}
202
- dr , err := InstantQueryCodec .DecodeResponse (context .Background (), hr , nil )
218
+ dr , err := Codec .DecodeResponse (context .Background (), hr , nil )
203
219
require .NoError (t , err )
204
220
resps = append (resps , dr )
205
221
}
206
- resp , err := InstantQueryCodec .MergeResponse (resps ... )
222
+ resp , err := Codec .MergeResponse (resps ... )
207
223
assert .Equal (t , err , tc .expectedErr )
208
224
if err != nil {
209
225
return
210
226
}
211
- dr , err := InstantQueryCodec .EncodeResponse (context .Background (), resp )
227
+ dr , err := Codec .EncodeResponse (context .Background (), resp )
212
228
assert .Equal (t , err , tc .expectedErr )
213
229
contents , err := io .ReadAll (dr .Body )
214
230
assert .Equal (t , err , tc .expectedErr )
0 commit comments