All checks were successful
Server Check Build (NixCN CMS) TeamCity build finished
Signed-off-by: Asai Neko <sugar@sne.moe>
93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
package tracer
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/spf13/viper"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// ---- ShortPath ----
|
|
|
|
func TestShortPathWithMatch(t *testing.T) {
|
|
input := "/home/user/projects/cms-server/internal/foo/bar.go"
|
|
assert.Equal(t, "cms-server/internal/foo/bar.go", ShortPath(input))
|
|
}
|
|
|
|
func TestShortPathNoMatch(t *testing.T) {
|
|
input := "/home/user/other-project/main.go"
|
|
assert.Equal(t, input, ShortPath(input))
|
|
}
|
|
|
|
func TestShortPathEmpty(t *testing.T) {
|
|
assert.Equal(t, "", ShortPath(""))
|
|
}
|
|
|
|
// ---- StartSpan ----
|
|
|
|
func TestStartSpanReturnsSpan(t *testing.T) {
|
|
viper.Set("server.application", "test-app")
|
|
defer viper.Reset()
|
|
|
|
ctx, span := StartSpan(context.Background(), "test_lib", "test_op")
|
|
require.NotNil(t, span)
|
|
require.NotNil(t, ctx)
|
|
span.End()
|
|
}
|
|
|
|
func TestStartSpanWithAdditionInfo(t *testing.T) {
|
|
viper.Set("server.application", "test-app")
|
|
defer viper.Reset()
|
|
|
|
info := AdditionSpanLayerInfo{
|
|
LayerName: "test-layer",
|
|
FuncName: "TestFunc",
|
|
FilePath: "/home/user/cms-server/foo.go",
|
|
Line: 42,
|
|
}
|
|
|
|
ctx, span := StartSpan(context.Background(), "test_lib", "test_op", info)
|
|
require.NotNil(t, span)
|
|
require.NotNil(t, ctx)
|
|
span.End()
|
|
}
|
|
|
|
func TestStartSpanContextPropagation(t *testing.T) {
|
|
viper.Set("server.application", "test-app")
|
|
defer viper.Reset()
|
|
|
|
type key struct{}
|
|
parent := context.WithValue(context.Background(), key{}, "sentinel")
|
|
|
|
childCtx, span := StartSpan(parent, "test_lib", "test_op")
|
|
defer span.End()
|
|
|
|
// Context values must be preserved across span creation.
|
|
assert.Equal(t, "sentinel", childCtx.Value(key{}))
|
|
}
|
|
|
|
// ---- Init / Shutdown ----
|
|
|
|
func TestTracerInitReturnsShutdown(t *testing.T) {
|
|
viper.Set("server.application", "test-app")
|
|
viper.Set("server.service_name", "test-svc")
|
|
// Point to a non-existent endpoint so exporters fail fast without
|
|
// blocking the test — Init logs the errors and continues.
|
|
viper.Set("tracer.otel_controller_endpoint", "localhost:1")
|
|
defer viper.Reset()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
shutdown := Init(ctx)
|
|
require.NotNil(t, shutdown)
|
|
|
|
// Shutdown should complete without panicking even though exporters
|
|
// never connected.
|
|
shutCtx, shutCancel := context.WithCancel(context.Background())
|
|
defer shutCancel()
|
|
_ = shutdown(shutCtx)
|
|
}
|