瀏覽代碼

Merge pull request #45910 from corhere/backport-23.0/improve-test-flakiness

[23.0 backport] make tests less flaky
Sebastiaan van Stijn 2 年之前
父節點
當前提交
80cb2ed333

+ 3 - 3
integration/container/daemon_linux_test.go

@@ -141,7 +141,7 @@ func TestDaemonHostGatewayIP(t *testing.T) {
 	// Verify the IP in /etc/hosts is same as host-gateway-ip
 	// Verify the IP in /etc/hosts is same as host-gateway-ip
 	d := daemon.New(t)
 	d := daemon.New(t)
 	// Verify the IP in /etc/hosts is same as the default bridge's IP
 	// Verify the IP in /etc/hosts is same as the default bridge's IP
-	d.StartWithBusybox(t)
+	d.StartWithBusybox(t, "--iptables=false")
 	c := d.NewClientT(t)
 	c := d.NewClientT(t)
 	ctx := context.Background()
 	ctx := context.Background()
 	cID := container.Run(ctx, t, c,
 	cID := container.Run(ctx, t, c,
@@ -158,7 +158,7 @@ func TestDaemonHostGatewayIP(t *testing.T) {
 	d.Stop(t)
 	d.Stop(t)
 
 
 	// Verify the IP in /etc/hosts is same as host-gateway-ip
 	// Verify the IP in /etc/hosts is same as host-gateway-ip
-	d.StartWithBusybox(t, "--host-gateway-ip=6.7.8.9")
+	d.StartWithBusybox(t, "--iptables=false", "--host-gateway-ip=6.7.8.9")
 	cID = container.Run(ctx, t, c,
 	cID = container.Run(ctx, t, c,
 		container.WithExtraHost("host.docker.internal:host-gateway"),
 		container.WithExtraHost("host.docker.internal:host-gateway"),
 	)
 	)
@@ -220,7 +220,7 @@ func TestRestartDaemonWithRestartingContainer(t *testing.T) {
 	assert.NilError(t, err)
 	assert.NilError(t, err)
 	assert.NilError(t, os.WriteFile(configPath, configBytes, 0600))
 	assert.NilError(t, os.WriteFile(configPath, configBytes, 0600))
 
 
-	d.Start(t)
+	d.Start(t, "--iptables=false")
 
 
 	ctxTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
 	ctxTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
 	defer cancel()
 	defer cancel()

+ 1 - 1
integration/container/daemon_test.go

@@ -43,7 +43,7 @@ func TestContainerKillOnDaemonStart(t *testing.T) {
 	assert.Assert(t, inspect.State.Running)
 	assert.Assert(t, inspect.State.Running)
 
 
 	assert.NilError(t, d.Kill())
 	assert.NilError(t, d.Kill())
-	d.Start(t)
+	d.Start(t, "--iptables=false")
 
 
 	inspect, err = client.ContainerInspect(ctx, id)
 	inspect, err = client.ContainerInspect(ctx, id)
 	assert.Check(t, is.Nil(err))
 	assert.Check(t, is.Nil(err))

+ 1 - 1
integration/image/import_test.go

@@ -27,7 +27,7 @@ func TestImportExtremelyLargeImageWorks(t *testing.T) {
 
 
 	// Spin up a new daemon, so that we can run this test in parallel (it's a slow test)
 	// Spin up a new daemon, so that we can run this test in parallel (it's a slow test)
 	d := daemon.New(t)
 	d := daemon.New(t)
-	d.Start(t)
+	d.Start(t, "--iptables=false")
 	defer d.Stop(t)
 	defer d.Stop(t)
 
 
 	client := d.NewClientT(t)
 	client := d.NewClientT(t)

+ 27 - 10
pkg/plugins/client_test.go

@@ -3,6 +3,7 @@ package plugins // import "github.com/docker/docker/pkg/plugins"
 import (
 import (
 	"bytes"
 	"bytes"
 	"encoding/json"
 	"encoding/json"
+	"errors"
 	"io"
 	"io"
 	"net/http"
 	"net/http"
 	"net/http/httptest"
 	"net/http/httptest"
@@ -13,7 +14,6 @@ import (
 
 
 	"github.com/docker/docker/pkg/plugins/transport"
 	"github.com/docker/docker/pkg/plugins/transport"
 	"github.com/docker/go-connections/tlsconfig"
 	"github.com/docker/go-connections/tlsconfig"
-	"github.com/pkg/errors"
 	"gotest.tools/v3/assert"
 	"gotest.tools/v3/assert"
 	is "gotest.tools/v3/assert/cmp"
 	is "gotest.tools/v3/assert/cmp"
 )
 )
@@ -240,22 +240,39 @@ func TestClientWithRequestTimeout(t *testing.T) {
 		Timeout() bool
 		Timeout() bool
 	}
 	}
 
 
-	timeout := 1 * time.Millisecond
+	unblock := make(chan struct{})
 	testHandler := func(w http.ResponseWriter, r *http.Request) {
 	testHandler := func(w http.ResponseWriter, r *http.Request) {
-		time.Sleep(timeout + 10*time.Millisecond)
+		select {
+		case <-unblock:
+		case <-r.Context().Done():
+		}
 		w.WriteHeader(http.StatusOK)
 		w.WriteHeader(http.StatusOK)
 	}
 	}
 
 
 	srv := httptest.NewServer(http.HandlerFunc(testHandler))
 	srv := httptest.NewServer(http.HandlerFunc(testHandler))
-	defer srv.Close()
+	defer func() {
+		close(unblock)
+		srv.Close()
+	}()
 
 
 	client := &Client{http: srv.Client(), requestFactory: &testRequestWrapper{srv}}
 	client := &Client{http: srv.Client(), requestFactory: &testRequestWrapper{srv}}
-	_, err := client.callWithRetry("/Plugin.Hello", nil, false, WithRequestTimeout(timeout))
-	assert.Assert(t, is.ErrorContains(err, ""), "expected error")
-
-	var tErr timeoutError
-	assert.Assert(t, errors.As(err, &tErr))
-	assert.Assert(t, tErr.Timeout())
+	errCh := make(chan error, 1)
+	go func() {
+		_, err := client.callWithRetry("/Plugin.Hello", nil, false, WithRequestTimeout(time.Millisecond))
+		errCh <- err
+	}()
+
+	timer := time.NewTimer(5 * time.Second)
+	defer timer.Stop()
+	select {
+	case err := <-errCh:
+		var tErr timeoutError
+		if assert.Check(t, errors.As(err, &tErr), "want timeout error, got %T", err) {
+			assert.Check(t, tErr.Timeout())
+		}
+	case <-timer.C:
+		t.Fatal("client request did not time out in time")
+	}
 }
 }
 
 
 type testRequestWrapper struct {
 type testRequestWrapper struct {