Skip to content

Commit

Permalink
[E2E] Add Regression Testing for Send E2E Feature Test (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
h5law authored Oct 23, 2023
1 parent 2c56c12 commit fc87034
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 22 deletions.
103 changes: 85 additions & 18 deletions e2e/tests/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,37 @@ package e2e
import (
"fmt"
"regexp"
"strconv"
"strings"
"testing"
"time"

"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"
)

var addrRe *regexp.Regexp
var (
addrRe *regexp.Regexp
amountRe *regexp.Regexp
accNameToAddrMap = make(map[string]string)
keyRingFlag = "--keyring-backend=test"
)

func init() {
addrRe = regexp.MustCompile(`address:\s+(pokt1\w+)`)
addrRe = regexp.MustCompile(`address: (\S+)\s+name: (\S+)`)
amountRe = regexp.MustCompile(`amount: "(.+?)"\s+denom: upokt`)
}

type suite struct {
gocuke.TestingT
pocketd *pocketdBin
pocketd *pocketdBin
scenarioState map[string]any // temporary state for each scenario
}

func (s *suite) Before() {
s.pocketd = new(pocketdBin)
s.scenarioState = make(map[string]any)
s.buildAddrMap()
}

// TestFeatures runs the e2e tests specified in any .features files in this directory
Expand Down Expand Up @@ -56,17 +67,15 @@ func (s *suite) TheUserShouldBeAbleToSeeStandardOutputContaining(arg1 string) {
}
}

func (s *suite) TheUserSendsUpoktToAnotherAddress(amount int64) {
addrs := s.getAddresses()
func (s *suite) TheUserSendsUpoktFromAccountToAccount(amount int64, accName1, accName2 string) {
args := []string{
"tx",
"bank",
"send",
addrs[0],
addrs[1],
accNameToAddrMap[accName1],
accNameToAddrMap[accName2],
fmt.Sprintf("%dupokt", amount),
"--keyring-backend",
"test",
keyRingFlag,
"-y",
}
res, err := s.pocketd.RunCommandOnHost("", args...)
Expand All @@ -76,20 +85,78 @@ func (s *suite) TheUserSendsUpoktToAnotherAddress(amount int64) {
s.pocketd.result = res
}

func (s *suite) getAddresses() [2]string {
var strs [2]string
func (s *suite) TheAccountHasABalanceGreaterThanUpokt(accName string, amount int64) {
bal := s.getAccBalance(accName)
if int64(bal) < amount {
s.Fatalf("account %s does not have enough upokt: %d < %d", accName, bal, amount)
}
s.scenarioState[accName] = bal // save the balance for later
}

func (s *suite) AnAccountExistsFor(accName string) {
bal := s.getAccBalance(accName)
s.scenarioState[accName] = bal // save the balance for later
}

func (s *suite) TheAccountBalanceOfShouldBeUpoktThanBefore(accName string, amount int64, condition string) {
prev, ok := s.scenarioState[accName]
if !ok {
s.Fatalf("no previous balance found for %s", accName)
}

bal := s.getAccBalance(accName)
switch condition {
case "more":
if bal <= prev.(int) {
s.Fatalf("account %s expected to have more upokt but: %d <= %d", accName, bal, prev)
}
case "less":
if bal >= prev.(int) {
s.Fatalf("account %s expected to have less upokt but: %d >= %d", accName, bal, prev)
}
default:
s.Fatalf("unknown condition %s", condition)
}
}

func (s *suite) TheUserShouldWaitForSeconds(dur int64) {
time.Sleep(time.Duration(dur) * time.Second)
}

func (s *suite) buildAddrMap() {
s.Helper()
res, err := s.pocketd.RunCommand(
"keys", "list", "--keyring-backend", "test",
"keys", "list", keyRingFlag,
)
if err != nil {
s.Fatalf("error getting keys: %s", err)
}
matches := addrRe.FindAllStringSubmatch(res.Stdout, -1)
if len(matches) >= 2 {
strs[0] = matches[0][1]
strs[1] = matches[len(matches)-1][1]
} else {
s.Fatalf("could not find two addresses in output: %s", res.Stdout)
for _, match := range matches {
name := match[2]
address := match[1]
accNameToAddrMap[name] = address
}
}

func (s *suite) getAccBalance(accName string) int {
s.Helper()
args := []string{
"query",
"bank",
"balances",
accNameToAddrMap[accName],
}
res, err := s.pocketd.RunCommandOnHost("", args...)
if err != nil {
s.Fatalf("error getting balance: %s", err)
}
s.pocketd.result = res
match := amountRe.FindStringSubmatch(res.Stdout)
if len(match) < 2 {
s.Fatalf("no balance found for %s", accName)
}
return strs
found, err := strconv.Atoi(match[1])
require.NoError(s, err)
return found
}
2 changes: 1 addition & 1 deletion e2e/tests/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
defaultRPCURL = fmt.Sprintf("tcp://%s:%d", defaultRPCHost, defaultRPCPort)
}
if defaultHome == "" {
defaultHome = "./localnet/pocketd"
defaultHome = "../../localnet/pocketd"
}
}

Expand Down
7 changes: 6 additions & 1 deletion e2e/tests/send.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ Feature: Tx Namespace

Scenario: User can send uPOKT
Given the user has the pocketd binary installed
When the user sends 10000 uPOKT to another address
And the account "app1" has a balance greater than "1000" uPOKT
And an account exists for "app2"
When the user sends "1000" uPOKT from account "app1" to account "app2"
Then the user should be able to see standard output containing "txhash:"
And the user should be able to see standard output containing "code: 0"
And the pocketd binary should exit without error
And the user should wait for "5" seconds
And the account balance of "app1" should be "1000" uPOKT "less" than before
And the account balance of "app2" should be "1000" uPOKT "more" than before
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
cosmossdk.io/math v1.0.1
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.3
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.1.0
Expand All @@ -22,6 +23,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.3.0
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1
google.golang.org/grpc v1.56.1
gopkg.in/yaml.v2 v2.4.0
)
Expand Down Expand Up @@ -66,7 +68,6 @@ require (
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
Expand Down Expand Up @@ -265,7 +266,6 @@ require (
gonum.org/v1/gonum v0.11.0 // indirect
google.golang.org/api v0.122.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down

0 comments on commit fc87034

Please sign in to comment.