Skip to content

Commit

Permalink
faucet tests in e2e (#231)
Browse files Browse the repository at this point in the history
* add faucet status check test

* add test for checking  endpoint as well

* only skip faucet tests if faucet port is not defined

* fix running credit testcase

* remove exposing faucet for simapp due to incompatibility issues with cosmjs and simapp

* use big.Int instead of floats, check for expected difference between before and after balance is atleast the expected value

* add comment

* remove use of big.Int with float

* fix typo in expected value
  • Loading branch information
Anmol1696 authored Sep 13, 2023
1 parent 92b3827 commit 78bd5e2
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 8 deletions.
3 changes: 3 additions & 0 deletions tests/e2e/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Chain struct {
Name string `name:"name" json:"name" yaml:"name"`
Type string `name:"type" json:"type" yaml:"type"`
NumValidators int `name:"num-validators" json:"num_validators" yaml:"numValidators"`
Faucet *Feature `name:"faucet" json:"faucet" yaml:"faucet"`
Ports Port `name:"ports" json:"ports" yaml:"ports"`
Genesis map[string]interface{} `name:"genesis" json:"genesis" yaml:"genesis"`
}
Expand All @@ -13,6 +14,7 @@ type Port struct {
Rpc int `name:"rpc" json:"rpc" yaml:"rpc"`
Grpc int `name:"grpc" json:"grpc" yaml:"grpc"`
Exposer int `name:"exposer" json:"exposer" yaml:"exposer"`
Faucet int `name:"faucet" json:"faucet" yaml:"faucet"`
}

type Relayer struct {
Expand All @@ -36,6 +38,7 @@ type Config struct {
Relayers []*Relayer `name:"relayers" json:"relayers" yaml:"relayers"`
Explorer *Feature `name:"explorer" json:"explorer" yaml:"explorer"`
Registry *Feature `name:"registry" json:"registry" yaml:"registry"`
Faucet *Feature `name:"faucet" json:"faucet" yaml:"faucet"`
}

// HasChainId returns true if chain id found in list of chains
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/configs/injective.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ chains:
rest: 1313
rpc: 26653
exposer: 38083
faucet: 8001
- name: cosmoshub-4
type: cosmos
numValidators: 1
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/configs/multi-validator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ chains:
rest: 1313
rpc: 26653
exposer: 38083
faucet: 8000
resources:
limits:
cpu: "0.4"
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/configs/one-chain-custom-scripts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ chains:
rest: 1313
rpc: 26653
exposer: 38083
faucet: 8003
resources:
cpu: "0.5"
memory: 500M
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/configs/one-chain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ chains:
rest: 1313
rpc: 26653
exposer: 38083
faucet: 8003
resources:
cpu: "0.5"
memory: 500M
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/configs/one-custom-chain.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ chains:
rest: 1313
rpc: 26653
exposer: 38083
faucet: 8003
resources:
cpu: "0.5"
memory: 500M
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/configs/simapp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ chains:
rest: 1317
rpc: 26657
grpc: 9091
faucet: 8001
exposer: 8002
resources:
cpu: "0.2"
Expand Down
145 changes: 145 additions & 0 deletions tests/e2e/faucet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package e2e

import (
"bytes"
"encoding/json"
"fmt"
pb "github.com/cosmology-tech/starship/registry/registry"
"net/http"
urlpkg "net/url"
"strconv"
)

func (s *TestSuite) MakeFaucetRequest(chain *Chain, req *http.Request, unmarshal map[string]interface{}) {
host := fmt.Sprintf("http://0.0.0.0:%d%s", chain.Ports.Faucet, req.URL.String())

url, err := urlpkg.Parse(host)
s.Require().NoError(err)

req.URL = url

body := s.MakeRequest(req, 200)

err = json.NewDecoder(body).Decode(&unmarshal)
s.Require().NoError(err)
}

func (s *TestSuite) TestFaucet_Status() {
s.T().Log("running test for /status endpoint for faucet")

for _, chain := range s.config.Chains {
s.Run(fmt.Sprintf("facuet test for: %s", chain.Name), func() {
if chain.Ports.Faucet == 0 {
s.T().Skip("faucet not exposed via ports")
}

req, err := http.NewRequest(http.MethodGet, "/status", nil)
s.Require().NoError(err)

resp := map[string]interface{}{}
s.MakeFaucetRequest(chain, req, resp)

s.Require().Equal("ok", resp["status"])
})
}
}

func (s *TestSuite) MakeChainGetRequest(chain *Chain, endpoint string, unmarshal any) {
url := fmt.Sprintf("http://0.0.0.0:%d%s", chain.Ports.Rest, endpoint)
req, err := http.NewRequest(http.MethodGet, url, nil)
s.Require().NoError(err)
body := s.MakeRequest(req, 200)

err = json.NewDecoder(body).Decode(&unmarshal)
s.Require().NoError(err)
}

func (s *TestSuite) getChainAccounts(chain *Chain) []string {
var accounts []string

data := map[string]interface{}{}
s.MakeChainGetRequest(chain, "/cosmos/auth/v1beta1/accounts", &data)
s.Require().Contains(data, "accounts")

for _, acc := range data["accounts"].([]interface{}) {
accMap := acc.(map[string]interface{})
s.Require().Contains(accMap, "@type")
if accMap["@type"].(string) != "/cosmos.auth.v1beta1.BaseAccount" {
continue
}
s.Require().NotEmpty(accMap["address"].(string))
accounts = append(accounts, accMap["address"].(string))
}

s.Require().GreaterOrEqual(len(accounts), 1)
return accounts
}

func (s *TestSuite) getChainDenoms(chain *Chain) string {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/chains/%s", chain.Name), nil)
s.Require().NoError(err)

respChain := &pb.ChainRegistry{}
s.MakeRegistryRequest(req, respChain)
s.Require().Equal(chain.Name, respChain.ChainId)

s.Require().NotEmpty(respChain.Fees.FeeTokens[0].Denom)

return respChain.Fees.FeeTokens[0].Denom
}

func (s *TestSuite) getAccountBalance(chain *Chain, address string, denom string) float64 {
data := map[string]interface{}{}
s.MakeChainGetRequest(chain, fmt.Sprintf("/cosmos/bank/v1beta1/balances/%s", address), &data)
s.Require().Contains(data, "balances")

for _, bal := range data["balances"].([]interface{}) {
balMap := bal.(map[string]interface{})
if balMap["denom"].(string) == denom {
b, err := strconv.ParseFloat(balMap["amount"].(string), 64)
s.Require().NoError(err)
return b
}
}
return float64(0)
}

func (s *TestSuite) TestFaucet_Credit() {
s.T().Log("running test for /credit endpoint for faucet")

// expected amount to be credited via faucet
expCreditedAmt := float64(10000000000)

for _, chain := range s.config.Chains {
s.Run(fmt.Sprintf("facuet test for: %s", chain.Name), func() {
if chain.Ports.Faucet == 0 {
s.T().Skip("faucet not exposed via ports")
}

// fetch denom and address from an account on chain
accounts := s.getChainAccounts(chain)
denom := s.getChainDenoms(chain)
addr := accounts[len(accounts)-1]
beforeBalance := s.getAccountBalance(chain, addr, denom)

body := map[string]string{
"denom": denom,
"address": addr,
}
postBody, err := json.Marshal(body)
s.Require().NoError(err)
resp, err := http.Post(
fmt.Sprintf("http://0.0.0.0:%d/credit", chain.Ports.Faucet),
"application/json",
bytes.NewBuffer(postBody))
s.Require().NoError(err)
s.Require().Equal(200, resp.StatusCode)

afterBalance := s.getAccountBalance(chain, addr, denom)

// note sometimes expected difference is 9x expected value (bug due to using holder address for test)
// hence checking for difference is atleast expected value
s.Require().GreaterOrEqual(afterBalance-beforeBalance, expCreditedAmt)
})
}
}
6 changes: 0 additions & 6 deletions tests/e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ module github.com/cosmology-tech/starship/tests/e2e

go 1.19

replace (
github.com/cosmology-tech/starship/exposer => ../../exposer
github.com/cosmology-tech/starship/registry => ../../registry
)

require (
github.com/cosmology-tech/starship/exposer v0.0.0-20230413092908-7da9e8a24b31
github.com/cosmology-tech/starship/registry v0.0.0-20230411094226-129001b2f52a
Expand All @@ -30,7 +25,6 @@ require (
google.golang.org/genproto v0.0.0-20230320184635-7606e756e683 // indirect
google.golang.org/grpc v1.53.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace (
Expand Down

0 comments on commit 78bd5e2

Please sign in to comment.