Skip to content

Commit

Permalink
integration tests bind logs to test
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Dec 8, 2023
1 parent 5ccdc4d commit 00489d1
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 50 deletions.
8 changes: 2 additions & 6 deletions tests/integration/basic_example_native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,11 @@ func TestBasicExampleNative(t *testing.T) { //nolint:gocyclo
ydb.WithSessionPoolSizeLimit(limit),
ydb.WithConnectionTTL(5*time.Second),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.FromString(os.Getenv("YDB_LOG_SEVERITY_LEVEL"))),
log.WithColoring(),
),
newLoggerWithMinLevel(t, log.FromString(os.Getenv("YDB_LOG_SEVERITY_LEVEL"))),
trace.MatchDetails(`ydb\.(driver|table|discovery|retry|scheme).*`),
),
ydb.WithPanicCallback(func(e interface{}) {
_, _ = fmt.Fprintf(os.Stderr, "panic recovered:%v:\n%s", e, debug.Stack())
os.Exit(1)
t.Fatalf("panic recovered:%v:\n%s", e, debug.Stack())
}),
ydb.WithTraceTable(
*shutdownTrace.Compose(
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/connection_secure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/config"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
"github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)
Expand Down Expand Up @@ -53,9 +52,7 @@ func TestConnectionSecure(t *testing.T) {
ydb.WithConnectionTTL(time.Millisecond*10000),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLogger(t),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
)
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ func TestConnection(t *testing.T) {
ydb.WithConnectionTTL(time.Millisecond*10000),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
ydb.WithUserAgent(userAgent),
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/connection_with_compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func TestConnectionWithCompression(t *testing.T) {
ydb.WithConnectionTTL(time.Millisecond*10000),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
ydb.WithUserAgent(userAgent),
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ func TestDiscovery(t *testing.T) {
ydb.WithConnectionTTL(time.Second*1),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
log.WithColoring(),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|repeater).*`),
),
ydb.WithUserAgent(userAgent),
Expand Down
48 changes: 45 additions & 3 deletions tests/integration/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"database/sql"
"os"
"path"
"strings"
"testing"
"text/template"
"time"
Expand Down Expand Up @@ -90,9 +91,7 @@ func (scope *scopeT) Driver(opts ...ydb.Option) *ydb.Driver {
append(opts,
ydb.WithAccessTokenCredentials(token),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
scope.LoggerMinLevel(log.WARN),
trace.DetailsAll,
),
)...,
Expand Down Expand Up @@ -147,6 +146,18 @@ func (scope *scopeT) Folder() string {
}).(string)
}

func (scope *scopeT) Logger() *testLogger {
return scope.Cache(nil, nil, func() (res interface{}, err error) {
return newLogger(scope.t), nil
}).(*testLogger)
}

func (scope *scopeT) LoggerMinLevel(level log.Level) *testLogger {
return scope.Cache(level, nil, func() (res interface{}, err error) {
return newLoggerWithMinLevel(scope.t, level), nil
}).(*testLogger)
}

type tableNameParams struct {
tableName string
createTableQueryTemplate string
Expand Down Expand Up @@ -224,3 +235,34 @@ func (scope *scopeT) TableName(opts ...func(t *tableNameParams)) string {
func (scope *scopeT) TablePath(opts ...func(t *tableNameParams)) string {
return path.Join(scope.Folder(), scope.TableName(opts...))
}

// logger for tests
type testLogger struct {
test testing.TB
minLevel log.Level
}

func newLogger(t testing.TB) *testLogger {
return newLoggerWithMinLevel(t, 0)
}

func newLoggerWithMinLevel(t testing.TB, level log.Level) *testLogger {
return &testLogger{test: t, minLevel: level}
}

func (t testLogger) Log(ctx context.Context, msg string, fields ...log.Field) {
lvl := log.LevelFromContext(ctx)
if lvl < t.minLevel {
return
}

names := log.NamesFromContext(ctx)

loggerName := strings.Join(names, ".")
values := make(map[string]string)
for _, field := range fields {
values[field.Key()] = field.String()
}

t.test.Logf("[%s] %s: %v (%v)", lvl, loggerName, msg, values)
}
4 changes: 1 addition & 3 deletions tests/integration/ratelimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ func TestRatelimiter(t *testing.T) {
),
ydb.WithBalancer(balancers.SingleConn()),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|retry|ratelimiter|coordination).*`),
),
)
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/scripting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
"github.com/ydb-platform/ydb-go-sdk/v3/config"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
"github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
"github.com/ydb-platform/ydb-go-sdk/v3/scripting"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
Expand All @@ -38,9 +37,7 @@ func TestScripting(t *testing.T) {
ydb.WithConnectionTTL(time.Millisecond*10000),
ydb.WithMinTLSVersion(tls.VersionTLS10),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.TRACE),
),
newLogger(t),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
ydb.WithUserAgent("scripting"),
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/table_create_table_description_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func TestCreateTableDescription(t *testing.T) {

db, err := ydb.Open(ctx, os.Getenv("YDB_CONNECTION_STRING"),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
)
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/table_create_table_partitions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ func TestTableCreateTablePartitions(t *testing.T) {

db, err := ydb.Open(ctx, os.Getenv("YDB_CONNECTION_STRING"),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.WARN),
),
newLoggerWithMinLevel(t, log.WARN),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
)
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/table_long_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
"github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/indexed"
Expand Down Expand Up @@ -43,9 +42,7 @@ func TestLongStream(t *testing.T) {
),
ydb.WithDiscoveryInterval(0), // disable re-discovery on upsert time
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.TRACE),
),
newLogger(t),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
)
Expand Down
6 changes: 1 addition & 5 deletions tests/integration/table_multiple_result_sets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
"github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/table"
"github.com/ydb-platform/ydb-go-sdk/v3/table/options"
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
Expand Down Expand Up @@ -44,10 +43,7 @@ func TestTableMultipleResultSets(t *testing.T) {
"", // corner case for check replacement of endpoint+database+secure
ydb.WithConnectionString(os.Getenv("YDB_CONNECTION_STRING")),
ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.TRACE),
log.WithColoring(),
),
newLogger(t),
trace.MatchDetails(`ydb\.(driver|discovery|retry|scheme).*`),
),
)
Expand Down
6 changes: 1 addition & 5 deletions tests/integration/topic_read_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"encoding/binary"
"errors"
"io"
"os"
"runtime/pprof"
"strconv"
"strings"
Expand All @@ -25,7 +24,6 @@ import (
"github.com/ydb-platform/ydb-go-sdk/v3/internal/empty"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xatomic"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
"github.com/ydb-platform/ydb-go-sdk/v3/log"
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions"
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicsugar"
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topictypes"
Expand Down Expand Up @@ -126,9 +124,7 @@ func TestManyConcurentReadersWriters(t *testing.T) {
tb := xtest.MakeSyncedTest(t)
ctx := xtest.Context(tb)
db := connect(tb, ydb.WithLogger(
log.Default(os.Stderr,
log.WithMinLevel(log.TRACE),
),
newLogger(t),
trace.DetailsAll,
))

Expand Down

0 comments on commit 00489d1

Please sign in to comment.