From f0eb1c080e9a4e85bb6da3554416d7d89c533ba6 Mon Sep 17 00:00:00 2001 From: arkadiuszos4chain Date: Wed, 13 Dec 2023 13:11:42 +0100 Subject: [PATCH] chore(BUX-000): clean tests logs - use Nop logger --- bux_test.go | 7 +++++ chainstate/chainstate_test.go | 4 ++- chainstate/client_options_test.go | 8 +++--- client_options_test.go | 42 ++++++++++++++++++++++-------- client_test.go | 5 +++- logging/logging.go | 2 +- model_transaction_config_test.go | 25 +++++++++++++++--- paymail_test.go | 31 +++++++++++++++++++--- taskmanager/client_options_test.go | 8 +++--- 9 files changed, 102 insertions(+), 30 deletions(-) diff --git a/bux_test.go b/bux_test.go index d5ca6a9f..34d3aeb7 100644 --- a/bux_test.go +++ b/bux_test.go @@ -19,6 +19,7 @@ import ( "github.com/mrz1836/go-cache" "github.com/mrz1836/go-datastore" "github.com/rafaeljusto/redigomock" + "github.com/rs/zerolog" "github.com/stretchr/testify/require" ) @@ -83,9 +84,12 @@ func DefaultClientOpts(debug, shared bool) []ClientOps { func CreateTestSQLiteClient(t *testing.T, debug, shared bool, clientOpts ...ClientOps) (context.Context, ClientInterface, func()) { ctx := tester.GetNewRelicCtx(t, "app-test", "test-transaction") + logger := zerolog.Nop() + // Set the default options, add migrate models opts := DefaultClientOpts(debug, shared) opts = append(opts, WithAutoMigrate(BaseModels...)) + opts = append(opts, WithLogger(&logger)) opts = append(opts, clientOpts...) // Create the client @@ -106,9 +110,12 @@ func CreateTestSQLiteClient(t *testing.T, debug, shared bool, clientOpts ...Clie func CreateBenchmarkSQLiteClient(b *testing.B, debug, shared bool, clientOpts ...ClientOps) (context.Context, ClientInterface, func()) { ctx := context.Background() + logger := zerolog.Nop() + // Set the default options, add migrate models opts := DefaultClientOpts(debug, shared) opts = append(opts, WithAutoMigrate(BaseModels...)) + opts = append(opts, WithLogger(&logger)) opts = append(opts, clientOpts...) // Create the client diff --git a/chainstate/chainstate_test.go b/chainstate/chainstate_test.go index d87d3d24..6c771a53 100644 --- a/chainstate/chainstate_test.go +++ b/chainstate/chainstate_test.go @@ -5,14 +5,16 @@ import ( "testing" "time" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // NewTestClient returns a test client func NewTestClient(ctx context.Context, t *testing.T, opts ...ClientOps) ClientInterface { + logger := zerolog.Nop() c, err := NewClient( - ctx, append(opts, WithDebugging())..., + ctx, append(opts, WithDebugging(), WithLogger(&logger))..., ) require.NoError(t, err) require.NotNil(t, c) diff --git a/chainstate/client_options_test.go b/chainstate/client_options_test.go index 923f31d0..11136f18 100644 --- a/chainstate/client_options_test.go +++ b/chainstate/client_options_test.go @@ -6,8 +6,8 @@ import ( "testing" "time" - "github.com/BuxOrg/bux/logging" broadcast_client_mock "github.com/bitcoin-sv/go-broadcast-client/broadcast/broadcast-client-mock" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -305,10 +305,10 @@ func TestWithLogger(t *testing.T) { options := &clientOptions{ config: &syncConfig{}, } - customLogger := logging.GetDefaultLogger() - opt := WithLogger(customLogger) + customLogger := zerolog.Nop() + opt := WithLogger(&customLogger) opt(options) - assert.Equal(t, customLogger, options.logger) + assert.Equal(t, &customLogger, options.logger) }) } diff --git a/client_options_test.go b/client_options_test.go index 7f68beb4..d2e08869 100644 --- a/client_options_test.go +++ b/client_options_test.go @@ -18,6 +18,7 @@ import ( "github.com/mrz1836/go-cachestore" "github.com/mrz1836/go-datastore" "github.com/newrelic/go-agent/v3/newrelic" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -70,8 +71,9 @@ func Test_newRelicOptions_getOrStartTxn(t *testing.T) { require.NoError(t, err) require.NotNil(t, app) + logger := zerolog.Nop() opts := DefaultClientOpts(false, true) - opts = append(opts, WithNewRelic(app)) + opts = append(opts, WithNewRelic(app), WithLogger(&logger)) var tc ClientInterface tc, err = NewClient( @@ -90,8 +92,9 @@ func Test_newRelicOptions_getOrStartTxn(t *testing.T) { }) t.Run("invalid ctx and txn", func(t *testing.T) { + logger := zerolog.Nop() opts := DefaultClientOpts(false, true) - opts = append(opts, WithNewRelic(nil)) + opts = append(opts, WithNewRelic(nil), WithLogger(&logger)) tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...) require.NoError(t, err) @@ -148,8 +151,9 @@ func TestWithUserAgent(t *testing.T) { }) t.Run("empty user agent", func(t *testing.T) { + logger := zerolog.Nop() opts := DefaultClientOpts(false, true) - opts = append(opts, WithUserAgent("")) + opts = append(opts, WithUserAgent(""), WithLogger(&logger)) tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...) require.NoError(t, err) @@ -163,8 +167,9 @@ func TestWithUserAgent(t *testing.T) { t.Run("custom user agent", func(t *testing.T) { customAgent := "custom-user-agent" + logger := zerolog.Nop() opts := DefaultClientOpts(false, true) - opts = append(opts, WithUserAgent(customAgent)) + opts = append(opts, WithUserAgent(customAgent), WithLogger(&logger)) tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...) require.NoError(t, err) @@ -382,12 +387,16 @@ func TestWithFreeCacheConnection(t *testing.T) { }) t.Run("using a nil client", func(t *testing.T) { + logger := zerolog.Nop() + tc, err := NewClient( tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), WithFreeCacheConnection(nil), WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory), WithSQLite(&datastore.SQLiteConfig{Shared: true}), - WithMinercraft(&chainstate.MinerCraftBase{})) + WithMinercraft(&chainstate.MinerCraftBase{}), + WithLogger(&logger), + ) require.NoError(t, err) require.NotNil(t, tc) @@ -400,13 +409,15 @@ func TestWithFreeCacheConnection(t *testing.T) { t.Run("using an existing connection", func(t *testing.T) { fc := freecache.NewCache(cachestore.DefaultCacheSize) - + logger := zerolog.Nop() tc, err := NewClient( tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), WithFreeCacheConnection(fc), WithTaskQ(taskmanager.DefaultTaskQConfig(testQueueName), taskmanager.FactoryMemory), WithSQLite(&datastore.SQLiteConfig{Shared: true}), - WithMinercraft(&chainstate.MinerCraftBase{})) + WithMinercraft(&chainstate.MinerCraftBase{}), + WithLogger(&logger), + ) require.NoError(t, err) require.NotNil(t, tc) defer CloseClient(context.Background(), t, tc) @@ -459,9 +470,14 @@ func TestWithTaskQ(t *testing.T) { // todo: test cases where config is nil, or cannot load TaskQ t.Run("using taskq using memory", func(t *testing.T) { + + logger := zerolog.Nop() + tcOpts := DefaultClientOpts(true, true) + tcOpts = append(tcOpts, WithLogger(&logger)) + tc, err := NewClient( tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), - DefaultClientOpts(false, true)..., + tcOpts..., ) require.NoError(t, err) require.NotNil(t, tc) @@ -478,6 +494,8 @@ func TestWithTaskQ(t *testing.T) { t.Skip("skipping live local redis tests") } + logger := zerolog.Nop() + tc, err := NewClient( tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), WithTaskQUsingRedis( @@ -491,6 +509,7 @@ func TestWithTaskQ(t *testing.T) { }), WithSQLite(tester.SQLiteTestConfig(false, true)), WithMinercraft(&chainstate.MinerCraftBase{}), + WithLogger(&logger), ) require.NoError(t, err) require.NotNil(t, tc) @@ -522,19 +541,20 @@ func TestWithLogger(t *testing.T) { defer CloseClient(context.Background(), t, tc) assert.NotNil(t, tc.Logger()) + assert.Equal(t, logging.GetDefaultLogger(), tc.Logger()) }) t.Run("test applying option", func(t *testing.T) { - customLogger := logging.GetDefaultLogger() + customLogger := zerolog.Nop() opts := DefaultClientOpts(false, true) - opts = append(opts, WithLogger(customLogger)) + opts = append(opts, WithLogger(&customLogger)) tc, err := NewClient(tester.GetNewRelicCtx(t, defaultNewRelicApp, defaultNewRelicTx), opts...) require.NoError(t, err) require.NotNil(t, tc) defer CloseClient(context.Background(), t, tc) - assert.Equal(t, customLogger, tc.Logger()) + assert.Equal(t, &customLogger, tc.Logger()) }) } diff --git a/client_test.go b/client_test.go index 0d04fde5..0a12bd5c 100644 --- a/client_test.go +++ b/client_test.go @@ -8,6 +8,7 @@ import ( "github.com/bitcoin-sv/go-paymail" "github.com/mrz1836/go-cachestore" "github.com/mrz1836/go-datastore" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -254,13 +255,15 @@ func TestPaymailOptions_ServerConfig(t *testing.T) { }) t.Run("valid server config", func(t *testing.T) { + logger := zerolog.Nop() opts := DefaultClientOpts(false, true) opts = append(opts, WithPaymailSupport( []string{testDomain}, defaultSenderPaymail, defaultAddressResolutionPurpose, false, false, - )) + ), + WithLogger(&logger)) tc, err := NewClient(context.Background(), opts...) require.NoError(t, err) diff --git a/logging/logging.go b/logging/logging.go index ad3c439a..c1f1867e 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -9,7 +9,7 @@ import ( // GetDefaultLogger generates and returns a default logger instance. func GetDefaultLogger() *zerolog.Logger { - logger := ecszerolog.New(os.Stdout, ecszerolog.Level(zerolog.DebugLevel)). + logger := ecszerolog.New(os.Stdout, ecszerolog.Level(zerolog.InfoLevel)). With(). Timestamp(). Caller(). diff --git a/model_transaction_config_test.go b/model_transaction_config_test.go index fdc6a34d..07b48d67 100644 --- a/model_transaction_config_test.go +++ b/model_transaction_config_test.go @@ -9,6 +9,7 @@ import ( "github.com/BuxOrg/bux/chainstate" "github.com/BuxOrg/bux/utils" magic "github.com/bitcoinschema/go-map" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -193,9 +194,13 @@ func TestTransactionConfig_processOutput(t *testing.T) { t.Run("basic paymail address resolution - valid response", func(t *testing.T) { client := newTestPaymailClient(t, []string{testDomain}) + logger := zerolog.Nop() + tcOpts := DefaultClientOpts(true, true) + tcOpts = append(tcOpts, WithLogger(&logger)) + tc, err := NewClient( context.Background(), - DefaultClientOpts(true, true)..., + tcOpts..., ) require.NoError(t, err) require.NotNil(t, tc) @@ -232,9 +237,13 @@ func TestTransactionConfig_processOutput(t *testing.T) { client := newTestPaymailClient(t, []string{testDomain, handleDomain}) + logger := zerolog.Nop() + tcOpts := DefaultClientOpts(true, true) + tcOpts = append(tcOpts, WithLogger(&logger)) + tc, err := NewClient( context.Background(), - DefaultClientOpts(true, true)..., + tcOpts..., ) require.NoError(t, err) require.NotNil(t, tc) @@ -271,9 +280,13 @@ func TestTransactionConfig_processOutput(t *testing.T) { client := newTestPaymailClient(t, []string{testDomain, handleDomain}) + logger := zerolog.Nop() + tcOpts := DefaultClientOpts(true, true) + tcOpts = append(tcOpts, WithLogger(&logger)) + tc, err := NewClient( context.Background(), - DefaultClientOpts(true, true)..., + tcOpts..., ) require.NoError(t, err) require.NotNil(t, tc) @@ -307,9 +320,13 @@ func TestTransactionConfig_processOutput(t *testing.T) { t.Run("p2p paymail address resolution - valid response", func(t *testing.T) { client := newTestPaymailClient(t, []string{testDomain}) + logger := zerolog.Nop() + tcOpts := DefaultClientOpts(true, true) + tcOpts = append(tcOpts, WithLogger(&logger)) + tc, err := NewClient( context.Background(), - DefaultClientOpts(true, true)..., + tcOpts..., ) require.NoError(t, err) require.NotNil(t, tc) diff --git a/paymail_test.go b/paymail_test.go index 78c43343..1bc0b0eb 100644 --- a/paymail_test.go +++ b/paymail_test.go @@ -14,6 +14,7 @@ import ( "github.com/jarcoal/httpmock" "github.com/mrz1836/go-cache" "github.com/mrz1836/go-datastore" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -214,6 +215,7 @@ func Test_getCapabilities(t *testing.T) { testMaxActiveConnections, testMaxIdleConnections, ) + logger := zerolog.Nop() tc, err := NewClient(context.Background(), WithRedisConnection(redisClient), @@ -222,6 +224,7 @@ func Test_getCapabilities(t *testing.T) { WithChainstateOptions(false, false, false, false), WithDebugging(), WithMinercraft(&chainstate.MinerCraftBase{}), + WithLogger(&logger), ) require.NoError(t, err) require.NotNil(t, tc) @@ -254,6 +257,7 @@ func Test_getCapabilities(t *testing.T) { testMaxActiveConnections, testMaxIdleConnections, ) + logger := zerolog.Nop() tc, err := NewClient(context.Background(), WithRedisConnection(redisClient), @@ -262,6 +266,7 @@ func Test_getCapabilities(t *testing.T) { WithChainstateOptions(false, false, false, false), WithDebugging(), WithMinercraft(&chainstate.MinerCraftBase{}), + WithLogger(&logger), ) require.NoError(t, err) require.NotNil(t, tc) @@ -286,9 +291,13 @@ func Test_getCapabilities(t *testing.T) { t.Run("valid response - no cache found", func(t *testing.T) { client := newTestPaymailClient(t, []string{testDomain}) + logger := zerolog.Nop() + tcOpts := DefaultClientOpts(true, true) + tcOpts = append(tcOpts, WithLogger(&logger)) + tc, err := NewClient( context.Background(), - DefaultClientOpts(true, true)..., + tcOpts..., ) require.NoError(t, err) require.NotNil(t, tc) @@ -311,9 +320,13 @@ func Test_getCapabilities(t *testing.T) { t.Run("multiple requests for same capabilities", func(t *testing.T) { client := newTestPaymailClient(t, []string{testDomain}) + logger := zerolog.Nop() + tcOpts := DefaultClientOpts(true, true) + tcOpts = append(tcOpts, WithLogger(&logger)) + tc, err := NewClient( context.Background(), - DefaultClientOpts(true, true)..., + tcOpts..., ) require.NoError(t, err) require.NotNil(t, tc) @@ -357,6 +370,7 @@ func Test_resolvePaymailAddress(t *testing.T) { testMaxActiveConnections, testMaxIdleConnections, ) + logger := zerolog.Nop() tc, err := NewClient(context.Background(), WithRedisConnection(redisClient), @@ -365,6 +379,7 @@ func Test_resolvePaymailAddress(t *testing.T) { WithChainstateOptions(false, false, false, false), WithDebugging(), WithMinercraft(&chainstate.MinerCraftBase{}), + WithLogger(&logger), ) require.NoError(t, err) require.NotNil(t, tc) @@ -408,9 +423,13 @@ func Test_resolvePaymailAddress(t *testing.T) { t.Run("valid response - no cache found", func(t *testing.T) { client := newTestPaymailClient(t, []string{testDomain}) + logger := zerolog.Nop() + tcOpts := DefaultClientOpts(true, true) + tcOpts = append(tcOpts, WithLogger(&logger)) + tc, err := NewClient( context.Background(), - DefaultClientOpts(true, true)..., + tcOpts..., ) require.NoError(t, err) require.NotNil(t, tc) @@ -446,9 +465,13 @@ func Test_resolvePaymailAddress(t *testing.T) { t.Run("multiple requests for same address resolution", func(t *testing.T) { client := newTestPaymailClient(t, []string{testDomain}) + logger := zerolog.Nop() + tcOpts := DefaultClientOpts(true, true) + tcOpts = append(tcOpts, WithLogger(&logger)) + tc, err := NewClient( context.Background(), - DefaultClientOpts(true, true)..., + tcOpts..., ) require.NoError(t, err) require.NotNil(t, tc) diff --git a/taskmanager/client_options_test.go b/taskmanager/client_options_test.go index c7fc6af6..dca6cba3 100644 --- a/taskmanager/client_options_test.go +++ b/taskmanager/client_options_test.go @@ -3,7 +3,7 @@ package taskmanager import ( "testing" - "github.com/BuxOrg/bux/logging" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" ) @@ -92,10 +92,10 @@ func TestWithLogger(t *testing.T) { t.Run("test applying option", func(t *testing.T) { options := &clientOptions{} - customClient := logging.GetDefaultLogger() - opt := WithLogger(customClient) + customLogger := zerolog.Nop() + opt := WithLogger(&customLogger) opt(options) - assert.Equal(t, customClient, options.logger) + assert.Equal(t, &customLogger, options.logger) }) }