Browse code

integration: add wait

Cherry-picked several WIP commits from
https://github.com/moby/moby/commits/b0a592798f4d9d7162f8aedca89ada3a29d60e2c/

Originally-authored-by: Rodrigo Campos <rodrigoca@microsoft.com>
Co-Authored-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Akihiro Suda authored on 2024/10/22 00:16:26
Showing 8 changed files
... ...
@@ -17,6 +17,7 @@ import (
17 17
 	"github.com/docker/docker/testutil/fakecontext"
18 18
 	"gotest.tools/v3/assert"
19 19
 	is "gotest.tools/v3/assert/cmp"
20
+	"gotest.tools/v3/poll"
20 21
 	"gotest.tools/v3/skip"
21 22
 )
22 23
 
... ...
@@ -86,6 +87,8 @@ func TestBuildSquashParent(t *testing.T) {
86 86
 		container.WithImage(name),
87 87
 		container.WithCmd("/bin/sh", "-c", "cat /hello"),
88 88
 	)
89
+
90
+	poll.WaitOn(t, container.IsStopped(ctx, client, cid))
89 91
 	reader, err := client.ContainerLogs(ctx, cid, containertypes.LogsOptions{
90 92
 		ShowStdout: true,
91 93
 	})
... ...
@@ -19,6 +19,7 @@ import (
19 19
 	"github.com/docker/docker/testutil/fakecontext"
20 20
 	"github.com/docker/docker/testutil/fixtures/load"
21 21
 	"gotest.tools/v3/assert"
22
+	"gotest.tools/v3/poll"
22 23
 	"gotest.tools/v3/skip"
23 24
 )
24 25
 
... ...
@@ -118,6 +119,8 @@ func TestBuildUserNamespaceValidateCapabilitiesAreV2(t *testing.T) {
118 118
 		container.WithImage(imageTag),
119 119
 		container.WithCmd("/sbin/getcap", "-n", "/bin/sleep"),
120 120
 	)
121
+
122
+	poll.WaitOn(t, container.IsStopped(ctx, clientNoUserRemap, cid))
121 123
 	logReader, err := clientNoUserRemap.ContainerLogs(ctx, cid, containertypes.LogsOptions{
122 124
 		ShowStdout: true,
123 125
 	})
... ...
@@ -15,6 +15,7 @@ import (
15 15
 	"github.com/docker/docker/testutil/daemon"
16 16
 	"gotest.tools/v3/assert"
17 17
 	is "gotest.tools/v3/assert/cmp"
18
+	"gotest.tools/v3/poll"
18 19
 	"gotest.tools/v3/skip"
19 20
 )
20 21
 
... ...
@@ -53,6 +54,7 @@ func TestCreateWithCDIDevices(t *testing.T) {
53 53
 	}
54 54
 	assert.Check(t, is.DeepEqual(inspect.HostConfig.DeviceRequests, expectedRequests))
55 55
 
56
+	poll.WaitOn(t, container.IsStopped(ctx, apiClient, id))
56 57
 	reader, err := apiClient.ContainerLogs(ctx, id, containertypes.LogsOptions{
57 58
 		ShowStdout: true,
58 59
 	})
... ...
@@ -23,6 +23,7 @@ func TestDiff(t *testing.T) {
23 23
 		{Kind: containertypes.ChangeAdd, Path: "/foo/bar"},
24 24
 	}
25 25
 
26
+	poll.WaitOn(t, container.IsStopped(ctx, apiClient, cID))
26 27
 	items, err := apiClient.ContainerDiff(ctx, cID)
27 28
 	assert.NilError(t, err)
28 29
 	assert.DeepEqual(t, expected, items)
