提交 9a5bb52f 作者: Juan Batiz-Benet

Merge pull request #1194 from ipfs/fix/iptb-fails

increase iptb test stability
...@@ -256,7 +256,7 @@ ...@@ -256,7 +256,7 @@
}, },
{ {
"ImportPath": "github.com/whyrusleeping/iptb", "ImportPath": "github.com/whyrusleeping/iptb",
"Rev": "4fa36405d0baea7773676f83fba9695e9a560473" "Rev": "3970c95a864f1a40037f796ff596607ce8ae43be"
}, },
{ {
"ImportPath": "golang.org/x/crypto/blowfish", "ImportPath": "golang.org/x/crypto/blowfish",
......
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net" "net/http"
"os" "os"
"os/exec" "os/exec"
"path" "path"
...@@ -15,7 +16,9 @@ import ( ...@@ -15,7 +16,9 @@ import (
"syscall" "syscall"
"time" "time"
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
serial "github.com/ipfs/go-ipfs/repo/fsrepo/serialize" serial "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
manet "github.com/jbenet/go-multiaddr-net"
) )
// GetNumNodes returns the number of testbed nodes configured in the testbed directory // GetNumNodes returns the number of testbed nodes configured in the testbed directory
...@@ -66,6 +69,15 @@ type initCfg struct { ...@@ -66,6 +69,15 @@ type initCfg struct {
Count int Count int
Force bool Force bool
Bootstrap string Bootstrap string
PortStart int
}
func (c *initCfg) swarmAddrForPeer(i int) string {
return fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", c.PortStart+i)
}
func (c *initCfg) apiAddrForPeer(i int) string {
return fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", c.PortStart+1000+i)
} }
func IpfsInit(cfg *initCfg) error { func IpfsInit(cfg *initCfg) error {
...@@ -121,7 +133,7 @@ func IpfsInit(cfg *initCfg) error { ...@@ -121,7 +133,7 @@ func IpfsInit(cfg *initCfg) error {
return nil return nil
} }
func starBootstrap(cfg *initCfg) error { func starBootstrap(icfg *initCfg) error {
// '0' node is the bootstrap node // '0' node is the bootstrap node
cfgpath := path.Join(IpfsDirN(0), "config") cfgpath := path.Join(IpfsDirN(0), "config")
bcfg, err := serial.Load(cfgpath) bcfg, err := serial.Load(cfgpath)
...@@ -129,15 +141,15 @@ func starBootstrap(cfg *initCfg) error { ...@@ -129,15 +141,15 @@ func starBootstrap(cfg *initCfg) error {
return err return err
} }
bcfg.Bootstrap = nil bcfg.Bootstrap = nil
bcfg.Addresses.Swarm = []string{"/ip4/127.0.0.1/tcp/4002"} bcfg.Addresses.Swarm = []string{icfg.swarmAddrForPeer(0)}
bcfg.Addresses.API = "/ip4/127.0.0.1/tcp/5002" bcfg.Addresses.API = icfg.apiAddrForPeer(0)
bcfg.Addresses.Gateway = "" bcfg.Addresses.Gateway = ""
err = serial.WriteConfigFile(cfgpath, bcfg) err = serial.WriteConfigFile(cfgpath, bcfg)
if err != nil { if err != nil {
return err return err
} }
for i := 1; i < cfg.Count; i++ { for i := 1; i < icfg.Count; i++ {
cfgpath := path.Join(IpfsDirN(i), "config") cfgpath := path.Join(IpfsDirN(i), "config")
cfg, err := serial.Load(cfgpath) cfg, err := serial.Load(cfgpath)
if err != nil { if err != nil {
...@@ -147,9 +159,9 @@ func starBootstrap(cfg *initCfg) error { ...@@ -147,9 +159,9 @@ func starBootstrap(cfg *initCfg) error {
cfg.Bootstrap = []string{fmt.Sprintf("%s/ipfs/%s", bcfg.Addresses.Swarm[0], bcfg.Identity.PeerID)} cfg.Bootstrap = []string{fmt.Sprintf("%s/ipfs/%s", bcfg.Addresses.Swarm[0], bcfg.Identity.PeerID)}
cfg.Addresses.Gateway = "" cfg.Addresses.Gateway = ""
cfg.Addresses.Swarm = []string{ cfg.Addresses.Swarm = []string{
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", 4002+i), icfg.swarmAddrForPeer(i),
} }
cfg.Addresses.API = fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5002+i) cfg.Addresses.API = icfg.apiAddrForPeer(i)
err = serial.WriteConfigFile(cfgpath, cfg) err = serial.WriteConfigFile(cfgpath, cfg)
if err != nil { if err != nil {
return err return err
...@@ -158,8 +170,8 @@ func starBootstrap(cfg *initCfg) error { ...@@ -158,8 +170,8 @@ func starBootstrap(cfg *initCfg) error {
return nil return nil
} }
func clearBootstrapping(cfg *initCfg) error { func clearBootstrapping(icfg *initCfg) error {
for i := 0; i < cfg.Count; i++ { for i := 0; i < icfg.Count; i++ {
cfgpath := path.Join(IpfsDirN(i), "config") cfgpath := path.Join(IpfsDirN(i), "config")
cfg, err := serial.Load(cfgpath) cfg, err := serial.Load(cfgpath)
if err != nil { if err != nil {
...@@ -168,10 +180,8 @@ func clearBootstrapping(cfg *initCfg) error { ...@@ -168,10 +180,8 @@ func clearBootstrapping(cfg *initCfg) error {
cfg.Bootstrap = nil cfg.Bootstrap = nil
cfg.Addresses.Gateway = "" cfg.Addresses.Gateway = ""
cfg.Addresses.Swarm = []string{ cfg.Addresses.Swarm = []string{icfg.swarmAddrForPeer(i)}
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", 4002+i), cfg.Addresses.API = icfg.apiAddrForPeer(i)
}
cfg.Addresses.API = fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", 5002+i)
err = serial.WriteConfigFile(cfgpath, cfg) err = serial.WriteConfigFile(cfgpath, cfg)
if err != nil { if err != nil {
return err return err
...@@ -222,6 +232,7 @@ func IpfsKill() error { ...@@ -222,6 +232,7 @@ func IpfsKill() error {
} }
func IpfsStart(waitall bool) error { func IpfsStart(waitall bool) error {
var addrs []string
n := GetNumNodes() n := GetNumNodes()
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
dir := IpfsDirN(i) dir := IpfsDirN(i)
...@@ -259,22 +270,55 @@ func IpfsStart(waitall bool) error { ...@@ -259,22 +270,55 @@ func IpfsStart(waitall bool) error {
// Make sure node 0 is up before starting the rest so // Make sure node 0 is up before starting the rest so
// bootstrapping works properly // bootstrapping works properly
if i == 0 || waitall { if i == 0 || waitall {
err := waitForLive(fmt.Sprintf("localhost:%d", 5002+i)) cfg, err := serial.Load(path.Join(IpfsDirN(i), "config"))
if err != nil {
return err
}
maddr := ma.StringCast(cfg.Addresses.API)
_, addr, err := manet.DialArgs(maddr)
if err != nil {
return err
}
addrs = append(addrs, addr)
err = waitOnAPI(cfg.Identity.PeerID, addr)
if err != nil {
return err
}
}
}
if waitall {
for i := 0; i < n; i++ {
err := waitOnSwarmPeers(addrs[i])
if err != nil { if err != nil {
return err return err
} }
} }
} }
return nil return nil
} }
// waitForLive polls the given endpoint until it is up, or until func waitOnAPI(peerid, addr string) error {
// a timeout
func waitForLive(addr string) error {
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
c, err := net.Dial("tcp", addr) resp, err := http.Get("http://" + addr + "/api/v0/id")
if err == nil { if err == nil {
c.Close() out := make(map[string]interface{})
err := json.NewDecoder(resp.Body).Decode(&out)
if err != nil {
return fmt.Errorf("liveness check failed: %s", err)
}
id, ok := out["ID"]
if !ok {
return fmt.Errorf("liveness check failed: ID field not present in output")
}
idstr := id.(string)
if idstr != peerid {
return fmt.Errorf("liveness check failed: unexpected peer at endpoint")
}
return nil return nil
} }
time.Sleep(time.Millisecond * 200) time.Sleep(time.Millisecond * 200)
...@@ -282,6 +326,29 @@ func waitForLive(addr string) error { ...@@ -282,6 +326,29 @@ func waitForLive(addr string) error {
return fmt.Errorf("node at %s failed to come online in given time period", addr) return fmt.Errorf("node at %s failed to come online in given time period", addr)
} }
func waitOnSwarmPeers(addr string) error {
for i := 0; i < 50; i++ {
resp, err := http.Get("http://" + addr + "/api/v0/swarm/peers")
if err == nil {
out := make(map[string]interface{})
err := json.NewDecoder(resp.Body).Decode(&out)
if err != nil {
return fmt.Errorf("liveness check failed: %s", err)
}
peers := out["Strings"].([]interface{})
if len(peers) == 0 {
time.Sleep(time.Millisecond * 200)
continue
}
return nil
}
time.Sleep(time.Millisecond * 200)
}
return fmt.Errorf("node at %s failed to bootstrap in given time period", addr)
}
// GetPeerID reads the config of node 'n' and returns its peer ID // GetPeerID reads the config of node 'n' and returns its peer ID
func GetPeerID(n int) (string, error) { func GetPeerID(n int) (string, error) {
cfg, err := serial.Load(path.Join(IpfsDirN(n), "config")) cfg, err := serial.Load(path.Join(IpfsDirN(n), "config"))
...@@ -371,6 +438,7 @@ func handleErr(s string, err error) { ...@@ -371,6 +438,7 @@ func handleErr(s string, err error) {
func main() { func main() {
cfg := new(initCfg) cfg := new(initCfg)
flag.IntVar(&cfg.Count, "n", 0, "number of ipfs nodes to initialize") flag.IntVar(&cfg.Count, "n", 0, "number of ipfs nodes to initialize")
flag.IntVar(&cfg.PortStart, "p", 4002, "port to start allocations from")
flag.BoolVar(&cfg.Force, "f", false, "force initialization (overwrite existing configs)") flag.BoolVar(&cfg.Force, "f", false, "force initialization (overwrite existing configs)")
flag.StringVar(&cfg.Bootstrap, "bootstrap", "star", "select bootstrapping style for cluster") flag.StringVar(&cfg.Bootstrap, "bootstrap", "star", "select bootstrapping style for cluster")
......
...@@ -11,7 +11,7 @@ test_description="Test ipfs repo operations" ...@@ -11,7 +11,7 @@ test_description="Test ipfs repo operations"
export IPTB_ROOT="`pwd`/.iptb" export IPTB_ROOT="`pwd`/.iptb"
test_expect_success "set up an iptb cluster" ' test_expect_success "set up an iptb cluster" '
iptb -n=4 init && iptb -n=4 -p=9000 init &&
iptb -wait start iptb -wait start
' '
......
...@@ -11,7 +11,7 @@ test_description="Test multiple ipfs nodes" ...@@ -11,7 +11,7 @@ test_description="Test multiple ipfs nodes"
export IPTB_ROOT="`pwd`/.iptb" export IPTB_ROOT="`pwd`/.iptb"
test_expect_success "set up a few nodes" ' test_expect_success "set up a few nodes" '
iptb -n=3 init && iptb -n=3 -p=9200 init &&
iptb -wait start iptb -wait start
' '
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论