Browse code

client/image: use gotest.tools-style asserts

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

Paweł Gronowski authored on 2025/05/17 02:14:36
Showing 12 changed files
... ...
@@ -200,20 +200,12 @@ func TestImageBuild(t *testing.T) {
200 200
 			}),
201 201
 		}
202 202
 		buildResponse, err := client.ImageBuild(context.Background(), nil, buildCase.buildOptions)
203
-		if err != nil {
204
-			t.Fatal(err)
205
-		}
206
-		if buildResponse.OSType != "MyOS" {
207
-			t.Fatalf("expected OSType to be 'MyOS', got %s", buildResponse.OSType)
208
-		}
203
+		assert.NilError(t, err)
204
+		assert.Check(t, is.Equal(buildResponse.OSType, "MyOS"))
209 205
 		response, err := io.ReadAll(buildResponse.Body)
210
-		if err != nil {
211
-			t.Fatal(err)
212
-		}
206
+		assert.NilError(t, err)
213 207
 		buildResponse.Body.Close()
214
-		if string(response) != "body" {
215
-			t.Fatalf("expected Body to contain 'body' string, got %s", response)
216
-		}
208
+		assert.Check(t, is.Equal(string(response), "body"))
217 209
 	}
218 210
 }
219 211
 
... ...
@@ -225,8 +217,6 @@ func TestGetDockerOS(t *testing.T) {
225 225
 	}
226 226
 	for header, os := range cases {
227 227
 		g := getDockerOS(header)
228
-		if g != os {
229
-			t.Fatalf("Expected %s, got %s", os, g)
230
-		}
228
+		assert.Check(t, is.Equal(g, os))
231 229
 	}
232 230
 }
... ...
@@ -64,17 +64,10 @@ func TestImageCreate(t *testing.T) {
64 64
 	createResponse, err := client.ImageCreate(context.Background(), specifiedReference, image.CreateOptions{
65 65
 		RegistryAuth: expectedRegistryAuth,
66 66
 	})
67
-	if err != nil {
68
-		t.Fatal(err)
69
-	}
67
+	assert.NilError(t, err)
70 68
 	response, err := io.ReadAll(createResponse)
71
-	if err != nil {
72
-		t.Fatal(err)
73
-	}
74
-	if err = createResponse.Close(); err != nil {
75
-		t.Fatal(err)
76
-	}
77
-	if string(response) != "body" {
78
-		t.Fatalf("expected Body to contain 'body' string, got %s", response)
79
-	}
69
+	assert.NilError(t, err)
70
+	err = createResponse.Close()
71
+	assert.NilError(t, err)
72
+	assert.Check(t, is.Equal(string(response), "body"))
80 73
 }
... ...
@@ -89,7 +89,7 @@ func TestImageImport(t *testing.T) {
89 89
 
90 90
 			body, err := io.ReadAll(resp)
91 91
 			assert.NilError(t, err)
92
-			assert.Equal(t, string(body), expectedOutput)
92
+			assert.Check(t, is.Equal(string(body), expectedOutput))
93 93
 		})
94 94
 	}
95 95
 }
... ...
@@ -8,7 +8,6 @@ import (
8 8
 	"fmt"
9 9
 	"io"
10 10
 	"net/http"
11
-	"reflect"
12 11
 	"strings"
13 12
 	"testing"
14 13
 
... ...
@@ -70,15 +69,9 @@ func TestImageInspect(t *testing.T) {
70 70
 	}
71 71
 
72 72
 	imageInspect, err := client.ImageInspect(context.Background(), "image_id")
73
-	if err != nil {
74
-		t.Fatal(err)
75
-	}
76
-	if imageInspect.ID != "image_id" {
77
-		t.Fatalf("expected `image_id`, got %s", imageInspect.ID)
78
-	}
79
-	if !reflect.DeepEqual(imageInspect.RepoTags, expectedTags) {
80
-		t.Fatalf("expected `%v`, got %v", expectedTags, imageInspect.RepoTags)
81
-	}
73
+	assert.NilError(t, err)
74
+	assert.Check(t, is.Equal(imageInspect.ID, "image_id"))
75
+	assert.Check(t, is.DeepEqual(imageInspect.RepoTags, expectedTags))
82 76
 }
83 77
 
