-
Notifications
You must be signed in to change notification settings - Fork 123
/
test_setup.go
151 lines (131 loc) · 4.05 KB
/
test_setup.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
package interchaintest
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/docker/docker/client"
"github.com/strangelove-ventures/interchaintest/v8/dockerutil"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
)
const (
testPathName = "test-path"
FaucetAccountKeyName = "faucet"
)
// KeepDockerVolumesOnFailure sets whether volumes associated with a particular test
// are retained or deleted following a test failure.
//
// The value is false by default, but can be initialized to true by setting the
// environment variable ICTEST_SKIP_FAILURE_CLEANUP to a non-empty value.
// Alternatively, importers of the interchaintest package may call KeepDockerVolumesOnFailure(true).
func KeepDockerVolumesOnFailure(b bool) {
dockerutil.KeepVolumesOnFailure = b
}
// DockerSetup returns a new Docker Client and the ID of a configured network, associated with t.
//
// If any part of the setup fails, t.Fatal is called.
func DockerSetup(t dockerutil.DockerSetupTestingT) (*client.Client, string) {
t.Helper()
return dockerutil.DockerSetup(t)
}
// startup both chains
// creates wallets in the relayer for src and dst chain
// funds relayer src and dst wallets on respective chain in genesis
// creates a faucet account on the both chains (separate fullnode)
// funds faucet accounts in genesis.
func StartChainPair(
t *testing.T,
ctx context.Context,
rep *testreporter.Reporter,
cli *client.Client,
networkID string,
srcChain, dstChain ibc.Chain,
f RelayerFactory,
preRelayerStartFuncs []func([]ibc.ChannelOutput),
) (ibc.Relayer, error) {
t.Helper()
relayerImpl := f.Build(t, cli, networkID)
ic := NewInterchain().
AddChain(srcChain).
AddChain(dstChain).
AddRelayer(relayerImpl, "r").
AddLink(InterchainLink{
Chain1: srcChain,
Chain2: dstChain,
Relayer: relayerImpl,
Path: testPathName,
})
blockSqlite := DefaultBlockDatabaseFilepath()
t.Logf("View block history using sqlite console at %s", blockSqlite)
eRep := rep.RelayerExecReporter(t)
if err := ic.Build(ctx, eRep, InterchainBuildOptions{
TestName: t.Name(),
Client: cli,
NetworkID: networkID,
GitSha: GitSha,
BlockDatabaseFile: blockSqlite,
}); err != nil {
return nil, err
}
t.Cleanup(func() {
_ = ic.Close()
})
return relayerImpl, nil
}
// StopStartRelayerWithPreStartFuncs will stop the relayer if it is currently running,
// then execute the preRelayerStartFuncs and wait for all to complete before starting
// the relayer.
func StopStartRelayerWithPreStartFuncs(
t *testing.T,
ctx context.Context,
srcChainID string,
relayerImpl ibc.Relayer,
eRep *testreporter.RelayerExecReporter,
preRelayerStartFuncs []func([]ibc.ChannelOutput),
pathNames ...string,
) ([]ibc.ChannelOutput, error) {
t.Helper()
if err := relayerImpl.StopRelayer(ctx, eRep); err != nil {
t.Logf("error stopping relayer: %v", err)
}
channels, err := relayerImpl.GetChannels(ctx, eRep, srcChainID)
if err != nil {
return nil, fmt.Errorf("failed to get channels: %w", err)
}
if len(channels) == 0 {
return nil, fmt.Errorf("channel count invalid. expected: > 0, actual: %d", len(channels))
}
wg := sync.WaitGroup{}
for _, preRelayerStart := range preRelayerStartFuncs {
if preRelayerStart == nil {
continue
}
wg.Add(1)
go func() {
preRelayerStart(channels)
wg.Done()
}()
}
wg.Wait()
if len(pathNames) == 0 {
if err := relayerImpl.StartRelayer(ctx, eRep, testPathName); err != nil {
return nil, fmt.Errorf("failed to start relayer: %w", err)
}
} else {
if err := relayerImpl.StartRelayer(ctx, eRep, pathNames...); err != nil {
return nil, fmt.Errorf("failed to start relayer: %w", err)
}
}
// TODO: cleanup since this will stack multiple StopRelayer calls for
// multiple calls to this func, requires StopRelayer to be idempotent.
t.Cleanup(func() {
if err := relayerImpl.StopRelayer(ctx, eRep); err != nil {
t.Logf("error stopping relayer: %v", err)
}
})
// wait for relayer(s) to start up
time.Sleep(5 * time.Second)
return channels, nil
}