generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 120
/
suite.go
162 lines (136 loc) · 4.59 KB
/
suite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/cosmos/cosmos-sdk/x/mint"
feegrant "cosmossdk.io/x/feegrant/module"
"cosmossdk.io/x/upgrade"
"github.com/cosmos/cosmos-sdk/x/auth"
authz "github.com/cosmos/cosmos-sdk/x/authz/module"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/gov/client"
"github.com/cosmos/cosmos-sdk/x/params"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/ibc-go/modules/capability"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
"cosmossdk.io/math"
starship "github.com/cosmology-tech/starship/clients/go/client"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
)
var configFile = "../configs/devnet.yaml"
type TestSuite struct {
suite.Suite
config *starship.Config
chainClients starship.ChainClients
}
func (s *TestSuite) SetupTest() {
s.T().Log("setting up e2e integration test suite...")
// read config file from yaml
yamlFile, err := os.ReadFile(configFile)
s.Require().NoError(err)
config := &starship.Config{}
err = yaml.Unmarshal(yamlFile, config)
s.Require().NoError(err)
s.config = config
// create chain clients
modulesBasicMap := map[string][]module.AppModuleBasic{}
for _, chain := range config.Chains {
modulesBasicMap[chain.Name] = []module.AppModuleBasic{
auth.AppModuleBasic{},
authz.AppModuleBasic{},
bank.AppModuleBasic{},
capability.AppModuleBasic{},
gov.NewAppModuleBasic(
[]client.ProposalHandler{
paramsclient.ProposalHandler,
},
),
crisis.AppModuleBasic{},
distribution.AppModuleBasic{},
feegrant.AppModuleBasic{},
mint.AppModuleBasic{},
params.AppModuleBasic{},
slashing.AppModuleBasic{},
staking.AppModuleBasic{},
upgrade.AppModuleBasic{},
transfer.AppModuleBasic{},
ibc.AppModuleBasic{},
}
}
chainClients, err := starship.NewChainClients(zap.L(), config, modulesBasicMap)
s.Require().NoError(err)
s.chainClients = chainClients
}
func (s *TestSuite) MakeRequest(req *http.Request, expCode int) io.Reader {
resp, err := http.DefaultClient.Do(req)
s.Require().NoError(err, "trying to make request", zap.Any("request", req))
s.Require().Equal(expCode, resp.StatusCode, "response code did not match")
return resp.Body
}
// WaitForHeight will wait till the chain reaches the block height
func (s *TestSuite) WaitForHeight(chain *starship.ChainClient, height int64) {
s.Require().Eventuallyf(
func() bool {
curHeight, err := chain.GetHeight()
s.Assert().NoError(err)
if curHeight >= height {
return true
}
return false
},
300*time.Second,
5*time.Second,
"waited for too long, still height did not reach desired block height",
)
}
func (s *TestSuite) TransferTokens(chain *starship.ChainClient, addr string, amount int, denom string) {
coin, err := sdk.ParseCoinNormalized(fmt.Sprintf("%d%s", amount, denom))
s.Require().NoError(err)
// Build transaction message
req := &banktypes.MsgSend{
FromAddress: chain.Address,
ToAddress: addr,
Amount: sdk.Coins{coin},
}
res, err := chain.Client.SendMsg(context.Background(), req, "Transfer tokens for e2e tests")
s.Require().NoError(err)
s.Require().NotEmpty(res)
}
func (s *TestSuite) IBCTransferTokens(chain1, chain2 *starship.ChainClient, chain2Addr string, amount int) {
channel, err := chain1.GetIBCChannel(chain2.GetChainID())
s.Require().NoError(err)
denom, err := chain1.GetChainDenom()
s.Require().NoError(err)
coin := sdk.Coin{Denom: denom, Amount: math.NewInt(int64(amount))}
req := &transfertypes.MsgTransfer{
SourcePort: channel.Chain_2.PortId,
SourceChannel: channel.Chain_2.ChannelId,
Token: coin,
Sender: chain1.Address,
Receiver: chain2Addr,
TimeoutHeight: clienttypes.NewHeight(12300, 45600),
TimeoutTimestamp: 0,
Memo: fmt.Sprintf("testsetup: transfer token from %s to %s", chain1.GetChainID(), chain2.GetChainID()),
}
res, err := chain1.Client.SendMsg(context.Background(), req, "")
s.Require().NoError(err)
s.Require().NotEmpty(res)
time.Sleep(5 * time.Second)
}