84 78
 func TestImageInspectWithPlatform(t *testing.T) {
... ...
@@ -120,7 +113,7 @@ func TestImageInspectWithPlatform(t *testing.T) {
120 120
 
121 121
 	imageInspect, err := client.ImageInspect(context.Background(), "image_id", ImageInspectWithPlatform(requestedPlatform))
122 122
 	assert.NilError(t, err)
123
-	assert.Equal(t, imageInspect.ID, "image_id")
124
-	assert.Equal(t, imageInspect.Architecture, "arm64")
125
-	assert.Equal(t, imageInspect.Os, "linux")
123
+	assert.Check(t, is.Equal(imageInspect.ID, "image_id"))
124
+	assert.Check(t, is.Equal(imageInspect.Architecture, "arm64"))
125
+	assert.Check(t, is.Equal(imageInspect.Os, "linux"))
126 126
 }
... ...
@@ -111,12 +111,8 @@ func TestImageList(t *testing.T) {
111 111
 		}
112 112
 
113 113
 		images, err := client.ImageList(context.Background(), listCase.options)
114
-		if err != nil {
115
-			t.Fatal(err)
116
-		}
117
-		if len(images) != 2 {
118
-			t.Fatalf("expected 2 images, got %v", images)
119
-		}
114
+		assert.NilError(t, err)
115
+		assert.Check(t, is.Len(images, 2))
120 116
 	}
121 117
 }
122 118
 
... ...
@@ -157,12 +153,8 @@ func TestImageListApiBefore125(t *testing.T) {
157 157
 	}
158 158
 
159 159
 	images, err := client.ImageList(context.Background(), options)
160
-	if err != nil {
161
-		t.Fatal(err)
162
-	}
163
-	if len(images) != 2 {
164
-		t.Fatalf("expected 2 images, got %v", images)
165
-	}
160
+	assert.NilError(t, err)
161
+	assert.Check(t, is.Len(images, 2))
166 162
 }
167 163
 
168 164
 // Checks if shared-size query parameter is set/not being set correctly
... ...
@@ -194,7 +186,7 @@ func TestImageListWithSharedSize(t *testing.T) {
194 194
 				version: tc.version,
195 195
 			}
196 196
 			_, err := client.ImageList(context.Background(), tc.options)
197
-			assert.Check(t, err)
197
+			assert.NilError(t, err)
198 198
 			expectedSet := tc.sharedSize != ""
199 199
 			assert.Check(t, is.Equal(query.Has(sharedSize), expectedSet))
200 200
 			assert.Check(t, is.Equal(query.Get(sharedSize), tc.sharedSize))
... ...
@@ -106,7 +106,7 @@ func TestImagesPrune(t *testing.T) {
106 106
 		}
107 107
 
108 108
 		report, err := client.ImagesPrune(context.Background(), listCase.filters)
109
-		assert.Check(t, err)
109
+		assert.NilError(t, err)
110 110
 		assert.Check(t, is.Len(report.ImagesDeleted, 2))
111 111
 		assert.Check(t, is.Equal(uint64(9999), report.SpaceReclaimed))
112 112
 	}
... ...
@@ -24,9 +24,7 @@ func TestImagePullReferenceParseError(t *testing.T) {
24 24
 	}
25 25
 	// An empty reference is an invalid reference
26 26
 	_, err := client.ImagePull(context.Background(), "", image.PullOptions{})
27
-	if err == nil || !strings.Contains(err.Error(), "invalid reference format") {
28
-		t.Fatalf("expected an error, got %v", err)
29
-	}
27
+	assert.Check(t, is.ErrorContains(err, "invalid reference format"))
30 28
 }
31 29
 
32 30
 func TestImagePullAnyError(t *testing.T) {
... ...
@@ -55,9 +53,7 @@ func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
55 55
 	_, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{
56 56
 		PrivilegeFunc: privilegeFunc,
57 57
 	})
58
-	if err == nil || err.Error() != "Error requesting privilege" {
59
-		t.Fatalf("expected an error requesting privilege, got %v", err)
60
-	}
58
+	assert.Check(t, is.Error(err, "Error requesting privilege"))
61 59
 }
62 60
 
63 61
 func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
... ...
@@ -112,16 +108,10 @@ func TestImagePullWithPrivilegedFuncNoError(t *testing.T) {
112 112
 		RegistryAuth:  "NotValid",
113 113
 		PrivilegeFunc: privilegeFunc,
114 114
 	})
115
-	if err != nil {
116
-		t.Fatal(err)
117
-	}
115
+	assert.NilError(t, err)
118 116
 	body, err := io.ReadAll(resp)
