Browse Source

Merge pull request #591 from docker/flaky-aci

Attempt to fix flaky ACI tests
Chris Crone 5 years ago
parent
commit
7545485f78
2 changed files with 22 additions and 2 deletions
  1. 2 2
      tests/aci-e2e/e2e-aci_test.go
  2. 20 0
      tests/framework/e2e.go

+ 2 - 2
tests/aci-e2e/e2e-aci_test.go

@@ -198,7 +198,7 @@ func TestContainerRunVolume(t *testing.T) {
 	})
 
 	t.Run("http get", func(t *testing.T) {
-		r, err := http.Get(endpoint)
+		r, err := HTTPGetWithRetry(endpoint, 3)
 		assert.NilError(t, err)
 		assert.Equal(t, r.StatusCode, http.StatusOK)
 		b, err := ioutil.ReadAll(r.Body)
@@ -434,7 +434,7 @@ func TestComposeUpUpdate(t *testing.T) {
 		assert.Assert(t, is.Len(containerInspect.Ports, 1))
 		endpoint := fmt.Sprintf("http://%s:%d", containerInspect.Ports[0].HostIP, containerInspect.Ports[0].HostPort)
 
-		r, err := http.Get(endpoint + "/words/noun")
+		r, err := HTTPGetWithRetry(endpoint+"/words/noun", 3)
 		assert.NilError(t, err)
 		assert.Equal(t, r.StatusCode, http.StatusOK)
 		b, err := ioutil.ReadAll(r.Body)

+ 20 - 0
tests/framework/e2e.go

@@ -22,12 +22,14 @@ import (
 	"errors"
 	"fmt"
 	"io/ioutil"
+	"net/http"
 	"os"
 	"os/exec"
 	"path/filepath"
 	"runtime"
 	"strings"
 	"testing"
+	"time"
 
 	"gotest.tools/v3/assert"
 	is "gotest.tools/v3/assert/cmp"
@@ -192,3 +194,21 @@ func ParseContainerInspect(stdout string) (*containers.Container, error) {
 	}
 	return &res, nil
 }
+
+// HTTPGetWithRetry performs an HTTP GET on an `endpoint`.
+// In the case of an error it retries the same request after a 5 second sleep,
+// returning the error if count of `tries` is reached
+func HTTPGetWithRetry(endpoint string, tries int) (*http.Response, error) {
+	var (
+		r   *http.Response
+		err error
+	)
+	for t := 0; t < tries; t++ {
+		r, err = http.Get(endpoint)
+		if err == nil || t == tries-1 {
+			break
+		}
+		time.Sleep(5 * time.Second)
+	}
+	return r, err
+}