auto-assign port in ipam tests

This commit is contained in:
Matthias Radestock committed 2015-05-29 12:35:23 +01:00
1 parent c93e789b6b
commit fb721558af
1 file changed
+16 -14
+16 -14
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,9 +66,8 @@ 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) fmt.Println("Http test on port", port)
go listenHTTP(port, alloc)
time.Sleep(100 * time.Millisecond) // Allow for http server to get going 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) 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) fmt.Println("Http 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,9 +139,8 @@ 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) fmt.Println("Http test on port", port)
go listenHTTP(port, alloc)
time.Sleep(100 * time.Millisecond) // Allow for http server to get going time.Sleep(100 * time.Millisecond) // Allow for http server to get going