119
-	if err != nil {
120
-		t.Fatal(err)
121
-	}
122
-	if string(body) != "hello world" {
123
-		t.Fatalf("expected 'hello world', got %s", string(body))
124
-	}
117
+	assert.NilError(t, err)
118
+	assert.Check(t, is.Equal(string(body), "hello world"))
125 119
 }
126 120
 
127 121
 func TestImagePullWithoutErrors(t *testing.T) {
... ...
@@ -210,16 +200,10 @@ func TestImagePullWithoutErrors(t *testing.T) {
210 210
 			resp, err := client.ImagePull(context.Background(), pullCase.reference, image.PullOptions{
211 211
 				All: pullCase.all,
212 212
 			})
213
-			if err != nil {
214
-				t.Fatal(err)
215
-			}
213
+			assert.NilError(t, err)
216 214
 			body, err := io.ReadAll(resp)
217
-			if err != nil {
218
-				t.Fatal(err)
219
-			}
220
-			if string(body) != expectedOutput {
221
-				t.Fatalf("expected '%s', got %s", expectedOutput, string(body))
222
-			}
215
+			assert.NilError(t, err)
216
+			assert.Check(t, is.Equal(string(body), expectedOutput))
223 217
 		})
224 218
 	}
225 219
 }
... ...
@@ -24,14 +24,10 @@ func TestImagePushReferenceError(t *testing.T) {
24 24
 	}
25 25
 	// An empty reference is an invalid reference
26 26
 	_, err := client.ImagePush(context.Background(), "", image.PushOptions{})
27
-	if err == nil || !strings.Contains(err.Error(), "invalid reference format") {
28
-		t.Fatalf("expected an error, got %v", err)
29
-	}
27
+	assert.Check(t, is.ErrorContains(err, "invalid reference format"))
30 28
 	// An canonical reference cannot be pushed
31 29
 	_, err = client.ImagePush(context.Background(), "repo@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", image.PushOptions{})
32
-	if err == nil || err.Error() != "cannot push a digest reference" {
33
-		t.Fatalf("expected an error, got %v", err)
34
-	}
30
+	assert.Check(t, is.Error(err, "cannot push a digest reference"))
35 31
 }
36 32
 
37 33
 func TestImagePushAnyError(t *testing.T) {
... ...
@@ -60,9 +56,7 @@ func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
60 60
 	_, err := client.ImagePush(context.Background(), "myimage", image.PushOptions{
61 61
 		PrivilegeFunc: privilegeFunc,
62 62
 	})
63
-	if err == nil || err.Error() != "Error requesting privilege" {
64
-		t.Fatalf("expected an error requesting privilege, got %v", err)
65
-	}
63
+	assert.Check(t, is.Error(err, "Error requesting privilege"))
66 64
 }
67 65
 
68 66
 func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
... ...
@@ -113,16 +107,10 @@ func TestImagePushWithPrivilegedFuncNoError(t *testing.T) {
113 113
 		RegistryAuth:  "NotValid",
114 114
 		PrivilegeFunc: privilegeFunc,
115 115
 	})
116
-	if err != nil {
117
-		t.Fatal(err)
118
-	}
116
+	assert.NilError(t, err)
119 117
 	body, err := io.ReadAll(resp)
120
-	if err != nil {
121
-		t.Fatal(err)
122
-	}
123
-	if string(body) != "hello world" {
124
-		t.Fatalf("expected 'hello world', got %s", string(body))
125
-	}
118
+	assert.NilError(t, err)
119
+	assert.Check(t, is.Equal(string(body), "hello world"))
126 120
 }
127 121
 
128 122
 func TestImagePushWithoutErrors(t *testing.T) {
... ...
@@ -208,16 +196,10 @@ func TestImagePushWithoutErrors(t *testing.T) {
208 208
 			resp, err := client.ImagePush(context.Background(), tc.reference, image.PushOptions{
209 209
 				All: tc.all,
210 210
 			})
211
-			if err != nil {
212
-				t.Fatal(err)
213
-			}
211
+			assert.NilError(t, err)
214 212
 			body, err := io.ReadAll(resp)
215
-			if err != nil {
216
-				t.Fatal(err)
217
-			}
218
-			if string(body) != expectedOutput {
219
-				t.Fatalf("expected '%s', got %s", expectedOutput, string(body))
220
-			}
213
+			assert.NilError(t, err)
214
+			assert.Check(t, is.Equal(string(body), expectedOutput))
221 215
 		})
