Browse code

client/container: use gotest.tools-style asserts

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2025/05/17 02:14:30
Showing 22 changed files
... ...
@@ -98,10 +98,6 @@ func TestContainerCommit(t *testing.T) {
98 98
 		Changes:   expectedChanges,
99 99
 		Pause:     false,
100 100
 	})
101
-	if err != nil {
102
-		t.Fatal(err)
103
-	}
104
-	if r.ID != "new_container_id" {
105
-		t.Fatalf("expected `new_container_id`, got %s", r.ID)
106
-	}
101
+	assert.NilError(t, err)
102
+	assert.Check(t, is.Equal(r.ID, "new_container_id"))
107 103
 }
... ...
@@ -8,6 +8,7 @@ import (
8 8
 	"fmt"
9 9
 	"io"
10 10
 	"net/http"
11
+	"os"
11 12
 	"strings"
12 13
 	"testing"
13 14
 
... ...
@@ -51,9 +52,7 @@ func TestContainerStatPathNoHeaderError(t *testing.T) {
51 51
 		}),
52 52
 	}
53 53
 	_, err := client.ContainerStatPath(context.Background(), "container_id", "path/to/file")
54
-	if err == nil {
55
-		t.Fatalf("expected an error, got nothing")
56
-	}
54
+	assert.Check(t, err != nil, "expected an error, got nothing")
57 55
 }
58 56
 
59 57
 func TestContainerStatPath(t *testing.T) {
... ...
@@ -90,15 +89,9 @@ func TestContainerStatPath(t *testing.T) {
90 90
 		}),
91 91
 	}
92 92
 	stat, err := client.ContainerStatPath(context.Background(), "container_id", expectedPath)
93
-	if err != nil {
94
-		t.Fatal(err)
95
-	}
96
-	if stat.Name != "name" {
97
-		t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
98
-	}
99
-	if stat.Mode != 0o700 {
100
-		t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
101
-	}
93
+	assert.NilError(t, err)
94
+	assert.Check(t, is.Equal(stat.Name, "name"))
95
+	assert.Check(t, is.Equal(stat.Mode, os.FileMode(0o700)))
102 96
 }
103 97
 
104 98
 func TestCopyToContainerError(t *testing.T) {
... ...
@@ -132,9 +125,7 @@ func TestCopyToContainerEmptyResponse(t *testing.T) {
132 132
 		client: newMockClient(errorMock(http.StatusNoContent, "No content")),
133 133
 	}
134 134
 	err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), container.CopyToContainerOptions{})
135
-	if err != nil {
136
-		t.Fatalf("unexpected error: %v", err)
137
-	}
135
+	assert.NilError(t, err)
138 136
 }
139 137
 
140 138
 func TestCopyToContainer(t *testing.T) {
... ...
@@ -178,9 +169,7 @@ func TestCopyToContainer(t *testing.T) {
178 178
 	err := client.CopyToContainer(context.Background(), "container_id", expectedPath, bytes.NewReader([]byte("content")), container.CopyToContainerOptions{
179 179
 		AllowOverwriteDirWithFile: false,
180 180
 	})
181
-	if err != nil {
182
-		t.Fatal(err)
183
-	}
181
+	assert.NilError(t, err)
184 182
 }
185 183
 
186 184
 func TestCopyFromContainerError(t *testing.T) {
... ...
@@ -229,9 +218,7 @@ func TestCopyFromContainerEmptyResponse(t *testing.T) {
229 229
 		}),
230 230
 	}
231 231
 	_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
232
-	if err != nil {
233
-		t.Fatalf("unexpected error: %v", err)
234
-	}
232
+	assert.NilError(t, err)
235 233
 }
236 234
 
237 235
 func TestCopyFromContainerNoHeaderError(t *testing.T) {
... ...
@@ -244,9 +231,7 @@ func TestCopyFromContainerNoHeaderError(t *testing.T) {
244 244
 		}),
245 245
 	}
246 246
 	_, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file")
247
-	if err == nil {
248
-		t.Fatalf("expected an error, got nothing")
249
-	}
247
+	assert.Check(t, err != nil, "expected an error, got nothing")
250 248
 }
251 249
 
