-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_setup.go
246 lines (201 loc) · 6.99 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
package test
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"sync"
"testing"
"github.com/cosmos/relayer/relayer"
"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.Chain, 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, err := ioutil.TempDir("", "relayer-test")
require.NoError(t, err)
// 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))
}
// make each container and initialize the chains
for i, tc := range testChains {
c := newTestChain(t, tc)
chains[i] = c
wg.Add(1)
genPrivValKeyJSON(tc.seed)
go spinUpTestContainer(t, rchan, pool, c, dir, &wg, tc)
}
// wait for all containers to be created
wg.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, dir string, wg *sync.WaitGroup, tc testChain) {
defer wg.Done()
var (
err error
debug bool
resource *dockertest.Resource
)
// add extra logging if TEST_DEBUG=true
if val, ok := os.LookupEnv("TEST_DEBUG"); ok {
debug, err = strconv.ParseBool(val)
if err != nil {
debug = false
}
}
// initialize the chain
require.NoError(t, c.Init(dir, tc.t.timeout, nil, debug))
// create the test key
require.NoError(t, c.CreateTestKey())
containerName := c.ChainID
// setup docker options
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,
c.MustGetAddress(),
getPrivValFileName(tc.seed),
},
PortBindings: map[dc.Port][]dc.PortBinding{
dc.Port(tc.t.rpcPort): {{HostPort: c.GetRPCPort()}},
},
}
// err = removeTestContainer(pool, containerName)
require.NoError(t, removeTestContainer(pool, containerName))
// create the proper docker image with port forwarding setup
d, err := os.Getwd()
require.NoError(t, err)
buildOpts := &dockertest.BuildOptions{
Dockerfile: tc.t.dockerfile,
ContextDir: path.Dir(d),
}
resource, err = pool.BuildAndRunWithBuildOptions(buildOpts, dockerOpts)
require.NoError(t, err)
c.Log(fmt.Sprintf("- [%s] SPUN UP IN CONTAINER %s from %s", c.ChainID,
resource.Container.Name, resource.Container.Config.Image))
// retry polling the container until status doesn't error
if err = pool.Retry(c.StatusErr); err != nil {
require.NoError(t, fmt.Errorf("could not connect to container at %s: %s", c.RPCAddr, err))
}
c.Log(fmt.Sprintf("- [%s] CONTAINER AVAILABLE AT PORT %s", c.ChainID, c.RPCAddr))
rchan <- resource
}
// 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.Chain) {
// 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 i, 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)
chains[i].Log(fmt.Sprintf("- [%s] SPUN DOWN 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.Chain, 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, srcPort, dstPort string) (*relayer.Path, error) {
p := relayer.GenPath(src.ChainID, dst.ChainID, srcPort, dstPort, "UNORDERED", "ics20-1")
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)
}