Merge pull request #792 from rade/720_auto_assigned_dns_http_test_port

use an automatically assigned port for http tests

Fixes #720.
This commit is contained in:
Matthias Radestock committed 2015-05-29 15:51:38 +01:00
commit e0f4da4641
4 files changed
+30 -31

No files matched your search

+15 -20
View File
@@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"math/rand" "net"
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
@@ -30,20 +30,25 @@ func doHTTP(method string, url string) (resp *http.Response, err error) {
return http.DefaultClient.Do(req) return http.DefaultClient.Do(req)
} }
func listenHTTP(port int, alloc *Allocator) { func listenHTTP(alloc *Allocator) int {
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { router.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, fmt.Sprintln(alloc)) io.WriteString(w, fmt.Sprintln(alloc))
}) })
alloc.HandleHTTP(router) alloc.HandleHTTP(router)
srv := &http.Server{ httpListener, err := net.Listen("tcp", ":0")
Addr: fmt.Sprintf(":%d", port), if err != nil {
Handler: router,
}
if err := srv.ListenAndServe(); err != nil {
common.Error.Fatal("Unable to create http listener: ", err) 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 { func allocURL(port int, containerID string) string {
@@ -61,11 +66,7 @@ func TestHttp(t *testing.T) {
) )
alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1) 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
alloc.claimRingForTesting() alloc.claimRingForTesting()
// Ask the http server for a new address // Ask the http server for a new address
@@ -98,9 +99,7 @@ func TestBadHttp(t *testing.T) {
alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1) alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1)
defer alloc.Stop() defer alloc.Stop()
port := rand.Intn(10000) + 32768 port := listenHTTP(alloc)
fmt.Println("BadHttp test on port", port)
go listenHTTP(port, alloc)
alloc.claimRingForTesting() alloc.claimRingForTesting()
cidr1 := HTTPPost(t, allocURL(port, containerID)) cidr1 := HTTPPost(t, allocURL(port, containerID))
@@ -136,11 +135,7 @@ func impTestHTTPCancel(t *testing.T) {
alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1) alloc := makeAllocatorWithMockGossip(t, "08:00:27:01:c3:9a", testCIDR1, 1)
defer alloc.Stop() defer alloc.Stop()
alloc.claimRingForTesting() 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
// Stop the alloc so nothing actually works // Stop the alloc so nothing actually works
unpause := alloc.pause() unpause := alloc.pause()
+3 -4
View File
@@ -17,7 +17,7 @@ func httpErrorAndLog(level *log.Logger, w http.ResponseWriter, msg string,
level.Printf("[http] "+logmsg, logargs...) 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() muxRouter := mux.NewRouter()
@@ -90,8 +90,7 @@ func ListenHTTP(version string, server *DNSServer, domain string, db Zone, port
http.Handle("/", muxRouter) http.Handle("/", muxRouter)
address := fmt.Sprintf(":%d", port) if err := http.Serve(listener, nil); err != nil {
if err := http.ListenAndServe(address, nil); err != nil { Error.Fatal("[http] Unable to serve http: ", err)
Error.Fatal("[http] Unable to create http listener: ", err)
} }
} }
+6 -6
View File
@@ -2,13 +2,11 @@ package nameserver
import ( import (
"fmt" "fmt"
"math/rand"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"testing" "testing"
"time"
wt "github.com/weaveworks/weave/testing" wt "github.com/weaveworks/weave/testing"
) )
@@ -35,11 +33,13 @@ func TestHttp(t *testing.T) {
wt.AssertNoErr(t, err) wt.AssertNoErr(t, err)
defer zone.Stop() defer zone.Stop()
port := rand.Intn(10000) + 32768 httpListener, err := net.Listen("tcp", ":0")
fmt.Println("Http test on port", port) if err != nil {
go ListenHTTP("", nil, testDomain, zone, port) t.Fatal("Unable to create http listener: ", err)
}
time.Sleep(100 * time.Millisecond) // Allow for http server to get going port := httpListener.Addr().(*net.TCPAddr).Port
go ServeHTTP(httpListener, "", nil, testDomain, zone)
// Ask the http server to add our test address into the database // Ask the http server to add our test address into the database
addrParts := strings.Split(testAddr1, "/") addrParts := strings.Split(testAddr1, "/")
+6 -1
View File
@@ -127,8 +127,13 @@ func main() {
Error.Fatal("[main] Failed to initialize the WeaveDNS server", err) 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 SignalHandlerLoop(srv)
go weavedns.ListenHTTP(version, srv, domain, zone, httpPort) go weavedns.ServeHTTP(httpListener, version, srv, domain, zone)
err = srv.Start() err = srv.Start()
if err != nil { if err != nil {