TestGetEtcdVersion_NotListening is looking for a closed port first

This commit is contained in:
Anastasis Andronidis 2015-05-28 14:39:32 +02:00
parent ce7a3ea34d
commit bb9fb19cc2

View File

@ -19,12 +19,16 @@ package tools
import ( import (
"errors" "errors"
"fmt" "fmt"
"math/rand"
"net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"path" "path"
"reflect" "reflect"
"strconv"
"sync" "sync"
"testing" "testing"
"time"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api" "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
@ -711,7 +715,24 @@ func TestGetEtcdVersion_ErrorStatus(t *testing.T) {
} }
func TestGetEtcdVersion_NotListening(t *testing.T) { func TestGetEtcdVersion_NotListening(t *testing.T) {
_, err := GetEtcdVersion("http://127.0.0.1:4001") portIsOpen := func(port int) bool {
conn, err := net.DialTimeout("tcp", "127.0.0.1:"+strconv.Itoa(port), 1*time.Second)
if err == nil {
conn.Close()
return true
}
return false
}
port := rand.Intn((1 << 16) - 1)
for tried := 0; portIsOpen(port); tried++ {
if tried >= 10 {
t.Fatal("Couldn't find a closed TCP port to continue testing")
}
port++
}
_, err := GetEtcdVersion("http://127.0.0.1:" + strconv.Itoa(port))
assert.NotNil(t, err) assert.NotNil(t, err)
} }