github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/cmd/swarm/run_test.go (about) 1 // Copyright 2017 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "net" 23 "os" 24 "path/filepath" 25 "runtime" 26 "testing" 27 "time" 28 29 "github.com/docker/docker/pkg/reexec" 30 "github.com/vntchain/go-vnt/accounts" 31 "github.com/vntchain/go-vnt/accounts/keystore" 32 "github.com/vntchain/go-vnt/internal/cmdtest" 33 "github.com/vntchain/go-vnt/node" 34 "github.com/vntchain/go-vnt/rpc" 35 "github.com/vntchain/go-vnt/swarm" 36 "github.com/vntchain/go-vnt/vntp2p" 37 ) 38 39 func init() { 40 // Run the app if we've been exec'd as "swarm-test" in runSwarm. 41 reexec.Register("swarm-test", func() { 42 if err := app.Run(os.Args); err != nil { 43 fmt.Fprintln(os.Stderr, err) 44 os.Exit(1) 45 } 46 os.Exit(0) 47 }) 48 } 49 50 func TestMain(m *testing.M) { 51 // check if we have been reexec'd 52 if reexec.Init() { 53 return 54 } 55 os.Exit(m.Run()) 56 } 57 58 func runSwarm(t *testing.T, args ...string) *cmdtest.TestCmd { 59 tt := cmdtest.NewTestCmd(t, nil) 60 61 // Boot "swarm". This actually runs the test binary but the TestMain 62 // function will prevent any tests from running. 63 tt.Run("swarm-test", args...) 64 65 return tt 66 } 67 68 type testCluster struct { 69 Nodes []*testNode 70 TmpDir string 71 } 72 73 // newTestCluster starts a test swarm cluster of the given size. 74 // 75 // A temporary directory is created and each node gets a data directory inside 76 // it. 77 // 78 // Each node listens on 127.0.0.1 with random ports for both the HTTP and p2p 79 // ports (assigned by first listening on 127.0.0.1:0 and then passing the ports 80 // as flags). 81 // 82 // When starting more than one node, they are connected together using the 83 // admin SetPeer RPC method. 84 func newTestCluster(t *testing.T, size int) *testCluster { 85 cluster := &testCluster{} 86 defer func() { 87 if t.Failed() { 88 cluster.Shutdown() 89 } 90 }() 91 92 tmpdir, err := ioutil.TempDir("", "swarm-test") 93 if err != nil { 94 t.Fatal(err) 95 } 96 cluster.TmpDir = tmpdir 97 98 // start the nodes 99 cluster.Nodes = make([]*testNode, 0, size) 100 for i := 0; i < size; i++ { 101 dir := filepath.Join(cluster.TmpDir, fmt.Sprintf("swarm%02d", i)) 102 if err := os.Mkdir(dir, 0700); err != nil { 103 t.Fatal(err) 104 } 105 106 node := newTestNode(t, dir) 107 node.Name = fmt.Sprintf("swarm%02d", i) 108 109 cluster.Nodes = append(cluster.Nodes, node) 110 } 111 112 if size == 1 { 113 return cluster 114 } 115 116 // connect the nodes together 117 for _, node := range cluster.Nodes { 118 if err := node.Client.Call(nil, "admin_addPeer", cluster.Nodes[0].Vnode); err != nil { 119 t.Fatal(err) 120 } 121 } 122 123 // wait until all nodes have the correct number of peers 124 outer: 125 for _, node := range cluster.Nodes { 126 var peers []*vntp2p.PeerInfo 127 for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(50 * time.Millisecond) { 128 if err := node.Client.Call(&peers, "admin_peers"); err != nil { 129 t.Fatal(err) 130 } 131 if len(peers) == len(cluster.Nodes)-1 { 132 continue outer 133 } 134 } 135 t.Fatalf("%s only has %d / %d peers", node.Name, len(peers), len(cluster.Nodes)-1) 136 } 137 138 return cluster 139 } 140 141 func (c *testCluster) Shutdown() { 142 for _, node := range c.Nodes { 143 node.Shutdown() 144 } 145 os.RemoveAll(c.TmpDir) 146 } 147 148 type testNode struct { 149 Name string 150 Addr string 151 URL string 152 Vnode string 153 Dir string 154 Client *rpc.Client 155 Cmd *cmdtest.TestCmd 156 } 157 158 const testPassphrase = "swarm-test-passphrase" 159 160 func getTestAccount(t *testing.T, dir string) (conf *node.Config, account accounts.Account) { 161 // create key 162 conf = &node.Config{ 163 DataDir: dir, 164 IPCPath: "bzzd.ipc", 165 } 166 n, err := node.New(conf) 167 if err != nil { 168 t.Fatal(err) 169 } 170 account, err = n.AccountManager().Backends(keystore.KeyStoreType)[0].(*keystore.KeyStore).NewAccount(testPassphrase) 171 if err != nil { 172 t.Fatal(err) 173 } 174 175 // use a unique IPCPath when running tests on Windows 176 if runtime.GOOS == "windows" { 177 conf.IPCPath = fmt.Sprintf("bzzd-%s.ipc", account.Address.String()) 178 } 179 180 return conf, account 181 } 182 183 func newTestNode(t *testing.T, dir string) *testNode { 184 185 conf, account := getTestAccount(t, dir) 186 node := &testNode{Dir: dir} 187 188 // assign ports 189 httpPort, err := assignTCPPort() 190 if err != nil { 191 t.Fatal(err) 192 } 193 p2pPort, err := assignTCPPort() 194 if err != nil { 195 t.Fatal(err) 196 } 197 198 // start the node 199 node.Cmd = runSwarm(t, 200 "--port", p2pPort, 201 "--nodiscover", 202 "--datadir", dir, 203 "--ipcpath", conf.IPCPath, 204 "--vns-api", "", 205 "--bzzaccount", account.Address.String(), 206 "--bzznetworkid", "321", 207 "--bzzport", httpPort, 208 "--verbosity", "6", 209 ) 210 node.Cmd.InputLine(testPassphrase) 211 defer func() { 212 if t.Failed() { 213 node.Shutdown() 214 } 215 }() 216 217 // wait for the node to start 218 for start := time.Now(); time.Since(start) < 10*time.Second; time.Sleep(50 * time.Millisecond) { 219 node.Client, err = rpc.Dial(conf.IPCEndpoint()) 220 if err == nil { 221 break 222 } 223 } 224 if node.Client == nil { 225 t.Fatal(err) 226 } 227 228 // load info 229 var info swarm.Info 230 if err := node.Client.Call(&info, "bzz_info"); err != nil { 231 t.Fatal(err) 232 } 233 node.Addr = net.JoinHostPort("127.0.0.1", info.Port) 234 node.URL = "http://" + node.Addr 235 236 var nodeInfo vntp2p.NodeInfo 237 if err := node.Client.Call(&nodeInfo, "admin_nodeInfo"); err != nil { 238 t.Fatal(err) 239 } 240 node.Vnode = "/ip4/127.0.0.1/tcp/" + p2pPort + "/ipfs/" + nodeInfo.ID 241 242 return node 243 } 244 245 func (n *testNode) Shutdown() { 246 if n.Cmd != nil { 247 n.Cmd.Kill() 248 } 249 } 250 251 func assignTCPPort() (string, error) { 252 l, err := net.Listen("tcp", "127.0.0.1:0") 253 if err != nil { 254 return "", err 255 } 256 l.Close() 257 _, port, err := net.SplitHostPort(l.Addr().String()) 258 if err != nil { 259 return "", err 260 } 261 return port, nil 262 }