252 250
 func TestCopyFromContainer(t *testing.T) {
... ...
@@ -285,23 +270,12 @@ func TestCopyFromContainer(t *testing.T) {
285 285
 		}),
286 286
 	}
287 287
 	r, stat, err := client.CopyFromContainer(context.Background(), "container_id", expectedPath)
288
-	if err != nil {
289
-		t.Fatal(err)
290
-	}
291
-	if stat.Name != "name" {
292
-		t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name)
293
-	}
294
-	if stat.Mode != 0o700 {
295
-		t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode)
296
-	}
288
+	assert.NilError(t, err)
289
+	assert.Check(t, is.Equal(stat.Name, "name"))
290
+	assert.Check(t, is.Equal(stat.Mode, os.FileMode(0o700)))
291
+
297 292
 	content, err := io.ReadAll(r)
298
-	if err != nil {
299
-		t.Fatal(err)
300
-	}
301
-	if err := r.Close(); err != nil {
302
-		t.Fatal(err)
303
-	}
304
-	if string(content) != "content" {
305
-		t.Fatalf("expected content to be 'content', got %s", string(content))
306
-	}
293
+	assert.NilError(t, err)
294
+	assert.Check(t, is.Equal(string(content), "content"))
295
+	assert.NilError(t, r.Close())
307 296
 }
... ...
@@ -64,12 +64,8 @@ func TestContainerCreateWithName(t *testing.T) {
64 64
 	}
65 65
 
66 66
 	r, err := client.ContainerCreate(context.Background(), nil, nil, nil, nil, "container_name")
67
-	if err != nil {
68
-		t.Fatal(err)
69
-	}
70
-	if r.ID != "container_id" {
71
-		t.Fatalf("expected `container_id`, got %s", r.ID)
72
-	}
67
+	assert.NilError(t, err)
68
+	assert.Check(t, is.Equal(r.ID, "container_id"))
73 69
 }
74 70
 
75 71
 // TestContainerCreateAutoRemove validates that a client using API 1.24 always disables AutoRemove. When using API 1.25
... ...
@@ -97,20 +93,22 @@ func TestContainerCreateAutoRemove(t *testing.T) {
97 97
 			}, nil
98 98
 		}
99 99
 	}
100
-
101
-	client := &Client{
102
-		client:  newMockClient(autoRemoveValidator(false)),
103
-		version: "1.24",
104
-	}
105
-	if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, ""); err != nil {
106
-		t.Fatal(err)
100
+	testCases := []struct {
101
+		version            string
102
+		expectedAutoRemove bool
103
+	}{
104
+		{version: "1.24", expectedAutoRemove: false},
105
+		{version: "1.25", expectedAutoRemove: true},
107 106
 	}
108
-	client = &Client{
109
-		client:  newMockClient(autoRemoveValidator(true)),
110
-		version: "1.25",
111
-	}
112
-	if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, ""); err != nil {
113
-		t.Fatal(err)
107
+	for _, tc := range testCases {
108
+		t.Run(tc.version, func(t *testing.T) {
109
+			client := &Client{
110
+				client:  newMockClient(autoRemoveValidator(tc.expectedAutoRemove)),
111
+				version: tc.version,
112
+			}
113
+			_, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, "")
114
+			assert.NilError(t, err)
115
+		})
114 116
 	}
115 117
 }
116 118
 
... ...
@@ -67,6 +67,6 @@ func TestContainerDiff(t *testing.T) {
67 67
 	}
68 68
 
69 69
 	changes, err := client.ContainerDiff(context.Background(), "container_id")
70
-	assert.Check(t, err)
70
+	assert.NilError(t, err)
71 71
 	assert.Check(t, is.DeepEqual(changes, expected))
72 72
 }
... ...
@@ -82,12 +82,8 @@ func TestContainerExecCreate(t *testing.T) {
82 82
 	r, err := client.ContainerExecCreate(context.Background(), "container_id", container.ExecOptions{
83 83
 		User: "user",
84 84
 	})
85
-	if err != nil {
86
-		t.Fatal(err)
87
-	}
88
-	if r.ID != "exec_id" {
89
-		t.Fatalf("expected `exec_id`, got %s", r.ID)
90
-	}
85
+	assert.NilError(t, err)
86
+	assert.Check(t, is.Equal(r.ID, "exec_id"))
91 87
 }
