From 9f2b12c624a4a07c6662d1a5f1bced28b6eb86da Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Mon, 13 May 2019 00:49:32 -0700 Subject: [PATCH] gvisor/runsc/tests: set timeout for http.Get() WaitForHTTP tries GET requests on a port until the call succeeds or timeout. But we want to be sure that one of our attempts will not stuck for the whole timeout. All timeouts are increased to 30 seconds, because test cases with smaller timeouts fail sometimes even for the native container runtime (runc). PiperOrigin-RevId: 247888467 Change-Id: I03cfd3275286bc686a78fd26da43231d20667851 --- kokoro/run_tests.sh | 1 + runsc/test/image/image_test.go | 41 +++++++++++++++------- runsc/test/integration/integration_test.go | 4 +-- runsc/test/testutil/testutil.go | 9 ++++- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/kokoro/run_tests.sh b/kokoro/run_tests.sh index fbe353a1e..aa88b5cbf 100755 --- a/kokoro/run_tests.sh +++ b/kokoro/run_tests.sh @@ -150,6 +150,7 @@ run_docker_tests() { bazel test \ "${BAZEL_BUILD_FLAGS[@]}" \ --test_env=RUNSC_RUNTIME="${RUNTIME}${v}" \ + --test_output=all \ //runsc/test/image:image_test \ //runsc/test/integration:integration_test done diff --git a/runsc/test/image/image_test.go b/runsc/test/image/image_test.go index 8322dd001..b969731b0 100644 --- a/runsc/test/image/image_test.go +++ b/runsc/test/image/image_test.go @@ -24,6 +24,7 @@ package image import ( "fmt" "io/ioutil" + "log" "net/http" "os" "path/filepath" @@ -46,7 +47,7 @@ func TestHelloWorld(t *testing.T) { } } -func testHTTPServer(port int) error { +func runHTTPRequest(port int) error { url := fmt.Sprintf("http://localhost:%d/not-found", port) resp, err := http.Get(url) if err != nil { @@ -78,6 +79,26 @@ func testHTTPServer(port int) error { return nil } +func testHTTPServer(t *testing.T, port int) { + const requests = 10 + ch := make(chan error, requests) + for i := 0; i < requests; i++ { + go func() { + start := time.Now() + err := runHTTPRequest(port) + log.Printf("Response time %v: %v", time.Since(start).String(), err) + ch <- err + }() + } + + for i := 0; i < requests; i++ { + err := <-ch + if err != nil { + t.Errorf("testHTTPServer(%d) failed: %v", port, err) + } + } +} + func TestHttpd(t *testing.T) { if err := testutil.Pull("httpd"); err != nil { t.Fatalf("docker pull failed: %v", err) @@ -103,13 +124,11 @@ func TestHttpd(t *testing.T) { } // Wait until it's up and running. - if err := testutil.WaitForHTTP(port, 10*time.Second); err != nil { - t.Fatalf("WaitForHTTP() timeout: %v", err) + if err := testutil.WaitForHTTP(port, 30*time.Second); err != nil { + t.Errorf("WaitForHTTP() timeout: %v", err) } - if err := testHTTPServer(port); err != nil { - t.Fatalf("testHTTPServer(%d) failed: %v", port, err) - } + testHTTPServer(t, port) } func TestNginx(t *testing.T) { @@ -137,13 +156,11 @@ func TestNginx(t *testing.T) { } // Wait until it's up and running. - if err := testutil.WaitForHTTP(port, 10*time.Second); err != nil { - t.Fatalf("WaitForHTTP() timeout: %v", err) + if err := testutil.WaitForHTTP(port, 30*time.Second); err != nil { + t.Errorf("WaitForHTTP() timeout: %v", err) } - if err := testHTTPServer(port); err != nil { - t.Fatalf("testHTTPServer(%d) failed: %v", port, err) - } + testHTTPServer(t, port) } func TestMysql(t *testing.T) { @@ -240,7 +257,7 @@ func TestTomcat(t *testing.T) { } // Wait until it's up and running. - if err := testutil.WaitForHTTP(port, 10*time.Second); err != nil { + if err := testutil.WaitForHTTP(port, 30*time.Second); err != nil { t.Fatalf("WaitForHTTP() timeout: %v", err) } diff --git a/runsc/test/integration/integration_test.go b/runsc/test/integration/integration_test.go index de17dd3c2..c51cab3ae 100644 --- a/runsc/test/integration/integration_test.go +++ b/runsc/test/integration/integration_test.go @@ -68,7 +68,7 @@ func TestLifeCycle(t *testing.T) { if err != nil { t.Fatal("docker.FindPort(80) failed: ", err) } - if err := testutil.WaitForHTTP(port, 10*time.Second); err != nil { + if err := testutil.WaitForHTTP(port, 30*time.Second); err != nil { t.Fatal("WaitForHTTP() timeout:", err) } client := http.Client{Timeout: time.Duration(2 * time.Second)} @@ -138,7 +138,7 @@ func TestPauseResume(t *testing.T) { } // Wait until it's up and running. - if err := testutil.WaitForHTTP(port, 20*time.Second); err != nil { + if err := testutil.WaitForHTTP(port, 30*time.Second); err != nil { t.Fatal("WaitForHTTP() timeout:", err) } diff --git a/runsc/test/testutil/testutil.go b/runsc/test/testutil/testutil.go index 6a4c045a8..9efb1ba8e 100644 --- a/runsc/test/testutil/testutil.go +++ b/runsc/test/testutil/testutil.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "math/rand" "net/http" "os" @@ -266,8 +267,14 @@ func Poll(cb func() error, timeout time.Duration) error { // WaitForHTTP tries GET requests on a port until the call succeeds or timeout. func WaitForHTTP(port int, timeout time.Duration) error { cb := func() error { - resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", port)) + c := &http.Client{ + // Calculate timeout to be able to do minimum 5 attempts. + Timeout: timeout / 5, + } + url := fmt.Sprintf("http://localhost:%d/", port) + resp, err := c.Get(url) if err != nil { + log.Printf("Waiting %s: %v", url, err) return err } resp.Body.Close()