diff --git a/node/node.go b/node/node.go index 05e847e00..f18e07333 100644 --- a/node/node.go +++ b/node/node.go @@ -53,6 +53,8 @@ import ( "github.com/line/ostracon/types" tmtime "github.com/line/ostracon/types/time" "github.com/line/ostracon/version" + + _ "github.com/lib/pq" // Register the Postgres database driver. ) //------------------------------------------------------------------------------ diff --git a/node/node_test.go b/node/node_test.go index 49edadb32..e19090f90 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -9,6 +9,9 @@ import ( "testing" "time" + "github.com/ory/dockertest" + "github.com/ory/dockertest/docker" + "github.com/line/tm-db/v2/memdb" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -465,6 +468,59 @@ func TestNodeNewNodeTxIndexIndexer(t *testing.T) { require.NoError(t, err) require.NotNil(t, n) } + { + // Change to psql for test + config.TxIndex.Indexer = "psql" + n, err := doTest(DefaultDBProvider) + require.Error(t, err) + require.Equal(t, "no psql-conn is set for the \"psql\" indexer", err.Error()) + require.Nil(t, n) + + //config.TxIndex.PsqlConn = "cannot test with no-import postgres driver" + //n, err = doTest(DefaultDBProvider) + //require.Error(t, err) + //require.Equal(t, "creating psql indexer: sql: unknown driver \"postgres\" (forgotten import?)", err.Error()) + //require.Nil(t, n) + + config.TxIndex.PsqlConn = makeTestPsqlConn(t) + n, err = doTest(DefaultDBProvider) + require.NoError(t, err) + require.NotNil(t, n) + } +} + +func makeTestPsqlConn(t *testing.T) string { + user := "postgres" + password := "secret" + port := "5432" + dsn := "postgres://%s:%s@localhost:%s/%s?sslmode=disable" + dbName := "postgres" + + pool, err := dockertest.NewPool(os.Getenv("DOCKER_URL")) + if err != nil { + require.NoError(t, err) + } + resource, err := pool.RunWithOptions(&dockertest.RunOptions{ + Repository: "postgres", + Tag: "13", + Env: []string{ + "POSTGRES_USER=" + user, + "POSTGRES_PASSWORD=" + password, + "POSTGRES_DB=" + dbName, + "listen_addresses = '*'", + }, + ExposedPorts: []string{port}, + }, func(config *docker.HostConfig) { + // set AutoRemove to true so that stopped container goes away by itself + config.AutoRemove = true + config.RestartPolicy = docker.RestartPolicy{ + Name: "no", + } + }) + if err != nil { + require.NoError(t, err) + } + return fmt.Sprintf(dsn, user, password, resource.GetPort(port+"/tcp"), dbName) } func state(nVals int, height int64) (sm.State, dbm.DB, []types.PrivValidator) { diff --git a/state/indexer/sink/psql/backport_test.go b/state/indexer/sink/psql/backport_test.go index 5fb8c884f..ed36be7d0 100644 --- a/state/indexer/sink/psql/backport_test.go +++ b/state/indexer/sink/psql/backport_test.go @@ -1,11 +1,82 @@ package psql import ( + "context" + "testing" + + abci "github.com/line/ostracon/abci/types" + "github.com/line/ostracon/libs/pubsub/query" "github.com/line/ostracon/state/indexer" "github.com/line/ostracon/state/txindex" + "github.com/line/ostracon/types" + "github.com/stretchr/testify/require" ) var ( _ indexer.BlockIndexer = BackportBlockIndexer{} _ txindex.TxIndexer = BackportTxIndexer{} ) + +func TestBackportTxIndexer_AddBatch(t *testing.T) { + indexer := &EventSink{store: testDB(), chainID: chainID} + txIndexer := indexer.TxIndexer() + err := txIndexer.AddBatch(&txindex.Batch{}) + require.NoError(t, err) +} + +func TestBackportTxIndexer_Index(t *testing.T) { + indexer := &EventSink{store: testDB(), chainID: chainID} + txIndexer := indexer.TxIndexer() + err := txIndexer.Index(&abci.TxResult{}) + require.Error(t, err) + require.Contains(t, err.Error(), "finding block ID: ") + + blockIndexer := indexer.BlockIndexer() + err = blockIndexer.Index(types.EventDataNewBlockHeader{}) + require.NoError(t, err) + err = txIndexer.Index(&abci.TxResult{}) + require.NoError(t, err) +} + +func TestBackportTxIndexer_Get(t *testing.T) { + indexer := &EventSink{store: testDB(), chainID: chainID} + txIndexer := indexer.TxIndexer() + result, err := txIndexer.Get([]byte{1}) + require.Error(t, err) + require.Equal(t, "the TxIndexer.Get method is not supported", err.Error()) + require.Nil(t, result) +} + +func TestBackportTxIndexer_Search(t *testing.T) { + indexer := &EventSink{store: testDB(), chainID: chainID} + txIndexer := indexer.TxIndexer() + result, err := txIndexer.Get([]byte{1}) + require.Error(t, err) + require.Equal(t, "the TxIndexer.Get method is not supported", err.Error()) + require.Nil(t, result) +} + +func TestBackportBlockIndexer_Has(t *testing.T) { + indexer := &EventSink{store: testDB(), chainID: chainID} + blockIndexer := indexer.BlockIndexer() + result, err := blockIndexer.Has(0) + require.Error(t, err) + require.Equal(t, "the BlockIndexer.Has method is not supported", err.Error()) + require.False(t, result) +} + +func TestBackportBlockIndexer_Index(t *testing.T) { + indexer := &EventSink{store: testDB(), chainID: chainID} + blockIndexer := indexer.BlockIndexer() + err := blockIndexer.Index(types.EventDataNewBlockHeader{}) + require.NoError(t, err) +} + +func TestBackportBlockIndexer_Search(t *testing.T) { + indexer := &EventSink{store: testDB(), chainID: chainID} + blockIndexer := indexer.BlockIndexer() + result, err := blockIndexer.Search(context.Background(), &query.Query{}) + require.Error(t, err) + require.Equal(t, "the BlockIndexer.Search method is not supported", err.Error()) + require.Nil(t, result) +}