mirror of
https://github.com/moby/moby.git
synced 2026-08-07 08:31:39 +00:00
client/image: use gotest.tools-style asserts
Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"))
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"))
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user