Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and parallelize integration tests #216

Merged
merged 15 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/soroban-rpc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ jobs:
- name: Run Soroban RPC Integration Tests
run: |
make install_rust
go test -race -timeout 60m -v ./cmd/soroban-rpc/internal/test/...
go test -race -timeout 60m -v ./cmd/soroban-rpc/internal/integrationtest/...
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package integrationtest

import (
"net"
Expand All @@ -11,10 +11,13 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/integrationtest/infrastructure"
)

func TestArchiveUserAgent(t *testing.T) {
archiveHost := net.JoinHostPort("localhost", strconv.Itoa(StellarCoreArchivePort))
ports := infrastructure.NewTestPorts(t)
archiveHost := net.JoinHostPort("localhost", strconv.Itoa(int(ports.CoreArchivePort)))
proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: archiveHost})
userAgents := sync.Map{}
historyArchiveProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -23,11 +26,12 @@ func TestArchiveUserAgent(t *testing.T) {
}))
defer historyArchiveProxy.Close()

cfg := &TestConfig{
cfg := &infrastructure.TestConfig{
TestPorts: &ports,
HistoryArchiveURL: historyArchiveProxy.URL,
}

NewTest(t, cfg)
infrastructure.NewTest(t, cfg)

_, ok := userAgents.Load("soroban-rpc/0.0.0")
assert.True(t, ok, "rpc service should set user agent for history archives")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package integrationtest

import (
"bytes"
Expand All @@ -7,15 +7,17 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/integrationtest/infrastructure"
)

// TestCORS ensures that we receive the correct CORS headers as a response to an HTTP request.
// 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, nil)
test := infrastructure.NewTest(t, nil)

request, err := http.NewRequest("POST", test.sorobanRPCURL(), bytes.NewBufferString("{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"getHealth\"}"))
request, err := http.NewRequest("POST", test.GetSorobanRPCURL(), bytes.NewBufferString("{\"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"getHealth\"}"))
require.NoError(t, err)
request.Header.Set("Content-Type", "application/json")
origin := "testorigin.com"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
package test
package integrationtest

import (
"context"
"testing"

"github.com/stellar/go/keypair"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/integrationtest/infrastructure"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/methods"
)

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

client := test.GetRPCLient()
sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
account := txnbuild.NewSimpleAccount(address, 0)

// Submit soroban transaction
contractBinary := getHelloWorldContract(t)
params := preflightTransactionParams(t, client, txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
createInstallContractCodeOperation(account.AccountID, contractBinary),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
})
tx, err := txnbuild.NewTransaction(params)
assert.NoError(t, err)
sorobanTxResponse := sendSuccessfulTransaction(t, client, sourceAccount, tx)
sorobanTxResponse, _ := test.UploadHelloWorldContract()
var sorobanTxResult xdr.TransactionResult
require.NoError(t, xdr.SafeUnmarshalBase64(sorobanTxResponse.ResultXdr, &sorobanTxResult))
sorobanTotalFee := sorobanTxResult.FeeCharged
Expand All @@ -46,28 +26,18 @@ func TestGetFeeStats(t *testing.T) {
sorobanResourceFeeCharged := sorobanFees.TotalRefundableResourceFeeCharged + sorobanFees.TotalNonRefundableResourceFeeCharged
sorobanInclusionFee := uint64(sorobanTotalFee - sorobanResourceFeeCharged)

seq, err := test.MasterAccount().GetSequenceNumber()
require.NoError(t, err)
// Submit classic transaction
params = txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
&txnbuild.BumpSequence{BumpTo: account.Sequence + 100},
},
BaseFee: txnbuild.MinBaseFee,
Memo: nil,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
}
tx, err = txnbuild.NewTransaction(params)
assert.NoError(t, err)
classicTxResponse := sendSuccessfulTransaction(t, client, sourceAccount, tx)
classicTxResponse := test.SendMasterOperation(
&txnbuild.BumpSequence{BumpTo: seq + 100},
)
var classicTxResult xdr.TransactionResult
require.NoError(t, xdr.SafeUnmarshalBase64(classicTxResponse.ResultXdr, &classicTxResult))
classicFee := uint64(classicTxResult.FeeCharged)