... ...
@@ -6,6 +6,7 @@ import (
6 6
 	"fmt"
7 7
 	"io"
8 8
 	"net"
9
+	"strconv"
9 10
 	"strings"
10 11
 	"testing"
11 12
 	"time"
... ...
@@ -25,13 +26,25 @@ func TestNetworkNat(t *testing.T) {
25 25
 
26 26
 	ctx := setupTest(t)
27 27
 
28
-	msg := "it works"
29
-	startServerContainer(ctx, t, msg, 8080)
28
+	const msg = "it works"
29
+	const port = 8080
30
+	startServerContainer(ctx, t, msg, port)
30 31
 
31 32
 	endpoint := getExternalAddress(t)
32
-	conn, err := net.Dial("tcp", net.JoinHostPort(endpoint.String(), "8080"))
33
-	assert.NilError(t, err)
34
-	defer conn.Close()
33
+
34
+	var conn net.Conn
35
+	addr := net.JoinHostPort(endpoint.String(), strconv.Itoa(port))
36
+	poll.WaitOn(t, func(t poll.LogT) poll.Result {
37
+		var err error
38
+		conn, err = net.Dial("tcp", addr)
39
+		if err != nil {
40
+			return poll.Continue("waiting for %s to be accessible: %v", addr, err)
41
+		}
42
+		return poll.Success()
43
+	})
44
+	defer func() {
45
+		assert.Check(t, conn.Close())
46
+	}()
35 47
 
36 48
 	data, err := io.ReadAll(conn)
37 49
 	assert.NilError(t, err)
... ...
@@ -40,16 +53,26 @@ func TestNetworkNat(t *testing.T) {
40 40
 
41 41
 func TestNetworkLocalhostTCPNat(t *testing.T) {
42 42
 	skip.If(t, testEnv.IsRemoteDaemon)
43
-	skip.If(t, testEnv.GitHubActions, "FIXME: https://github.com/moby/moby/issues/41561")
44 43
 
45 44
 	ctx := setupTest(t)
46 45
 
47
-	msg := "hi yall"
48
-	startServerContainer(ctx, t, msg, 8081)
49
-
50
-	conn, err := net.Dial("tcp", "localhost:8081")
51
-	assert.NilError(t, err)
52
-	defer conn.Close()
46
+	const msg = "hi yall"
47
+	const port = 8081
48
+	startServerContainer(ctx, t, msg, port)
49
+
50
+	var conn net.Conn
51
+	addr := net.JoinHostPort("localhost", strconv.Itoa(port))
52
+	poll.WaitOn(t, func(t poll.LogT) poll.Result {
53
+		var err error
54
+		conn, err = net.Dial("tcp", addr)
55
+		if err != nil {
56
+			return poll.Continue("waiting for %s to be accessible: %v", addr, err)
57
+		}
58
+		return poll.Success()
59
+	})
60
+	defer func() {
61
+		assert.Check(t, conn.Close())
62
+	}()
53 63
 
54 64
 	data, err := io.ReadAll(conn)
55 65
 	assert.NilError(t, err)
... ...
@@ -10,6 +10,7 @@ import (
10 10
 	"github.com/docker/docker/testutil/daemon"
11 11
 	"gotest.tools/v3/assert"
12 12
 	is "gotest.tools/v3/assert/cmp"
13
+	"gotest.tools/v3/poll"
13 14
 	"gotest.tools/v3/skip"
14 15
 )
15 16
 
... ...
@@ -61,7 +62,8 @@ func TestUsernsCommit(t *testing.T) {
61 61
 	clientUserRemap := dUserRemap.NewClientT(t)
62 62
 	defer clientUserRemap.Close()
63 63
 
64
-	container.Run(ctx, t, clientUserRemap, container.WithName(t.Name()), container.WithImage("busybox"), container.WithCmd("sh", "-c", "echo hello world > /hello.txt && chown 1000:1000 /hello.txt"))
64
+	cID := container.Run(ctx, t, clientUserRemap, container.WithName(t.Name()), container.WithImage("busybox"), container.WithCmd("sh", "-c", "echo hello world > /hello.txt && chown 1000:1000 /hello.txt"))
65
+	poll.WaitOn(t, container.IsStopped(ctx, clientUserRemap, cID))
65 66
 	img, err := clientUserRemap.ContainerCommit(ctx, t.Name(), containertypes.CommitOptions{})
66 67
 	assert.NilError(t, err)
67 68
 
... ...
@@ -25,6 +25,7 @@ import (
25 25
 	"gotest.tools/v3/assert"
26 26
 	is "gotest.tools/v3/assert/cmp"
27 27
 	"gotest.tools/v3/icmd"
28
+	"gotest.tools/v3/poll"
28 29
 	"gotest.tools/v3/skip"
29 30
 )
30 31
 
... ...
@@ -254,8 +255,16 @@ func TestProxy4To6(t *testing.T) {
254 254
 	inspect := container.Inspect(ctx, t, c, serverId)
255 255
 	hostPort := inspect.NetworkSettings.Ports["80/tcp"][0].HostPort
256 256
 
257
-	resp, err := http.Get("http://[::1]:" + hostPort)
258
-	assert.NilError(t, err)
257
+	var resp *http.Response
258
+	addr := "http://[::1]:" + hostPort
259
+	poll.WaitOn(t, func(t poll.LogT) poll.Result {
260
+		var err error
261
+		resp, err = http.Get(addr) // #nosec G107 -- Ignore "Potential HTTP request made with variable url"
262
+		if err != nil {
263
+			return poll.Continue("waiting for %s to be accessible: %v", addr, err)
264
+		}
265
+		return poll.Success()
266
+	})
259 267
 	assert.Check(t, is.Equal(resp.StatusCode, 404))
260 268
 }
261 269
 
... ...
@@ -9,10 +9,12 @@ import (
9 9
 
10 10
 	"github.com/docker/docker/api/types"
11 11
 	"github.com/docker/docker/api/types/container"
12
+	testContainer "github.com/docker/docker/integration/internal/container"
12 13
 	"github.com/docker/docker/pkg/stdcopy"
13 14
 	"github.com/docker/docker/testutil"
14 15
 	"github.com/docker/docker/testutil/daemon"
15 16
 	"gotest.tools/v3/assert"
17
+	"gotest.tools/v3/poll"
16 18
 )
17 19
 
18 20
 // TestReadPluginNoRead tests that reads are supported even if the plugin isn't capable.
... ...
@@ -65,6 +67,7 @@ func TestReadPluginNoRead(t *testing.T) {
65 65
 			err = client.ContainerStart(ctx, c.ID, container.StartOptions{})
66 66
 			assert.Assert(t, err)
67 67
 
68
+			poll.WaitOn(t, testContainer.IsStopped(ctx, client, c.ID))
68 69
 			logs, err := client.ContainerLogs(ctx, c.ID, container.LogsOptions{ShowStdout: true})
69 70
 			if !test.logsSupported {
70 71
 				assert.Assert(t, err != nil)