-
Notifications
You must be signed in to change notification settings - Fork 607
/
e2e_setup_test.go
315 lines (251 loc) · 8.77 KB
/
e2e_setup_test.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package e2e
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/spf13/viper"
"github.com/stretchr/testify/suite"
tmconfig "github.com/tendermint/tendermint/config"
tmjson "github.com/tendermint/tendermint/libs/json"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)
const (
osmoBalance = 200000000000
osmoDenom = "uosmo"
stakeBalance = 110000000000
stakeAmount = 100000000000
stakeDenom = "stake"
minGasPrice = "0.00001"
)
var (
initBalanceStr = fmt.Sprintf("%d%s,%d%s", osmoBalance, osmoDenom, stakeBalance, stakeDenom)
stakeAmountInt, _ = sdk.NewIntFromString(fmt.Sprintf("%d", stakeAmount))
stakeAmountCoin = sdk.NewCoin(stakeDenom, stakeAmountInt)
)
type IntegrationTestSuite struct {
suite.Suite
tmpDirs []string
chain *chain
dkrPool *dockertest.Pool
dkrNet *dockertest.Network
valResources map[string][]*dockertest.Resource
}
func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up e2e integration test suite...")
var err error
s.chain, err = newChain()
s.Require().NoError(err)
s.dkrPool, err = dockertest.NewPool("")
s.Require().NoError(err)
s.dkrNet, err = s.dkrPool.CreateNetwork(fmt.Sprintf("%s-testnet", s.chain.id))
s.Require().NoError(err)
s.valResources = make(map[string][]*dockertest.Resource)
// The boostrapping phase is as follows:
//
// 1. Initialize Osmosis validator nodes.
// 2. Create and initialize Osmosis validator genesis files (one chain)
s.T().Logf("starting e2e infrastructure for chain A; chain-id: %s; datadir: %s", s.chain.id, s.chain.dataDir)
s.initNodes(s.chain)
s.initGenesis(s.chain)
s.initValidatorConfigs(s.chain)
s.runValidators(s.chain, 0)
}
func (s *IntegrationTestSuite) TearDownSuite() {
if str := os.Getenv("OSMOSIS_E2E_SKIP_CLEANUP"); len(str) > 0 {
skipCleanup, err := strconv.ParseBool(str)
s.Require().NoError(err)
if skipCleanup {
return
}
}
s.T().Log("tearing down e2e integration test suite...")
for _, vr := range s.valResources {
for _, r := range vr {
s.Require().NoError(s.dkrPool.Purge(r))
}
}
s.Require().NoError(s.dkrPool.RemoveNetwork(s.dkrNet))
os.RemoveAll(s.chain.dataDir)
for _, td := range s.tmpDirs {
os.RemoveAll(td)
}
}
func (s *IntegrationTestSuite) initNodes(c *chain) {
s.Require().NoError(c.createAndInitValidators(2))
// initialize a genesis file for the first validator
val0ConfigDir := c.validators[0].configDir()
for _, val := range c.validators {
s.Require().NoError(
addGenesisAccount(val0ConfigDir, "", initBalanceStr, val.keyInfo.GetAddress()),
)
}
// copy the genesis file to the remaining validators
for _, val := range c.validators[1:] {
_, err := copyFile(
filepath.Join(val0ConfigDir, "config", "genesis.json"),
filepath.Join(val.configDir(), "config", "genesis.json"),
)
s.Require().NoError(err)
}
}
func (s *IntegrationTestSuite) initGenesis(c *chain) {
serverCtx := server.NewDefaultContext()
config := serverCtx.Config
config.SetRoot(c.validators[0].configDir())
config.Moniker = c.validators[0].moniker
genFilePath := config.GenesisFile()
appGenState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFilePath)
s.Require().NoError(err)
var bankGenState banktypes.GenesisState
s.Require().NoError(cdc.UnmarshalJSON(appGenState[banktypes.ModuleName], &bankGenState))
bankGenState.DenomMetadata = append(bankGenState.DenomMetadata, banktypes.Metadata{
Description: "An example stable token",
Display: osmoDenom,
Base: osmoDenom,
Symbol: osmoDenom,
Name: osmoDenom,
DenomUnits: []*banktypes.DenomUnit{
{
Denom: osmoDenom,
Exponent: 0,
},
},
})
bz, err := cdc.MarshalJSON(&bankGenState)
s.Require().NoError(err)
appGenState[banktypes.ModuleName] = bz
var genUtilGenState genutiltypes.GenesisState
s.Require().NoError(cdc.UnmarshalJSON(appGenState[genutiltypes.ModuleName], &genUtilGenState))
// generate genesis txs
genTxs := make([]json.RawMessage, len(c.validators))
for i, val := range c.validators {
createValmsg, err := val.buildCreateValidatorMsg(stakeAmountCoin)
s.Require().NoError(err)
signedTx, err := val.signMsg(createValmsg)
s.Require().NoError(err)
txRaw, err := cdc.MarshalJSON(signedTx)
s.Require().NoError(err)
genTxs[i] = txRaw
}
genUtilGenState.GenTxs = genTxs
bz, err = cdc.MarshalJSON(&genUtilGenState)
s.Require().NoError(err)
appGenState[genutiltypes.ModuleName] = bz
bz, err = json.MarshalIndent(appGenState, "", " ")
s.Require().NoError(err)
genDoc.AppState = bz
bz, err = tmjson.MarshalIndent(genDoc, "", " ")
s.Require().NoError(err)
// write the updated genesis file to each validator
for _, val := range c.validators {
writeFile(filepath.Join(val.configDir(), "config", "genesis.json"), bz)
}
}
func (s *IntegrationTestSuite) initValidatorConfigs(c *chain) {
for i, val := range c.validators {
tmCfgPath := filepath.Join(val.configDir(), "config", "config.toml")
vpr := viper.New()
vpr.SetConfigFile(tmCfgPath)
s.Require().NoError(vpr.ReadInConfig())
valConfig := &tmconfig.Config{}
s.Require().NoError(vpr.Unmarshal(valConfig))
valConfig.P2P.ListenAddress = "tcp://0.0.0.0:26656"
valConfig.P2P.AddrBookStrict = false
valConfig.P2P.ExternalAddress = fmt.Sprintf("%s:%d", val.instanceName(), 26656)
valConfig.RPC.ListenAddress = "tcp://0.0.0.0:26657"
valConfig.StateSync.Enable = false
valConfig.LogLevel = "info"
var peers []string
for j := 0; j < len(c.validators); j++ {
if i == j {
continue
}
peer := c.validators[j]
peerID := fmt.Sprintf("%s@%s%d:26656", peer.nodeKey.ID(), peer.moniker, j)
peers = append(peers, peerID)
}
valConfig.P2P.PersistentPeers = strings.Join(peers, ",")
tmconfig.WriteConfigFile(tmCfgPath, valConfig)
// set application configuration
appCfgPath := filepath.Join(val.configDir(), "config", "app.toml")
appConfig := srvconfig.DefaultConfig()
appConfig.API.Enable = true
appConfig.MinGasPrices = fmt.Sprintf("%s%s", minGasPrice, osmoDenom)
srvconfig.WriteConfigFile(appCfgPath, appConfig)
}
}
func (s *IntegrationTestSuite) runValidators(c *chain, portOffset int) {
s.T().Logf("starting Osmosis %s validator containers...", c.id)
s.valResources[c.id] = make([]*dockertest.Resource, len(c.validators))
for i, val := range c.validators {
runOpts := &dockertest.RunOptions{
Name: val.instanceName(),
NetworkID: s.dkrNet.Network.ID,
Mounts: []string{
fmt.Sprintf("%s/:/osmosis/.osmosisd", val.configDir()),
},
Repository: "cosmos/osmosisd-e2e",
}
// expose the first validator for debugging and communication
if val.index == 0 {
runOpts.PortBindings = map[docker.Port][]docker.PortBinding{
"1317/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 1317+portOffset)}},
"6060/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 6060+portOffset)}},
"6061/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 6061+portOffset)}},
"6062/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 6062+portOffset)}},
"6063/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 6063+portOffset)}},
"6064/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 6064+portOffset)}},
"6065/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 6065+portOffset)}},
"9090/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 9090+portOffset)}},
"26656/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 26656+portOffset)}},
"26657/tcp": {{HostIP: "", HostPort: fmt.Sprintf("%d", 26657+portOffset)}},
}
}
resource, err := s.dkrPool.RunWithOptions(runOpts, noRestart)
s.Require().NoError(err)
s.valResources[c.id][i] = resource
s.T().Logf("started Osmosis %s validator container: %s", c.id, resource.Container.ID)
}
rpcClient, err := rpchttp.New("tcp://localhost:26657", "/websocket")
s.Require().NoError(err)
s.Require().Eventually(
func() bool {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
status, err := rpcClient.Status(ctx)
if err != nil {
return false
}
// let the node produce a few blocks
if status.SyncInfo.CatchingUp || status.SyncInfo.LatestBlockHeight < 3 {
return false
}
return true
},
5*time.Minute,
time.Second,
"Osmosis node failed to produce blocks",
)
}
func noRestart(config *docker.HostConfig) {
// in this case we don't want the nodes to restart on failure
config.RestartPolicy = docker.RestartPolicy{
Name: "no",
}
}