forked from mdelapenya/junit2otlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
373 lines (310 loc) · 10.6 KB
/
main_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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/network"
"github.com/testcontainers/testcontainers-go/wait"
)
const exporterEndpointKey = "OTEL_EXPORTER_OTLP_ENDPOINT"
type TestReader struct {
testFile string
}
func (tr *TestReader) Read() ([]byte, error) {
b, err := os.ReadFile(tr.testFile)
if err != nil {
return []byte{}, err
}
return b, nil
}
type TestAttributeValue struct {
IntValue string `json:"intValue"`
StringValue string `json:"stringValue"`
}
type TestAttribute struct {
Key string `json:"key"`
Value TestAttributeValue `json:"value,omitempty"`
}
type ResourceSpans struct {
Spans []ResourceSpan `json:"resourceSpans"`
}
type ResourceSpan struct {
Resource struct {
Attributes []TestAttribute `json:"attributes"`
} `json:"resource"`
InstrumentationLibrarySpans []struct {
InstrumentationLibrary struct {
Name string `json:"name"`
} `json:"instrumentationLibrary"`
Spans []struct {
TraceID string `json:"traceId"`
SpanID string `json:"spanId"`
ParentSpanID string `json:"parentSpanId"`
Name string `json:"name"`
Kind string `json:"kind"`
StartTimeUnixNano string `json:"startTimeUnixNano"`
EndTimeUnixNano string `json:"endTimeUnixNano"`
Attributes []TestAttribute `json:"attributes"`
Status struct {
} `json:"status"`
} `json:"spans"`
} `json:"instrumentationLibrarySpans"`
}
type ResourceMetrics struct {
Metrics []ResourceMetric `json:"resourceMetrics"`
}
type ResourceMetric struct {
Resource struct {
Attributes []TestAttribute `json:"attributes"`
} `json:"resource"`
InstrumentationLibraryMetrics []struct {
InstrumentationLibrary struct {
Name string `json:"name"`
} `json:"instrumentationLibrary"`
Metrics []struct {
Name string `json:"name"`
Description string `json:"description"`
Sum struct {
DataPoints []struct {
Attributes []TestAttribute `json:"attributes"`
StartTimeUnixNano string `json:"startTimeUnixNano"`
TimeUnixNano string `json:"timeUnixNano"`
AsInt string `json:"asInt"`
} `json:"dataPoints"`
AggregationTemporality string `json:"aggregationTemporality"`
IsMonotonic bool `json:"isMonotonic"`
} `json:"sum"`
} `json:"metrics"`
} `json:"instrumentationLibraryMetrics"`
SchemaURL string `json:"schemaUrl"`
}
// TestReport holds references to the File exporter defined by the otel-collector
type TestReport struct {
resourceSpans ResourceSpans
resourceMetrics ResourceMetrics
}
func assertStringValueInAttribute(t *testing.T, att TestAttributeValue, expected string) {
t.Helper()
require.Equal(t, expected, att.StringValue)
}
func requireAttributeInArray(t *testing.T, attributes []TestAttribute, key string) TestAttribute {
t.Helper()
for _, att := range attributes {
if att.Key == key {
return att
}
}
t.Fatalf("attribute with key '%s' not found", key)
return TestAttribute{}
}
func setupRuntimeDependencies(t *testing.T) (context.Context, string, testcontainers.Container) {
ctx := context.Background()
// create file for otel to store the traces
tmpDir := t.TempDir()
reportFilePath := filepath.Join(tmpDir, "otel-collector.json")
reportFile, err := os.Create(reportFilePath)
require.NoError(t, err)
defer reportFile.Close()
// create docker network for the containers
nw, err := network.New(ctx)
testcontainers.CleanupNetwork(t, nw)
require.NoError(t, err)
networkName := nw.Name
jaeger, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "jaegertracing/all-in-one:latest",
ExposedPorts: []string{
"14250/tcp",
"14268/tcp",
"16686/tcp",
},
Networks: []string{networkName},
},
Started: true,
})
testcontainers.CleanupContainer(t, jaeger)
require.NoError(t, err)
otelCollector, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "otel/opentelemetry-collector-contrib-dev:93a9885459c9406db8ac446f77f290b02542e8d5",
ExposedPorts: []string{
"1888/tcp", // pprof extension
"13133/tcp", // health_check extension
"4317/tcp", // OTLP gRPC receiver
"55679/tcp", // zpages extension
},
Files: []testcontainers.ContainerFile{
{
ContainerFilePath: "/etc/otel/config.yaml",
HostFilePath: filepath.Join("testdata", "otel-collector-config.yml"),
},
{
Reader: reportFile,
ContainerFilePath: "/tmp/tests.json",
},
},
WaitingFor: wait.ForListeningPort("4317/tcp"),
},
Started: true,
})
testcontainers.CleanupContainer(t, otelCollector)
require.NoError(t, err)
collectorPort, err := otelCollector.MappedPort(ctx, "4317/tcp")
require.NoError(t, err)
t.Setenv(exporterEndpointKey, "http://localhost:"+collectorPort.Port())
t.Setenv("OTEL_EXPORTER_OTLP_SPAN_INSECURE", "true")
t.Setenv("OTEL_EXPORTER_OTLP_INSECURE", "true")
t.Setenv("OTEL_EXPORTER_OTLP_METRIC_INSECURE", "true")
t.Setenv("OTEL_EXPORTER_OTLP_HEADERS", "")
t.Setenv("OTEL_SERVICE_NAME", "jaeger-srv-test")
return ctx, reportFilePath, otelCollector
}
func Test_Main_SampleXML(t *testing.T) {
t.Setenv("BRANCH", "main")
batchSizeFlag = 25
ctx, reportFilePath, otelCollector := setupRuntimeDependencies(t)
defer func() {
batchSizeFlag = defaultMaxBatchSize
// clean up test report
os.Remove(reportFilePath)
}()
err := Main(context.Background(), &TestReader{testFile: "TEST-sample.xml"})
require.NoError(t, err)
// wait for the file to be written by the otel-exporter
var out bytes.Buffer
err = wait.ForFile("/tmp/tests.json").
WithStartupTimeout(time.Second*10).
WithPollInterval(time.Second).
WithMatcher(func(r io.Reader) error {
if _, err := io.Copy(&out, r); err != nil {
return fmt.Errorf("copy: %w", err)
}
return nil
}).WaitUntilReady(ctx, otelCollector)
require.NoError(t, err)
// assert using the generated file
// merge both JSON files
// 1. get the spans and metrics JSONs, they are separated by \n
// 2. remote white spaces
// 3. unmarshal each resource separately
// 4. assign each resource to the test report struct
jsons := strings.Split(strings.TrimSpace(out.String()), "\n")
if len(jsons) != 2 {
t.Errorf("expected 2 JSONs, got %d - %s", len(jsons), jsons)
}
jsonSpans := ""
jsonMetrics := ""
// the order of the lines is not guaranteed
if strings.Contains(jsons[0], "resourceSpans") {
jsonSpans = strings.TrimSpace(jsons[0])
jsonMetrics = strings.TrimSpace(jsons[1])
} else {
jsonSpans = strings.TrimSpace(jsons[1])
jsonMetrics = strings.TrimSpace(jsons[0])
}
var resSpans ResourceSpans
err = json.Unmarshal([]byte(jsonSpans), &resSpans)
require.NoError(t, err)
var resMetrics ResourceMetrics
err = json.Unmarshal([]byte(jsonMetrics), &resMetrics)
require.NoError(t, err)
testReport := TestReport{
resourceSpans: resSpans,
resourceMetrics: resMetrics,
}
resourceSpans := testReport.resourceSpans.Spans[0]
srvNameAttribute := requireAttributeInArray(t, resourceSpans.Resource.Attributes, "service.name")
require.NoError(t, err)
require.Equal(t, "service.name", srvNameAttribute.Key)
assertStringValueInAttribute(t, srvNameAttribute.Value, "jaeger-srv-test")
srvVersionAttribute := requireAttributeInArray(t, resourceSpans.Resource.Attributes, "service.version")
require.NoError(t, err)
require.Equal(t, "service.version", srvVersionAttribute.Key)
assertStringValueInAttribute(t, srvVersionAttribute.Value, "")
instrumentationLibrarySpans := resourceSpans.InstrumentationLibrarySpans[0]
require.Equal(t, "jaeger-srv-test", instrumentationLibrarySpans.InstrumentationLibrary.Name)
spans := instrumentationLibrarySpans.Spans
// there are 15 elements:
// 1 testsuites element (root element)
// 3 testsuite element
// 11 testcase elements
expectedSpansCount := 15
require.Equal(t, expectedSpansCount, len(spans))
aTestCase := spans[2]
require.Equal(t, "TestCheckConfigDirsCreatesWorkspaceAtHome", aTestCase.Name)
require.Equal(t, "SPAN_KIND_INTERNAL", aTestCase.Kind)
codeFunction := requireAttributeInArray(t, aTestCase.Attributes, "code.function")
require.NoError(t, err)
assertStringValueInAttribute(t, codeFunction.Value, "TestCheckConfigDirsCreatesWorkspaceAtHome")
testClassName := requireAttributeInArray(t, aTestCase.Attributes, "tests.case.classname")
require.NoError(t, err)
assertStringValueInAttribute(t, testClassName.Value, "github.com/elastic/e2e-testing/cli/config")
goVersion := requireAttributeInArray(t, aTestCase.Attributes, "go.version")
require.NoError(t, err)
assertStringValueInAttribute(t, goVersion.Value, "go1.16.3 linux/amd64")
// last span is server type
aTestCase = spans[expectedSpansCount-1]
require.Equal(t, "SPAN_KIND_SERVER", aTestCase.Kind)
}
func Test_GetServiceVariable(t *testing.T) {
var otlpTests = []struct {
fallback string
getFn func() string
setFlag func(string)
otelVariable string
}{
{
fallback: Junit2otlp,
getFn: getOtlpServiceName,
setFlag: func(value string) {
serviceNameFlag = value
},
otelVariable: "OTEL_SERVICE_NAME",
},
{
fallback: "",
getFn: getOtlpServiceVersion,
setFlag: func(value string) {
serviceVersionFlag = value
},
otelVariable: "OTEL_SERVICE_VERSION",
},
}
for _, otlpotlpTest := range otlpTests {
t.Run(otlpotlpTest.otelVariable, func(t *testing.T) {
t.Run("no-env/no-flag/fallback", func(t *testing.T) {
t.Setenv(otlpotlpTest.otelVariable, "")
otlpotlpTest.setFlag("")
actualValue := otlpotlpTest.getFn()
require.Equal(t, otlpotlpTest.fallback, actualValue)
})
t.Run("env/no-flag/env", func(t *testing.T) {
t.Setenv(otlpotlpTest.otelVariable, "foobar")
otlpotlpTest.setFlag("")
actualValue := otlpotlpTest.getFn()
require.Equal(t, "foobar", actualValue)
})
t.Run("no-env/flag/flag", func(t *testing.T) {
t.Setenv(otlpotlpTest.otelVariable, "")
otlpotlpTest.setFlag("this-is-a-flag")
actualValue := otlpotlpTest.getFn()
require.Equal(t, "this-is-a-flag", actualValue)
})
t.Run("env/flag/flag", func(t *testing.T) {
t.Setenv(otlpotlpTest.otelVariable, "foobar")
otlpotlpTest.setFlag("this-is-a-flag")
actualValue := otlpotlpTest.getFn()
require.Equal(t, "this-is-a-flag", actualValue)
})
})
}
}