From 00489d1ca467cb9d2563143b178648fba3c0690e Mon Sep 17 00:00:00 2001 From: Timofey Koolin Date: Fri, 8 Dec 2023 18:17:56 +0300 Subject: [PATCH] integration tests bind logs to test --- .../integration/basic_example_native_test.go | 8 +--- tests/integration/connection_secure_test.go | 5 +- tests/integration/connection_test.go | 4 +- .../connection_with_compression_test.go | 4 +- tests/integration/discovery_test.go | 5 +- tests/integration/helpers_test.go | 48 +++++++++++++++++-- tests/integration/ratelimiter_test.go | 4 +- tests/integration/scripting_test.go | 5 +- .../table_create_table_description_test.go | 4 +- .../table_create_table_partitions_test.go | 4 +- tests/integration/table_long_stream_test.go | 5 +- .../table_multiple_result_sets_test.go | 6 +-- tests/integration/topic_read_writer_test.go | 6 +-- 13 files changed, 58 insertions(+), 50 deletions(-) diff --git a/tests/integration/basic_example_native_test.go b/tests/integration/basic_example_native_test.go index a2635774e..f7d5deb6a 100644 --- a/tests/integration/basic_example_native_test.go +++ b/tests/integration/basic_example_native_test.go @@ -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( diff --git a/tests/integration/connection_secure_test.go b/tests/integration/connection_secure_test.go index 4164e5486..c78c4fb13 100644 --- a/tests/integration/connection_secure_test.go +++ b/tests/integration/connection_secure_test.go @@ -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" ) @@ -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).*`), ), ) diff --git a/tests/integration/connection_test.go b/tests/integration/connection_test.go index 4690e89a1..d4aecc63c 100644 --- a/tests/integration/connection_test.go +++ b/tests/integration/connection_test.go @@ -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), diff --git a/tests/integration/connection_with_compression_test.go b/tests/integration/connection_with_compression_test.go index ff7b428d7..cdb15ddac 100644 --- a/tests/integration/connection_with_compression_test.go +++ b/tests/integration/connection_with_compression_test.go @@ -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), diff --git a/tests/integration/discovery_test.go b/tests/integration/discovery_test.go index cb9e11632..410605bbf 100644 --- a/tests/integration/discovery_test.go +++ b/tests/integration/discovery_test.go @@ -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), diff --git a/tests/integration/helpers_test.go b/tests/integration/helpers_test.go index c96c4b7b3..b84f17bd7 100644 --- a/tests/integration/helpers_test.go +++ b/tests/integration/helpers_test.go @@ -9,6 +9,7 @@ import ( "database/sql" "os" "path" + "strings" "testing" "text/template" "time" @@ -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, ), )..., @@ -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 @@ -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) +} diff --git a/tests/integration/ratelimiter_test.go b/tests/integration/ratelimiter_test.go index 6699e8077..7e4c0493a 100644 --- a/tests/integration/ratelimiter_test.go +++ b/tests/integration/ratelimiter_test.go @@ -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).*`), ), ) diff --git a/tests/integration/scripting_test.go b/tests/integration/scripting_test.go index 602f3ed10..fb3cc9d49 100644 --- a/tests/integration/scripting_test.go +++ b/tests/integration/scripting_test.go @@ -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" @@ -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"), diff --git a/tests/integration/table_create_table_description_test.go b/tests/integration/table_create_table_description_test.go index 35cb6e99c..553156b29 100644 --- a/tests/integration/table_create_table_description_test.go +++ b/tests/integration/table_create_table_description_test.go @@ -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).*`), ), ) diff --git a/tests/integration/table_create_table_partitions_test.go b/tests/integration/table_create_table_partitions_test.go index ab6ddfe84..9208f4f1e 100644 --- a/tests/integration/table_create_table_partitions_test.go +++ b/tests/integration/table_create_table_partitions_test.go @@ -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).*`), ), ) diff --git a/tests/integration/table_long_stream_test.go b/tests/integration/table_long_stream_test.go index 95216c732..1d38fac6e 100644 --- a/tests/integration/table_long_stream_test.go +++ b/tests/integration/table_long_stream_test.go @@ -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" @@ -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).*`), ), ) diff --git a/tests/integration/table_multiple_result_sets_test.go b/tests/integration/table_multiple_result_sets_test.go index ff367eba1..6833ca6b3 100644 --- a/tests/integration/table_multiple_result_sets_test.go +++ b/tests/integration/table_multiple_result_sets_test.go @@ -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" @@ -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).*`), ), ) diff --git a/tests/integration/topic_read_writer_test.go b/tests/integration/topic_read_writer_test.go index 4b162d0c6..c13fe4472 100644 --- a/tests/integration/topic_read_writer_test.go +++ b/tests/integration/topic_read_writer_test.go @@ -9,7 +9,6 @@ import ( "encoding/binary" "errors" "io" - "os" "runtime/pprof" "strconv" "strings" @@ -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" @@ -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, ))