Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Jul 27, 2023
1 parent 11302f7 commit 8a1cf4e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
60 changes: 56 additions & 4 deletions exporters/autoexport/exporter_test.go
Expand Up @@ -16,6 +16,7 @@ package autoexport

import (
"context"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -26,7 +27,7 @@ import (

func TestOTLPExporterReturnedWhenNoEnvOrFallbackExporterConfigured(t *testing.T) {
exporter, err := NewSpanExporter(context.Background())
assert.Nil(t, err)
assert.NoError(t, err)
assert.NotNil(t, exporter)
assert.IsType(t, &otlptrace.Exporter{}, exporter)
}
Expand All @@ -37,7 +38,7 @@ func TestFallbackExporterReturnedWhenNoEnvExporterConfigured(t *testing.T) {
context.Background(),
WithFallbackSpanExporter(testExporter),
)
assert.Nil(t, err)
assert.NoError(t, err)
assert.Equal(t, testExporter, exporter)
}

Expand All @@ -49,8 +50,59 @@ func TestEnvExporterIsPreferredOverFallbackExporter(t *testing.T) {
context.Background(),
WithFallbackSpanExporter(testExporter),
)
assert.Nil(t, err)
assert.IsType(t, &otlptrace.Exporter{}, exporter)
assert.NoError(t, err)
assertOTLPHTTPExporter(t, exporter)
}

func TestEnvExporterOTLPOverHTTP(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "otlp")
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf")

exporter, err := NewSpanExporter(context.Background())
assert.NoError(t, err)
assertOTLPHTTPExporter(t, exporter)
}

func TestEnvExporterOTLPOverGRPC(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "otlp")
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "grpc")

exporter, err := NewSpanExporter(context.Background())
assert.NoError(t, err)
assertOTLPGRPCExporter(t, exporter)
}

func TestEnvExporterOTLPInvalidProtocol(t *testing.T) {
t.Setenv("OTEL_TRACES_EXPORTER", "otlp")
t.Setenv("OTEL_EXPORTER_OTLP_PROTOCOL", "invalid")

exporter, err := NewSpanExporter(context.Background())
assert.Error(t, err)
assert.Nil(t, exporter)
}

func assertOTLPHTTPExporter(t *testing.T, got trace.SpanExporter) {
t.Helper()

if !assert.IsType(t, &otlptrace.Exporter{}, got) {
return
}

// Implementation detail hack. This may break when bumping OTLP exporter modules as it uses unexported API.
clientType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("client").Elem().Type().String()
assert.Equal(t, "*otlptracehttp.client", clientType)
}

func assertOTLPGRPCExporter(t *testing.T, got trace.SpanExporter) {
t.Helper()

if !assert.IsType(t, &otlptrace.Exporter{}, got) {
return
}

// Implementation detail hack. This may break when bumping OTLP exporter modules as it uses unexported API.
clientType := reflect.Indirect(reflect.ValueOf(got)).FieldByName("client").Elem().Type().String()
assert.Equal(t, "*otlptracegrpc.client", clientType)
}

type testExporter struct{}
Expand Down
11 changes: 6 additions & 5 deletions exporters/autoexport/registry_test.go
Expand Up @@ -75,7 +75,7 @@ func TestRegistryIsConcurrentSafe(t *testing.T) {
defer wg.Done()
assert.NotPanics(t, func() {
exp, err := r.load(context.Background(), exporterName)
assert.Nil(t, err, "missing exporter in registry")
assert.NoError(t, err, "missing exporter in registry")
assert.IsType(t, &stdouttrace.Exporter{}, exp)
})
}()
Expand All @@ -86,12 +86,13 @@ func TestRegistryIsConcurrentSafe(t *testing.T) {
func TestSubsequentCallsToGetExporterReturnsNewInstances(t *testing.T) {
const exporterType = "otlp"
exp1, err := spanExporter(context.Background(), exporterType)
assert.Nil(t, err)
assert.IsType(t, &otlptrace.Exporter{}, exp1)
assert.NoError(t, err)
assertOTLPHTTPExporter(t, exp1)

exp2, err := spanExporter(context.Background(), exporterType)
assert.Nil(t, err)
assert.IsType(t, &otlptrace.Exporter{}, exp2)
assert.NoError(t, err)
assertOTLPHTTPExporter(t, exp2)

assert.NotSame(t, exp1, exp2)
}

Expand Down

0 comments on commit 8a1cf4e

Please sign in to comment.