Skip to content

Commit

Permalink
Add unittest for network flags validation
Browse files Browse the repository at this point in the history
  • Loading branch information
urvisavla committed Jul 11, 2024
1 parent 7ecb0a4 commit 776bb50
Showing 1 changed file with 115 additions and 16 deletions.
131 changes: 115 additions & 16 deletions services/horizon/cmd/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,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 @@ -107,38 +121,121 @@ func (s *DBCommandsTestSuite) TestDbReingestAndFillGapsCmds() {
errorMessage string
}{
{
name: "default ledgerbackend",
args: []string{"1", "100"},
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: "captive-core ledgerbackend",
args: []string{"1", "100", "--ledgerbackend", "captive-core"},
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: "invalid ledgerbackend",
args: []string{"1", "100", "--ledgerbackend", "unknown"},
name: "datastore; w/o individual network flags",
args: []string{
"1", "100",
"--ledgerbackend", "datastore",
"--datastore-config", "../config.storagebackend.toml",
},
expectError: true,
errorMessage: "invalid ledger backend: unknown, must be 'captive-core' or 'datastore'",
errorMessage: "network-passphrase must be set",
},
{
name: "datastore ledgerbackend without config",
args: []string{"1", "100", "--ledgerbackend", "datastore"},
name: "datastore; no history-archive-urls flag",
args: []string{
"1", "100",
"--ledgerbackend", "datastore",
"--datastore-config", "../config.storagebackend.toml",
"--network-passphrase", "passphrase",
},
expectError: true,
errorMessage: "datastore config file is required for datastore backend type",
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 ledgerbackend missing config file",
args: []string{"1", "100", "--ledgerbackend", "datastore", "--datastore-config", "invalid.config.toml"},
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 ledgerbackend",
args: []string{"1", "100", "--ledgerbackend", "datastore", "--datastore-config", "../config.storagebackend.toml"},
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 {
Expand All @@ -152,10 +249,12 @@ func (s *DBCommandsTestSuite) TestDbReingestAndFillGapsCmds() {
for _, command := range commands {
for _, tt := range tests {
s.T().Run(tt.name+"_"+command.name, func(t *testing.T) {

Check failure on line 251 in services/horizon/cmd/db_test.go

View workflow job for this annotation

GitHub Actions / golangci

s.T undefined (type *DBCommandsTestSuite has no field or method T) (typecheck)
args := append(command.cmd, tt.args...)
resetFlags()

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

Expand Down

0 comments on commit 776bb50

Please sign in to comment.