Skip to content

Commit

Permalink
feat: tracing middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 committed Sep 9, 2024
1 parent 3cb9bb3 commit ee8a03a
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions router/middleware/tracing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// SPDX-License-Identifier: Apache-2.0

package middleware

import (
"errors"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/tracing"

Check failure on line 13 in router/middleware/tracing_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] router/middleware/tracing_test.go#L13

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
Raw output
router/middleware/tracing_test.go:13: File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
	"github.com/go-vela/server/tracing"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"

Check failure on line 15 in router/middleware/tracing_test.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] router/middleware/tracing_test.go#L15

File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
Raw output
router/middleware/tracing_test.go:15: File is not `gci`-ed with --skip-generated -s standard -s default -s blank -s dot -s prefix(github.com/go-vela) --custom-order (gci)
	"go.opentelemetry.io/otel/trace"
)

func TestMiddleware_TracingClient(t *testing.T) {
// setup types
var got *tracing.Client
want := &tracing.Client{
Config: tracing.Config{
EnableTracing: true,
},
}

// setup context
gin.SetMode(gin.TestMode)

resp := httptest.NewRecorder()
context, engine := gin.CreateTestContext(resp)
context.Request, _ = http.NewRequest(http.MethodGet, "/health", nil)

// setup mock server
engine.Use(TracingClient(want))
engine.GET("/health", func(c *gin.Context) {
got = c.Value("tracing").(*tracing.Client)

c.Status(http.StatusOK)
})

// run test
engine.ServeHTTP(context.Writer, context.Request)

if !reflect.DeepEqual(got, want) {
t.Errorf("TracingClient is %v, want %v", got, want)
}
}

func TestMiddleware_TracingInstrumentation(t *testing.T) {
// setup types
tt := []struct {
tc *tracing.Client
assert func(trace.SpanContext) error
}{
{
tc: &tracing.Client{
Config: tracing.Config{
EnableTracing: false,
ServiceName: "vela-test",
},
TracerProvider: sdktrace.NewTracerProvider(),
},
assert: func(got trace.SpanContext) error {
if !reflect.DeepEqual(got, trace.SpanContext{}) {
return errors.New("span context is not empty")
}
return nil
},
},
{
tc: &tracing.Client{
Config: tracing.Config{
EnableTracing: true,
ServiceName: "vela-test",
},
TracerProvider: sdktrace.NewTracerProvider(),
},
assert: func(got trace.SpanContext) error {
if reflect.DeepEqual(got, trace.SpanContext{}) {
return errors.New("span context is empty")
}
return nil
},
},
}

// setup context
gin.SetMode(gin.TestMode)

for _, test := range tt {
got := trace.SpanContext{}
resp := httptest.NewRecorder()
context, engine := gin.CreateTestContext(resp)
context.Request, _ = http.NewRequest(http.MethodGet, "/health", nil)

// setup mock server
engine.Use(TracingInstrumentation(test.tc))
engine.GET("/health", func(c *gin.Context) {
got = trace.SpanContextFromContext(c.Request.Context())

c.Status(http.StatusOK)
})

// run test
engine.ServeHTTP(context.Writer, context.Request)

if resp.Code != http.StatusOK {
t.Errorf("TracingInstrumentation returned %v, want %v", resp.Code, http.StatusOK)
}

err := test.assert(got)
if err != nil {
t.Errorf("TracingInstrumentation test assertion failed: %s", err)
}
}
}

0 comments on commit ee8a03a

Please sign in to comment.