92 88
 
93 89
 func TestContainerExecStartError(t *testing.T) {
... ...
@@ -127,9 +123,7 @@ func TestContainerExecStart(t *testing.T) {
127 127
 		Detach: true,
128 128
 		Tty:    false,
129 129
 	})
130
-	if err != nil {
131
-		t.Fatal(err)
132
-	}
130
+	assert.NilError(t, err)
133 131
 }
134 132
 
135 133
 func TestContainerExecInspectError(t *testing.T) {
... ...
@@ -162,13 +156,7 @@ func TestContainerExecInspect(t *testing.T) {
162 162
 	}
163 163
 
164 164
 	inspect, err := client.ContainerExecInspect(context.Background(), "exec_id")
165
-	if err != nil {
166
-		t.Fatal(err)
167
-	}
168
-	if inspect.ExecID != "exec_id" {
169
-		t.Fatalf("expected ExecID to be `exec_id`, got %s", inspect.ExecID)
170
-	}
171
-	if inspect.ContainerID != "container_id" {
172
-		t.Fatalf("expected ContainerID `container_id`, got %s", inspect.ContainerID)
173
-	}
165
+	assert.NilError(t, err)
166
+	assert.Check(t, is.Equal(inspect.ExecID, "exec_id"))
167
+	assert.Check(t, is.Equal(inspect.ContainerID, "container_id"))
174 168
 }
... ...
@@ -45,15 +45,9 @@ func TestContainerExport(t *testing.T) {
45 45
 		}),
46 46
 	}
47 47
 	body, err := client.ContainerExport(context.Background(), "container_id")
48
-	if err != nil {
49
-		t.Fatal(err)
50
-	}
48
+	assert.NilError(t, err)
51 49
 	defer body.Close()
52 50
 	content, err := io.ReadAll(body)
53
-	if err != nil {
54
-		t.Fatal(err)
55
-	}
56
-	if string(content) != "response" {
57
-		t.Fatalf("expected response to contain 'response', got %s", string(content))
58
-	}
51
+	assert.NilError(t, err)
52
+	assert.Check(t, is.Equal(string(content), "response"))
59 53
 }
... ...
@@ -83,16 +83,8 @@ func TestContainerInspect(t *testing.T) {
83 83
 	}
84 84
 
85 85
 	r, err := client.ContainerInspect(context.Background(), "container_id")
86
-	if err != nil {
87
-		t.Fatal(err)
88
-	}
89
-	if r.ID != "container_id" {
90
-		t.Fatalf("expected `container_id`, got %s", r.ID)
91
-	}
92
-	if r.Image != "image" {
93
-		t.Fatalf("expected `image`, got %s", r.Image)
94
-	}
95
-	if r.Name != "name" {
96
-		t.Fatalf("expected `name`, got %s", r.Name)
97
-	}
86
+	assert.NilError(t, err)
87
+	assert.Check(t, is.Equal(r.ID, "container_id"))
88
+	assert.Check(t, is.Equal(r.Image, "image"))
89
+	assert.Check(t, is.Equal(r.Name, "name"))
98 90
 }
... ...
@@ -49,7 +49,5 @@ func TestContainerKill(t *testing.T) {
49 49
 	}
50 50
 
51 51
 	err := client.ContainerKill(context.Background(), "container_id", "SIGKILL")
52
-	if err != nil {
53
-		t.Fatal(err)
54
-	}
52
+	assert.NilError(t, err)
55 53
 }
... ...
@@ -88,10 +88,6 @@ func TestContainerList(t *testing.T) {
88 88
 			filters.Arg("before", "container"),
89 89
 		),
90 90
 	})
91
-	if err != nil {
92
-		t.Fatal(err)
93
-	}
94
-	if len(containers) != 2 {
95
-		t.Fatalf("expected 2 containers, got %v", containers)
96
-	}
91
+	assert.NilError(t, err)
92
+	assert.Check(t, is.Len(containers, 2))
97 93
 }
... ...
@@ -36,7 +36,5 @@ func TestContainerPause(t *testing.T) {
36 36
 		}),
37 37
 	}
38 38
 	err := client.ContainerPause(context.Background(), "container_id")
39
-	if err != nil {
40
-		t.Fatal(err)
41
-	}
39
+	assert.NilError(t, err)
42 40
 }
