Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/poll daemon #851

Merged
merged 5 commits into from
Mar 4, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
106 changes: 106 additions & 0 deletions cmd/pollEndpoint/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// pollEndpoint is a helper utility that waits for a http endpoint to be reachable and return with http.StatusOK
package main
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move this binary into /thirdparty/pollEndpoint ?


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.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"need two protocols in host flag (/ip/tcp)"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
_, 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd try to return out to a simple func like:

func main() { 
  if err := run(); err != nil {
    log.Println("failed.")
    os.Exit(1)
  }
}

that way if someone comes in and modifies things, the os.Exit isn't hard to work around then

}

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--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd break this up into smaller funcs?

}

log.Println("failed.")
os.Exit(1)
}
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= ../cmd/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