Skip to content

Commit

Permalink
Merge pull request #851 from jbenet/test/pollDaemon
Browse files Browse the repository at this point in the history
Test/poll daemon
  • Loading branch information
jbenet committed Mar 4, 2015
2 parents 51a8bcb + 14090d4 commit 0adde5c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

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= ../thirdparty/pollEndpoint

all: deps

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion test/sharness/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# NOTE: run with TEST_VERBOSE=1 for verbose sharness tests.

T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh))
BINS = bin/random bin/multihash bin/ipfs
BINS = bin/random bin/multihash bin/ipfs bin/pollEndpoint
SHARNESS = lib/sharness/sharness.sh
IPFS_ROOT = ../..

Expand Down
9 changes: 4 additions & 5 deletions test/sharness/lib/test-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,14 @@ 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" ||
test_fsh cat actual_daemon || test_fsh cat daemon_err
pollEndpoint -ep=/version -host=$ADDR_API -v -tout=1s -tries=60 2>poll_apierr > poll_apiout ||
test_fsh cat actual_daemon || test_fsh cat daemon_err || test_fsh cat poll_apierr || test_fsh cat poll_apiout
'

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" ||
test_fsh cat daemon_err
pollEndpoint -ep=/version -host=$ADDR_GWAY -v -tout=1s -tries=60 2>poll_gwerr > poll_gwout ||
test_fsh cat daemon_err || test_fsh cat poll_gwerr || test_fsh cat poll_gwout
'
fi
}
Expand Down
93 changes: 93 additions & 0 deletions thirdparty/pollEndpoint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// pollEndpoint is a helper utility that waits for a http endpoint to be reachable and return with http.StatusOK
package main

import (
"flag"
"fmt"
"net/http"
"net/url"
"os"
"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 two protocols in host flag (/ip/tcp)")
}
_, 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}

err := checkOK(http.Get(u.String()))
if err == nil {
f["took"] = time.Since(start)
log.WithFields(f).Println("status ok - endpoint reachable")
os.Exit(0)
}
f["error"] = err
log.WithFields(f).Debug("get failed")
time.Sleep(*timeout)
*tries--
}

log.Println("failed.")
os.Exit(1)
}

func checkOK(resp *http.Response, err error) error {
if err == nil { // request worked
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
return fmt.Errorf("Response not OK. %d %s", resp.StatusCode, resp.Status)
} else if urlErr, ok := err.(*url.Error); ok { // expected error from http.Get()
if urlErr.Op != "Get" || urlErr.URL != *endpoint {
return fmt.Errorf("wrong url or endpoint error from http.Get() %#v", urlErr)
}
}
return err
}

0 comments on commit 0adde5c

Please sign in to comment.