From c93e789b6b32c62db0807164b3eebd57715d6e64 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 29 May 2015 10:21:41 +0100 Subject: [PATCH 1/4] use an automatically assigned port for http tests instead of random port, which can clash Fixes #720. --- nameserver/http.go | 7 +++---- nameserver/http_test.go | 10 +++++++--- weavedns/main.go | 7 ++++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/nameserver/http.go b/nameserver/http.go index 1f9a1e2b..64c0394a 100644 --- a/nameserver/http.go +++ b/nameserver/http.go @@ -17,7 +17,7 @@ func httpErrorAndLog(level *log.Logger, w http.ResponseWriter, msg string, level.Printf("[http] "+logmsg, logargs...) } -func ListenHTTP(version string, server *DNSServer, domain string, db Zone, port int) { +func ServeHTTP(listener net.Listener, version string, server *DNSServer, domain string, db Zone) { muxRouter := mux.NewRouter() @@ -90,8 +90,7 @@ func ListenHTTP(version string, server *DNSServer, domain string, db Zone, port http.Handle("/", muxRouter) - address := fmt.Sprintf(":%d", port) - if err := http.ListenAndServe(address, nil); err != nil { - Error.Fatal("[http] Unable to create http listener: ", err) + if err := http.Serve(listener, nil); err != nil { + Error.Fatal("[http] Unable to serve http: ", err) } } diff --git a/nameserver/http_test.go b/nameserver/http_test.go index b0175fe7..4a44eb9f 100644 --- a/nameserver/http_test.go +++ b/nameserver/http_test.go @@ -2,7 +2,6 @@ package nameserver import ( "fmt" - "math/rand" "net" "net/http" "net/url" @@ -35,9 +34,14 @@ func TestHttp(t *testing.T) { wt.AssertNoErr(t, err) defer zone.Stop() - port := rand.Intn(10000) + 32768 + httpListener, err := net.Listen("tcp", ":0") + if err != nil { + t.Fatal("Unable to create http listener: ", err) + } + + port := httpListener.Addr().(*net.TCPAddr).Port fmt.Println("Http test on port", port) - go ListenHTTP("", nil, testDomain, zone, port) + go ServeHTTP(httpListener, "", nil, testDomain, zone) time.Sleep(100 * time.Millisecond) // Allow for http server to get going diff --git a/weavedns/main.go b/weavedns/main.go index bf0a89e3..0fc48523 100644 --- a/weavedns/main.go +++ b/weavedns/main.go @@ -127,8 +127,13 @@ func main() { Error.Fatal("[main] Failed to initialize the WeaveDNS server", err) } + httpListener, err := net.Listen("tcp", fmt.Sprintf(":%d", httpPort)) + if err != nil { + Error.Fatal("[main] Unable to create http listener: ", err) + } + go SignalHandlerLoop(srv) - go weavedns.ListenHTTP(version, srv, domain, zone, httpPort) + go weavedns.ServeHTTP(httpListener, version, srv, domain, zone) err = srv.Start() if err != nil { From fb721558afb8d9d2716d0b05498ffd259eac31c9 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 29 May 2015 12:32:10 +0100 Subject: [PATCH 2/4] auto-assign port in ipam tests --- ipam/http_test.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/ipam/http_test.go b/ipam/http_test.go index a42bf5ba..dd2adc71 100644 --- a/ipam/http_test.go +++ b/ipam/http_test.go @@ -4,7 +4,7 @@ import ( "fmt" "io" "io/ioutil" - "math/rand" + "net" "net/http" "strings" "testing" @@ -30,20 +30,25 @@ func doHTTP(method string, url string) (resp *http.Response, err error) { return http.DefaultClient.Do(req) } -func listenHTTP(port int, alloc *Allocator) { +func listenHTTP(alloc *Allocator) int { router := mux.NewRouter() router.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, fmt.Sprintln(alloc)) }) alloc.HandleHTTP(router) - srv := &http.Server{ - Addr: fmt.Sprintf(":%d", port), - Handler: router, - } - if err := srv.ListenAndServe(); err != nil { + httpListener, err := net.Listen("tcp", ":0") + if err != nil { common.Error.Fatal("Unable to create http listener: ", err) } + + go func() { + srv := &http.Server{Handler: router} + if err := srv.Serve(httpListener); err != nil { + common.Error.Fatal("Unable to serve http: ", err) + } + }() + return httpListener.Addr().(*net.TCPAddr).Port } func allocURL(port int, containerID string) string { @@ -61,9 +66,8 @@ func TestHttp(t *testing.T) { ) alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1) - port := rand.Intn(10000) + 32768 + port := listenHTTP(alloc) fmt.Println("Http test on port", port) - go listenHTTP(port, alloc) time.Sleep(100 * time.Millisecond) // Allow for http server to get going @@ -98,9 +102,8 @@ func TestBadHttp(t *testing.T) { alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1) defer alloc.Stop() - port := rand.Intn(10000) + 32768 - fmt.Println("BadHttp test on port", port) - go listenHTTP(port, alloc) + port := listenHTTP(alloc) + fmt.Println("Http test on port", port) alloc.claimRingForTesting() cidr1 := HTTPPost(t, allocURL(port, containerID)) @@ -136,9 +139,8 @@ func impTestHTTPCancel(t *testing.T) { alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1) defer alloc.Stop() alloc.claimRingForTesting() - port := rand.Intn(10000) + 32768 + port := listenHTTP(alloc) fmt.Println("Http test on port", port) - go listenHTTP(port, alloc) time.Sleep(100 * time.Millisecond) // Allow for http server to get going From 3ad08c0f2a84fcd0cccb4d81e10b3af2e4bbd20f Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 29 May 2015 14:05:24 +0100 Subject: [PATCH 3/4] don't print the test port it's of no interest whatsoever --- ipam/http_test.go | 3 --- nameserver/http_test.go | 1 - 2 files changed, 4 deletions(-) diff --git a/ipam/http_test.go b/ipam/http_test.go index dd2adc71..911f3ef0 100644 --- a/ipam/http_test.go +++ b/ipam/http_test.go @@ -67,7 +67,6 @@ func TestHttp(t *testing.T) { alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1) port := listenHTTP(alloc) - fmt.Println("Http test on port", port) time.Sleep(100 * time.Millisecond) // Allow for http server to get going @@ -103,7 +102,6 @@ func TestBadHttp(t *testing.T) { alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1) defer alloc.Stop() port := listenHTTP(alloc) - fmt.Println("Http test on port", port) alloc.claimRingForTesting() cidr1 := HTTPPost(t, allocURL(port, containerID)) @@ -140,7 +138,6 @@ func impTestHTTPCancel(t *testing.T) { defer alloc.Stop() alloc.claimRingForTesting() port := listenHTTP(alloc) - fmt.Println("Http test on port", port) time.Sleep(100 * time.Millisecond) // Allow for http server to get going diff --git a/nameserver/http_test.go b/nameserver/http_test.go index 4a44eb9f..805624c5 100644 --- a/nameserver/http_test.go +++ b/nameserver/http_test.go @@ -40,7 +40,6 @@ func TestHttp(t *testing.T) { } port := httpListener.Addr().(*net.TCPAddr).Port - fmt.Println("Http test on port", port) go ServeHTTP(httpListener, "", nil, testDomain, zone) time.Sleep(100 * time.Millisecond) // Allow for http server to get going From 08a17ec376429e19096a7e2c08a9ec5177fb29e0 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Fri, 29 May 2015 14:17:03 +0100 Subject: [PATCH 4/4] no need for sleeps to wait for http server Since we start the tcp listener in the main thread now, connections can be made immediately; they will just queue up until there is a handler. --- ipam/http_test.go | 4 ---- nameserver/http_test.go | 3 --- 2 files changed, 7 deletions(-) diff --git a/ipam/http_test.go b/ipam/http_test.go index 911f3ef0..b8c972f0 100644 --- a/ipam/http_test.go +++ b/ipam/http_test.go @@ -68,8 +68,6 @@ func TestHttp(t *testing.T) { alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1) port := listenHTTP(alloc) - time.Sleep(100 * time.Millisecond) // Allow for http server to get going - alloc.claimRingForTesting() // Ask the http server for a new address cidr1 := HTTPPost(t, allocURL(port, containerID)) @@ -139,8 +137,6 @@ func impTestHTTPCancel(t *testing.T) { alloc.claimRingForTesting() port := listenHTTP(alloc) - time.Sleep(100 * time.Millisecond) // Allow for http server to get going - // Stop the alloc so nothing actually works unpause := alloc.pause() diff --git a/nameserver/http_test.go b/nameserver/http_test.go index 805624c5..44e5bfc0 100644 --- a/nameserver/http_test.go +++ b/nameserver/http_test.go @@ -7,7 +7,6 @@ import ( "net/url" "strings" "testing" - "time" wt "github.com/weaveworks/weave/testing" ) @@ -42,8 +41,6 @@ func TestHttp(t *testing.T) { port := httpListener.Addr().(*net.TCPAddr).Port go ServeHTTP(httpListener, "", nil, testDomain, zone) - time.Sleep(100 * time.Millisecond) // Allow for http server to get going - // Ask the http server to add our test address into the database addrParts := strings.Split(testAddr1, "/") addrURL := fmt.Sprintf("http://localhost:%d/name/%s/%s", port, containerID, addrParts[0])