Skip to content

Commit

Permalink
[E2E] Add (Un)Stake Tests (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
h5law authored Oct 31, 2023
1 parent fd0db1d commit dd18066
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 18 deletions.
10 changes: 5 additions & 5 deletions e2e/tests/help.feature
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Feature: Root Namespace

Scenario: User Needs Help
Given the user has the pocketd binary installed
When the user runs the command "help"
Then the user should be able to see standard output containing "Available Commands"
And the pocketd binary should exit without error
Scenario: User Needs Help
Given the user has the pocketd binary installed
When the user runs the command "help"
Then the user should be able to see standard output containing "Available Commands"
And the pocketd binary should exit without error
84 changes: 82 additions & 2 deletions e2e/tests/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ var (
)

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

type suite struct {
Expand Down Expand Up @@ -123,6 +123,85 @@ func (s *suite) TheUserShouldWaitForSeconds(dur int64) {
time.Sleep(time.Duration(dur) * time.Second)
}

func (s *suite) TheUserStakesAWithUpoktFromTheAccount(actorType string, amount int64, accName string) {
args := []string{
"tx",
actorType,
fmt.Sprintf("stake-%s", actorType),
fmt.Sprintf("%dupokt", amount),
"--from",
accName,
keyRingFlag,
"-y",
}
res, err := s.pocketd.RunCommandOnHost("", args...)
if err != nil {
s.Fatalf("error staking %s: %s", actorType, err)
}
s.pocketd.result = res
}

func (s *suite) TheUserUnstakesAFromTheAccount(actorType string, accName string) {
args := []string{
"tx",
actorType,
fmt.Sprintf("unstake-%s", actorType),
"--from",
accName,
keyRingFlag,
"-y",
}
res, err := s.pocketd.RunCommandOnHost("", args...)
if err != nil {
s.Fatalf("error unstaking %s: %s", actorType, err)
}
s.pocketd.result = res
}

func (s *suite) TheForAccountIsNotStaked(actorType, accName string) {
found, _ := s.getStakedAmount(actorType, accName)
if found {
s.Fatalf("account %s should not be staked", accName)
}
}

func (s *suite) TheForAccountIsStakedWithUpokt(actorType, accName string, amount int64) {
found, stakeAmount := s.getStakedAmount(actorType, accName)
if !found {
s.Fatalf("account %s should be staked", accName)
}
if int64(stakeAmount) != amount {
s.Fatalf("account %s stake amount is not %d", accName, amount)
}
}

func (s *suite) getStakedAmount(actorType, accName string) (bool, int) {
s.Helper()
args := []string{
"query",
actorType,
fmt.Sprintf("list-%s", actorType),
}
res, err := s.pocketd.RunCommandOnHost("", args...)
if err != nil {
s.Fatalf("error getting %s: %s", actorType, err)
}
s.pocketd.result = res
found := strings.Contains(res.Stdout, accNameToAddrMap[accName])
amount := 0
if found {
escapedAddress := regexp.QuoteMeta(accNameToAddrMap[accName])
stakedAmountRe := regexp.MustCompile(`address: ` + escapedAddress + `\s+stake:\s+amount: "(\d+)"`)
matches := stakedAmountRe.FindStringSubmatch(res.Stdout)
if len(matches) < 2 {
s.Fatalf("no stake amount found for %s", accName)
}
amount, err = strconv.Atoi(matches[1])
require.NoError(s, err)
}
return found, amount
}

func (s *suite) buildAddrMap() {
s.Helper()
res, err := s.pocketd.RunCommand(
Expand All @@ -131,6 +210,7 @@ func (s *suite) buildAddrMap() {
if err != nil {
s.Fatalf("error getting keys: %s", err)
}
s.pocketd.result = res
matches := addrRe.FindAllStringSubmatch(res.Stdout, -1)
for _, match := range matches {
name := match[2]
Expand Down
22 changes: 11 additions & 11 deletions e2e/tests/send.feature
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Feature: Tx Namespace

Scenario: User can send uPOKT
Given the user has the pocketd binary installed
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
Scenario: User can send uPOKT
Given the user has the pocketd binary installed
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
25 changes: 25 additions & 0 deletions e2e/tests/stake.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Stake Namespaces

Scenario: User can stake a Gateway
Given the user has the pocketd binary installed
And the "gateway" for account "gateway1" is not staked
And the account "gateway1" has a balance greater than "1000" uPOKT
When the user stakes a "gateway" with "1000" uPOKT from the account "gateway1"
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 "gateway" for account "gateway1" is staked with "1000" uPOKT
And the account balance of "gateway1" should be "1000" uPOKT "less" than before

Scenario: User can unstake a Gateway
Given the user has the pocketd binary installed
And the "gateway" for account "gateway1" is staked with "1000" uPOKT
And an account exists for "gateway1"
When the user unstakes a "gateway" from the account "gateway1"
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 "gateway" for account "gateway1" is not staked
And the account balance of "gateway1" should be "1000" uPOKT "more" than before

0 comments on commit dd18066

Please sign in to comment.