222 216
 	}
223 217
 }
... ...
@@ -96,11 +96,7 @@ func TestImageRemove(t *testing.T) {
96 96
 			Force:         removeCase.force,
97 97
 			PruneChildren: removeCase.pruneChildren,
98 98
 		})
99
-		if err != nil {
100
-			t.Fatal(err)
101
-		}
102
-		if len(imageDeletes) != 2 {
103
-			t.Fatalf("expected 2 deleted images, got %v", imageDeletes)
104
-		}
99
+		assert.NilError(t, err)
100
+		assert.Check(t, is.Len(imageDeletes, 2))
105 101
 	}
106 102
 }
... ...
@@ -81,7 +81,7 @@ func TestImageSave(t *testing.T) {
81 81
 
82 82
 			body, err := io.ReadAll(resp)
83 83
 			assert.NilError(t, err)
84
-			assert.Equal(t, string(body), expectedOutput)
84
+			assert.Check(t, is.Equal(string(body), expectedOutput))
85 85
 		})
86 86
 	}
87 87
 }
... ...
@@ -43,9 +43,7 @@ func TestImageSearchWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
43 43
 	_, err := client.ImageSearch(context.Background(), "some-image", registry.SearchOptions{
44 44
 		PrivilegeFunc: privilegeFunc,
45 45
 	})
46
-	if err == nil || err.Error() != "Error requesting privilege" {
47
-		t.Fatalf("expected an error requesting privilege, got %v", err)
48
-	}
46
+	assert.Check(t, is.Error(err, "Error requesting privilege"))
49 47
 }
50 48
 
51 49
 func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
... ...
@@ -104,12 +102,8 @@ func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
104 104
 		RegistryAuth:  "NotValid",
105 105
 		PrivilegeFunc: privilegeFunc,
106 106
 	})
107
-	if err != nil {
108
-		t.Fatal(err)
109
-	}
110
-	if len(results) != 1 {
111
-		t.Fatalf("expected 1 result, got %v", results)
112
-	}
107
+	assert.NilError(t, err)
108
+	assert.Check(t, is.Len(results, 1))
113 109
 }
114 110
 
115 111
 func TestImageSearchWithoutErrors(t *testing.T) {
... ...
@@ -150,10 +144,6 @@ func TestImageSearchWithoutErrors(t *testing.T) {
150 150
 			filters.Arg("stars", "3"),
151 151
 		),
152 152
 	})
153
-	if err != nil {
154
-		t.Fatal(err)
155
-	}
156
-	if len(results) != 1 {
157
-		t.Fatalf("expected a result, got %v", results)
158
-	}
153
+	assert.NilError(t, err)
154
+	assert.Check(t, is.Len(results, 1))
159 155
 }
... ...
@@ -32,9 +32,7 @@ func TestImageTagInvalidReference(t *testing.T) {
32 32
 	}
33 33
 
34 34
 	err := client.ImageTag(context.Background(), "image_id", "aa/asdf$$^/aa")
35
-	if err == nil || err.Error() != `Error parsing reference: "aa/asdf$$^/aa" is not a valid repository/tag: invalid reference format` {
36
-		t.Fatalf("expected ErrReferenceInvalidFormat, got %v", err)
37
-	}
35
+	assert.Check(t, is.Error(err, `Error parsing reference: "aa/asdf$$^/aa" is not a valid repository/tag: invalid reference format`))
38 36
 }
39 37
 
40 38
 // Ensure we don't allow the use of invalid repository names or tags; these tag operations should fail.
... ...
@@ -89,9 +87,7 @@ func TestImageTagHexSource(t *testing.T) {
89 89
 	}
90 90
 
91 91
 	err := client.ImageTag(context.Background(), "0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca67d01a470d", "repo:tag")
92
-	if err != nil {
93
-		t.Fatalf("got error: %v", err)
94
-	}
92
+	assert.NilError(t, err)
95 93
 }
96 94
 
97 95
 func TestImageTag(t *testing.T) {
... ...
@@ -173,8 +169,6 @@ func TestImageTag(t *testing.T) {
173 173
 			}),
174 174
 		}
175 175
 		err := client.ImageTag(context.Background(), "image_id", tagCase.reference)
176
-		if err != nil {
177
-			t.Fatal(err)
178
-		}
176
+		assert.NilError(t, err)
179 177
 	}
180 178
 }