forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathlog_store_test.go
289 lines (263 loc) · 6.63 KB
/
log_store_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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package sls
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"testing"
"time"
"github.com/Netflix/go-env"
"github.com/stretchr/testify/suite"
)
func TestGetLogsTestSuite(t *testing.T) {
suite.Run(t, new(GetLogsTestSuite))
}
type GetLogsTestSuite struct {
suite.Suite
env TestEnvInfo
client *Client
}
func (s *GetLogsTestSuite) SetupSuite() {
_, err := env.UnmarshalFromEnviron(&s.env)
s.Require().NoError(err)
s.client = &Client{
Endpoint: s.env.Endpoint,
AccessKeyID: s.env.AccessKeyID,
AccessKeySecret: s.env.AccessKeySecret,
UserAgent: DefaultLogUserAgent,
}
}
func (s *GetLogsTestSuite) TearDownSuite() {
}
func (s *GetLogsTestSuite) TestGetLogsV2() {
exists, err := s.client.CheckProjectExist(s.env.ProjectName)
s.Require().NoError(err)
s.True(exists)
t := uint32(time.Now().Unix())
timens := uint32(time.Now().UnixNano() % 1e9)
key, val := "key1", "val1"
lg := &LogGroup{
Logs: []*Log{
{
Time: &t,
TimeNs: &timens,
Contents: []*LogContent{
{
Key: &key,
Value: &val,
},
{
Key: &key,
Value: &val,
},
},
},
},
}
// write log
err = s.client.PostLogStoreLogs(s.env.ProjectName, s.env.LogstoreName, lg, nil)
s.Require().NoError(err)
// get logs
time.Sleep(time.Second * 20)
req := &GetLogRequest{
From: int64(t - 900),
To: int64(t + 10),
Lines: 100,
Query: "val1 | with_pack_meta",
}
// old
ls := convertLogstore(s.client, s.env.ProjectName, s.env.LogstoreName)
resp, err := getLogsV2Old(ls, req)
s.Require().NoError(err)
// new
resp2, err := s.client.GetLogsV2(s.env.ProjectName, s.env.LogstoreName, req)
s.Require().NoError(err)
// these headers need not to be compared
filtered := map[string]bool{}
addFilter := func(key string) {
filtered[strings.ToLower(key)] = true
}
addFilter(HTTPHeaderDate)
addFilter(ElapsedMillisecond)
addFilter(RequestIDHeader)
addFilter(HTTPHeaderLogDate)
addFilter("x-log-time")
addFilter(HTTPHeaderContentType)
addFilter(HTTPHeaderContentLength)
addFilter(GetLogsQueryInfo)
// compare headers
for k := range resp.Header {
key := strings.ToLower(k)
if _, ok := filtered[key]; !ok {
s.EqualValuesf(resp.Header.Get(k), resp2.Header.Get(k), "header key %s", k)
}
}
// compare values
s.EqualValues(resp.Progress, resp2.Progress)
s.EqualValues(resp.Count, resp2.Count)
s.EqualValues(resp.HasSQL, resp2.HasSQL)
// compare query info
queryInfoV2 := resp.Contents
queryInfoV3 := resp2.Contents
fmt.Println(queryInfoV2)
fmt.Println(queryInfoV3)
var info2 interface{}
var info3 interface{}
s.Require().NoError(json.Unmarshal([]byte(queryInfoV2), &info2))
s.Require().NoError(json.Unmarshal([]byte(queryInfoV3), &info3))
s.EqualValues(info2, info3)
}
func (s *GetLogsTestSuite) TestConstructQueryInfo() {
v3Meta := &GetLogsV3ResponseMeta{
Keys: nil,
Terms: nil,
Marker: nil,
Mode: nil,
PhraseQueryInfo: nil,
Shard: nil,
ScanBytes: nil,
IsAccurate: nil,
ColumnTypes: nil,
Highlights: nil,
}
contents, err := v3Meta.constructQueryInfo()
s.Require().NoError(err)
s.Equal("{}", contents)
b := false
v3Meta.IsAccurate = &b
contents, err = v3Meta.constructQueryInfo()
s.Require().NoError(err)
s.Equal("{\"isAccurate\":0}", contents)
b = true
contents, err = v3Meta.constructQueryInfo()
s.Require().NoError(err)
s.Equal("{\"isAccurate\":1}", contents)
v3Meta.Keys = make([]string, 0)
shard := 0
v3Meta.Shard = &shard
contents, err = v3Meta.constructQueryInfo()
s.Require().NoError(err)
s.Equal("{\"shard\":0,\"isAccurate\":1}", contents)
}
func (s *GetLogsTestSuite) TestMarshalLines() {
logs := []map[string]string{
{
"key1": "va1",
"key2": " sdsadsa",
},
{
"keOIIO y1": "NKJ*((*va1",
"ke y2": " sdsadsa",
},
{
"keA DSy1": "va 2>>122e",
"key2": " sds2adsa",
},
}
data, err := json.Marshal(logs)
s.Require().NoError(err)
var msg []json.RawMessage
err = json.Unmarshal(data, &msg)
s.Require().NoError(err)
}
func (s *GetLogsTestSuite) TestGetLogLinesV2() {
exists, err := s.client.CheckProjectExist(s.env.ProjectName)
s.Require().NoError(err)
s.True(exists)
t := uint32(time.Now().Unix())
timens := uint32(time.Now().UnixNano() % 1e9)
key, val := "key1", "val1"
lg := &LogGroup{
Logs: []*Log{
{
Time: &t,
TimeNs: &timens,
Contents: []*LogContent{
{
Key: &key,
Value: &val,
},
{
Key: &key,
Value: &val,
},
},
},
},
}
// write log
err = s.client.PostLogStoreLogs(s.env.ProjectName, s.env.LogstoreName, lg, nil)
s.Require().NoError(err)
// get logs
time.Sleep(time.Second * 20)
req := &GetLogRequest{
From: int64(t - 900),
To: int64(t + 10),
Lines: 100,
}
// new
resp, err := s.client.GetLogLinesV2(s.env.ProjectName, s.env.LogstoreName, req)
s.Require().NoError(err)
s.Greater(resp.Count, int64(0))
s.Equal("Complete", resp.Progress)
s.Greater(len(resp.Lines), int(0))
// fmt.Println(string(resp.Lines[0]))
}
// use HTTP GET, for testing
func getLogsV2Old(s *LogStore, req *GetLogRequest) (*GetLogsResponse, error) {
rsp, b, logRsp, err := getLogsOld(s, req)
if err == nil && len(b) != 0 {
logs := []map[string]string{}
err = json.Unmarshal(b, &logs)
if err != nil {
return nil, NewBadResponseError(string(b), rsp.Header, rsp.StatusCode)
}
logRsp.Logs = logs
}
return logRsp, err
}
// getLogs query logs with [from, to) time range
func getLogsOld(s *LogStore, req *GetLogRequest) (*http.Response, []byte, *GetLogsResponse, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Accept": "application/json",
}
urlVal := req.ToURLParams()
uri := fmt.Sprintf("/logstores/%s?%s", s.Name, urlVal.Encode())
r, err := request(s.project, "GET", uri, h, nil)
if err != nil {
return nil, nil, nil, NewClientError(err)
}
defer r.Body.Close()
body, _ := ioutil.ReadAll(r.Body)
if r.StatusCode != http.StatusOK {
err := new(Error)
if jErr := json.Unmarshal(body, err); jErr != nil {
return nil, nil, nil, NewBadResponseError(string(body), r.Header, r.StatusCode)
}
return nil, nil, nil, err
}
count, err := strconv.ParseInt(r.Header.Get(GetLogsCountHeader), 10, 32)
if err != nil {
return nil, nil, nil, err
}
var contents string
if _, ok := r.Header[GetLogsQueryInfo]; ok {
if len(r.Header[GetLogsQueryInfo]) > 0 {
contents = r.Header[GetLogsQueryInfo][0]
}
}
hasSQL := false
if r.Header.Get(HasSQLHeader) == "true" {
hasSQL = true
}
return r, body, &GetLogsResponse{
Progress: r.Header[ProgressHeader][0],
Count: count,
Contents: contents,
HasSQL: hasSQL,
Header: r.Header,
}, nil
}