Skip to content

Commit

Permalink
split test it into several unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
coryb committed Aug 20, 2021
1 parent bb044ed commit 47dbbcc
Showing 1 changed file with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,14 @@ type clientTraceTestFixture struct {
Address string
URL string
Client *http.Client
SpanRecorder *oteltest.SpanRecorder
SpanRecorder *tracetest.SpanRecorder
}

func prepareClientTraceTest(t *testing.T) clientTraceTestFixture {
fixture := clientTraceTestFixture{}
fixture.SpanRecorder = &oteltest.SpanRecorder{}
fixture.SpanRecorder = tracetest.NewSpanRecorder()
otel.SetTracerProvider(
oteltest.NewTracerProvider(oteltest.WithSpanRecorder(fixture.SpanRecorder)),
trace.NewTracerProvider(trace.WithSpanProcessor(fixture.SpanRecorder)),
)

ts := httptest.NewServer(
Expand Down Expand Up @@ -299,7 +299,7 @@ func TestWithoutSubSpans(t *testing.T) {
require.NoError(t, err)
resp.Body.Close()
// no spans created because we were just using background context without span
require.Len(t, fixture.SpanRecorder.Completed(), 0)
require.Len(t, fixture.SpanRecorder.Ended(), 0)

// Start again with a "real" span in the context, now tracing should add
// events and annotations.
Expand All @@ -318,27 +318,19 @@ func TestWithoutSubSpans(t *testing.T) {
resp.Body.Close()
span.End()
// we just have the one span we created
require.Len(t, fixture.SpanRecorder.Completed(), 1)
recSpan := fixture.SpanRecorder.Completed()[0]
require.Len(t, fixture.SpanRecorder.Ended(), 1)
recSpan := fixture.SpanRecorder.Ended()[0]

gotAttributes := recSpan.Attributes()
require.Len(t, gotAttributes, 4)
assert.Equal(t,
attribute.StringValue("gzip"),
gotAttributes[attribute.Key("http.accept-encoding")],
)
assert.Equal(t,
attribute.StringValue("oteltest/1.1"),
gotAttributes[attribute.Key("http.user-agent")],
)
// verify redacted auth headers
assert.Equal(t,
attribute.StringValue("****"),
gotAttributes[attribute.Key("http.authorization")],
)
assert.Equal(t,
attribute.StringValue(fixture.Address),
gotAttributes[attribute.Key("http.host")],
[]attribute.KeyValue{
attribute.Key("http.host").String(fixture.Address),
attribute.Key("http.user-agent").String("oteltest/1.1"),
attribute.Key("http.authorization").String("****"),
attribute.Key("http.accept-encoding").String("gzip"),
},
gotAttributes,
)

type attrMap = map[attribute.Key]attribute.Value
Expand Down Expand Up @@ -377,14 +369,18 @@ func TestWithoutSubSpans(t *testing.T) {
}
require.Len(t, recSpan.Events(), len(expectedEvents))
for i, e := range recSpan.Events() {
attrs := attrMap{}
for _, a := range e.Attributes {
attrs[a.Key] = a.Value
}
expected := expectedEvents[i]
assert.Equal(t, expected.Event, e.Name)
if expected.VerifyAttrs == nil {
assert.Nil(t, e.Attributes, "Event %q has no attributes", e.Name)
} else {
e := e // make loop var lexical
t.Run(e.Name, func(t *testing.T) {
expected.VerifyAttrs(t, e.Attributes)
expected.VerifyAttrs(t, attrs)
})
}
}
Expand All @@ -406,13 +402,17 @@ func TestWithRedactedHeaders(t *testing.T) {
require.NoError(t, err)
resp.Body.Close()
span.End()
require.Len(t, fixture.SpanRecorder.Completed(), 1)
recSpan := fixture.SpanRecorder.Completed()[0]
require.Len(t, fixture.SpanRecorder.Ended(), 1)
recSpan := fixture.SpanRecorder.Ended()[0]

gotAttributes := recSpan.Attributes()
assert.Equal(t,
attribute.StringValue("****"),
gotAttributes[attribute.Key("http.user-agent")],
[]attribute.KeyValue{
attribute.Key("http.host").String(fixture.Address),
attribute.Key("http.user-agent").String("****"),
attribute.Key("http.accept-encoding").String("gzip"),
},
gotAttributes,
)
}

Expand All @@ -432,8 +432,8 @@ func TestWithoutHeaders(t *testing.T) {
require.NoError(t, err)
resp.Body.Close()
span.End()
require.Len(t, fixture.SpanRecorder.Completed(), 1)
recSpan := fixture.SpanRecorder.Completed()[0]
require.Len(t, fixture.SpanRecorder.Ended(), 1)
recSpan := fixture.SpanRecorder.Ended()[0]

gotAttributes := recSpan.Attributes()
require.Len(t, gotAttributes, 0)
Expand All @@ -450,18 +450,24 @@ func TestWithInsecureHeaders(t *testing.T) {
),
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fixture.URL, nil)
req.Header.Set("User-Agent", "oteltest/1.1")
req.Header.Set("Authorization", "Bearer token123")
require.NoError(t, err)
resp, err := fixture.Client.Do(req)
require.NoError(t, err)
resp.Body.Close()
span.End()
require.Len(t, fixture.SpanRecorder.Completed(), 1)
recSpan := fixture.SpanRecorder.Completed()[0]
require.Len(t, fixture.SpanRecorder.Ended(), 1)
recSpan := fixture.SpanRecorder.Ended()[0]

gotAttributes := recSpan.Attributes()
assert.Equal(t,
attribute.StringValue("Bearer token123"),
gotAttributes[attribute.Key("http.authorization")],
[]attribute.KeyValue{
attribute.Key("http.host").String(fixture.Address),
attribute.Key("http.user-agent").String("oteltest/1.1"),
attribute.Key("http.authorization").String("Bearer token123"),
attribute.Key("http.accept-encoding").String("gzip"),
},
gotAttributes,
)
}

0 comments on commit 47dbbcc

Please sign in to comment.