Commit 56a32a30 authored by Henry's avatar Henry

using pollEndpoint to block in tests for 'daemon ready' (updates #844)

parent 51a8bcbf
......@@ -244,6 +244,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
corehttp.CommandsOption(*req.Context()),
corehttp.WebUIOption,
gateway.ServeOption(),
corehttp.VersionOption(),
}
if rootRedirect != nil {
opts = append(opts, rootRedirect)
......
// pollEndpoint is a helper utility that waits for a http endpoint to be reachable and return with http.StatusOK
package main
import (
"flag"
"net"
"net/http"
"net/url"
"os"
"syscall"
"time"
log "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/Sirupsen/logrus"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
)
var (
host = flag.String("host", "/ip4/127.0.0.1/tcp/5001", "the multiaddr host to dial on")
endpoint = flag.String("ep", "/version", "which http endpoint path to hit")
tries = flag.Int("tries", 10, "how many tries to make before failing")
timeout = flag.Duration("tout", time.Second, "how long to wait between attempts")
verbose = flag.Bool("v", false, "verbose logging")
)
func main() {
flag.Parse()
// extract address from host flag
addr, err := ma.NewMultiaddr(*host)
if err != nil {
log.WithField("err", err).Fatal("NewMultiaddr() failed")
}
p := addr.Protocols()
if len(p) < 2 {
log.WithField("addr", addr).Fatal("need to protocolls in host flag.")
}
_, host, err := manet.DialArgs(addr)
if err != nil {
log.WithField("err", err).Fatal("manet.DialArgs() failed")
}
if *verbose { // lower log level
log.SetLevel(log.DebugLevel)
}
// construct url to dial
var u url.URL
u.Scheme = "http"
u.Host = host
u.Path = *endpoint
// show what we got
start := time.Now()
log.WithFields(log.Fields{
"when": start,
"tries": *tries,
"timeout": *timeout,
"url": u.String(),
}).Debug("starting")
for *tries > 0 {
f := log.Fields{"tries": *tries}
resp, err := http.Get(u.String())
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
f["took"] = time.Since(start)
log.WithFields(f).Println("status ok - endpoint reachable")
os.Exit(0)
}
f["status"] = resp.Status
log.WithFields(f).Warn("response not okay")
} else if urlErr, ok := err.(*url.Error); ok { // expected error from http.Get()
f["urlErr"] = urlErr
if urlErr.Op != "Get" || urlErr.URL != *endpoint {
f["op"] = urlErr.Op
f["url"] = urlErr.URL
log.WithFields(f).Error("way to funky buisness..!")
}
if opErr, ok := urlErr.Err.(*net.OpError); ok {
f["opErr"] = opErr
f["connRefused"] = opErr.Err == syscall.ECONNREFUSED
f["temporary"] = opErr.Temporary()
log.WithFields(f).Println("net.OpError")
}
} else { // unexpected error from http.Get()
f["err"] = err
log.WithFields(f).Error("unknown error")
}
time.Sleep(*timeout)
*tries--
}
log.Println("failed.")
os.Exit(1)
}
BINS = bin/random bin/multihash bin/ipfs
BINS = bin/random bin/multihash bin/ipfs bin/pollEndpoint
IPFS_ROOT = ../
IPFS_CMD = ../cmd/ipfs
RANDOM_SRC = ../Godeps/_workspace/src/github.com/jbenet/go-random
MULTIHASH_SRC = ../Godeps/_workspace/src/github.com/jbenet/go-multihash
POLLENDPOINT_SRC= ../cmd/pollEndpoint
all: deps
......@@ -23,6 +24,9 @@ bin/multihash: $(MULTIHASH_SRC)/**/*.go
bin/ipfs: $(IPFS_ROOT)/**/*.go
go build -o bin/ipfs $(IPFS_CMD)
bin/pollEndpoint: $(POLLENDPOINT_SRC)/*.go
go build -o bin/pollEndpoint $(POLLENDPOINT_SRC)
test: test_expensive
test_expensive:
......
......@@ -175,14 +175,13 @@ test_launch_ipfs_daemon() {
ADDR_API="/ip4/127.0.0.1/tcp/5001"
test_expect_success "'ipfs daemon' is ready" '
IPFS_PID=$! &&
test_wait_output_n_lines_60_sec actual_daemon 2 &&
test_run_repeat_60_sec "grep \"API server listening on $ADDR_API\" actual_daemon" ||
pollEndpoint -ep=/version -host=$ADDR_API -v -tout=1s -tries=60 2>poll_err > poll_apiout ||
test_fsh cat actual_daemon || test_fsh cat daemon_err
'
if test "$ADDR_GWAY" != ""; then
test_expect_success "'ipfs daemon' output includes Gateway address" '
test_run_repeat_60_sec "grep \"Gateway server listening on $ADDR_GWAY\" actual_daemon" ||
pollEndpoint -ep=/version -host=$ADDR_GWAY -v -tout=1s -tries=60 2>poll_err > poll_gwout ||
test_fsh cat daemon_err
'
fi
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment