Skip to content

Commit

Permalink
#8: added integration test coverage to assert user agent config on hi…
Browse files Browse the repository at this point in the history
…story archives
  • Loading branch information
sreuland committed Feb 9, 2024
1 parent 2c465a5 commit 5028a92
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 32 deletions.
25 changes: 25 additions & 0 deletions cmd/soroban-rpc/internal/test/archive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package test

import (
"net/http"
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

func TestArchiveUserAgent(t *testing.T) {
userAgents := sync.Map{}
cfg := &Test{
historyArchiveProxyCallback: func(r *http.Request) {
userAgents.Store(r.Header["User-Agent"][0], "")
},
}
NewTest(t, cfg)

_, ok := userAgents.Load("testing")
assert.True(t, ok, "rpc service should set user agent for history archives")

_, ok = userAgents.Load("testing/captivecore")
assert.True(t, ok, "rpc captive core should set user agent for history archives")
}
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func getCLIDefaultAccount(t *testing.T) string {
}

func NewCLITest(t *testing.T) *Test {
test := NewTest(t)
test := NewTest(t, nil)
fundAccount(t, test, getCLIDefaultAccount(t), "1000000")
return test
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/test/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Specifically, when we include an Origin header in the request, a soroban-rpc should response
// with a corresponding Access-Control-Allow-Origin.
func TestCORS(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

request, err := http.NewRequest("POST", test.sorobanRPCURL(), bytes.NewBufferString("{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"getHealth\"}"))
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/soroban-rpc/internal/test/get_ledger_entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func TestGetLedgerEntriesNotFound(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -56,7 +56,7 @@ func TestGetLedgerEntriesNotFound(t *testing.T) {
}

func TestGetLedgerEntriesInvalidParams(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand All @@ -74,7 +74,7 @@ func TestGetLedgerEntriesInvalidParams(t *testing.T) {
}

func TestGetLedgerEntriesSucceeds(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down
6 changes: 3 additions & 3 deletions cmd/soroban-rpc/internal/test/get_ledger_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

func TestGetLedgerEntryNotFound(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestGetLedgerEntryNotFound(t *testing.T) {
}

func TestGetLedgerEntryInvalidParams(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand All @@ -67,7 +67,7 @@ func TestGetLedgerEntryInvalidParams(t *testing.T) {
}

func TestGetLedgerEntrySucceeds(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/test/get_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestGetNetworkSucceeds(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/test/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestHealth(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down
37 changes: 32 additions & 5 deletions cmd/soroban-rpc/internal/test/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -47,14 +51,17 @@ type Test struct {

daemon *daemon.Daemon

historyArchiveProxy *httptest.Server
historyArchiveProxyCallback func(*http.Request)

coreClient *stellarcore.Client

masterAccount txnbuild.Account
shutdownOnce sync.Once
shutdownCalls []func()
}

func NewTest(t *testing.T) *Test {
func NewTest(t *testing.T, i *Test) *Test {
if os.Getenv("SOROBAN_RPC_INTEGRATION_TESTS_ENABLED") == "" {
t.Skip("skipping integration test: SOROBAN_RPC_INTEGRATION_TESTS_ENABLED not set")
}
Expand All @@ -63,14 +70,30 @@ func NewTest(t *testing.T) *Test {
t.Fatal("missing SOROBAN_RPC_INTEGRATION_TESTS_CAPTIVE_CORE_BIN")
}

i := &Test{
t: t,
composePath: findDockerComposePath(),
if i == nil {
i = &Test{}
}

i.t = t
i.composePath = findDockerComposePath()
i.masterAccount = &txnbuild.SimpleAccount{
AccountID: i.MasterKey().Address(),
Sequence: 0,
}

origin, err := url.Parse("http://localhost:1570")
if err != nil {
panic(err)
}
proxy := httputil.NewSingleHostReverseProxy(origin)

i.historyArchiveProxy = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if i.historyArchiveProxyCallback != nil {
i.historyArchiveProxyCallback(r)
}
proxy.ServeHTTP(w, r)
}))

i.runComposeCommand("up", "--detach", "--quiet-pull", "--no-color")
i.prepareShutdownHandlers()
i.coreClient = &stellarcore.Client{URL: "http://localhost:" + strconv.Itoa(stellarCorePort)}
Expand Down Expand Up @@ -138,14 +161,15 @@ func (i *Test) launchDaemon(coreBinaryPath string) {
config.CaptiveCoreHTTPPort = 0
config.FriendbotURL = friendbotURL
config.NetworkPassphrase = StandaloneNetworkPassphrase
config.HistoryArchiveURLs = []string{"http://localhost:1570"}
config.HistoryArchiveURLs = []string{i.historyArchiveProxy.URL}
config.LogLevel = logrus.DebugLevel
config.SQLiteDBPath = path.Join(i.t.TempDir(), "soroban_rpc.sqlite")
config.IngestionTimeout = 10 * time.Minute
config.EventLedgerRetentionWindow = ledgerbucketwindow.DefaultEventLedgerRetentionWindow
config.CheckpointFrequency = checkpointFrequency
config.MaxHealthyLedgerLatency = time.Second * 10
config.PreflightEnableDebug = true
config.HistoryArchiveUserAgent = "testing"

i.daemon = daemon.MustNew(&config)
go i.daemon.Run()
Expand Down Expand Up @@ -203,6 +227,9 @@ func (i *Test) prepareShutdownHandlers() {
if i.daemon != nil {
i.daemon.Close()
}
if i.historyArchiveProxy != nil {
i.historyArchiveProxy.Close()
}
i.runComposeCommand("down", "-v")
},
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/test/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestMetrics(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)
metrics := getMetrics(test)
buildMetric := fmt.Sprintf(
"soroban_rpc_build_info{branch=\"%s\",build_timestamp=\"%s\",commit=\"%s\",goversion=\"%s\",version=\"%s\"} 1",
Expand Down
20 changes: 10 additions & 10 deletions cmd/soroban-rpc/internal/test/simulate_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func preflightTransactionParams(t *testing.T, client *jrpc2.Client, params txnbu
}

func TestSimulateTransactionSucceeds(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -307,7 +307,7 @@ func TestSimulateTransactionSucceeds(t *testing.T) {
}

func TestSimulateTransactionWithAuth(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestSimulateTransactionWithAuth(t *testing.T) {
}

func TestSimulateInvokeContractTransactionSucceeds(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -550,7 +550,7 @@ func TestSimulateInvokeContractTransactionSucceeds(t *testing.T) {
}

func TestSimulateTransactionError(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -590,7 +590,7 @@ func TestSimulateTransactionError(t *testing.T) {
}

func TestSimulateTransactionMultipleOperations(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -625,7 +625,7 @@ func TestSimulateTransactionMultipleOperations(t *testing.T) {
}

func TestSimulateTransactionWithoutInvokeHostFunction(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -656,7 +656,7 @@ func TestSimulateTransactionWithoutInvokeHostFunction(t *testing.T) {
}

func TestSimulateTransactionUnmarshalError(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand All @@ -673,7 +673,7 @@ func TestSimulateTransactionUnmarshalError(t *testing.T) {
}

func TestSimulateTransactionExtendAndRestoreFootprint(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -906,7 +906,7 @@ func waitUntilLedgerEntryTTL(t *testing.T, client *jrpc2.Client, ledgerKey xdr.L
}

func TestSimulateInvokePrng_u64_in_range(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -1017,7 +1017,7 @@ func TestSimulateInvokePrng_u64_in_range(t *testing.T) {
}

func TestSimulateSystemEvent(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down
12 changes: 6 additions & 6 deletions cmd/soroban-rpc/internal/test/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

func TestSendTransactionSucceedsWithoutResults(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand All @@ -46,7 +46,7 @@ func TestSendTransactionSucceedsWithoutResults(t *testing.T) {
}

func TestSendTransactionSucceedsWithResults(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestSendTransactionSucceedsWithResults(t *testing.T) {
}

func TestSendTransactionBadSequence(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestSendTransactionBadSequence(t *testing.T) {
}

func TestSendTransactionFailedInsufficientResourceFee(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestSendTransactionFailedInsufficientResourceFee(t *testing.T) {
}

func TestSendTransactionFailedInLedger(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down Expand Up @@ -266,7 +266,7 @@ func TestSendTransactionFailedInLedger(t *testing.T) {
}

func TestSendTransactionFailedInvalidXDR(t *testing.T) {
test := NewTest(t)
test := NewTest(t, nil)

ch := jhttp.NewChannel(test.sorobanRPCURL(), nil)
client := jrpc2.NewClient(ch, nil)
Expand Down

0 comments on commit 5028a92

Please sign in to comment.