... ...
@@ -109,7 +109,7 @@ func TestContainersPrune(t *testing.T) {
109 109
 		}
110 110
 
111 111
 		report, err := client.ContainersPrune(context.Background(), listCase.filters)
112
-		assert.Check(t, err)
112
+		assert.NilError(t, err)
113 113
 		assert.Check(t, is.Len(report.ContainersDeleted, 2))
114 114
 		assert.Check(t, is.Equal(uint64(9999), report.SpaceReclaimed))
115 115
 	}
... ...
@@ -71,5 +71,5 @@ func TestContainerRemove(t *testing.T) {
71 71
 		RemoveVolumes: true,
72 72
 		Force:         true,
73 73
 	})
74
-	assert.Check(t, err)
74
+	assert.NilError(t, err)
75 75
 }
... ...
@@ -49,7 +49,5 @@ func TestContainerRename(t *testing.T) {
49 49
 	}
50 50
 
51 51
 	err := client.ContainerRename(context.Background(), "container_id", "newName")
52
-	if err != nil {
53
-		t.Fatal(err)
54
-	}
52
+	assert.NilError(t, err)
55 53
 }
... ...
@@ -77,7 +77,7 @@ func TestContainerResize(t *testing.T) {
77 77
 				client: newMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)),
78 78
 			}
79 79
 			err := client.ContainerResize(context.Background(), "container_id", tc.opts)
80
-			assert.Check(t, err)
80
+			assert.NilError(t, err)
81 81
 		})
82 82
 	}
83 83
 }
... ...
@@ -120,7 +120,7 @@ func TestContainerExecResize(t *testing.T) {
120 120
 				client: newMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)),
121 121
 			}
122 122
 			err := client.ContainerExecResize(context.Background(), "exec_id", tc.opts)
123
-			assert.Check(t, err)
123
+			assert.NilError(t, err)
124 124
 		})
125 125
 	}
126 126
 }
... ...
@@ -70,7 +70,5 @@ func TestContainerRestart(t *testing.T) {
70 70
 		Signal:  "SIGKILL",
71 71
 		Timeout: &timeout,
72 72
 	})
73
-	if err != nil {
74
-		t.Fatal(err)
75
-	}
73
+	assert.NilError(t, err)
76 74
 }
... ...
@@ -60,7 +60,5 @@ func TestContainerStart(t *testing.T) {
60 60
 	}
61 61
 
62 62
 	err := client.ContainerStart(context.Background(), "container_id", container.StartOptions{CheckpointID: "checkpoint_id"})
63
-	if err != nil {
64
-		t.Fatal(err)
65
-	}
63
+	assert.NilError(t, err)
66 64
 }
... ...
@@ -64,16 +64,10 @@ func TestContainerStats(t *testing.T) {
64 64
 			}),
65 65
 		}
66 66
 		resp, err := client.ContainerStats(context.Background(), "container_id", c.stream)
67
-		if err != nil {
68
-			t.Fatal(err)
69
-		}
67
+		assert.NilError(t, err)
70 68
 		defer resp.Body.Close()
71 69
 		content, err := io.ReadAll(resp.Body)
72
-		if err != nil {
73
-			t.Fatal(err)
74
-		}
75
-		if string(content) != "response" {
76
-			t.Fatalf("expected response to contain 'response', got %s", string(content))
77
-		}
70
+		assert.NilError(t, err)
71
+		assert.Check(t, is.Equal(string(content), "response"))
78 72
 	}
79 73
 }
... ...
@@ -70,7 +70,5 @@ func TestContainerStop(t *testing.T) {
70 70
 		Signal:  "SIGKILL",
71 71
 		Timeout: &timeout,
72 72
 	})
73
-	if err != nil {
74
-		t.Fatal(err)
75
-	}
73
+	assert.NilError(t, err)
76 74
 }
