From e2f95d667c7bc80f1e33f8eb33c42a14ec068631 Mon Sep 17 00:00:00 2001 From: Karthik Iyer Date: Mon, 25 Nov 2024 11:34:26 -0800 Subject: [PATCH] Add helpers in intergration tests for creating captive core config --- .../internal/integration/parameters_test.go | 30 ++------ .../internal/test/integration/integration.go | 70 ++++++++++++++----- 2 files changed, 58 insertions(+), 42 deletions(-) diff --git a/services/horizon/internal/integration/parameters_test.go b/services/horizon/internal/integration/parameters_test.go index c7e0d0c75b..e8f55829b8 100644 --- a/services/horizon/internal/integration/parameters_test.go +++ b/services/horizon/internal/integration/parameters_test.go @@ -41,32 +41,10 @@ var networkParamArgs = map[string]string{ horizon.NetworkPassphraseFlagName: "", } -const ( - SimpleCaptiveCoreToml = ` - PEER_PORT=11725 - ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING=true - - UNSAFE_QUORUM=true - FAILURE_SAFETY=0 - - [[VALIDATORS]] - NAME="local_core" - HOME_DOMAIN="core.local" - PUBLIC_KEY="GD5KD2KEZJIGTC63IGW6UMUSMVUVG5IHG64HUTFWCHVZH2N2IBOQN7PS" - ADDRESS="localhost" - QUALITY="MEDIUM"` - - StellarCoreURL = "http://localhost:11626" -) - -var ( - CaptiveCoreConfigErrMsg = "error generating captive core configuration: invalid config: " -) - // Ensures that BUCKET_DIR_PATH is not an allowed value for Captive Core. func TestBucketDirDisallowed(t *testing.T) { config := `BUCKET_DIR_PATH="/tmp" - ` + SimpleCaptiveCoreToml + ` + integration.SimpleCaptiveCoreToml confName, _, cleanup := createCaptiveCoreConfig(config) defer cleanup() @@ -103,7 +81,7 @@ func TestEnvironmentPreserved(t *testing.T) { testConfig := integration.GetTestConfig() testConfig.HorizonEnvironment = map[string]string{ - "STELLAR_CORE_URL": StellarCoreURL, + "STELLAR_CORE_URL": integration.StellarCoreURL, } test := integration.NewTest(t, *testConfig) @@ -112,7 +90,7 @@ func TestEnvironmentPreserved(t *testing.T) { test.WaitForHorizonIngest() envValue := os.Getenv("STELLAR_CORE_URL") - assert.Equal(t, StellarCoreURL, envValue) + assert.Equal(t, integration.StellarCoreURL, envValue) test.Shutdown() @@ -252,7 +230,7 @@ func TestNetworkEnvironmentVariable(t *testing.T) { // Ensures that the filesystem ends up in the correct state with Captive Core. func TestCaptiveCoreConfigFilesystemState(t *testing.T) { - confName, storagePath, cleanup := createCaptiveCoreConfig(SimpleCaptiveCoreToml) + confName, storagePath, cleanup := createCaptiveCoreConfig(integration.SimpleCaptiveCoreToml) defer cleanup() localParams := integration.MergeMaps(defaultCaptiveCoreParameters, map[string]string{ diff --git a/services/horizon/internal/test/integration/integration.go b/services/horizon/internal/test/integration/integration.go index 107cb33759..a7683c0715 100644 --- a/services/horizon/internal/test/integration/integration.go +++ b/services/horizon/internal/test/integration/integration.go @@ -4,6 +4,7 @@ package integration import ( "context" "fmt" + "github.com/stellar/go/ingest/ledgerbackend" "io/ioutil" "os" "os/exec" @@ -41,13 +42,29 @@ import ( const ( StandaloneNetworkPassphrase = "Standalone Network ; February 2017" - stellarCorePostgresPassword = "mysecretpassword" - horizonDefaultPort = "8000" - adminPort = 6060 - stellarCorePort = 11626 - stellarCorePostgresPort = 5641 - historyArchivePort = 1570 - sorobanRPCPort = 8080 + HorizonDefaultPort = "8000" + AdminPort = 6060 + StellarCorePort = 11626 + HistoryArchivePort = 1570 + SorobanRPCPort = 8080 + HistoryArchiveUrl = "http://localhost:1570" +) + +const ( + SimpleCaptiveCoreToml = ` + PEER_PORT=11725 + ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING=true + + UNSAFE_QUORUM=true + FAILURE_SAFETY=0 + + [[VALIDATORS]] + NAME="local_core" + HOME_DOMAIN="core.local" + PUBLIC_KEY="GD5KD2KEZJIGTC63IGW6UMUSMVUVG5IHG64HUTFWCHVZH2N2IBOQN7PS" + ADDRESS="localhost" + QUALITY="MEDIUM"` + StellarCoreURL = "http://localhost:11626" ) const HorizonInitErrStr = "cannot initialize Horizon" @@ -163,7 +180,7 @@ func NewTest(t *testing.T, config Config) *Test { } i.prepareShutdownHandlers() - i.coreClient = &stellarcore.Client{URL: "http://localhost:" + strconv.Itoa(stellarCorePort)} + i.coreClient = &stellarcore.Client{URL: "http://localhost:" + strconv.Itoa(StellarCorePort)} if !config.SkipCoreContainerCreation { i.waitForCore() if i.config.EnableSorobanRPC { @@ -436,12 +453,12 @@ func (i *Test) getDefaultArgs() map[string]string { // Compose YAML file itself rather than hardcoding it. return map[string]string{ "ingest": "false", - "history-archive-urls": fmt.Sprintf("http://%s:%d", "localhost", historyArchivePort), + "history-archive-urls": HistoryArchiveUrl, "db-url": i.testDB.RO_DSN, "stellar-core-url": i.coreClient.URL, "network-passphrase": i.passPhrase, "apply-migrations": "true", - "port": horizonDefaultPort, + "port": HorizonDefaultPort, // due to ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING "checkpoint-frequency": "8", "per-hour-rate-limit": "0", // disable rate limiting @@ -537,7 +554,7 @@ func (i *Test) setupHorizonAdminClient(ingestArgs map[string]string) error { func (i *Test) setupHorizonClient(webArgs map[string]string) { hostname := "localhost" - horizonPort := horizonDefaultPort + horizonPort := HorizonDefaultPort if port, ok := webArgs["port"]; ok { horizonPort = port } @@ -547,6 +564,27 @@ func (i *Test) setupHorizonClient(webArgs map[string]string) { } } +func (i *Test) CreateDefaultCaptiveCoreConfig() (*ledgerbackend.CaptiveCoreConfig, error) { + captiveCoreConfig := ledgerbackend.CaptiveCoreConfig{ + BinaryPath: os.Getenv("HORIZON_INTEGRATION_TESTS_CAPTIVE_CORE_BIN"), + HistoryArchiveURLs: []string{HistoryArchiveUrl}, + NetworkPassphrase: StandaloneNetworkPassphrase, + CheckpointFrequency: 8, // This is required for accelerated archive creation for integration test + } + + tomlParams := ledgerbackend.CaptiveCoreTomlParams{ + NetworkPassphrase: StandaloneNetworkPassphrase, + HistoryArchiveURLs: []string{HistoryArchiveUrl}, + } + toml, err := ledgerbackend.NewCaptiveCoreTomlFromData([]byte(SimpleCaptiveCoreToml), tomlParams) + if err != nil { + return nil, err + } + + captiveCoreConfig.Toml = toml + return &captiveCoreConfig, nil +} + const maxWaitForCoreStartup = 30 * time.Second const maxWaitForCoreUpgrade = 5 * time.Second const coreStartupPingInterval = time.Second @@ -604,7 +642,7 @@ func (i *Test) waitForSorobanRPC() { for time.Since(start) < sorobanRPCInitTime { ctx, cancel := context.WithTimeout(context.Background(), sorobanRPCHealthCheckInterval) // TODO: soroban-tools should be exporting a proper Go client - ch := jhttp.NewChannel("http://localhost:"+strconv.Itoa(sorobanRPCPort), nil) + ch := jhttp.NewChannel("http://localhost:"+strconv.Itoa(SorobanRPCPort), nil) sorobanRPCClient := jrpc2.NewClient(ch, nil) callTime := time.Now() _, err := sorobanRPCClient.Call(ctx, "getHealth", nil) @@ -675,7 +713,7 @@ func (i *Test) simulateTransaction( i.syncWithSorobanRPC(uint32(root.HorizonSequence)) // TODO: soroban-tools should be exporting a proper Go client - ch := jhttp.NewChannel("http://localhost:"+strconv.Itoa(sorobanRPCPort), nil) + ch := jhttp.NewChannel("http://localhost:"+strconv.Itoa(SorobanRPCPort), nil) sorobanRPCClient := jrpc2.NewClient(ch, nil) txParams := GetBaseTransactionParamsWithFee(sourceAccount, txnbuild.MinBaseFee, op) txParams.IncrementSequenceNum = false @@ -702,7 +740,7 @@ func (i *Test) syncWithSorobanRPC(ledgerToWaitFor uint32) { result := struct { Sequence uint32 `json:"sequence"` }{} - ch := jhttp.NewChannel("http://localhost:"+strconv.Itoa(sorobanRPCPort), nil) + ch := jhttp.NewChannel("http://localhost:"+strconv.Itoa(SorobanRPCPort), nil) sorobanRPCClient := jrpc2.NewClient(ch, nil) err := sorobanRPCClient.CallResult(context.Background(), "getLatestLedger", nil, &result) assert.NoError(i.t, err) @@ -715,7 +753,7 @@ func (i *Test) syncWithSorobanRPC(ledgerToWaitFor uint32) { } func (i *Test) WaitUntilLedgerEntryTTL(ledgerKey xdr.LedgerKey) { - ch := jhttp.NewChannel("http://localhost:"+strconv.Itoa(sorobanRPCPort), nil) + ch := jhttp.NewChannel("http://localhost:"+strconv.Itoa(SorobanRPCPort), nil) client := jrpc2.NewClient(ch, nil) keyB64, err := xdr.MarshalBase64(ledgerKey) @@ -933,7 +971,7 @@ func (i *Test) StopHorizon() { // AdminPort returns Horizon admin port. func (i *Test) AdminPort() int { - return adminPort + return AdminPort } // Metrics URL returns Horizon metrics URL.