forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fabiolb#4 from austinhartzheim/add-tests
Added tests and refactored code.
- Loading branch information
Showing
3 changed files
with
125 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,130 @@ | ||
package trace | ||
|
||
import ( | ||
opentracing "github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/ext" | ||
mocktracer "github.com/opentracing/opentracing-go/mocktracer" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/fabiolb/fabio/config" | ||
opentracing "github.com/opentracing/opentracing-go" | ||
mocktracer "github.com/opentracing/opentracing-go/mocktracer" | ||
zipkin "github.com/openzipkin/zipkin-go-opentracing" | ||
zipkintypes "github.com/openzipkin/zipkin-go-opentracing/types" | ||
) | ||
|
||
func mimicTracerInject(req *http.Request) { | ||
// TODO maybe replace this will a call to opentracing.GlobalTracer().Inject() | ||
req.Header.Add("X-B3-TraceId", "1234562345678") | ||
req.Header.Add("X-B3-SpanId", "123456789") | ||
req.Header.Add("X-B3-ParentSpanId", "123456789") | ||
req.Header.Add("X-B3-Flags", "1") | ||
const testServiceName = "TEST-SERVICE" | ||
|
||
func TestCreateCollectorHTTP(t *testing.T) { | ||
if CreateCollector("http", "", "") == nil { | ||
t.Error("CreateCollector returned nil value.") | ||
t.FailNow() | ||
} | ||
} | ||
|
||
// go test -v ./trace | ||
func TestInjectHeaders(t *testing.T) { | ||
serviceName := "TESTING" | ||
func TestCreateSpanNoGlobalTracer(t *testing.T) { | ||
opentracing.SetGlobalTracer(nil) | ||
req, _ := http.NewRequest("GET", "http://example.com", nil) | ||
|
||
req, err := http.NewRequest("GET", "http://example.com", nil) | ||
if err != nil { | ||
t.Error("Error when creating new request.") | ||
if CreateSpan(req, testServiceName) != nil { | ||
t.Error("CreateSpan returned a non-nil result using a nil global tracer.") | ||
t.Fail() | ||
} | ||
mimicTracerInject(req) | ||
} | ||
|
||
func TestCreateSpanWithNoParent(t *testing.T) { | ||
tracer, _ := zipkin.NewTracer(nil) | ||
opentracing.SetGlobalTracer(tracer) | ||
|
||
req, _ := http.NewRequest("GET", "http://example.com", nil) | ||
if CreateSpan(req, testServiceName) == nil { | ||
t.Error("Received nil span while a global tracer was set.") | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestCreateSpanWithParent(t *testing.T) { | ||
mt := mocktracer.New() | ||
opentracing.SetGlobalTracer(mt) | ||
globalTracer := opentracing.GlobalTracer() | ||
parentSpan := globalTracer.StartSpan(testServiceName) | ||
|
||
requestIn, _ := http.NewRequest("GET", "http://example.com", nil) | ||
mt.Inject(parentSpan.Context(), | ||
opentracing.HTTPHeaders, | ||
opentracing.HTTPHeadersCarrier(requestIn.Header), | ||
) | ||
|
||
if CreateSpan(requestIn, testServiceName+"-child") == nil { | ||
t.Error("Received a nil span while a global tracer was set.") | ||
t.FailNow() | ||
} | ||
} | ||
|
||
var span opentracing.Span | ||
if globalTracer != nil { | ||
spanCtx, err := globalTracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header)) | ||
if err != nil { | ||
span = globalTracer.StartSpan(serviceName) | ||
} else { | ||
span = globalTracer.StartSpan(serviceName, ext.RPCServerOption(spanCtx)) | ||
} | ||
func TestInitializeTracer(t *testing.T) { | ||
opentracing.SetGlobalTracer(nil) | ||
InitializeTracer(&config.Tracing{TracingEnabled: true}) | ||
if opentracing.GlobalTracer() == nil { | ||
t.Error("InitializeTracer set a nil tracer.") | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestInitializeTracerWhileDisabled(t *testing.T) { | ||
opentracing.SetGlobalTracer(nil) | ||
InitializeTracer(&config.Tracing{TracingEnabled: false}) | ||
if opentracing.GlobalTracer() != nil { | ||
t.Error("InitializeTracer set a tracer while tracing was disabled.") | ||
t.FailNow() | ||
} | ||
} | ||
|
||
func TestInjectHeaders(t *testing.T) { | ||
mt := mocktracer.New() | ||
opentracing.SetGlobalTracer(mt) | ||
globalTracer := opentracing.GlobalTracer() | ||
span := globalTracer.StartSpan(testServiceName) | ||
|
||
req, _ := http.NewRequest("GET", "http://example.com", nil) | ||
InjectHeaders(span, req) | ||
|
||
if req.Header.Get("Mockpfx-Ids-Traceid") == "" { | ||
t.Error("Inject did not set the Traceid in the request.") | ||
t.Fail() | ||
} | ||
if req.Header.Get("Mockpfx-Ids-Spanid") == "" { | ||
t.Error("Inject did not set the Spanid in the request.") | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestInjectHeadersWithParentSpan(t *testing.T) { | ||
parentSpanId := uint64(12345) | ||
parentSpanContext := zipkin.SpanContext{ | ||
SpanID: parentSpanId, | ||
TraceID: zipkintypes.TraceID{High: uint64(1234), Low: uint64(4321)}, | ||
} | ||
|
||
tracer, _ := zipkin.NewTracer(nil) | ||
opentracing.SetGlobalTracer(tracer) | ||
globalTracer := opentracing.GlobalTracer() | ||
childSpan := globalTracer.StartSpan(testServiceName+"-CHILD", opentracing.ChildOf(parentSpanContext)) | ||
|
||
req, _ := http.NewRequest("GET", "http://example.com", nil) | ||
InjectHeaders(childSpan, req) | ||
|
||
if req.Header.Get("X-B3-Traceid") == "" { | ||
t.Error("Zipkin headers not set in request.") | ||
t.Error("Inject did not set the Traceid in the request.") | ||
t.Fail() | ||
} | ||
if req.Header.Get("X-B3-Spanid") == "" { | ||
t.Error("Inject did not set the Spanid in the request.") | ||
t.Fail() | ||
} | ||
if req.Header.Get("X-B3-Parentspanid") != "0000000000003039" { | ||
t.Error("Inject did not set the correct Parentspanid in the request.") | ||
t.Fail() | ||
} | ||
if req.Header.Get("X-B3-Traceid") != "1234562345678" { | ||
t.Error("Zipkin headers do not match the values set.") | ||
if req.Header.Get("x-B3-Traceid") != "00000000000004d200000000000010e1" { | ||
t.Error("Inject did not reuse the Traceid from the parent span") | ||
t.Fail() | ||
} | ||
} |