-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
integration_test.go
381 lines (331 loc) · 11.6 KB
/
integration_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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package integration
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/storage/dependencystore"
"github.com/jaegertracing/jaeger/storage/spanstore"
)
const (
iterations = 30
)
func TestParseAllFixtures(t *testing.T) {
fileList := []string{}
err := filepath.Walk("fixtures/traces", func(path string, f os.FileInfo, err error) error {
if !f.IsDir() && strings.HasSuffix(path, ".json") {
fileList = append(fileList, path)
}
return nil
})
require.NoError(t, err)
for _, file := range fileList {
t.Logf("Parsing %s", file)
getTraceFixtureExact(t, file)
}
}
type StorageIntegration struct {
SpanWriter spanstore.Writer
SpanReader spanstore.Reader
DependencyWriter dependencystore.Writer
DependencyReader dependencystore.Reader
// CleanUp() should ensure that the storage backend is clean before another test.
// called either before or after each test, and should be idempotent
CleanUp func() error
// Refresh() should ensure that the storage backend is up to date before being queried.
// called between set-up and queries in each test
Refresh func() error
}
// === SpanStore Integration Tests ===
// QueryFixtures and TraceFixtures are under ./fixtures/queries.json and ./fixtures/traces/*.json respectively.
// Each query fixture includes:
// Caption: describes the query we are testing
// Query: the query we are testing
// ExpectedFixture: the trace fixture that we want back from these queries.
// Queries are not necessarily numbered, but since each query requires a service name,
// the service name is formatted "query##-service".
type QueryFixtures struct {
Caption string
Query *spanstore.TraceQueryParameters
ExpectedFixtures []string
}
func (s *StorageIntegration) cleanUp(t *testing.T) {
require.NotNil(t, s.CleanUp, "CleanUp function must be provided")
require.NoError(t, s.CleanUp())
}
func (s *StorageIntegration) refresh(t *testing.T) {
require.NotNil(t, s.Refresh, "Refresh function must be provided")
require.NoError(t, s.Refresh())
}
func (s *StorageIntegration) waitForCondition(t *testing.T, predicate func(t *testing.T) bool) bool {
for i := 0; i < iterations; i++ {
t.Logf("Waiting for storage backend to update documents, iteration %d out of %d", i+1, iterations)
if predicate(t) {
return true
}
time.Sleep(100 * time.Millisecond) // Will wait up to 3 seconds at worst.
}
return predicate(t)
}
func (s *StorageIntegration) testGetServices(t *testing.T) {
defer s.cleanUp(t)
expected := []string{"example-service-1", "example-service-2", "example-service-3"}
s.loadParseAndWriteExampleTrace(t)
s.refresh(t)
var actual []string
found := s.waitForCondition(t, func(t *testing.T) bool {
actual, err := s.SpanReader.GetServices()
require.NoError(t, err)
return assert.ObjectsAreEqualValues(expected, actual)
})
if !assert.True(t, found) {
t.Log("\t Expected:", expected)
t.Log("\t Actual :", actual)
}
}
func (s *StorageIntegration) testGetLargeSpan(t *testing.T) {
defer s.cleanUp(t)
t.Log("Testing Large Trace over 10K ...")
expected := s.loadParseAndWriteLargeTrace(t)
expectedTraceID := expected.Spans[0].TraceID
s.refresh(t)
var actual *model.Trace
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.SpanReader.GetTrace(expectedTraceID)
return err == nil && len(actual.Spans) == len(expected.Spans)
})
if !assert.True(t, found) {
CompareTraces(t, expected, actual)
}
}
func (s *StorageIntegration) testGetOperations(t *testing.T) {
defer s.cleanUp(t)
expected := []string{"example-operation-1", "example-operation-3", "example-operation-4"}
s.loadParseAndWriteExampleTrace(t)
s.refresh(t)
var actual []string
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.SpanReader.GetOperations("example-service-1")
require.NoError(t, err)
return assert.ObjectsAreEqualValues(expected, actual)
})
if !assert.True(t, found) {
t.Log("\t Expected:", expected)
t.Log("\t Actual :", actual)
}
}
func (s *StorageIntegration) testGetTrace(t *testing.T) {
defer s.cleanUp(t)
expected := s.loadParseAndWriteExampleTrace(t)
expectedTraceID := expected.Spans[0].TraceID
s.refresh(t)
var actual *model.Trace
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
actual, err = s.SpanReader.GetTrace(expectedTraceID)
if err != nil {
t.Log(err)
}
return err == nil && len(actual.Spans) == len(expected.Spans)
})
if !assert.True(t, found) {
CompareTraces(t, expected, actual)
}
}
func (s *StorageIntegration) testFindTraces(t *testing.T) {
defer s.cleanUp(t)
// Note: all cases include ServiceName + StartTime range
queryTestCases := loadAndParseQueryTestCases(t)
// Each query test case only specifies matching traces, but does not provide counterexamples.
// To improve coverage we get all possible traces and store all of them before running queries.
allTraceFixtures := make(map[string]*model.Trace)
expectedTracesPerTestCase := make([][]*model.Trace, 0, len(queryTestCases))
for _, queryTestCase := range queryTestCases {
var expected []*model.Trace
for _, traceFixture := range queryTestCase.ExpectedFixtures {
trace, ok := allTraceFixtures[traceFixture]
if !ok {
trace = getTraceFixture(t, traceFixture)
err := s.writeTrace(t, trace)
require.NoError(t, err, "Unexpected error when writing trace %s to storage", traceFixture)
allTraceFixtures[traceFixture] = trace
}
expected = append(expected, trace)
}
expectedTracesPerTestCase = append(expectedTracesPerTestCase, expected)
}
s.refresh(t)
for i, queryTestCase := range queryTestCases {
t.Run(queryTestCase.Caption, func(t *testing.T) {
expected := expectedTracesPerTestCase[i]
actual := s.findTracesByQuery(t, queryTestCase.Query, expected)
CompareSliceOfTraces(t, expected, actual)
})
}
}
func (s *StorageIntegration) findTracesByQuery(t *testing.T, query *spanstore.TraceQueryParameters, expected []*model.Trace) []*model.Trace {
var traces []*model.Trace
found := s.waitForCondition(t, func(t *testing.T) bool {
var err error
traces, err = s.SpanReader.FindTraces(query)
if err == nil && tracesMatch(t, traces, expected) {
return true
}
t.Logf("FindTraces: expected: %d, actual: %d, match: false", len(expected), len(traces))
return false
})
require.True(t, found)
return traces
}
func (s *StorageIntegration) writeTraces(t *testing.T, traces []*model.Trace) error {
for _, trace := range traces {
if err := s.writeTrace(t, trace); err != nil {
return err
}
}
return nil
}
func (s *StorageIntegration) writeTrace(t *testing.T, trace *model.Trace) error {
for _, span := range trace.Spans {
if err := s.SpanWriter.WriteSpan(span); err != nil {
return err
}
}
return nil
}
func (s *StorageIntegration) loadParseAndWriteExampleTrace(t *testing.T) *model.Trace {
trace := getTraceFixture(t, "example_trace")
err := s.writeTrace(t, trace)
require.NoError(t, err, "Not expecting error when writing example_trace to storage")
return trace
}
func (s *StorageIntegration) loadParseAndWriteLargeTrace(t *testing.T) *model.Trace {
trace := getTraceFixture(t, "example_trace")
span := trace.Spans[0]
spns := make([]*model.Span, 1, 10008)
trace.Spans = spns
trace.Spans[0] = span
for i := 1; i < 10008; i++ {
s := new(model.Span)
*s = *span
s.StartTime = s.StartTime.Add(time.Second * time.Duration(i+1))
trace.Spans = append(trace.Spans, s)
}
err := s.writeTrace(t, trace)
require.NoError(t, err, "Not expecting error when writing example_trace to storage")
return trace
}
func getTraceFixture(t *testing.T, fixture string) *model.Trace {
fileName := fmt.Sprintf("fixtures/traces/%s.json", fixture)
return getTraceFixtureExact(t, fileName)
}
func getTraceFixtureExact(t *testing.T, fileName string) *model.Trace {
var trace model.Trace
loadAndParseJSONPB(t, fileName, &trace)
return &trace
}
func getTraceFixtures(t *testing.T, fixtures []string) []*model.Trace {
traces := make([]*model.Trace, len(fixtures))
for i, fixture := range fixtures {
trace := getTraceFixture(t, fixture)
traces[i] = trace
}
return traces
}
func loadAndParseJSONPB(t *testing.T, path string, object proto.Message) {
inStr, err := ioutil.ReadFile(path)
require.NoError(t, err, "Not expecting error when loading fixture %s", path)
err = jsonpb.Unmarshal(bytes.NewReader(correctTime(inStr)), object)
require.NoError(t, err, "Not expecting error when unmarshaling fixture %s", path)
}
func loadAndParseQueryTestCases(t *testing.T) []*QueryFixtures {
var queries []*QueryFixtures
loadAndParseJSON(t, "fixtures/queries.json", &queries)
return queries
}
func loadAndParseJSON(t *testing.T, path string, object interface{}) {
inStr, err := ioutil.ReadFile(path)
require.NoError(t, err, "Not expecting error when loading fixture %s", path)
err = json.Unmarshal(correctTime(inStr), object)
require.NoError(t, err, "Not expecting error when unmarshaling fixture %s", path)
}
// required, because we want to only query on recent traces, so we replace all the dates with recent dates.
func correctTime(json []byte) []byte {
jsonString := string(json)
today := time.Now().Format("2006-01-02")
yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
retString := strings.Replace(jsonString, "2017-01-26", today, -1)
retString = strings.Replace(retString, "2017-01-25", yesterday, -1)
return []byte(retString)
}
func tracesMatch(t *testing.T, actual []*model.Trace, expected []*model.Trace) bool {
if !assert.Equal(t, len(expected), len(actual), "Expecting certain number of traces") {
return false
}
return assert.Equal(t, spanCount(expected), spanCount(actual), "Expecting certain number of spans")
}
func spanCount(traces []*model.Trace) int {
var count int
for _, trace := range traces {
count += len(trace.Spans)
}
return count
}
// === DependencyStore Integration Tests ===
func (s *StorageIntegration) testGetDependencies(t *testing.T) {
if s.DependencyReader == nil || s.DependencyWriter == nil {
t.Skipf("Skipping GetDependencies test because dependency reader or writer is nil")
return
}
defer s.cleanUp(t)
expected := []model.DependencyLink{
{
Parent: "hello",
Child: "world",
CallCount: uint64(1),
},
{
Parent: "world",
Child: "hello",
CallCount: uint64(3),
},
}
require.NoError(t, s.DependencyWriter.WriteDependencies(time.Now(), expected))
s.refresh(t)
actual, err := s.DependencyReader.GetDependencies(time.Now(), 5*time.Minute)
assert.NoError(t, err)
assert.EqualValues(t, expected, actual)
}
func (s *StorageIntegration) IntegrationTestAll(t *testing.T) {
t.Run("GetServices", s.testGetServices)
t.Run("GetOperations", s.testGetOperations)
t.Run("GetTrace", s.testGetTrace)
t.Run("GetLargeSpans", s.testGetLargeSpan)
t.Run("FindTraces", s.testFindTraces)
t.Run("GetDependencies", s.testGetDependencies)
}