diff --git a/client/container_commit_test.go b/client/container_commit_test.go index 7f20d89464..0b49d48295 100644 --- a/client/container_commit_test.go +++ b/client/container_commit_test.go @@ -98,10 +98,6 @@ func TestContainerCommit(t *testing.T) { Changes: expectedChanges, Pause: false, }) - if err != nil { - t.Fatal(err) - } - if r.ID != "new_container_id" { - t.Fatalf("expected `new_container_id`, got %s", r.ID) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(r.ID, "new_container_id")) } diff --git a/client/container_copy_test.go b/client/container_copy_test.go index 3007496628..e3da96d297 100644 --- a/client/container_copy_test.go +++ b/client/container_copy_test.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "net/http" + "os" "strings" "testing" @@ -51,9 +52,7 @@ func TestContainerStatPathNoHeaderError(t *testing.T) { }), } _, err := client.ContainerStatPath(context.Background(), "container_id", "path/to/file") - if err == nil { - t.Fatalf("expected an error, got nothing") - } + assert.Check(t, err != nil, "expected an error, got nothing") } func TestContainerStatPath(t *testing.T) { @@ -90,15 +89,9 @@ func TestContainerStatPath(t *testing.T) { }), } stat, err := client.ContainerStatPath(context.Background(), "container_id", expectedPath) - if err != nil { - t.Fatal(err) - } - if stat.Name != "name" { - t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name) - } - if stat.Mode != 0o700 { - t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(stat.Name, "name")) + assert.Check(t, is.Equal(stat.Mode, os.FileMode(0o700))) } func TestCopyToContainerError(t *testing.T) { @@ -132,9 +125,7 @@ func TestCopyToContainerEmptyResponse(t *testing.T) { client: newMockClient(errorMock(http.StatusNoContent, "No content")), } err := client.CopyToContainer(context.Background(), "container_id", "path/to/file", bytes.NewReader([]byte("")), container.CopyToContainerOptions{}) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } + assert.NilError(t, err) } func TestCopyToContainer(t *testing.T) { @@ -178,9 +169,7 @@ func TestCopyToContainer(t *testing.T) { err := client.CopyToContainer(context.Background(), "container_id", expectedPath, bytes.NewReader([]byte("content")), container.CopyToContainerOptions{ AllowOverwriteDirWithFile: false, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } func TestCopyFromContainerError(t *testing.T) { @@ -229,9 +218,7 @@ func TestCopyFromContainerEmptyResponse(t *testing.T) { }), } _, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file") - if err != nil { - t.Fatalf("unexpected error: %v", err) - } + assert.NilError(t, err) } func TestCopyFromContainerNoHeaderError(t *testing.T) { @@ -244,9 +231,7 @@ func TestCopyFromContainerNoHeaderError(t *testing.T) { }), } _, _, err := client.CopyFromContainer(context.Background(), "container_id", "path/to/file") - if err == nil { - t.Fatalf("expected an error, got nothing") - } + assert.Check(t, err != nil, "expected an error, got nothing") } func TestCopyFromContainer(t *testing.T) { @@ -285,23 +270,12 @@ func TestCopyFromContainer(t *testing.T) { }), } r, stat, err := client.CopyFromContainer(context.Background(), "container_id", expectedPath) - if err != nil { - t.Fatal(err) - } - if stat.Name != "name" { - t.Fatalf("expected container path stat name to be 'name', got '%s'", stat.Name) - } - if stat.Mode != 0o700 { - t.Fatalf("expected container path stat mode to be 0700, got '%v'", stat.Mode) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(stat.Name, "name")) + assert.Check(t, is.Equal(stat.Mode, os.FileMode(0o700))) + content, err := io.ReadAll(r) - if err != nil { - t.Fatal(err) - } - if err := r.Close(); err != nil { - t.Fatal(err) - } - if string(content) != "content" { - t.Fatalf("expected content to be 'content', got %s", string(content)) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(string(content), "content")) + assert.NilError(t, r.Close()) } diff --git a/client/container_create_test.go b/client/container_create_test.go index 30fc06b5fd..68086486fc 100644 --- a/client/container_create_test.go +++ b/client/container_create_test.go @@ -64,12 +64,8 @@ func TestContainerCreateWithName(t *testing.T) { } r, err := client.ContainerCreate(context.Background(), nil, nil, nil, nil, "container_name") - if err != nil { - t.Fatal(err) - } - if r.ID != "container_id" { - t.Fatalf("expected `container_id`, got %s", r.ID) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(r.ID, "container_id")) } // 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) { }, nil } } - - client := &Client{ - client: newMockClient(autoRemoveValidator(false)), - version: "1.24", + testCases := []struct { + version string + expectedAutoRemove bool + }{ + {version: "1.24", expectedAutoRemove: false}, + {version: "1.25", expectedAutoRemove: true}, } - if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, ""); err != nil { - t.Fatal(err) - } - client = &Client{ - client: newMockClient(autoRemoveValidator(true)), - version: "1.25", - } - if _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, ""); err != nil { - t.Fatal(err) + for _, tc := range testCases { + t.Run(tc.version, func(t *testing.T) { + client := &Client{ + client: newMockClient(autoRemoveValidator(tc.expectedAutoRemove)), + version: tc.version, + } + _, err := client.ContainerCreate(context.Background(), nil, &container.HostConfig{AutoRemove: true}, nil, nil, "") + assert.NilError(t, err) + }) } } diff --git a/client/container_diff_test.go b/client/container_diff_test.go index bf80a3d163..e5220b4590 100644 --- a/client/container_diff_test.go +++ b/client/container_diff_test.go @@ -67,6 +67,6 @@ func TestContainerDiff(t *testing.T) { } changes, err := client.ContainerDiff(context.Background(), "container_id") - assert.Check(t, err) + assert.NilError(t, err) assert.Check(t, is.DeepEqual(changes, expected)) } diff --git a/client/container_exec_test.go b/client/container_exec_test.go index 045cf92702..8df69813b4 100644 --- a/client/container_exec_test.go +++ b/client/container_exec_test.go @@ -82,12 +82,8 @@ func TestContainerExecCreate(t *testing.T) { r, err := client.ContainerExecCreate(context.Background(), "container_id", container.ExecOptions{ User: "user", }) - if err != nil { - t.Fatal(err) - } - if r.ID != "exec_id" { - t.Fatalf("expected `exec_id`, got %s", r.ID) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(r.ID, "exec_id")) } func TestContainerExecStartError(t *testing.T) { @@ -127,9 +123,7 @@ func TestContainerExecStart(t *testing.T) { Detach: true, Tty: false, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } func TestContainerExecInspectError(t *testing.T) { @@ -162,13 +156,7 @@ func TestContainerExecInspect(t *testing.T) { } inspect, err := client.ContainerExecInspect(context.Background(), "exec_id") - if err != nil { - t.Fatal(err) - } - if inspect.ExecID != "exec_id" { - t.Fatalf("expected ExecID to be `exec_id`, got %s", inspect.ExecID) - } - if inspect.ContainerID != "container_id" { - t.Fatalf("expected ContainerID `container_id`, got %s", inspect.ContainerID) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(inspect.ExecID, "exec_id")) + assert.Check(t, is.Equal(inspect.ContainerID, "container_id")) } diff --git a/client/container_export_test.go b/client/container_export_test.go index 3001b5c678..20b207b10f 100644 --- a/client/container_export_test.go +++ b/client/container_export_test.go @@ -45,15 +45,9 @@ func TestContainerExport(t *testing.T) { }), } body, err := client.ContainerExport(context.Background(), "container_id") - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) defer body.Close() content, err := io.ReadAll(body) - if err != nil { - t.Fatal(err) - } - if string(content) != "response" { - t.Fatalf("expected response to contain 'response', got %s", string(content)) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(string(content), "response")) } diff --git a/client/container_inspect_test.go b/client/container_inspect_test.go index dbfd4641a8..ac6df75789 100644 --- a/client/container_inspect_test.go +++ b/client/container_inspect_test.go @@ -83,16 +83,8 @@ func TestContainerInspect(t *testing.T) { } r, err := client.ContainerInspect(context.Background(), "container_id") - if err != nil { - t.Fatal(err) - } - if r.ID != "container_id" { - t.Fatalf("expected `container_id`, got %s", r.ID) - } - if r.Image != "image" { - t.Fatalf("expected `image`, got %s", r.Image) - } - if r.Name != "name" { - t.Fatalf("expected `name`, got %s", r.Name) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(r.ID, "container_id")) + assert.Check(t, is.Equal(r.Image, "image")) + assert.Check(t, is.Equal(r.Name, "name")) } diff --git a/client/container_kill_test.go b/client/container_kill_test.go index 2a22dd78fd..3a8b261177 100644 --- a/client/container_kill_test.go +++ b/client/container_kill_test.go @@ -49,7 +49,5 @@ func TestContainerKill(t *testing.T) { } err := client.ContainerKill(context.Background(), "container_id", "SIGKILL") - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } diff --git a/client/container_list_test.go b/client/container_list_test.go index a6f404f06f..50e07d1a79 100644 --- a/client/container_list_test.go +++ b/client/container_list_test.go @@ -88,10 +88,6 @@ func TestContainerList(t *testing.T) { filters.Arg("before", "container"), ), }) - if err != nil { - t.Fatal(err) - } - if len(containers) != 2 { - t.Fatalf("expected 2 containers, got %v", containers) - } + assert.NilError(t, err) + assert.Check(t, is.Len(containers, 2)) } diff --git a/client/container_pause_test.go b/client/container_pause_test.go index 90418f0c30..03cb05d0e4 100644 --- a/client/container_pause_test.go +++ b/client/container_pause_test.go @@ -36,7 +36,5 @@ func TestContainerPause(t *testing.T) { }), } err := client.ContainerPause(context.Background(), "container_id") - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } diff --git a/client/container_prune_test.go b/client/container_prune_test.go index b6f3eddbf6..078960803f 100644 --- a/client/container_prune_test.go +++ b/client/container_prune_test.go @@ -109,7 +109,7 @@ func TestContainersPrune(t *testing.T) { } report, err := client.ContainersPrune(context.Background(), listCase.filters) - assert.Check(t, err) + assert.NilError(t, err) assert.Check(t, is.Len(report.ContainersDeleted, 2)) assert.Check(t, is.Equal(uint64(9999), report.SpaceReclaimed)) } diff --git a/client/container_remove_test.go b/client/container_remove_test.go index b11a98db85..88391ee6aa 100644 --- a/client/container_remove_test.go +++ b/client/container_remove_test.go @@ -71,5 +71,5 @@ func TestContainerRemove(t *testing.T) { RemoveVolumes: true, Force: true, }) - assert.Check(t, err) + assert.NilError(t, err) } diff --git a/client/container_rename_test.go b/client/container_rename_test.go index 27215a0b5e..1e39055336 100644 --- a/client/container_rename_test.go +++ b/client/container_rename_test.go @@ -49,7 +49,5 @@ func TestContainerRename(t *testing.T) { } err := client.ContainerRename(context.Background(), "container_id", "newName") - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } diff --git a/client/container_resize_test.go b/client/container_resize_test.go index 7874421c79..768d2dd8b0 100644 --- a/client/container_resize_test.go +++ b/client/container_resize_test.go @@ -77,7 +77,7 @@ func TestContainerResize(t *testing.T) { client: newMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)), } err := client.ContainerResize(context.Background(), "container_id", tc.opts) - assert.Check(t, err) + assert.NilError(t, err) }) } } @@ -120,7 +120,7 @@ func TestContainerExecResize(t *testing.T) { client: newMockClient(resizeTransport(t, expectedURL, tc.expectedHeight, tc.expectedWidth)), } err := client.ContainerExecResize(context.Background(), "exec_id", tc.opts) - assert.Check(t, err) + assert.NilError(t, err) }) } } diff --git a/client/container_restart_test.go b/client/container_restart_test.go index 6adc296139..b71b3c7cec 100644 --- a/client/container_restart_test.go +++ b/client/container_restart_test.go @@ -70,7 +70,5 @@ func TestContainerRestart(t *testing.T) { Signal: "SIGKILL", Timeout: &timeout, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } diff --git a/client/container_start_test.go b/client/container_start_test.go index d233c953c5..a94297de11 100644 --- a/client/container_start_test.go +++ b/client/container_start_test.go @@ -60,7 +60,5 @@ func TestContainerStart(t *testing.T) { } err := client.ContainerStart(context.Background(), "container_id", container.StartOptions{CheckpointID: "checkpoint_id"}) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } diff --git a/client/container_stats_test.go b/client/container_stats_test.go index f10cf63ec3..29cb6971d3 100644 --- a/client/container_stats_test.go +++ b/client/container_stats_test.go @@ -64,16 +64,10 @@ func TestContainerStats(t *testing.T) { }), } resp, err := client.ContainerStats(context.Background(), "container_id", c.stream) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) defer resp.Body.Close() content, err := io.ReadAll(resp.Body) - if err != nil { - t.Fatal(err) - } - if string(content) != "response" { - t.Fatalf("expected response to contain 'response', got %s", string(content)) - } + assert.NilError(t, err) + assert.Check(t, is.Equal(string(content), "response")) } } diff --git a/client/container_stop_test.go b/client/container_stop_test.go index 98e4161a08..5c9de12f79 100644 --- a/client/container_stop_test.go +++ b/client/container_stop_test.go @@ -70,7 +70,5 @@ func TestContainerStop(t *testing.T) { Signal: "SIGKILL", Timeout: &timeout, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } diff --git a/client/container_top_test.go b/client/container_top_test.go index e6aef48886..26afe7e4e8 100644 --- a/client/container_top_test.go +++ b/client/container_top_test.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "net/http" - "reflect" "strings" "testing" @@ -71,13 +70,7 @@ func TestContainerTop(t *testing.T) { } processList, err := client.ContainerTop(context.Background(), "container_id", []string{"arg1", "arg2"}) - if err != nil { - t.Fatal(err) - } - if !reflect.DeepEqual(expectedProcesses, processList.Processes) { - t.Fatalf("Processes: expected %v, got %v", expectedProcesses, processList.Processes) - } - if !reflect.DeepEqual(expectedTitles, processList.Titles) { - t.Fatalf("Titles: expected %v, got %v", expectedTitles, processList.Titles) - } + assert.NilError(t, err) + assert.Check(t, is.DeepEqual(expectedProcesses, processList.Processes)) + assert.Check(t, is.DeepEqual(expectedTitles, processList.Titles)) } diff --git a/client/container_unpause_test.go b/client/container_unpause_test.go index 17ea5123bf..bca7d02500 100644 --- a/client/container_unpause_test.go +++ b/client/container_unpause_test.go @@ -44,7 +44,5 @@ func TestContainerUnpause(t *testing.T) { }), } err := client.ContainerUnpause(context.Background(), "container_id") - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } diff --git a/client/container_update_test.go b/client/container_update_test.go index 772b835201..4d8d297a31 100644 --- a/client/container_update_test.go +++ b/client/container_update_test.go @@ -61,7 +61,5 @@ func TestContainerUpdate(t *testing.T) { Name: "always", }, }) - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) } diff --git a/client/container_wait_test.go b/client/container_wait_test.go index d2d02cc9da..32e18ee2b0 100644 --- a/client/container_wait_test.go +++ b/client/container_wait_test.go @@ -74,11 +74,9 @@ func TestContainerWait(t *testing.T) { resultC, errC := client.ContainerWait(context.Background(), "container_id", "") select { case err := <-errC: - t.Fatal(err) + assert.NilError(t, err) case result := <-resultC: - if result.StatusCode != 15 { - t.Fatalf("expected a status code equal to '15', got %d", result.StatusCode) - } + assert.Check(t, is.Equal(result.StatusCode, int64(15))) } } @@ -101,9 +99,7 @@ func TestContainerWaitProxyInterrupt(t *testing.T) { resultC, errC := client.ContainerWait(context.Background(), "container_id", "") select { case err := <-errC: - if !strings.Contains(err.Error(), msg) { - t.Fatalf("Expected: %s, Actual: %s", msg, err.Error()) - } + assert.Check(t, is.ErrorContains(err, msg)) case result := <-resultC: t.Fatalf("Unexpected result: %v", result) } @@ -129,9 +125,7 @@ func TestContainerWaitProxyInterruptLong(t *testing.T) { select { case err := <-errC: // LimitReader limiting isn't exact, because of how the Readers do chunking. - if len(err.Error()) > containerWaitErrorMsgLimit*2 { - t.Fatalf("Expected error to be limited around %d, actual length: %d", containerWaitErrorMsgLimit, len(err.Error())) - } + assert.Check(t, len(err.Error()) <= containerWaitErrorMsgLimit*2, "Expected error to be limited around %d, actual length: %d", containerWaitErrorMsgLimit, len(err.Error())) case result := <-resultC: t.Fatalf("Unexpected result: %v", result) } @@ -164,9 +158,7 @@ func TestContainerWaitErrorHandling(t *testing.T) { resultC, errC := client.ContainerWait(ctx, "container_id", "") select { case err := <-errC: - if err.Error() != test.exp.Error() { - t.Fatalf("ContainerWait() errC = %v; want %v", err, test.exp) - } + assert.Check(t, is.Equal(err.Error(), test.exp.Error())) return case result := <-resultC: t.Fatalf("expected to not get a wait result, got %d", result.StatusCode)