... ...
@@ -7,7 +7,6 @@ import (
7 7
 	"fmt"
8 8
 	"io"
9 9
 	"net/http"
10
-	"reflect"
11 10
 	"strings"
12 11
 	"testing"
13 12
 
... ...
@@ -71,13 +70,7 @@ func TestContainerTop(t *testing.T) {
71 71
 	}
72 72
 
73 73
 	processList, err := client.ContainerTop(context.Background(), "container_id", []string{"arg1", "arg2"})
74
-	if err != nil {
75
-		t.Fatal(err)
76
-	}
77
-	if !reflect.DeepEqual(expectedProcesses, processList.Processes) {
78
-		t.Fatalf("Processes: expected %v, got %v", expectedProcesses, processList.Processes)
79
-	}
80
-	if !reflect.DeepEqual(expectedTitles, processList.Titles) {
81
-		t.Fatalf("Titles: expected %v, got %v", expectedTitles, processList.Titles)
82
-	}
74
+	assert.NilError(t, err)
75
+	assert.Check(t, is.DeepEqual(expectedProcesses, processList.Processes))
76
+	assert.Check(t, is.DeepEqual(expectedTitles, processList.Titles))
83 77
 }
... ...
@@ -44,7 +44,5 @@ func TestContainerUnpause(t *testing.T) {
44 44
 		}),
45 45
 	}
46 46
 	err := client.ContainerUnpause(context.Background(), "container_id")
47
-	if err != nil {
48
-		t.Fatal(err)
49
-	}
47
+	assert.NilError(t, err)
50 48
 }
... ...
@@ -61,7 +61,5 @@ func TestContainerUpdate(t *testing.T) {
61 61
 			Name: "always",
62 62
 		},
63 63
 	})
64
-	if err != nil {
65
-		t.Fatal(err)
66
-	}
64
+	assert.NilError(t, err)
67 65
 }
... ...
@@ -74,11 +74,9 @@ func TestContainerWait(t *testing.T) {
74 74
 	resultC, errC := client.ContainerWait(context.Background(), "container_id", "")
75 75
 	select {
76 76
 	case err := <-errC:
77
-		t.Fatal(err)
77
+		assert.NilError(t, err)
78 78
 	case result := <-resultC:
79
-		if result.StatusCode != 15 {
80
-			t.Fatalf("expected a status code equal to '15', got %d", result.StatusCode)
81
-		}
79
+		assert.Check(t, is.Equal(result.StatusCode, int64(15)))
82 80
 	}
83 81
 }
84 82
 
... ...
@@ -101,9 +99,7 @@ func TestContainerWaitProxyInterrupt(t *testing.T) {
101 101
 	resultC, errC := client.ContainerWait(context.Background(), "container_id", "")
102 102
 	select {
103 103
 	case err := <-errC:
104
-		if !strings.Contains(err.Error(), msg) {
105
-			t.Fatalf("Expected: %s, Actual: %s", msg, err.Error())
106
-		}
104
+		assert.Check(t, is.ErrorContains(err, msg))
107 105
 	case result := <-resultC:
108 106
 		t.Fatalf("Unexpected result: %v", result)
109 107
 	}
... ...
@@ -129,9 +125,7 @@ func TestContainerWaitProxyInterruptLong(t *testing.T) {
129 129
 	select {
130 130
 	case err := <-errC:
131 131
 		// LimitReader limiting isn't exact, because of how the Readers do chunking.
132
-		if len(err.Error()) > containerWaitErrorMsgLimit*2 {
133
-			t.Fatalf("Expected error to be limited around %d, actual length: %d", containerWaitErrorMsgLimit, len(err.Error()))
134
-		}
132
+		assert.Check(t, len(err.Error()) <= containerWaitErrorMsgLimit*2, "Expected error to be limited around %d, actual length: %d", containerWaitErrorMsgLimit, len(err.Error()))
135 133
 	case result := <-resultC:
136 134
 		t.Fatalf("Unexpected result: %v", result)
137 135
 	}
... ...
@@ -164,9 +158,7 @@ func TestContainerWaitErrorHandling(t *testing.T) {
164 164
 			resultC, errC := client.ContainerWait(ctx, "container_id", "")
165 165
 			select {
166 166
 			case err := <-errC:
167
-				if err.Error() != test.exp.Error() {
168
-					t.Fatalf("ContainerWait() errC = %v; want %v", err, test.exp)
169
-				}
167
+				assert.Check(t, is.Equal(err.Error(), test.exp.Error()))
170 168
 				return
171 169
 			case result := <-resultC:
172 170
 				t.Fatalf("expected to not get a wait result, got %d", result.StatusCode)