cleanup some of the e2e service reachability code.

pull/6/head
Mike Danese 2015-06-10 17:11:02 -07:00
parent d2b17a5c49
commit 145f59f12f
1 changed files with 39 additions and 41 deletions

View File

@ -33,6 +33,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields" "github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels" "github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/wait"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -1010,66 +1011,63 @@ func testLoadBalancerNotReachable(ingress api.LoadBalancerIngress, port int) {
} }
func testReachable(ip string, port int) { func testReachable(ip string, port int) {
var err error
var resp *http.Response
url := fmt.Sprintf("http://%s:%d", ip, port) url := fmt.Sprintf("http://%s:%d", ip, port)
if ip == "" { if ip == "" {
Failf("got empty IP for reachability check", url) Failf("Got empty IP for reachability check (%s)", url)
} }
if port == 0 { if port == 0 {
Failf("got port==0 for reachability check", url) Failf("Got port==0 for reachability check (%s)", url)
} }
By(fmt.Sprintf("Checking reachability of %s", url)) By(fmt.Sprintf("Checking reachability of %s", url))
for t := time.Now(); time.Since(t) < podStartTimeout; time.Sleep(5 * time.Second) { expectNoError(wait.Poll(poll, podStartTimeout, func() (bool, error) {
resp, err = httpGetNoConnectionPool(url) resp, err := httpGetNoConnectionPool(url)
if err == nil { if err != nil {
break Logf("Got error waiting for reachability of %s: %v", url, err)
return false, nil
} }
By(fmt.Sprintf("Got error waiting for reachability of %s: %v", url, err)) defer resp.Body.Close()
} body, err := ioutil.ReadAll(resp.Body)
Expect(err).NotTo(HaveOccurred()) if err != nil {
defer resp.Body.Close() Logf("Got error reading response from %s: %v", url, err)
return false, nil
body, err := ioutil.ReadAll(resp.Body) }
Expect(err).NotTo(HaveOccurred()) if resp.StatusCode != 200 {
if resp.StatusCode != 200 { return false, fmt.Errorf("received non-success return status %q trying to access %s; got body: %s", resp.Status, url, string(body))
Failf("received non-success return status %q trying to access %s; got body: %s", resp.Status, url, string(body)) }
} if !strings.Contains(string(body), "test-webserver") {
if !strings.Contains(string(body), "test-webserver") { return false, fmt.Errorf("received response body without expected substring 'test-webserver': %s", string(body))
Failf("received response body without expected substring 'test-webserver': %s", string(body)) }
} Logf("Successfully reached %v", url)
return true, nil
}))
} }
func testNotReachable(ip string, port int) { func testNotReachable(ip string, port int) {
var err error
var resp *http.Response
var body []byte
url := fmt.Sprintf("http://%s:%d", ip, port) url := fmt.Sprintf("http://%s:%d", ip, port)
if ip == "" { if ip == "" {
Failf("got empty IP for non-reachability check", url) Failf("Got empty IP for non-reachability check (%s)", url)
} }
if port == 0 { if port == 0 {
Failf("got port==0 for non-reachability check", url) Failf("Got port==0 for non-reachability check (%s)", url)
} }
for t := time.Now(); time.Since(t) < podStartTimeout; time.Sleep(5 * time.Second) { By(fmt.Sprintf("Checking that %s is not reachable", url))
resp, err = httpGetNoConnectionPool(url) expectNoError(wait.Poll(poll, podStartTimeout, func() (bool, error) {
resp, err := httpGetNoConnectionPool(url)
if err != nil { if err != nil {
break Logf("Successfully waited for the url %s to be unreachable.", url)
return true, nil
} }
body, err = ioutil.ReadAll(resp.Body) defer resp.Body.Close()
Expect(err).NotTo(HaveOccurred()) body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close() if err != nil {
By(fmt.Sprintf("Got success waiting for non-reachability of %s: %v", url, resp.Status)) Logf("Expecting %s to be unreachable but was reachable and got an error reading response: %v", url, err)
} return false, nil
if err == nil { }
Failf("able to reach service %s when should no longer have been reachable: %q body=%s", url, resp.Status, string(body)) Logf("Able to reach service %s when should no longer have been reachable, status:%d and body: %s", url, resp.Status, string(body))
} return false, nil
// TODO: Check type of error }))
By(fmt.Sprintf("Found (expected) error during not-reachability test %v", err))
} }
// Does an HTTP GET, but does not reuse TCP connections // Does an HTTP GET, but does not reuse TCP connections