forked from equinix-labs/otel-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_for_test.go
623 lines (612 loc) · 19 KB
/
data_for_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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
package main_test
// Data structures and data for functional testing of otel-cli.
// TODO: Results.SpanData could become a struct now
// TODO: add instructions for adding more tests
import (
"regexp"
"github.com/equinix-labs/otel-cli/otelcli"
)
type serverProtocol int
const (
grpcProtocol serverProtocol = iota
httpProtocol
)
type FixtureConfig struct {
CliArgs []string
Env map[string]string
// timeout for how long to wait for the whole test in failure cases
TestTimeoutMs int
// when true this test will be excluded under go -test.short mode
// TODO: maybe move this up to the suite?
IsLongTest bool
// either grpcProtocol or httpProtocol, defaults to grpc
ServerProtocol serverProtocol
// for timeout tests we need to start the server to generate the endpoint
// but do not want it to answer when otel-cli calls, this does that
StopServerBeforeExec bool
// run this fixture in the background, starting its server and otel-cli
// instance, then let those block in the background and continue running
// serial tests until it's "foreground" by a second fixtue with the same
// description in the same file
Background bool
Foreground bool
}
// mostly mirrors otelcli.StatusOutput but we need more
type Results struct {
// same as otelcli.StatusOutput but copied because embedding doesn't work for this
Config otelcli.Config `json:"config"`
SpanData map[string]string `json:"span_data"`
Env map[string]string `json:"env"`
Diagnostics otelcli.Diagnostics `json:"diagnostics"`
// these are specific to tests...
CliOutput string // merged stdout and stderr
CliOutputRe *regexp.Regexp // regular expression to clean the output before comparison
Spans int // number of spans received
Events int // number of events received
TimedOut bool // true when test timed out
CommandFailed bool // otel-cli failed / was killed
}
// Fixture represents a test fixture for otel-cli.
type Fixture struct {
Name string
Config FixtureConfig
Expect Results
}
// FixtureSuite is a list of Fixtures that run serially.
type FixtureSuite []Fixture
var suites = []FixtureSuite{
// otel-cli should not do anything when it is not explicitly configured"
{
{
Name: "nothing configured",
Config: FixtureConfig{
CliArgs: []string{"status"},
},
Expect: Results{
Config: otelcli.DefaultConfig(),
Diagnostics: otelcli.Diagnostics{
IsRecording: false,
NumArgs: 1,
},
},
},
},
// setting minimum envvars should result in a span being received
{
{
Name: "minimum configuration (recording, grpc)",
Config: FixtureConfig{
ServerProtocol: grpcProtocol,
CliArgs: []string{"status", "--endpoint", "{{endpoint}}"},
TestTimeoutMs: 1000,
},
Expect: Results{
// otel-cli should NOT set insecure when it auto-detects localhost
Config: otelcli.DefaultConfig().
WithEndpoint("{{endpoint}}").
WithInsecure(false),
SpanData: map[string]string{
"span_id": "*",
"trace_id": "*",
"server_meta": "proto=grpc",
},
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 3,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
},
Spans: 1,
},
}, {
Name: "minimum configuration (recording, http)",
Config: FixtureConfig{
ServerProtocol: httpProtocol,
CliArgs: []string{"status", "--endpoint", "http://{{endpoint}}"},
TestTimeoutMs: 1000,
},
Expect: Results{
// otel-cli should NOT set insecure when it auto-detects localhost
Config: otelcli.DefaultConfig().
WithEndpoint("http://{{endpoint}}").
WithInsecure(false),
SpanData: map[string]string{
"span_id": "*",
"trace_id": "*",
"server_meta": "content-type=application/x-protobuf,host={{endpoint}},method=POST,proto=HTTP/1.1,uri=/v1/traces",
},
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 3,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
},
Spans: 1,
},
},
},
// ensure things fail when they're supposed to fail
{
// otel is configured but there is no server listening so it should time out silently
{
Name: "timeout with no server",
Config: FixtureConfig{
CliArgs: []string{"span", "--timeout", "1s"},
Env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
},
// this needs to be less than the timeout in CliArgs
TestTimeoutMs: 500,
IsLongTest: true, // can be skipped with `go test -short`
StopServerBeforeExec: true, // there will be no server listening
},
Expect: Results{
Config: otelcli.DefaultConfig(),
// we want and expect a timeout and failure
TimedOut: true,
CommandFailed: true,
},
},
{
Name: "syntax errors in environment variables cause the command to fail",
Config: FixtureConfig{
CliArgs: []string{"span", "--fail", "--verbose"},
Env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
"OTEL_CLI_VERBOSE": "lmao", // invalid input
},
},
Expect: Results{
Config: otelcli.DefaultConfig(),
CommandFailed: true,
// strips the date off the log line before comparing to expectation
CliOutputRe: regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} `),
CliOutput: "Error while loading environment variables: could not parse OTEL_CLI_VERBOSE value " +
"\"lmao\" as an bool: strconv.ParseBool: parsing \"lmao\": invalid syntax\n",
},
},
{
Name: "https:// should fail when TLS is not available",
Config: FixtureConfig{
ServerProtocol: httpProtocol,
CliArgs: []string{"status", "--endpoint", "https://{{endpoint}}"},
TestTimeoutMs: 1000,
},
Expect: Results{
Config: otelcli.DefaultConfig().
WithEndpoint("https://{{endpoint}}"),
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 3,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
OtelError: `traces export: Post "https://{{endpoint}}/v1/traces": http: server gave HTTP response to HTTPS client`,
},
Spans: 0,
},
},
},
// otel-cli span with no OTLP config should do and print nothing
{
{
Name: "otel-cli span (unconfigured, non-recording)",
Config: FixtureConfig{
CliArgs: []string{"span", "--service", "main_test.go", "--name", "test-span-123", "--kind", "server"},
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
},
// config file
{
{
Name: "load a json config file",
Config: FixtureConfig{
CliArgs: []string{"status", "--config", "example-config.json"},
// this will take priority over the config
Env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
},
TestTimeoutMs: 1000,
},
Expect: Results{
Spans: 1,
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 3,
ParsedTimeoutMs: 1000,
},
Config: otelcli.DefaultConfig().
WithEndpoint("{{endpoint}}"). // tells the test framework to ignore/overwrite
WithTimeout("1s").
WithHeaders(map[string]string{"header1": "header1-value"}).
WithInsecure(true).
WithBlocking(false).
WithNoTlsVerify(true).
WithServiceName("configured_in_config_file").
WithSpanName("config_file_span").
WithKind("server").
WithAttributes(map[string]string{"attr1": "value1"}).
WithStatusCode("0").
WithStatusDescription("status description").
WithTraceparentCarrierFile("/tmp/traceparent.txt").
WithTraceparentIgnoreEnv(true).
WithTraceparentPrint(true).
WithTraceparentPrintExport(true).
WithTraceparentRequired(true).
WithBackgroundParentPollMs(100).
WithBackgroundSockdir("/tmp").
WithBackgroundWait(true).
WithSpanEndTime("now").
WithSpanEndTime("now").
WithEventName("config_file_event").
WithEventTime("now").
WithCfgFile("example-config.json").
WithVerbose(true).
WithFail(true),
},
},
},
// otel-cli with minimal config span sends a span that looks right
{
{
Name: "otel-cli span (recording)",
Config: FixtureConfig{
CliArgs: []string{"span", "--service", "main_test.go", "--name", "test-span-123", "--kind", "server"},
Env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
},
TestTimeoutMs: 1000,
},
Expect: Results{
Config: otelcli.DefaultConfig(),
SpanData: map[string]string{
"span_id": "*",
"trace_id": "*",
},
Spans: 1,
},
},
// OTEL_RESOURCE_ATTRIBUTES and OTEL_CLI_SERVICE_NAME should get merged into
// the span resource attributes
{
Name: "otel-cli span with envvar service name and attributes (recording)",
Config: FixtureConfig{
CliArgs: []string{"span", "--name", "test-span-service-name-and-attrs", "--kind", "server"},
Env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
"OTEL_CLI_SERVICE_NAME": "test-service-abc123",
"OTEL_CLI_ATTRIBUTES": "cafe=deadbeef,abc=123",
"OTEL_RESOURCE_ATTRIBUTES": "foo.bar=baz",
},
TestTimeoutMs: 1000,
},
Expect: Results{
Config: otelcli.DefaultConfig(),
SpanData: map[string]string{
"span_id": "*",
"trace_id": "*",
"attributes": "abc=123,cafe=deadbeef",
"service_attributes": "foo.bar=baz,service.name=test-service-abc123",
},
Spans: 1,
},
},
// OTEL_SERVICE_NAME
{
Name: "otel-cli span with envvar service name (recording)",
Config: FixtureConfig{
CliArgs: []string{"span"},
Env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
"OTEL_SERVICE_NAME": "test-service-123abc",
},
TestTimeoutMs: 1000,
},
Expect: Results{
Config: otelcli.DefaultConfig(),
SpanData: map[string]string{
"service_attributes": "service.name=test-service-123abc",
},
Spans: 1,
},
},
},
// otel-cli span --print-tp actually prints
{
{
Name: "otel-cli span --print-tp",
Config: FixtureConfig{
CliArgs: []string{"span", "--tp-print"},
Env: map[string]string{"TRACEPARENT": "00-f6c109f48195b451c4def6ab32f47b61-a5d2a35f2483004e-01"},
},
Expect: Results{
Config: otelcli.DefaultConfig(),
CliOutput: "" + // empty so the text below can indent and line up
"# trace id: 00000000000000000000000000000000\n" +
"# span id: 0000000000000000\n" +
"TRACEPARENT=00-f6c109f48195b451c4def6ab32f47b61-a5d2a35f2483004e-01\n",
},
},
},
// otel-cli span --print-tp propagates traceparent even when not recording
{
{
Name: "otel-cli span --tp-print --tp-export (non-recording)",
Config: FixtureConfig{
CliArgs: []string{"span", "--tp-print", "--tp-export"},
Env: map[string]string{
"TRACEPARENT": "00-f6c109f48195b451c4def6ab32f47b61-a5d2a35f2483004e-01",
},
},
Expect: Results{
Config: otelcli.DefaultConfig(),
CliOutput: "" +
"# trace id: 00000000000000000000000000000000\n" +
"# span id: 0000000000000000\n" +
"export TRACEPARENT=00-f6c109f48195b451c4def6ab32f47b61-a5d2a35f2483004e-01\n",
},
},
},
// otel-cli span background, non-recording, this uses the suite functionality
// and background tasks, which are a little clunky but get the job done
{
{
Name: "otel-cli span background (nonrecording)",
Config: FixtureConfig{
CliArgs: []string{"span", "background", "--timeout", "1s", "--sockdir", "."},
TestTimeoutMs: 2000,
Background: true, // sorta like & in shell
Foreground: false, // must be true later, like `fg` in shell
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
{
Name: "otel-cli span event",
Config: FixtureConfig{
CliArgs: []string{"span", "event", "--name", "an event happened", "--attrs", "ima=now,mondai=problem", "--sockdir", "."},
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
{
Name: "otel-cli span end",
Config: FixtureConfig{
CliArgs: []string{"span", "end", "--sockdir", "."},
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
{
// Name on foreground *must* match the backgrounded job
// TODO: ^^ this isn't great, find a better way
Name: "otel-cli span background (nonrecording)",
Config: FixtureConfig{
Foreground: true, // bring it back (fg) and finish up
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
},
// otel-cli span background, in recording mode
{
{
Name: "otel-cli span background (recording)",
Config: FixtureConfig{
CliArgs: []string{"span", "background", "--timeout", "1s", "--sockdir", ".", "--attrs", "abc=def"},
Env: map[string]string{"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}"},
TestTimeoutMs: 2000,
Background: true,
Foreground: false,
},
Expect: Results{
Config: otelcli.DefaultConfig(),
SpanData: map[string]string{
"span_id": "*",
"trace_id": "*",
"attributes": `abc=def`, // weird format because of limitation in OTLP server
},
Spans: 1,
Events: 1,
},
},
{
Name: "otel-cli span event",
Config: FixtureConfig{
CliArgs: []string{"span", "event", "--name", "an event happened", "--attrs", "ima=now,mondai=problem", "--sockdir", "."},
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
{
Name: "otel-cli span end",
Config: FixtureConfig{
CliArgs: []string{"span", "end", "--sockdir", "."},
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
{
Name: "otel-cli span background (recording)",
Config: FixtureConfig{
Foreground: true, // fg
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
},
// otel-cli exec runs echo
{
{
Name: "otel-cli span exec echo",
Config: FixtureConfig{
CliArgs: []string{"exec", "--service", "main_test.go", "--name", "test-span-123", "--kind", "server", "echo hello world"},
Env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
"TRACEPARENT": "00-edededededededededededededed9000-edededededededed-01",
},
},
Expect: Results{
Config: otelcli.DefaultConfig(),
SpanData: map[string]string{
"span_id": "*",
"trace_id": "edededededededededededededed9000",
},
CliOutput: "hello world\n",
Spans: 1,
},
},
},
// otel-cli exec runs otel-cli exec
{
{
Name: "otel-cli span exec (nested)",
Config: FixtureConfig{
CliArgs: []string{
"exec", "--name", "outer", "--endpoint", "{{endpoint}}", "--fail", "--verbose", "--",
"./otel-cli", "exec", "--name", "inner", "--endpoint", "{{endpoint}}", "--tp-required", "--fail", "--verbose", "echo hello world"},
},
Expect: Results{
Config: otelcli.DefaultConfig(),
SpanData: map[string]string{
"span_id": "*",
"trace_id": "*",
},
CliOutput: "hello world\n",
Spans: 2,
},
},
},
// validate OTEL_EXPORTER_OTLP_PROTOCOL / --protocol
{
// --protocol
{
Name: "--protocol grpc",
Config: FixtureConfig{
ServerProtocol: grpcProtocol,
CliArgs: []string{"status", "--endpoint", "{{endpoint}}", "--protocol", "grpc"},
TestTimeoutMs: 1000,
},
Expect: Results{
Config: otelcli.DefaultConfig().WithEndpoint("{{endpoint}}").WithProtocol("grpc"),
SpanData: map[string]string{
"server_meta": "proto=grpc",
},
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 5,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
},
Spans: 1,
},
},
{
Name: "--protocol http/protobuf",
Config: FixtureConfig{
ServerProtocol: httpProtocol,
CliArgs: []string{"status", "--endpoint", "http://{{endpoint}}", "--protocol", "http/protobuf"},
TestTimeoutMs: 1000,
},
Expect: Results{
Config: otelcli.DefaultConfig().WithEndpoint("http://{{endpoint}}").WithProtocol("http/protobuf"),
SpanData: map[string]string{
"server_meta": "content-type=application/x-protobuf,host={{endpoint}},method=POST,proto=HTTP/1.1,uri=/v1/traces",
},
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 5,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
},
Spans: 1,
},
},
{
Name: "protocol: bad config",
Config: FixtureConfig{
CliArgs: []string{"status", "--endpoint", "{{endpoint}}", "--protocol", "xxx", "--verbose", "--fail"},
TestTimeoutMs: 1000,
},
Expect: Results{
CommandFailed: true,
CliOutputRe: regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} `),
CliOutput: "invalid protocol setting \"xxx\"\n",
Config: otelcli.DefaultConfig().WithEndpoint("{{endpoint}}"),
Diagnostics: otelcli.Diagnostics{
IsRecording: false,
NumArgs: 7,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
},
Spans: 0,
},
},
// OTEL_EXPORTER_OTLP_PROTOCOL
{
Name: "OTEL_EXPORTER_OTLP_PROTOCOL grpc",
Config: FixtureConfig{
ServerProtocol: grpcProtocol,
// validate protocol can be set to grpc with an http endpoint
CliArgs: []string{"status", "--endpoint", "http://{{endpoint}}"},
TestTimeoutMs: 1000,
Env: map[string]string{
"OTEL_EXPORTER_OTLP_PROTOCOL": "grpc",
},
},
Expect: Results{
Config: otelcli.DefaultConfig().WithEndpoint("http://{{endpoint}}").WithProtocol("grpc"),
SpanData: map[string]string{
"server_meta": "proto=grpc",
},
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 3,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
},
Spans: 1,
},
},
{
Name: "OTEL_EXPORTER_OTLP_PROTOCOL http/protobuf",
Config: FixtureConfig{
ServerProtocol: httpProtocol,
CliArgs: []string{"status", "--endpoint", "http://{{endpoint}}"},
TestTimeoutMs: 1000,
Env: map[string]string{
"OTEL_EXPORTER_OTLP_PROTOCOL": "http/protobuf",
},
},
Expect: Results{
Config: otelcli.DefaultConfig().WithEndpoint("http://{{endpoint}}").WithProtocol("http/protobuf"),
SpanData: map[string]string{
"server_meta": "content-type=application/x-protobuf,host={{endpoint}},method=POST,proto=HTTP/1.1,uri=/v1/traces",
},
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 3,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
},
Spans: 1,
},
},
{
Name: "OTEL_EXPORTER_OTLP_PROTOCOL: bad config",
Config: FixtureConfig{
CliArgs: []string{"status", "--endpoint", "http://{{endpoint}}", "--fail", "--verbose"},
TestTimeoutMs: 1000,
Env: map[string]string{
"OTEL_EXPORTER_OTLP_PROTOCOL": "roflcopter",
},
},
Expect: Results{
CommandFailed: true,
CliOutputRe: regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} `),
CliOutput: "invalid protocol setting \"roflcopter\"\n",
Config: otelcli.DefaultConfig().WithEndpoint("http://{{endpoint}}"),
Diagnostics: otelcli.Diagnostics{
IsRecording: false,
NumArgs: 3,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
},
Spans: 0,
},
},
},
}