forked from cosmos/relayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_setup.go
286 lines (234 loc) · 7.93 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
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
package test
import (
"fmt"
"io"
"os"
"path"
"strings"
"sync"
"testing"
"time"
"github.com/cosmos/relayer/v2/relayer"
"golang.org/x/sync/errgroup"
"github.com/ory/dockertest/v3"
dc "github.com/ory/dockertest/v3/docker"
"github.com/stretchr/testify/require"
sdked25519 "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdkcryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
tmed25519 "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/privval"
)
// spinUpTestChains is to be passed any number of test chains with given configuration options
// to be created as individual docker containers at the beginning of a test. It is safe to run
// in parallel tests as all created resources are independent of eachother
func spinUpTestChains(t *testing.T, testChains ...testChain) relayer.Chains {
var (
resources []*dockertest.Resource
chains = make(relayer.Chains, len(testChains))
wg sync.WaitGroup
rchan = make(chan *dockertest.Resource, len(testChains))
testsDone = make(chan struct{})
contDone = make(chan struct{})
)
// Create temporary relayer test directory
dir := t.TempDir()
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
pool, err := dockertest.NewPool("")
if err != nil {
require.NoError(t, fmt.Errorf("could not connect to docker at %s: %w", pool.Client.Endpoint(), err))
}
var eg errgroup.Group
// make each container and initialize the chains
for _, tc := range testChains {
tc := tc
c := newTestChain(t, tc)
chains[tc.chainName] = c
wg.Add(1)
genPrivValKeyJSON(tc.seed)
eg.Go(func() error {
return spinUpTestContainer(t, rchan, pool, c, tc)
})
}
// wait for all containers to be created
require.NoError(t, eg.Wait())
// read all the containers out of the channel
for i := 0; i < len(chains); i++ {
r := <-rchan
resources = append(resources, r)
}
// close the channel
close(rchan)
// start the wait for cleanup function
go cleanUpTest(t, testsDone, contDone, resources, pool, dir, chains)
// set the test cleanup function
t.Cleanup(func() {
testsDone <- struct{}{}
<-contDone
})
// return the chains and the doneFunc
return chains
}
func removeTestContainer(pool *dockertest.Pool, containerName string) error {
containers, err := pool.Client.ListContainers(dc.ListContainersOptions{
All: true,
Filters: map[string][]string{
"name": {containerName},
},
})
if err != nil {
return fmt.Errorf("error while listing containers with name %s: %w", containerName, err)
}
if len(containers) == 0 {
return nil
}
err = pool.Client.RemoveContainer(dc.RemoveContainerOptions{
ID: containers[0].ID,
Force: true,
RemoveVolumes: true,
})
if err != nil {
return fmt.Errorf("error while removing container with name %s: %w", containerName, err)
}
return nil
}
// spinUpTestContainer spins up a test container with the given configuration
// A docker image is built for each chain using its provided configuration.
// This image is then ran using the options set below.
func spinUpTestContainer(t *testing.T, rchan chan<- *dockertest.Resource, pool *dockertest.Pool, c *relayer.Chain, tc testChain) error {
t.Helper()
var (
err error
resource *dockertest.Resource
)
// create the test key
if err := c.CreateTestKey(); err != nil {
return err
}
containerName := c.ChainID()
// setup docker options
addr, err := c.ChainProvider.Address()
if err != nil {
return err
}
dockerOpts := &dockertest.RunOptions{
Name: containerName,
Repository: containerName, // Name must match Repository
Tag: "latest", // Must match docker default build tag
ExposedPorts: []string{tc.t.rpcPort, c.GetRPCPort()},
Cmd: []string{
c.ChainID(),
addr,
// TODO getPrivValFileName() is not going to work with substrate.
// it's not immediately clear to me what we need to do here so will need to circle back on this.
getPrivValFileName(tc.seed),
},
PortBindings: map[dc.Port][]dc.PortBinding{
dc.Port(tc.t.rpcPort): {{HostPort: c.GetRPCPort()}},
},
}
if err := removeTestContainer(pool, containerName); err != nil {
return err
}
// create the proper docker image with port forwarding setup
d, err := os.Getwd()
if err != nil {
return err
}
buildOpts := &BuildOptions{
Dockerfile: tc.t.dockerfile,
ContextDir: path.Dir(d),
BuildArgs: tc.t.buildArgs,
}
hcOpt := func(hc *dc.HostConfig) {
hc.LogConfig.Type = "json-file"
}
resource, err = BuildAndRunWithBuildOptions(pool, buildOpts, dockerOpts, hcOpt)
if err != nil {
return err
}
t.Logf("Chain ID %s spun up in container %s from %s", c.ChainID(), resource.Container.Name, resource.Container.Config.Image)
// we used to poll here until the container is running without status errors but,
// we no longer expose the status error on the relayer.Chain struct.
// this sleep statement seems to work fine in all cases that we have seen over a few months.
time.Sleep(time.Second * 5)
t.Logf("Chain ID %s's container at port %s", c.ChainID(), c.RPCAddr)
rchan <- resource
return nil
}
// cleanUpTest is called as a goroutine to wait until the tests have completed and
// cleans up the docker containers and relayer config
func cleanUpTest(t *testing.T, testsDone <-chan struct{}, contDone chan<- struct{},
resources []*dockertest.Resource, pool *dockertest.Pool, dir string, chains relayer.Chains) {
// block here until tests are complete
<-testsDone
// clean up the tmp dir
if err := os.RemoveAll(dir); err != nil {
require.NoError(t, fmt.Errorf("{cleanUpTest} failed to rm dir(%w), %s ", err, dir))
}
// remove all the docker containers
for _, r := range resources {
if err := pool.Purge(r); err != nil {
require.NoError(t, fmt.Errorf("could not purge container %s: %w", r.Container.Name, err))
}
c := getLoggingChain(chains, r)
t.Logf("Spun down %s's container %s from %s", c.ChainID(), r.Container.Name, r.Container.Config.Image)
}
// Notify the other side that we have deleted the docker containers
contDone <- struct{}{}
}
// for the love of logs https://www.youtube.com/watch?v=DtsKcHmceqY
func getLoggingChain(chns relayer.Chains, rsr *dockertest.Resource) *relayer.Chain {
for _, c := range chns {
if strings.Contains(rsr.Container.Name, c.ChainID()) {
return c
}
}
return nil
}
func genTestPathAndSet(src, dst *relayer.Chain) (*relayer.Path, error) {
p := relayer.GenPath(src.ChainID(), dst.ChainID())
src.PathEnd = p.Src
dst.PathEnd = p.Dst
return p, nil
}
func genPrivValKeyJSON(seedNumber int) {
privKey := getPrivKey(seedNumber)
filePV := getFilePV(privKey, seedNumber)
filePV.Key.Save()
}
func getPrivKey(seedNumber int) tmed25519.PrivKey {
return tmed25519.GenPrivKeyFromSecret([]byte(seeds[seedNumber]))
}
func getSDKPrivKey(seedNumber int) sdkcryptotypes.PrivKey {
return sdked25519.GenPrivKeyFromSecret([]byte(seeds[seedNumber]))
}
func getFilePV(privKey tmed25519.PrivKey, seedNumber int) *privval.FilePV {
return privval.NewFilePV(privKey, getPrivValFileName(seedNumber), "/")
}
func getPrivValFileName(seedNumber int) string {
return fmt.Sprintf("./setup/valkeys/priv_val%d.json", seedNumber)
}
type BuildOptions struct {
Dockerfile string
ContextDir string
BuildArgs []dc.BuildArg
}
var muDockerBuild sync.Mutex
// BuildAndRunWithBuildOptions builds and starts a docker container.
// Optional modifier functions can be passed in order to change the hostconfig values not covered in RunOptions
func BuildAndRunWithBuildOptions(pool *dockertest.Pool, buildOpts *BuildOptions, runOpts *dockertest.RunOptions, hcOpts ...func(*dc.HostConfig)) (*dockertest.Resource, error) {
muDockerBuild.Lock()
defer muDockerBuild.Unlock()
err := pool.Client.BuildImage(dc.BuildImageOptions{
Name: runOpts.Name,
Dockerfile: buildOpts.Dockerfile,
OutputStream: io.Discard,
ContextDir: buildOpts.ContextDir,
BuildArgs: buildOpts.BuildArgs,
})
if err != nil {
return nil, err
}
runOpts.Repository = runOpts.Name
return pool.RunWithOptions(runOpts, hcOpts...)
}