Skip to content

Commit

Permalink
rework: getCaptiveCoreConfig tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-burghardt committed May 28, 2024
1 parent f951c14 commit 65edfa1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
12 changes: 0 additions & 12 deletions cmd/utils/custom_set_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"errors"
"fmt"
"os"
"path"

"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/stellar/go/keypair"
"github.com/stellar/go/support/config"
"github.com/stellar/go/support/log"
"github.com/stellar/wallet-backend/internal/ingest"
)

func unexpectedTypeError(key any, co *config.ConfigOption) error {
Expand Down Expand Up @@ -92,16 +90,6 @@ func SetConfigOptionCaptiveCoreConfigDir(co *config.ConfigOption) error {
return fmt.Errorf("captive core configuration files dir %s is not a directory", dirPath)
}

testnetConfigFile := path.Join(dirPath, ingest.ConfigFileNameTestnet)
if _, err := os.Stat(testnetConfigFile); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("captive core testnet configuration file %s does not exist in dir %s", testnetConfigFile, dirPath)
}

pubnetConfigFile := path.Join(dirPath, ingest.ConfigFileNamePubnet)
if _, err := os.Stat(pubnetConfigFile); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("captive core pubnet configuration file %s does not exist in dir %s", pubnetConfigFile, dirPath)
}

key, ok := co.ConfigKey.(*string)
if !ok {
return unexpectedTypeError(key, co)
Expand Down
5 changes: 0 additions & 5 deletions cmd/utils/custom_set_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,6 @@ func TestSetConfigOptionCaptiveCoreConfigDir(t *testing.T) {
envValue: "./custom_set_value_test.go",
wantErrContains: "captive core configuration files dir ./custom_set_value_test.go is not a directory",
},
{
name: "returns an error if the directory does not contain the configuration files",
envValue: "./",
wantErrContains: "captive core testnet configuration file stellar-core_testnet.cfg does not exist in dir ./",
},
{
name: "sets to ENV var value",
envValue: "../../internal/ingest/config",
Expand Down
20 changes: 13 additions & 7 deletions internal/ingest/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package ingest

import (
"context"
"errors"
"fmt"
"os"
"path"

"github.com/sirupsen/logrus"
Expand All @@ -14,11 +16,6 @@ import (
"github.com/stellar/wallet-backend/internal/services"
)

const (
ConfigFileNamePubnet = "stellar-core_pubnet.cfg"
ConfigFileNameTestnet = "stellar-core_testnet.cfg"
)

type Configs struct {
DatabaseURL string
NetworkPassphrase string
Expand Down Expand Up @@ -74,21 +71,30 @@ func setupDeps(cfg Configs) (*services.IngestManager, error) {
}, nil
}

const (
configFileNamePubnet = "stellar-core_pubnet.cfg"
configFileNameTestnet = "stellar-core_testnet.cfg"
)

func getCaptiveCoreConfig(cfg Configs) (ledgerbackend.CaptiveCoreConfig, error) {
var networkArchivesURLs []string
var configFilePath string

switch cfg.NetworkPassphrase {
case network.TestNetworkPassphrase:
networkArchivesURLs = network.TestNetworkhistoryArchiveURLs
configFilePath = path.Join(cfg.CaptiveCoreConfigDir, ConfigFileNameTestnet)
configFilePath = path.Join(cfg.CaptiveCoreConfigDir, configFileNameTestnet)
case network.PublicNetworkPassphrase:
networkArchivesURLs = network.PublicNetworkhistoryArchiveURLs
configFilePath = path.Join(cfg.CaptiveCoreConfigDir, ConfigFileNamePubnet)
configFilePath = path.Join(cfg.CaptiveCoreConfigDir, configFileNamePubnet)
default:
return ledgerbackend.CaptiveCoreConfig{}, fmt.Errorf("unknown network: %s", cfg.NetworkPassphrase)
}

if _, err := os.Stat(configFilePath); errors.Is(err, os.ErrNotExist) {
return ledgerbackend.CaptiveCoreConfig{}, fmt.Errorf("captive core configuration file not found in %s", configFilePath)
}

// Read configuration TOML
captiveCoreToml, err := ledgerbackend.NewCaptiveCoreTomlFromFile(configFilePath, ledgerbackend.CaptiveCoreTomlParams{
CoreBinaryPath: cfg.CaptiveCoreBinPath,
Expand Down
61 changes: 61 additions & 0 deletions internal/ingest/ingest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ingest

import (
"testing"

"github.com/stellar/go/network"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetCaptiveCoreConfig(t *testing.T) {
t.Run("testnet_success", func(t *testing.T) {
config, err := getCaptiveCoreConfig(Configs{
NetworkPassphrase: network.TestNetworkPassphrase,
CaptiveCoreBinPath: "/bin/path",
CaptiveCoreConfigDir: "./config",
})

require.NoError(t, err)
assert.Equal(t, "/bin/path", config.BinaryPath)
assert.Equal(t, network.TestNetworkPassphrase, config.NetworkPassphrase)
assert.Equal(t, network.TestNetworkhistoryArchiveURLs, config.HistoryArchiveURLs)
assert.Equal(t, true, config.UseDB)
assert.NotNil(t, config.Toml)
})

t.Run("pubnet_success", func(t *testing.T) {
config, err := getCaptiveCoreConfig(Configs{
NetworkPassphrase: network.PublicNetworkPassphrase,
CaptiveCoreBinPath: "/bin/path",
CaptiveCoreConfigDir: "./config",
})

require.NoError(t, err)
assert.Equal(t, "/bin/path", config.BinaryPath)
assert.Equal(t, network.PublicNetworkPassphrase, config.NetworkPassphrase)
assert.Equal(t, network.PublicNetworkhistoryArchiveURLs, config.HistoryArchiveURLs)
assert.Equal(t, true, config.UseDB)
assert.NotNil(t, config.Toml)
})

t.Run("unknown_network", func(t *testing.T) {
_, err := getCaptiveCoreConfig(Configs{
NetworkPassphrase: "Invalid SDF Network ; May 2024",
CaptiveCoreBinPath: "/bin/path",
CaptiveCoreConfigDir: "./config",
})

assert.ErrorContains(t, err, "unknown network: Invalid SDF Network ; May 2024")
})

t.Run("invalid_config_file", func(t *testing.T) {
_, err := getCaptiveCoreConfig(Configs{
NetworkPassphrase: network.TestNetworkPassphrase,
CaptiveCoreBinPath: "/bin/path",
CaptiveCoreConfigDir: "./invalid/path",
})

assert.ErrorContains(t, err, "captive core configuration file not found in invalid/path/stellar-core_testnet.cfg")
})
}

0 comments on commit 65edfa1

Please sign in to comment.