Skip to content

Commit

Permalink
Add a few more basic CLI integration tests (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Sep 8, 2023
1 parent 72d7cf9 commit dce1986
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
42 changes: 34 additions & 8 deletions cmd/soroban-rpc/internal/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,54 @@ import (
"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"
)

func TestInstallContractWithCLI(t *testing.T) {
func TestCLIContractInstall(t *testing.T) {
NewCLITest(t)
output, err := runCLICommand(t, "contract install --wasm ../../../../target/wasm32-unknown-unknown/test-wasms/test_hello_world.wasm")
require.NoError(t, err)
output, err := runCLICommand("contract install --wasm " + helloWorldContractPath)
assert.NoError(t, err)
wasm := getHelloWorldContract(t)
contractHash := xdr.Hash(sha256.Sum256(wasm))
require.Contains(t, string(output), contractHash.HexString())
require.Contains(t, output, contractHash.HexString())
}

func TestCLIContractDeploy(t *testing.T) {
NewCLITest(t)
output, err := runCLICommand("contract deploy --id 1 --wasm " + helloWorldContractPath)
assert.NoError(t, err)
require.Contains(t, output, "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM")
}

func TestCLIContractInvokeWithWasm(t *testing.T) {
NewCLITest(t)
output, err := runCLICommand(fmt.Sprintf("contract invoke --id 1 --wasm %s -- hello --world=world", helloWorldContractPath))
assert.NoError(t, err)
require.Contains(t, output, `["Hello","world"]`)
}

func TestCLIContractDeployAndInvoke(t *testing.T) {
NewCLITest(t)
output, err := runCLICommand("contract deploy --id 1 --wasm " + helloWorldContractPath)
assert.NoError(t, err)
contractID := strings.TrimSpace(output)
output, err = runCLICommand(fmt.Sprintf("contract invoke --id %s -- hello --world=world", contractID))
assert.NoError(t, err)
require.Contains(t, output, `["Hello","world"]`)
}

func runCLICommand(t *testing.T, cmd string) ([]byte, error) {
baseCmdArgs := []string{"run", "--", "--vv"}
func runCLICommand(cmd string) (string, error) {
baseCmdArgs := []string{"run", "-q", "--", "--vv"}
args := strings.Split(cmd, " ")
args = append(baseCmdArgs, args...)
c := exec.Command("cargo", args...)
c.Env = append(os.Environ(),
fmt.Sprintf("RPC_URL=http://localhost:%d/", sorobanRPCPort),
fmt.Sprintf("NETWORK_PASPRHASE=%s", StandaloneNetworkPassphrase),
)
return c.Output()
bin, err := c.CombinedOutput()
return string(bin), err
}

func NewCLITest(t *testing.T) *Test {
Expand All @@ -44,7 +70,7 @@ func NewCLITest(t *testing.T) *Test {

sourceAccount := keypair.Root(StandaloneNetworkPassphrase)

// Create default account used byt the CLI
// Create default account used by the CLI
tx, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: &txnbuild.SimpleAccount{
AccountID: keypair.Root(StandaloneNetworkPassphrase).Address(),
Expand Down
7 changes: 4 additions & 3 deletions cmd/soroban-rpc/internal/test/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ const (
goMonorepoGithubPath = "github.com/stellar/go"
friendbotURL = "http://localhost:8000/friendbot"
// Needed when Core is run with ARTIFICIALLY_ACCELERATE_TIME_FOR_TESTING=true
checkpointFrequency = 8
sorobanRPCPort = 8000
adminPort = 8080
checkpointFrequency = 8
sorobanRPCPort = 8000
adminPort = 8080
helloWorldContractPath = "../../../../target/wasm32-unknown-unknown/test-wasms/test_hello_world.wasm"
)

type Test struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/test/simulate_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
func getHelloWorldContract(t *testing.T) []byte {
_, filename, _, _ := runtime.Caller(0)
testDirName := path.Dir(filename)
contractFile := path.Join(testDirName, "../../../../target/wasm32-unknown-unknown/test-wasms/test_hello_world.wasm")
contractFile := path.Join(testDirName, helloWorldContractPath)
ret, err := os.ReadFile(contractFile)
if err != nil {
t.Fatalf("unable to read test_hello_world.wasm (%v) please run `make build-test-wasms` at the project root directory", err)
Expand Down

0 comments on commit dce1986

Please sign in to comment.