Skip to content

Commit

Permalink
services/horizon: Add unit test for new --ledgerbackend flag (#5382)
Browse files Browse the repository at this point in the history
* services/horizon: Reingest from precomputed TxMeta

* Global network config for reingestion

* Add unit test for new --ledgerbackend flag

* Fix unit test

* Add unittest for network flags validation
  • Loading branch information
urvisavla authored Jul 11, 2024
1 parent 4491049 commit 7234d6e
Showing 1 changed file with 175 additions and 2 deletions.
177 changes: 175 additions & 2 deletions services/horizon/cmd/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package cmd
import (
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

horizon "github.com/stellar/go/services/horizon/internal"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/ingest"
"github.com/stellar/go/support/db/dbtest"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

func TestDBCommandsTestSuite(t *testing.T) {
Expand All @@ -21,6 +22,20 @@ type DBCommandsTestSuite struct {
dsn string
}

func (s *DBCommandsTestSuite) SetupTest() {
resetFlags()
}

func resetFlags() {
RootCmd.ResetFlags()
dbFillGapsCmd.ResetFlags()
dbReingestRangeCmd.ResetFlags()

globalFlags.Init(RootCmd)
dbFillGapsCmdOpts.Init(dbFillGapsCmd)
dbReingestRangeCmdOpts.Init(dbReingestRangeCmd)
}

func (s *DBCommandsTestSuite) SetupSuite() {
runDBReingestRangeFn = func([]history.LedgerRange, bool, uint,
horizon.Config, ingest.StorageBackendConfig) error {
Expand Down Expand Up @@ -96,3 +111,161 @@ func (s *DBCommandsTestSuite) TestUsesParallelJobSizeWhenSetForBuffered() {
require.NoError(s.T(), RootCmd.Execute())
require.Equal(s.T(), parallelJobSize, uint32(5))
}

func (s *DBCommandsTestSuite) TestDbReingestAndFillGapsCmds() {
tests := []struct {
name string
args []string
ledgerBackend ingest.LedgerBackendType
expectError bool
errorMessage string
}{
{
name: "default; w/ individual network flags",
args: []string{
"1", "100",
"--network-passphrase", "passphrase",
"--history-archive-urls", "[]",
},
expectError: false,
},
{
name: "default; w/o individual network flags",
args: []string{
"1", "100",
},
expectError: true,
errorMessage: "network-passphrase must be set",
},
{
name: "default; no history-archive-urls flag",
args: []string{
"1", "100",
"--network-passphrase", "passphrase",
},
expectError: true,
errorMessage: "history-archive-urls must be set",
},
{
name: "default; w/ network parameter",
args: []string{
"1", "100",
"--network", "testnet",
},
expectError: false,
},
{
name: "datastore; w/ individual network flags",
args: []string{
"1", "100",
"--ledgerbackend", "datastore",
"--datastore-config", "../config.storagebackend.toml",
"--network-passphrase", "passphrase",
"--history-archive-urls", "[]",
},
expectError: false,
},
{
name: "datastore; w/o individual network flags",
args: []string{
"1", "100",
"--ledgerbackend", "datastore",
"--datastore-config", "../config.storagebackend.toml",
},
expectError: true,
errorMessage: "network-passphrase must be set",
},
{
name: "datastore; no history-archive-urls flag",
args: []string{
"1", "100",
"--ledgerbackend", "datastore",
"--datastore-config", "../config.storagebackend.toml",
"--network-passphrase", "passphrase",
},
expectError: true,
errorMessage: "history-archive-urls must be set",
},
{
name: "captive-core; valid",
args: []string{
"1", "100",
"--network", "testnet",
"--ledgerbackend", "captive-core",
},
expectError: false,
},
{
name: "invalid datastore",
args: []string{
"1", "100",
"--network", "testnet",
"--ledgerbackend", "unknown",
},
expectError: true,
errorMessage: "invalid ledger backend: unknown, must be 'captive-core' or 'datastore'",
},
{
name: "datastore; missing config file",
args: []string{
"1", "100",
"--network", "testnet",
"--ledgerbackend", "datastore",
"--datastore-config", "invalid.config.toml",
},
expectError: true,
errorMessage: "failed to load config file",
},
{
name: "datastore; w/ config",
args: []string{
"1", "100",
"--network", "testnet",
"--ledgerbackend", "datastore",
"--datastore-config", "../config.storagebackend.toml",
},
expectError: false,
},
{
name: "datastore; w/o config",
args: []string{
"1", "100",
"--network", "testnet",
"--ledgerbackend", "datastore",
},
expectError: true,
errorMessage: "datastore config file is required for datastore backend type",
},
}

commands := []struct {
cmd []string
name string
}{
{[]string{"db", "reingest", "range"}, "TestDbReingestRangeCmd"},
{[]string{"db", "fill-gaps"}, "TestDbFillGapsCmd"},
}

for _, command := range commands {
for _, tt := range tests {
s.T().Run(tt.name+"_"+command.name, func(t *testing.T) {
resetFlags()

var args []string
args = append(command.cmd, tt.args...)
RootCmd.SetArgs(append([]string{
"--db-url", s.dsn,
"--stellar-core-binary-path", "/test/core/bin/path",
}, args...))

if tt.expectError {
err := RootCmd.Execute()
require.Error(t, err)
require.Contains(t, err.Error(), tt.errorMessage)
} else {
require.NoError(t, RootCmd.Execute())
}
})
}
}
}

0 comments on commit 7234d6e

Please sign in to comment.