From 832e137b4bb11af393aa50c4f10955092ff5d57f Mon Sep 17 00:00:00 2001 From: krehermann Date: Tue, 26 Sep 2023 11:57:54 -0600 Subject: [PATCH] move test code --- core/internal/testutils/evmtest/evmtest.go | 7 ++---- core/internal/testutils/pgtest/pgtest.go | 12 ++--------- core/internal/testutils/pgtest/txdb.go | 6 ------ core/services/pg/locked_db.go | 25 ++++++++++------------ core/services/pg/locked_db_test.go | 14 ++++++++++++ 5 files changed, 29 insertions(+), 35 deletions(-) diff --git a/core/internal/testutils/evmtest/evmtest.go b/core/internal/testutils/evmtest/evmtest.go index 2332de24dc3..fa7023ee2cb 100644 --- a/core/internal/testutils/evmtest/evmtest.go +++ b/core/internal/testutils/evmtest/evmtest.go @@ -12,7 +12,6 @@ import ( "github.com/smartcontractkit/sqlx" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "github.com/test-go/testify/assert" "golang.org/x/exp/slices" "gopkg.in/guregu/null.v4" @@ -373,8 +372,6 @@ func (r *RawSub[T]) TrySend(t T) { } func NewScopedDB(t testing.TB, cfg config.Database) *sqlx.DB { - // hack to scope to evm schema. the value "evm" will need to be dynamic to support multiple relayers - url, err := pg.SchemaScopedConnection(cfg.URL(), "evm") - assert.NoError(t, err, "failed to create evm scoped db") - return pgtest.NewSqlxDB(t, pgtest.WithURL(*url)) + u := pg.SchemaScopedConnection(cfg.URL(), "evm") + return pgtest.NewSqlxDB(t, pg.WithURL(u)) } diff --git a/core/internal/testutils/pgtest/pgtest.go b/core/internal/testutils/pgtest/pgtest.go index f47a1853599..a9adbe12da5 100644 --- a/core/internal/testutils/pgtest/pgtest.go +++ b/core/internal/testutils/pgtest/pgtest.go @@ -39,12 +39,12 @@ func uniqueConnection(t testing.TB) *url.URL { return url } -func NewSqlxDB(t testing.TB, opts ...ConnectionOpt) *sqlx.DB { +func NewSqlxDB(t testing.TB, opts ...pg.ConnectionOpt) *sqlx.DB { testutils.SkipShortDB(t) url := uniqueConnection(t) for _, opt := range opts { - opt(url) + assert.NoError(t, opt(url)) } db, err := sqlx.Open(string(dialects.TransactionWrappedPostgres), url.String()) require.NoError(t, err) @@ -55,14 +55,6 @@ func NewSqlxDB(t testing.TB, opts ...ConnectionOpt) *sqlx.DB { return db } -type ConnectionOpt func(*url.URL) - -func WithURL(override url.URL) ConnectionOpt { - return func(u *url.URL) { - u = &override - } -} - func MustExec(t *testing.T, db *sqlx.DB, stmt string, args ...interface{}) { require.NoError(t, utils.JustError(db.Exec(stmt, args...))) } diff --git a/core/internal/testutils/pgtest/txdb.go b/core/internal/testutils/pgtest/txdb.go index 4f1273121d2..7f008247229 100644 --- a/core/internal/testutils/pgtest/txdb.go +++ b/core/internal/testutils/pgtest/txdb.go @@ -80,12 +80,6 @@ type txDriver struct { conns map[string]map[string]*conn // url -> (uuid -> db) so we can close per url } -func cleanseURL(u *url.URL) { - q := u.Query() - q.Del("uuid") - u.RawQuery = q.Encode() - -} func (d *txDriver) Open(connection string) (driver.Conn, error) { d.Lock() defer d.Unlock() diff --git a/core/services/pg/locked_db.go b/core/services/pg/locked_db.go index fae5bfa88e5..f4f8bfa3b36 100644 --- a/core/services/pg/locked_db.go +++ b/core/services/pg/locked_db.go @@ -2,11 +2,12 @@ package pg import ( "context" + "errors" + "fmt" "net/url" "time" "github.com/google/uuid" - "github.com/pkg/errors" "github.com/smartcontractkit/sqlx" @@ -71,7 +72,7 @@ func (l *lockedDb) Open(ctx context.Context, opts ...ConnectionOpt) (err error) l.db, err = openDB(l.appID, l.cfg) if err != nil { // l.db will be nil in case of error - return errors.Wrap(err, "failed to open db") + return fmt.Errorf("failed to open db: %w", err) } revert := func() { // Let Open() return the actual error, while l.Close() error is just logged. @@ -99,7 +100,7 @@ func (l *lockedDb) Open(ctx context.Context, opts ...ConnectionOpt) (err error) l.leaseLock = NewLeaseLock(l.db, l.appID, l.lggr, cfg) if err = l.leaseLock.TakeAndHold(ctx); err != nil { defer revert() - return errors.Wrap(err, "failed to take initial lease on database") + return fmt.Errorf("failed to take initial lease on database: %w", err) } } @@ -145,7 +146,7 @@ func openDB(appID uuid.UUID, cfg LockedDBConfig, opts ...ConnectionOpt) (db *sql static.SetConsumerName(&uri, "App", &appID) dialect := cfg.Dialect() for _, opt := range opts { - opt(&uri) + err = errors.Join(err, opt(&uri)) } db, err = NewConnection(uri.String(), dialect, cfg) return @@ -156,32 +157,28 @@ type ConnectionOpt func(*url.URL) error // WithSchema scopes the current url connection string to the given schema func WithSchema(schema string) ConnectionOpt { return func(url *url.URL) (err error) { - url, err = SchemaScopedConnection(*url, schema) + *url = SchemaScopedConnection(*url, schema) return } } // WithURL sets the url connection string -func WithURL(override string) ConnectionOpt { +func WithURL(override url.URL) ConnectionOpt { return func(url *url.URL) (err error) { - url, err = url.Parse(override) + (*url) = override return } } -func SchemaScopedConnection(conn url.URL, schema string) (*url.URL, error) { +func SchemaScopedConnection(conn url.URL, schema string) url.URL { // documentation on this options is limited; find by searching the official postgres docs // https://www.postgresql.org/docs/16/app-psql.html - //TODO real parsing for connection query string + u := conn opt := "options=-csearch_path=" - u, err := url.Parse(conn.String()) - if err != nil { - return nil, err - } queryVals := u.Query() queryVals.Add(opt, schema) u.RawQuery = queryVals.Encode() - return u, nil + return u } diff --git a/core/services/pg/locked_db_test.go b/core/services/pg/locked_db_test.go index aaf7ebf32c4..6f1007378f0 100644 --- a/core/services/pg/locked_db_test.go +++ b/core/services/pg/locked_db_test.go @@ -2,9 +2,12 @@ package pg_test import ( "context" + "net/url" "testing" "time" + "github.com/test-go/testify/assert" + "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" configtest "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest/v2" "github.com/smartcontractkit/chainlink/v2/core/logger" @@ -102,3 +105,14 @@ func TestOpenUnlockedDB(t *testing.T) { require.NoError(t, db1.Close()) require.NoError(t, db2.Close()) } + +func TestConnectionOpt(t *testing.T) { + t.Parallel() + + url := new(url.URL) + override, err := url.Parse("postgres://postgres:password@localhost:5432/chainlink_dev_test?sslmode=disable") + require.NoError(t, err) + pg.WithURL(*override)(url) + + assert.EqualValues(t, override, url) +}