forked from equinix-labs/otel-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_for_test.go
1377 lines (1364 loc) · 41.7 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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main_test
// This file implements data structures and data for functional testing of
// otel-cli.
//
// See: TESTING.md
//
// TODO: Results.SpanData could become a struct now
import (
"os"
"regexp"
"syscall"
"testing"
"time"
"github.com/equinix-labs/otel-cli/otelcli"
"github.com/equinix-labs/otel-cli/otlpclient"
tracepb "go.opentelemetry.io/proto/otlp/trace/v1"
)
type serverProtocol int
const (
grpcProtocol serverProtocol = iota
httpProtocol
)
// CheckFunc is a function that gets called after the test is run to do
// custom checking of values.
type CheckFunc func(t *testing.T, fixture Fixture, results Results)
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
// sets up the server with the test CA, requiring TLS
ServerTLSEnabled bool
// tells the server to require client certificate authentication
ServerTLSAuthEnabled bool
// 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
// for testing signal behavior a time to wait before sending a signal
// and the signal to send can be specified
KillAfter time.Duration
KillSignal os.Signal
}
// 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"`
Errors otlpclient.ErrorList `json:"errors"`
// these are specific to tests...
ServerMeta map[string]string
Headers map[string]string // headers sent by the client
ResourceSpans *tracepb.ResourceSpans
CliOutput string // merged stdout and stderr
CliOutputRe *regexp.Regexp // regular expression to clean the output before comparison
SpanCount int // number of spans received
EventCount int // number of events received
TimedOut bool // true when test timed out
CommandFailed bool // otel-cli was killed, did not exit() on its own
ExitCode int // the process exit code returned by otel-cli
Span *tracepb.Span
SpanEvents []*tracepb.Span_Event
}
// Fixture represents a test fixture for otel-cli.
type Fixture struct {
Name string
Config FixtureConfig
Endpoint string
TlsData TlsSettings
Expect Results
CheckFuncs []CheckFunc
}
// 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,
ParsedTimeoutMs: 1000,
},
},
},
},
// 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),
ServerMeta: map[string]string{
"proto": "grpc",
},
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 3,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
Endpoint: "*",
EndpointSource: "*",
},
SpanCount: 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),
ServerMeta: map[string]string{
"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,
Endpoint: "*",
EndpointSource: "*",
},
SpanCount: 1,
},
},
},
// everything works as expected with fully-loaded options
{
{
Name: "fully loaded command line",
Config: FixtureConfig{
ServerProtocol: grpcProtocol,
TestTimeoutMs: 1000,
Env: map[string]string{
"TRACEPARENT": "00-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA-AAAAAAAAAAAAAAAA-01",
},
CliArgs: []string{
"span",
"--endpoint", "{{endpoint}}",
"--protocol", "grpc",
"--insecure",
"--timeout", "500000us", // 500ms
"--fail", "--verbose",
"--name", "slartibartfast",
"--service", "Starship Bistromath",
"--kind", "server",
"--start", "308534400",
"--end", "1979-10-12T23:59:59Z",
"--attrs", "protagonist=DentArthurdent,medium=book",
"--otlp-headers", "lue=42",
"--force-span-id", "BBBBBBBBBBBBBBBB",
"--tp-required",
"--tp-print",
"--tp-export",
},
},
Expect: Results{
SpanCount: 1,
Config: otelcli.DefaultConfig(),
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 8,
ParsedTimeoutMs: 1000,
DetectedLocalhost: true,
InsecureSkipVerify: true,
},
SpanData: map[string]string{
"span_id": "*",
"trace_id": "*",
"attributes": `medium=book,protagonist=DentArthurdent`,
},
Headers: map[string]string{
":authority": "{{endpoint}}\n",
"content-type": "application/grpc\n",
"user-agent": "*",
"lue": "42\n",
},
CliOutput: "" +
"# trace id: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" +
"# span id: bbbbbbbbbbbbbbbb\n" +
"export TRACEPARENT=00-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-01\n",
},
},
},
// TLS connections
{
{
Name: "minimum configuration (tls, no-verify, recording, grpc)",
Config: FixtureConfig{
ServerProtocol: grpcProtocol,
CliArgs: []string{
"status",
"--endpoint", "https://{{endpoint}}",
"--protocol", "grpc",
// TODO: switch to --tls-no-verify before 1.0, for now keep testing it
"--verbose", "--fail", "--no-tls-verify",
},
TestTimeoutMs: 1000,
ServerTLSEnabled: true,
},
Expect: Results{
Config: otelcli.DefaultConfig().
WithEndpoint("https://{{endpoint}}").
WithProtocol("grpc").
WithVerbose(true).
WithTlsNoVerify(true),
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 8,
DetectedLocalhost: true,
InsecureSkipVerify: true,
ParsedTimeoutMs: 1000,
Endpoint: "*",
EndpointSource: "*",
},
SpanCount: 1,
},
},
{
Name: "minimum configuration (tls, no-verify, recording, https)",
Config: FixtureConfig{
ServerProtocol: httpProtocol,
CliArgs: []string{"status", "--endpoint", "https://{{endpoint}}", "--tls-no-verify"},
TestTimeoutMs: 2000,
ServerTLSEnabled: true,
},
Expect: Results{
// otel-cli should NOT set insecure when it auto-detects localhost
Config: otelcli.DefaultConfig().
WithTlsNoVerify(true).
WithEndpoint("https://{{endpoint}}"),
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 4,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
Endpoint: "*",
EndpointSource: "*",
},
SpanCount: 1,
},
},
{
Name: "minimum configuration (tls, client cert auth, recording, grpc)",
Config: FixtureConfig{
ServerProtocol: grpcProtocol,
CliArgs: []string{
"status",
"--endpoint", "https://{{endpoint}}",
"--protocol", "grpc",
"--verbose", "--fail",
"--tls-ca-cert", "{{tls_ca_cert}}",
"--tls-client-cert", "{{tls_client_cert}}",
"--tls-client-key", "{{tls_client_key}}",
},
TestTimeoutMs: 1000,
ServerTLSEnabled: true,
ServerTLSAuthEnabled: true,
},
Expect: Results{
Config: otelcli.DefaultConfig().
WithEndpoint("https://{{endpoint}}").
WithProtocol("grpc").
WithTlsCACert("{{tls_ca_cert}}").
WithTlsClientKey("{{tls_client_key}}").
WithTlsClientCert("{{tls_client_cert}}").
WithVerbose(true),
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 13,
DetectedLocalhost: true,
InsecureSkipVerify: true,
ParsedTimeoutMs: 1000,
Endpoint: "*",
EndpointSource: "*",
},
SpanCount: 1,
},
},
{
Name: "minimum configuration (tls, client cert auth, recording, https)",
Config: FixtureConfig{
ServerProtocol: httpProtocol,
CliArgs: []string{
"status",
"--endpoint", "https://{{endpoint}}",
"--verbose", "--fail",
"--tls-ca-cert", "{{tls_ca_cert}}",
"--tls-client-cert", "{{tls_client_cert}}",
"--tls-client-key", "{{tls_client_key}}",
},
TestTimeoutMs: 2000,
ServerTLSEnabled: true,
ServerTLSAuthEnabled: true,
},
Expect: Results{
Config: otelcli.DefaultConfig().
WithEndpoint("https://{{endpoint}}").
WithTlsCACert("{{tls_ca_cert}}").
WithTlsClientKey("{{tls_client_key}}").
WithTlsClientCert("{{tls_client_cert}}").
WithVerbose(true),
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 11,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
Endpoint: "*",
EndpointSource: "*",
},
SpanCount: 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(),
ExitCode: 1,
// 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,
Endpoint: "*",
EndpointSource: "*",
},
SpanCount: 0,
},
CheckFuncs: []CheckFunc{
func(t *testing.T, f Fixture, r Results) {
want := injectVars(`Post "https://{{endpoint}}/v1/traces": http: server gave HTTP response to HTTPS client`, f.Endpoint, f.TlsData)
if len(r.Errors) >= 1 {
if r.Errors[0].Error != want {
t.Errorf("Got the wrong error: %q", r.Errors[0].Error)
}
} else {
t.Errorf("Expected at least one error but got %d.", len(r.Errors))
}
},
},
},
},
// regression tests
{
{
// The span end time was missing when #175 merged, which showed up
// as 0ms spans. CheckFuncs was added to make this possible. This
// test runs sleep for 10ms and checks the duration of the span is
// at least 10ms.
Name: "#189 otel-cli exec sets span start time earlier than end time",
Config: FixtureConfig{
// note: relies on system sleep command supporting floats
// note: 10ms is hardcoded in a few places for this test and commentary
CliArgs: []string{"exec", "--endpoint", "{{endpoint}}", "sleep", "0.01"},
},
Expect: Results{
SpanCount: 1,
Config: otelcli.DefaultConfig().WithEndpoint("grpc://{{endpoint}}"),
},
CheckFuncs: []CheckFunc{
func(t *testing.T, f Fixture, r Results) {
//elapsed := r.Span.End.Sub(r.Span.Start)
elapsed := time.Duration((r.Span.EndTimeUnixNano - r.Span.StartTimeUnixNano) * uint64(time.Nanosecond))
if elapsed.Milliseconds() < 10 {
t.Errorf("elapsed test time not long enough. Expected 10ms, got %d ms", elapsed.Milliseconds())
}
},
},
},
{
Name: "#181 OTEL_ envvars should persist through to otel-cli exec",
Config: FixtureConfig{
CliArgs: []string{"status"},
Env: map[string]string{
"OTEL_FAKE_VARIABLE": "fake value",
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
"OTEL_EXPORTER_OTLP_CERTIFICATE": "{{tls_ca_cert}}",
"X_WHATEVER": "whatever",
},
},
Expect: Results{
SpanCount: 1,
Config: otelcli.DefaultConfig().WithEndpoint("{{endpoint}}").WithTlsCACert("{{tls_ca_cert}}"),
Env: map[string]string{
"OTEL_FAKE_VARIABLE": "fake value",
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
"OTEL_EXPORTER_OTLP_CERTIFICATE": "{{tls_ca_cert}}",
"X_WHATEVER": "whatever",
},
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
DetectedLocalhost: true,
NumArgs: 1,
ParsedTimeoutMs: 1000,
Endpoint: "*",
EndpointSource: "*",
},
},
},
{
Name: "#200 custom trace path in general endpoint gets signal path appended",
Config: FixtureConfig{
CliArgs: []string{"status", "--endpoint", "http://{{endpoint}}/mycollector"},
ServerProtocol: httpProtocol,
},
Expect: Results{
SpanCount: 1,
Config: otelcli.DefaultConfig().WithEndpoint("http://{{endpoint}}/mycollector"),
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
DetectedLocalhost: true,
NumArgs: 3,
ParsedTimeoutMs: 1000,
// spec says /v1/traces should get appended to any general endpoint URL
Endpoint: "http://{{endpoint}}/mycollector/v1/traces",
EndpointSource: "general",
},
},
},
{
Name: "#200 custom trace path on signal endpoint does not get modified",
Config: FixtureConfig{
CliArgs: []string{"status", "--traces-endpoint", "http://{{endpoint}}/mycollector/x/1"},
ServerProtocol: httpProtocol,
},
Expect: Results{
SpanCount: 1,
Config: otelcli.DefaultConfig().WithTracesEndpoint("http://{{endpoint}}/mycollector/x/1"),
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
DetectedLocalhost: true,
NumArgs: 3,
ParsedTimeoutMs: 1000,
Endpoint: "http://{{endpoint}}/mycollector/x/1",
EndpointSource: "signal",
},
},
},
{
Name: "#258 Commands that exit with a non-zero exit code should report a span",
Config: FixtureConfig{
CliArgs: []string{"exec",
"--endpoint", "{{endpoint}}",
"--verbose", "--fail",
"--", "false",
},
},
Expect: Results{
ExitCode: 1,
SpanCount: 1,
CliOutput: "",
CommandFailed: false, // otel-cli should exit voluntarily in this case
Config: otelcli.DefaultConfig().WithEndpoint("grpc://{{endpoint}}"),
},
},
{
Name: "#316 ensure process command and args are sent as attributes",
Config: FixtureConfig{
CliArgs: []string{"exec",
"--endpoint", "{{endpoint}}",
"--verbose", "--fail",
"--attrs", "zy=ab", // ensure CLI args still propagate
"--", "/bin/echo", "a", "z",
},
},
Expect: Results{
SpanCount: 1,
CliOutput: "a z\n",
SpanData: map[string]string{
"attributes": "/^process.command=/bin/echo,process.command_args=/bin/echo,a,z,process.owner=\\w+,process.parent_pid=\\d+,process.pid=\\d+,zy=ab/",
},
},
},
},
// 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{
SpanCount: 1,
Diagnostics: otelcli.Diagnostics{
IsRecording: true,
NumArgs: 3,
ParsedTimeoutMs: 1000,
Endpoint: "*",
EndpointSource: "*",
DetectedLocalhost: true,
Error: "could not open file '/tmp/traceparent.txt' for read: open /tmp/traceparent.txt: no such file or directory",
},
Env: map[string]string{
"OTEL_EXPORTER_OTLP_ENDPOINT": "{{endpoint}}",
},
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).
WithTlsNoVerify(true).
WithTlsCACert("/dev/null").
WithTlsClientKey("/dev/null").
WithTlsClientCert("/dev/null").
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(false).
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(),
SpanCount: 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",
},
SpanCount: 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",
},
SpanCount: 1,
},
},
},
// otel-cli span --print-tp actually prints
{
{
Name: "otel-cli span --print-tp (non-recording)",
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: f6c109f48195b451c4def6ab32f47b61\n" +
"# span id: a5d2a35f2483004e\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-00",
},
},
Expect: Results{
Config: otelcli.DefaultConfig(),
CliOutput: "" +
"# trace id: f6c109f48195b451c4def6ab32f47b61\n" +
"# span id: a5d2a35f2483004e\n" +
"export TRACEPARENT=00-f6c109f48195b451c4def6ab32f47b61-a5d2a35f2483004e-00\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
},
SpanCount: 1,
EventCount: 1,
},
// this validates options sent to otel-cli span end
CheckFuncs: []CheckFunc{
func(t *testing.T, f Fixture, r Results) {
if r.Span.Status.GetCode() != 2 {
t.Errorf("expected 2 for span status code, but got %d", r.Span.Status.GetCode())
}
if r.Span.Status.GetMessage() != "I can't do that Dave." {
t.Errorf("got wrong string for status description: %q", r.Span.Status.GetMessage())
}
},
},
},
{
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", ".",
// these are validated by checkfuncs defined above ^^
"--status-code", "error",
"--status-description", "I can't do that Dave.",
},
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
{
Name: "otel-cli span background (recording)",
Config: FixtureConfig{
Foreground: true, // fg
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
},
// otel-cli span background, add attrs on span end
{
{
Name: "otel-cli span background (recording) with attrs added on end",
Config: FixtureConfig{
CliArgs: []string{"span", "background", "--timeout", "1s", "--sockdir", "."},
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,ghi=jkl`, // weird format because of limitation in OTLP server
},
SpanCount: 1,
},
},
{
Name: "otel-cli span end",
Config: FixtureConfig{
CliArgs: []string{
"span", "end",
"--sockdir", ".",
"--attrs", "ghi=jkl,abc=def",
},
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
{
Name: "otel-cli span background (recording) with attrs added on end",
Config: FixtureConfig{
Foreground: true, // fg
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
},
// otel-cli span background with attrs, append attrs on span end
{
{
Name: "otel-cli span background (recording) with attrs append on end",
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,ghi=jkl`, // weird format because of limitation in OTLP server
},
SpanCount: 1,
},
},
{
Name: "otel-cli span end",
Config: FixtureConfig{
CliArgs: []string{
"span", "end",
"--sockdir", ".",
"--attrs", "ghi=jkl",
},
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
{
Name: "otel-cli span background (recording) with attrs append on end",
Config: FixtureConfig{
Foreground: true, // fg
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
},
// otel-cli span background, modify and add attrs on span end
{
{
Name: "otel-cli span background (recording) with attrs modified and added on end",
Config: FixtureConfig{
CliArgs: []string{"span", "background", "--timeout", "1s", "--sockdir", ".", "--attrs", "abc=123"},
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,ghi=jkl`, // weird format because of limitation in OTLP server
},
SpanCount: 1,
},
},
{
Name: "otel-cli span end",
Config: FixtureConfig{
CliArgs: []string{
"span", "end",
"--sockdir", ".",
"--attrs", "ghi=jkl,abc=def",
},
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
{
Name: "otel-cli span background (recording) with attrs modified and added on end",
Config: FixtureConfig{
Foreground: true, // fg
},
Expect: Results{Config: otelcli.DefaultConfig()},
},
},
// otel-cli exec runs echo
{
{
Name: "otel-cli span exec echo",
Config: FixtureConfig{
// intentionally calling a command with no args bc it's a special case in exec.go
CliArgs: []string{"exec", "--service", "main_test.go", "--name", "test-span-123", "--kind", "server", "echo"},
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: "\n",
SpanCount: 1,
},
},
},
// otel-cli exec runs otel-cli exec
{
{
Name: "otel-cli 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(),
CliOutput: "hello world\n",
SpanCount: 2,
},
},
},
// otel-cli exec echo "{{traceparent}}" and otel-cli exec --tp-disable-inject
{
{
Name: "otel-cli exec with arg injection injects the traceparent",
Config: FixtureConfig{
CliArgs: []string{
"exec", "--endpoint", "{{endpoint}}",