var result methods.GetFeeStatsResult
if err := client.CallResult(context.Background(), "getFeeStats", nil, &result); err != nil {
if err := test.GetRPCLient().CallResult(context.Background(), "getFeeStats", nil, &result); err != nil {
t.Fatalf("rpc call failed: %v", err)
}
expectedResult := methods.GetFeeStatsResult{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
package test
package integrationtest

import (
"context"
"crypto/sha256"
"testing"

"github.com/creachadair/jrpc2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/stellar/go/keypair"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/integrationtest/infrastructure"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/methods"
)

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

test := infrastructure.NewTest(t, nil)
client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address()
contractID := getContractID(t, sourceAccount, testSalt, StandaloneNetworkPassphrase)
contractIDHash := xdr.Hash(contractID)
hash := xdr.Hash{0xa, 0xb}
keyB64, err := xdr.MarshalBase64(xdr.LedgerKey{
Type: xdr.LedgerEntryTypeContractData,
ContractData: &xdr.LedgerKeyContractData{
Contract: xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeContract,
ContractId: &contractIDHash,
ContractId: &hash,
},
Key: xdr.ScVal{
Type: xdr.ScValTypeScvLedgerKeyContractInstance,
Expand All @@ -54,7 +49,7 @@ func TestGetLedgerEntriesNotFound(t *testing.T) {
}

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

client := test.GetRPCLient()

Expand All @@ -71,48 +66,9 @@ func TestGetLedgerEntriesInvalidParams(t *testing.T) {
}

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

client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)
address := sourceAccount.Address()
account := txnbuild.NewSimpleAccount(address, 0)
test := infrastructure.NewTest(t, nil)
_, contractID, contractHash := test.CreateHelloWorldContract()

contractBinary := getHelloWorldContract(t)
params := preflightTransactionParams(t, client, txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
createInstallContractCodeOperation(account.AccountID, contractBinary),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
})
tx, err := txnbuild.NewTransaction(params)
assert.NoError(t, err)
sendSuccessfulTransaction(t, client, sourceAccount, tx)

params = preflightTransactionParams(t, client, txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
createCreateContractOperation(address, contractBinary),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
})
tx, err = txnbuild.NewTransaction(params)
assert.NoError(t, err)
sendSuccessfulTransaction(t, client, sourceAccount, tx)

contractID := getContractID(t, address, testSalt, StandaloneNetworkPassphrase)

contractHash := sha256.Sum256(contractBinary)
contractCodeKeyB64, err := xdr.MarshalBase64(xdr.LedgerKey{
Type: xdr.LedgerEntryTypeContractCode,
ContractCode: &xdr.LedgerKeyContractCode{
Expand Down Expand Up @@ -146,7 +102,7 @@ func TestGetLedgerEntriesSucceeds(t *testing.T) {
}

var result methods.GetLedgerEntriesResponse
err = client.CallResult(context.Background(), "getLedgerEntries", request, &result)
err = test.GetRPCLient().CallResult(context.Background(), "getLedgerEntries", request, &result)
require.NoError(t, err)
require.Equal(t, 2, len(result.Entries))
require.Greater(t, result.LatestLedger, uint32(0))
Expand All @@ -159,7 +115,7 @@ func TestGetLedgerEntriesSucceeds(t *testing.T) {
var firstEntry xdr.LedgerEntryData
require.NoError(t, xdr.SafeUnmarshalBase64(result.Entries[0].XDR, &firstEntry))
require.Equal(t, xdr.LedgerEntryTypeContractCode, firstEntry.Type)
require.Equal(t, contractBinary, firstEntry.MustContractCode().Code)
require.Equal(t, infrastructure.GetHelloWorldContract(), firstEntry.MustContractCode().Code)

require.Greater(t, result.Entries[1].LastModifiedLedger, uint32(0))
require.LessOrEqual(t, result.Entries[1].LastModifiedLedger, result.LatestLedger)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package test
package integrationtest

import (
"context"
"crypto/sha256"
"testing"

"github.com/creachadair/jrpc2"
"github.com/stellar/go/txnbuild"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/stellar/go/keypair"
"github.com/stellar/go/xdr"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/integrationtest/infrastructure"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/methods"
)

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

client := test.GetRPCLient()

sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address()
contractID := getContractID(t, sourceAccount, testSalt, StandaloneNetworkPassphrase)
contractIDHash := xdr.Hash(contractID)
contractIDHash := xdr.Hash{0x1, 0x2}
keyB64, err := xdr.MarshalBase64(xdr.LedgerKey{
Type: xdr.LedgerEntryTypeContractData,
ContractData: &xdr.LedgerKeyContractData{
Expand All @@ -43,13 +37,14 @@ func TestGetLedgerEntryNotFound(t *testing.T) {
}

var result methods.GetLedgerEntryResponse
client := test.GetRPCLient()
jsonRPCErr := client.CallResult(context.Background(), "getLedgerEntry", request, &result).(*jrpc2.Error)
assert.Contains(t, jsonRPCErr.Message, "not found")
assert.Equal(t, jrpc2.InvalidRequest, jsonRPCErr.Code)
}

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

client := test.GetRPCLient()

Expand All @@ -64,31 +59,10 @@ func TestGetLedgerEntryInvalidParams(t *testing.T) {
}

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

client := test.GetRPCLient()

kp := keypair.Root(StandaloneNetworkPassphrase)
account := txnbuild.NewSimpleAccount(kp.Address(), 0)

contractBinary := getHelloWorldContract(t)
params := preflightTransactionParams(t, client, txnbuild.TransactionParams{
SourceAccount: &account,
IncrementSequenceNum: true,
Operations: []txnbuild.Operation{
createInstallContractCodeOperation(account.AccountID, contractBinary),
},
BaseFee: txnbuild.MinBaseFee,
Preconditions: txnbuild.Preconditions{
TimeBounds: txnbuild.NewInfiniteTimeout(),
},
})
tx, err := txnbuild.NewTransaction(params)
assert.NoError(t, err)
test := infrastructure.NewTest(t, nil)

sendSuccessfulTransaction(t, client, kp, tx)
_, contractHash := test.UploadHelloWorldContract()

contractHash := sha256.Sum256(contractBinary)
keyB64, err := xdr.MarshalBase64(xdr.LedgerKey{
Type: xdr.LedgerEntryTypeContractCode,
ContractCode: &xdr.LedgerKeyContractCode{
Expand All @@ -101,11 +75,11 @@ func TestGetLedgerEntrySucceeds(t *testing.T) {
}

var result methods.GetLedgerEntryResponse
err = client.CallResult(context.Background(), "getLedgerEntry", request, &result)
err = test.GetRPCLient().CallResult(context.Background(), "getLedgerEntry", request, &result)
assert.NoError(t, err)
assert.Greater(t, result.LatestLedger, uint32(0))
assert.GreaterOrEqual(t, result.LatestLedger, result.LastModifiedLedger)
var entry xdr.LedgerEntryData
assert.NoError(t, xdr.SafeUnmarshalBase64(result.XDR, &entry))
assert.Equal(t, contractBinary, entry.MustContractCode().Code)
assert.Equal(t, infrastructure.GetHelloWorldContract(), entry.MustContractCode().Code)
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package test
package integrationtest

import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/integrationtest/infrastructure"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/methods"
)

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

client := test.GetRPCLient()

Expand All @@ -19,7 +20,7 @@ func TestGetNetworkSucceeds(t *testing.T) {
var result methods.GetNetworkResponse
err := client.CallResult(context.Background(), "getNetwork", request, &result)
assert.NoError(t, err)
assert.Equal(t, friendbotURL, result.FriendbotURL)
assert.Equal(t, StandaloneNetworkPassphrase, result.Passphrase)
assert.Equal(t, infrastructure.FriendbotURL, result.FriendbotURL)
assert.Equal(t, infrastructure.StandaloneNetworkPassphrase, result.Passphrase)
assert.GreaterOrEqual(t, result.ProtocolVersion, 20)
}
Loading
Loading