From 6402a106e79b0696deb83c76ee2f7cea7e0bf208 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Fri, 16 May 2025 19:14:36 +0200 Subject: [PATCH] client/image: use gotest.tools-style asserts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- client/image_build_test.go | 20 +++++--------------- client/image_create_test.go | 17 +++++------------ client/image_import_test.go | 2 +- client/image_inspect_test.go | 19 ++++++------------- client/image_list_test.go | 18 +++++------------- client/image_prune_test.go | 2 +- client/image_pull_test.go | 32 ++++++++------------------------ client/image_push_test.go | 36 +++++++++--------------------------- client/image_remove_test.go | 8 ++------ client/image_save_test.go | 2 +- client/image_search_test.go | 20 +++++--------------- client/image_tag_test.go | 12 +++--------- 12 files changed, 51 insertions(+), 137 deletions(-) diff --git a/client/image_build_test.go b/client/image_build_test.go index 1f876be172..337eb75be1 100644 --- a/client/image_build_test.go +++ b/client/image_build_test.go @@ -200,20 +200,12 @@ func TestImageBuild(t *testing.T) { }), } buildResponse, err := client.ImageBuild(context.Background(), nil, buildCase.buildOptions) - if err != nil { - t.Fatal(err) - } - if buildResponse.OSType != "MyOS" { - t.Fatalf("expected OSType to be 'MyOS', got %s", buildResponse.OSType) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(buildResponse.OSType, "MyOS")) response, err := io.ReadAll(buildResponse.Body) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) buildResponse.Body.Close() - if string(response) != "body" { - t.Fatalf("expected Body to contain 'body' string, got %s", response) - } + assert.Check(t, is.Equal(string(response), "body")) } } @@ -225,8 +217,6 @@ func TestGetDockerOS(t *testing.T) { } for header, os := range cases { g := getDockerOS(header) - if g != os { - t.Fatalf("Expected %s, got %s", os, g) - } + assert.Check(t, is.Equal(g, os)) } } diff --git a/client/image_create_test.go b/client/image_create_test.go index 24af57257b..5a8cdf5d24 100644 --- a/client/image_create_test.go +++ b/client/image_create_test.go @@ -64,17 +64,10 @@ func TestImageCreate(t *testing.T) { createResponse, err := client.ImageCreate(context.Background(), specifiedReference, image.CreateOptions{ RegistryAuth: expectedRegistryAuth, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) response, err := io.ReadAll(createResponse) - if err != nil { - t.Fatal(err) - } - if err = createResponse.Close(); err != nil { - t.Fatal(err) - } - if string(response) != "body" { - t.Fatalf("expected Body to contain 'body' string, got %s", response) - } + assert.NilError(t, err) + err = createResponse.Close() + assert.NilError(t, err) + assert.Check(t, is.Equal(string(response), "body")) } diff --git a/client/image_import_test.go b/client/image_import_test.go index 5d3f58e212..ac6abdbec1 100644 --- a/client/image_import_test.go +++ b/client/image_import_test.go @@ -89,7 +89,7 @@ func TestImageImport(t *testing.T) { body, err := io.ReadAll(resp) assert.NilError(t, err) - assert.Equal(t, string(body), expectedOutput) + assert.Check(t, is.Equal(string(body), expectedOutput)) }) } } diff --git a/client/image_inspect_test.go b/client/image_inspect_test.go index 05f062bd81..0746c755aa 100644 --- a/client/image_inspect_test.go +++ b/client/image_inspect_test.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "net/http" - "reflect" "strings" "testing" @@ -70,15 +69,9 @@ func TestImageInspect(t *testing.T) { } imageInspect, err := client.ImageInspect(context.Background(), "image_id") - if err != nil { - t.Fatal(err) - } - if imageInspect.ID != "image_id" { - t.Fatalf("expected `image_id`, got %s", imageInspect.ID) - } - if !reflect.DeepEqual(imageInspect.RepoTags, expectedTags) { - t.Fatalf("expected `%v`, got %v", expectedTags, imageInspect.RepoTags) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(imageInspect.ID, "image_id")) + assert.Check(t, is.DeepEqual(imageInspect.RepoTags, expectedTags)) } func TestImageInspectWithPlatform(t *testing.T) { @@ -120,7 +113,7 @@ func TestImageInspectWithPlatform(t *testing.T) { imageInspect, err := client.ImageInspect(context.Background(), "image_id", ImageInspectWithPlatform(requestedPlatform)) assert.NilError(t, err) - assert.Equal(t, imageInspect.ID, "image_id") - assert.Equal(t, imageInspect.Architecture, "arm64") - assert.Equal(t, imageInspect.Os, "linux") + assert.Check(t, is.Equal(imageInspect.ID, "image_id")) + assert.Check(t, is.Equal(imageInspect.Architecture, "arm64")) + assert.Check(t, is.Equal(imageInspect.Os, "linux")) } diff --git a/client/image_list_test.go b/client/image_list_test.go index 8dbfdd9a34..c35ddd67a9 100644 --- a/client/image_list_test.go +++ b/client/image_list_test.go @@ -111,12 +111,8 @@ func TestImageList(t *testing.T) { } images, err := client.ImageList(context.Background(), listCase.options) - if err != nil { - t.Fatal(err) - } - if len(images) != 2 { - t.Fatalf("expected 2 images, got %v", images) - } + assert.NilError(t, err) + assert.Check(t, is.Len(images, 2)) } } @@ -157,12 +153,8 @@ func TestImageListApiBefore125(t *testing.T) { } images, err := client.ImageList(context.Background(), options) - if err != nil { - t.Fatal(err) - } - if len(images) != 2 { - t.Fatalf("expected 2 images, got %v", images) - } + assert.NilError(t, err) + assert.Check(t, is.Len(images, 2)) } // Checks if shared-size query parameter is set/not being set correctly @@ -194,7 +186,7 @@ func TestImageListWithSharedSize(t *testing.T) { version: tc.version, } _, err := client.ImageList(context.Background(), tc.options) - assert.Check(t, err) + assert.NilError(t, err) expectedSet := tc.sharedSize != "" assert.Check(t, is.Equal(query.Has(sharedSize), expectedSet)) assert.Check(t, is.Equal(query.Get(sharedSize), tc.sharedSize)) diff --git a/client/image_prune_test.go b/client/image_prune_test.go index 10414fb701..606a038f49 100644 --- a/client/image_prune_test.go +++ b/client/image_prune_test.go @@ -106,7 +106,7 @@ func TestImagesPrune(t *testing.T) { } report, err := client.ImagesPrune(context.Background(), listCase.filters) - assert.Check(t, err) + assert.NilError(t, err) assert.Check(t, is.Len(report.ImagesDeleted, 2)) assert.Check(t, is.Equal(uint64(9999), report.SpaceReclaimed)) } diff --git a/client/image_pull_test.go b/client/image_pull_test.go index 74acbf703f..3a8fb6d4c9 100644 --- a/client/image_pull_test.go +++ b/client/image_pull_test.go @@ -24,9 +24,7 @@ func TestImagePullReferenceParseError(t *testing.T) { } // An empty reference is an invalid reference _, err := client.ImagePull(context.Background(), "", image.PullOptions{}) - if err == nil || !strings.Contains(err.Error(), "invalid reference format") { - t.Fatalf("expected an error, got %v", err) - } + assert.Check(t, is.ErrorContains(err, "invalid reference format")) } func TestImagePullAnyError(t *testing.T) { @@ -55,9 +53,7 @@ func TestImagePullWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) { _, err := client.ImagePull(context.Background(), "myimage", image.PullOptions{ PrivilegeFunc: privilegeFunc, }) - if err == nil || err.Error() != "Error requesting privilege" { - t.Fatalf("expected an error requesting privilege, got %v", err) - } + assert.Check(t, is.Error(err, "Error requesting privilege")) } func TestImagePullWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) { @@ -112,16 +108,10 @@ func TestImagePullWithPrivilegedFuncNoError(t *testing.T) { RegistryAuth: "NotValid", PrivilegeFunc: privilegeFunc, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) body, err := io.ReadAll(resp) - if err != nil { - t.Fatal(err) - } - if string(body) != "hello world" { - t.Fatalf("expected 'hello world', got %s", string(body)) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(string(body), "hello world")) } func TestImagePullWithoutErrors(t *testing.T) { @@ -210,16 +200,10 @@ func TestImagePullWithoutErrors(t *testing.T) { resp, err := client.ImagePull(context.Background(), pullCase.reference, image.PullOptions{ All: pullCase.all, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) body, err := io.ReadAll(resp) - if err != nil { - t.Fatal(err) - } - if string(body) != expectedOutput { - t.Fatalf("expected '%s', got %s", expectedOutput, string(body)) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(string(body), expectedOutput)) }) } } diff --git a/client/image_push_test.go b/client/image_push_test.go index b555bd2d01..43c6b8c645 100644 --- a/client/image_push_test.go +++ b/client/image_push_test.go @@ -24,14 +24,10 @@ func TestImagePushReferenceError(t *testing.T) { } // An empty reference is an invalid reference _, err := client.ImagePush(context.Background(), "", image.PushOptions{}) - if err == nil || !strings.Contains(err.Error(), "invalid reference format") { - t.Fatalf("expected an error, got %v", err) - } + assert.Check(t, is.ErrorContains(err, "invalid reference format")) // An canonical reference cannot be pushed _, err = client.ImagePush(context.Background(), "repo@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", image.PushOptions{}) - if err == nil || err.Error() != "cannot push a digest reference" { - t.Fatalf("expected an error, got %v", err) - } + assert.Check(t, is.Error(err, "cannot push a digest reference")) } func TestImagePushAnyError(t *testing.T) { @@ -60,9 +56,7 @@ func TestImagePushWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) { _, err := client.ImagePush(context.Background(), "myimage", image.PushOptions{ PrivilegeFunc: privilegeFunc, }) - if err == nil || err.Error() != "Error requesting privilege" { - t.Fatalf("expected an error requesting privilege, got %v", err) - } + assert.Check(t, is.Error(err, "Error requesting privilege")) } func TestImagePushWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) { @@ -113,16 +107,10 @@ func TestImagePushWithPrivilegedFuncNoError(t *testing.T) { RegistryAuth: "NotValid", PrivilegeFunc: privilegeFunc, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) body, err := io.ReadAll(resp) - if err != nil { - t.Fatal(err) - } - if string(body) != "hello world" { - t.Fatalf("expected 'hello world', got %s", string(body)) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(string(body), "hello world")) } func TestImagePushWithoutErrors(t *testing.T) { @@ -208,16 +196,10 @@ func TestImagePushWithoutErrors(t *testing.T) { resp, err := client.ImagePush(context.Background(), tc.reference, image.PushOptions{ All: tc.all, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) body, err := io.ReadAll(resp) - if err != nil { - t.Fatal(err) - } - if string(body) != expectedOutput { - t.Fatalf("expected '%s', got %s", expectedOutput, string(body)) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(string(body), expectedOutput)) }) } } diff --git a/client/image_remove_test.go b/client/image_remove_test.go index dfe6c03441..33568836d1 100644 --- a/client/image_remove_test.go +++ b/client/image_remove_test.go @@ -96,11 +96,7 @@ func TestImageRemove(t *testing.T) { Force: removeCase.force, PruneChildren: removeCase.pruneChildren, }) - if err != nil { - t.Fatal(err) - } - if len(imageDeletes) != 2 { - t.Fatalf("expected 2 deleted images, got %v", imageDeletes) - } + assert.NilError(t, err) + assert.Check(t, is.Len(imageDeletes, 2)) } } diff --git a/client/image_save_test.go b/client/image_save_test.go index 39170ef031..2975c4085d 100644 --- a/client/image_save_test.go +++ b/client/image_save_test.go @@ -81,7 +81,7 @@ func TestImageSave(t *testing.T) { body, err := io.ReadAll(resp) assert.NilError(t, err) - assert.Equal(t, string(body), expectedOutput) + assert.Check(t, is.Equal(string(body), expectedOutput)) }) } } diff --git a/client/image_search_test.go b/client/image_search_test.go index 32e85e3276..61694da796 100644 --- a/client/image_search_test.go +++ b/client/image_search_test.go @@ -43,9 +43,7 @@ func TestImageSearchWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) { _, err := client.ImageSearch(context.Background(), "some-image", registry.SearchOptions{ PrivilegeFunc: privilegeFunc, }) - if err == nil || err.Error() != "Error requesting privilege" { - t.Fatalf("expected an error requesting privilege, got %v", err) - } + assert.Check(t, is.Error(err, "Error requesting privilege")) } func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) { @@ -104,12 +102,8 @@ func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) { RegistryAuth: "NotValid", PrivilegeFunc: privilegeFunc, }) - if err != nil { - t.Fatal(err) - } - if len(results) != 1 { - t.Fatalf("expected 1 result, got %v", results) - } + assert.NilError(t, err) + assert.Check(t, is.Len(results, 1)) } func TestImageSearchWithoutErrors(t *testing.T) { @@ -150,10 +144,6 @@ func TestImageSearchWithoutErrors(t *testing.T) { filters.Arg("stars", "3"), ), }) - if err != nil { - t.Fatal(err) - } - if len(results) != 1 { - t.Fatalf("expected a result, got %v", results) - } + assert.NilError(t, err) + assert.Check(t, is.Len(results, 1)) } diff --git a/client/image_tag_test.go b/client/image_tag_test.go index 0bdbcf3d1b..28270d32de 100644 --- a/client/image_tag_test.go +++ b/client/image_tag_test.go @@ -32,9 +32,7 @@ func TestImageTagInvalidReference(t *testing.T) { } err := client.ImageTag(context.Background(), "image_id", "aa/asdf$$^/aa") - if err == nil || err.Error() != `Error parsing reference: "aa/asdf$$^/aa" is not a valid repository/tag: invalid reference format` { - t.Fatalf("expected ErrReferenceInvalidFormat, got %v", err) - } + assert.Check(t, is.Error(err, `Error parsing reference: "aa/asdf$$^/aa" is not a valid repository/tag: invalid reference format`)) } // 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) { } err := client.ImageTag(context.Background(), "0d409d33b27e47423b049f7f863faa08655a8c901749c2b25b93ca67d01a470d", "repo:tag") - if err != nil { - t.Fatalf("got error: %v", err) - } + assert.NilError(t, err) } func TestImageTag(t *testing.T) { @@ -173,8 +169,6 @@ func TestImageTag(t *testing.T) { }), } err := client.ImageTag(context.Background(), "image_id", tagCase.reference) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } }