From ab1788b922e9909a0fbc4a7680c7e73b8c6ed5b0 Mon Sep 17 00:00:00 2001 From: "M. J. Fromberger" Date: Tue, 30 Nov 2021 12:57:44 -0800 Subject: [PATCH] Fix incorrect tests using the PSQL sink. (#7349) Some of our tests were creating a psql event sink and expecting it to report (or not report) certain kinds of errors. These tests were ill-founded in a couple of ways: 1. Tests that required the Postgres driver were not loading it. This led to spurious successes on tests that wanted "some error" from the sink constructor, but didn't exercise the right path. 2. Tests that wanted a Postgres sink to succeed without a database. These tests "passed" because they weren't actually establishing a connection to the database, but if they had would have failed for the lack of one. To fix this: - Load the postgres driver in tests that need it. - Verify connectivity before reporting successful creation of a PSQL event sink. - Remove tests that wanted a psql sink without a database, since that case is already tested elsewhere. --- cmd/tendermint/commands/reindex_event_test.go | 2 + internal/state/indexer/sink/psql/psql.go | 2 + node/node_test.go | 41 ++----------------- 3 files changed, 8 insertions(+), 37 deletions(-) diff --git a/cmd/tendermint/commands/reindex_event_test.go b/cmd/tendermint/commands/reindex_event_test.go index 452a6b2a8..c76ce6d9a 100644 --- a/cmd/tendermint/commands/reindex_event_test.go +++ b/cmd/tendermint/commands/reindex_event_test.go @@ -15,6 +15,8 @@ import ( "github.com/tendermint/tendermint/internal/state/mocks" prototmstate "github.com/tendermint/tendermint/proto/tendermint/state" "github.com/tendermint/tendermint/types" + + _ "github.com/lib/pq" // for the psql sink ) const ( diff --git a/internal/state/indexer/sink/psql/psql.go b/internal/state/indexer/sink/psql/psql.go index 4db6f4435..18e95b97d 100644 --- a/internal/state/indexer/sink/psql/psql.go +++ b/internal/state/indexer/sink/psql/psql.go @@ -39,6 +39,8 @@ func NewEventSink(connStr, chainID string) (*EventSink, error) { db, err := sql.Open(driverName, connStr) if err != nil { return nil, err + } else if err := db.Ping(); err != nil { + return nil, err } return &EventSink{ diff --git a/node/node_test.go b/node/node_test.go index 407bf93ea..3e4e83492 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -636,50 +636,17 @@ func TestNodeSetEventSink(t *testing.T) { assert.Contains(t, err.Error(), "the psql connection settings cannot be empty") t.Cleanup(cleanup(ns)) - var psqlConn = "test" - - cfg.TxIndex.Indexer = []string{"psql"} - cfg.TxIndex.PsqlConn = psqlConn - eventSinks = setupTest(t, cfg) - - assert.Equal(t, 1, len(eventSinks)) - assert.Equal(t, indexer.PSQL, eventSinks[0].Type()) - - cfg.TxIndex.Indexer = []string{"psql", "kv"} - cfg.TxIndex.PsqlConn = psqlConn - eventSinks = setupTest(t, cfg) - - assert.Equal(t, 2, len(eventSinks)) - // we use map to filter the duplicated sinks, so it's not guarantee the order when append sinks. - if eventSinks[0].Type() == indexer.KV { - assert.Equal(t, indexer.PSQL, eventSinks[1].Type()) - } else { - assert.Equal(t, indexer.PSQL, eventSinks[0].Type()) - assert.Equal(t, indexer.KV, eventSinks[1].Type()) - } - - cfg.TxIndex.Indexer = []string{"kv", "psql"} - cfg.TxIndex.PsqlConn = psqlConn - eventSinks = setupTest(t, cfg) - - assert.Equal(t, 2, len(eventSinks)) - if eventSinks[0].Type() == indexer.KV { - assert.Equal(t, indexer.PSQL, eventSinks[1].Type()) - } else { - assert.Equal(t, indexer.PSQL, eventSinks[0].Type()) - assert.Equal(t, indexer.KV, eventSinks[1].Type()) - } + // N.B. We can't create a PSQL event sink without starting a postgres + // instance for it to talk to. The indexer service tests exercise that case. var e = errors.New("found duplicated sinks, please check the tx-index section in the config.toml") - cfg.TxIndex.Indexer = []string{"psql", "kv", "Kv"} - cfg.TxIndex.PsqlConn = psqlConn + cfg.TxIndex.Indexer = []string{"null", "kv", "Kv"} ns, err = newDefaultNode(ctx, cfg, logger) require.Error(t, err) assert.Contains(t, err.Error(), e.Error()) t.Cleanup(cleanup(ns)) - cfg.TxIndex.Indexer = []string{"Psql", "kV", "kv", "pSql"} - cfg.TxIndex.PsqlConn = psqlConn + cfg.TxIndex.Indexer = []string{"Null", "kV", "kv", "nUlL"} ns, err = newDefaultNode(ctx, cfg, logger) require.Error(t, err) assert.Contains(t, err.Error(), e.Error())