forked from FactomProject/factomd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testHelper.go
253 lines (213 loc) · 6.78 KB
/
testHelper.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
package nettest
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"testing"
"github.com/FactomProject/factomd/p2p"
"github.com/FactomProject/factomd/state"
"github.com/FactomProject/factomd/testHelper"
)
type remoteNode struct {
address string
rpcPort int
}
type testNode struct {
state *state.State
fnodes map[int]remoteNode
seedAddress string
}
var DEV_NET string = "127.0.0.1:18110" // devnet port forward to fnode0 peer
var DOCKER_NETWORK string = "10.7.0.1:8110" // docker network
var SINGLE_NODE string = "127.0.0.1:39001" // local netTest simulator
var DEV_NET_CONFIG string = `
LocalSpecialPeers = "127.0.0.1:18110"
Exclusive = true
`
func SetupNode(seedNode string, minPeers int, t *testing.T) *testNode {
homeDir := testHelper.ResetSimHome(t)
var CmdLineOptions map[string]string
// Use identity 9 to run a follower
if seedNode == DOCKER_NETWORK {
testHelper.WriteConfigFile(9, 0, "", t)
// use config that mirrors docker-compose from ./support/dev/docker-compose
CmdLineOptions = map[string]string{
"--db": "Map",
"--network": "CUSTOM",
"--customnet": "net",
"--enablenet": "true",
"--blktime": "30",
"--count": "1",
"--startdelay": "0",
"--stdoutlog": "out.txt",
"--stderrlog": "out.txt",
"--checkheads": "false",
"--controlpanelsetting": "readwrite",
"--debuglog": "",
"--logPort": "39000",
"--port": "39001",
"--controlpanelport": "39002",
"--networkport": "39003",
"--peers": seedNode,
"--factomhome": homeDir,
}
}
// when interfacing w/ devnet don't talk to other peers
if seedNode == DEV_NET {
testHelper.WriteConfigFile(9, 0, DEV_NET_CONFIG, t)
CmdLineOptions = map[string]string{
"--db": "Map",
"--network": "LOCAL",
"--enablenet": "true",
"--blktime": "30",
"--debuglog": "",
"--logPort": "39000",
"--port": "39001",
"--controlpanelport": "39002",
"--networkport": "39003",
"--factomhome": homeDir,
}
}
n := &testNode{state: testHelper.StartPeer(CmdLineOptions)}
t.Log("Waiting for first block")
testHelper.WaitForBlock(n.state, 1)
n.seedAddress = seedNode
t.Log("Discovering Peers")
n.DiscoverPeers(minPeers)
return n
}
// status every minute for this local node
func (n *testNode) StatusEveryMinute() {
// REVIEW: this could hit the RPC port to pull summary from remote nodes
// instead of simply reporting on local fnode
testHelper.StatusEveryMinute(n.state)
}
// wait minutes on this local node
func (n *testNode) WaitMinutes(minutes int) {
testHelper.WaitMinutes(n.state, minutes)
}
// wait blocks on this local node
func (n *testNode) WaitBlocks(blocks int) {
testHelper.WaitBlocks(n.state, blocks)
}
// wait for block on this local node
func (n *testNode) WaitForBlock(block int) {
testHelper.WaitForBlock(n.state, block)
}
func (n *testNode) DiscoverPeers(minPeers int) {
peers := n.GetPeers()
for len(peers) < minPeers {
peers = n.GetPeers()
testHelper.WaitBlocks(n.state, 1) // let more time pass to discover peers
}
switch n.seedAddress {
case SINGLE_NODE: // local simulator
if minPeers != 0 {
panic("local testing only")
}
n.fnodes = map[int]remoteNode{
0: remoteNode{"127.0.0.1", 39001},
}
case DOCKER_NETWORK: // using ./support/dev/docker-compose.json
n.fnodes = map[int]remoteNode{
0: remoteNode{"10.7.0.1", 8088},
1: remoteNode{"10.7.0.2", 8088},
2: remoteNode{"10.7.0.3", 8088},
}
case DEV_NET: // using devnet port forwards
if minPeers != 0 {
panic("local testing only")
}
n.fnodes = map[int]remoteNode{
0: remoteNode{"127.0.0.1", 8000},
1: remoteNode{"127.0.0.1", 8001},
2: remoteNode{"127.0.0.1", 8002},
3: remoteNode{"127.0.0.1", 8003},
4: remoteNode{"127.0.0.1", 8004},
}
default:
/*
REVIEW: automatic peer discovery should be re-visited after DevNet setup includes VPN
main blocker currently is that DevNet is setup to forward ports to loopback
so we cannot target the RPC api using this discovered IP Address
*/
panic("netTest only support hardcoded networks")
}
}
// get list of peers from our local node
func (n *testNode) GetPeers() map[string]p2p.Peer {
return n.state.NetworkController.GetKnownPeers()
}
// build url for debug API
func (r remoteNode) getAPIUrl() string {
if r.address == "" {
panic("remoteNode is nil")
}
return fmt.Sprintf("http://%s:%v/debug", r.address, r.rpcPort)
}
type netInfo struct {
NodeName string
Role string
NetworkNumber int
NetworkName string
NetworkID uint32
}
func (r remoteNode) NetworkInfo() (info netInfo) {
url := r.getAPIUrl()
res, err := rpc(url, "network-info", "{}")
if err != nil {
panic(err)
}
json.Unmarshal(*res["result"], &info)
return info
}
// FIXME: add a return code for waiting
func (r remoteNode) WaitForBlock(newBlock int) {
rpc(r.getAPIUrl(), "wait-for-block", fmt.Sprintf(`{ "block": %v }`, newBlock))
}
func (r remoteNode) WaitBlocks(blks int) {
rpc(r.getAPIUrl(), "wait-blocks", fmt.Sprintf(`{ "blocks": %v }`, blks))
}
func (r remoteNode) WaitForMinute(minute int) {
rpc(r.getAPIUrl(), "wait-for-minute", fmt.Sprintf(`{ "minute": %v }`, minute))
}
func (r remoteNode) WaitMinutes(minutes int) {
rpc(r.getAPIUrl(), "wait-minutes", fmt.Sprintf(`{ "minutes": %v }`, minutes))
}
func (r remoteNode) RunCmd(cmd string) error {
_, err := rpc(r.getAPIUrl(), "sim-ctrl", fmt.Sprintf(`{ "Commands": ["%v"] }`, cmd))
return err
}
func (r remoteNode) WriteConfig(identityNumber int, extra string) error {
c := make(map[string]string)
c["Config"] = testHelper.GetConfig(identityNumber, extra)
cfg, _ := json.Marshal(c)
_, err := rpc(r.getAPIUrl(), "write-configuration", string(cfg))
return err
}
// make HTTP post
func postRequest(debugUrl string, jsonStr string) (*http.Response, error) {
req, err := http.NewRequest("POST", debugUrl, bytes.NewBuffer([]byte(jsonStr)))
if err != nil {
return nil, err
}
req.Header.Set("content-type", "text/plain;")
req.Host = "localhost:8088" // KLUDGE: mux routing expects this to match the bound port
client := &http.Client{}
return client.Do(req)
}
// make calls to the debug api
func rpc(apiUrl string, method string, params string) (response map[string]*json.RawMessage, err error) {
fmt.Printf("\nsend => %s", params)
r, err := postRequest(apiUrl, fmt.Sprintf(`{"jsonrpc": "2.0", "id": 0, "method": "%s", "params": %s}`, method, params))
defer r.Body.Close()
if err != nil {
return response, err
}
body, _ := ioutil.ReadAll(r.Body)
fmt.Printf("\nrpc => %s", body)
err = json.Unmarshal(body, &response)
return response, err
}