From 66cf0e3f5575cace57219607d042cc1c4cd536d2 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 3 May 2023 21:25:07 +0200 Subject: [PATCH] client: slightly improve ContainerDiff tests - use gotest.tools for asserting - check result returned Signed-off-by: Sebastiaan van Stijn --- client/container_diff_test.go | 45 +++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/client/container_diff_test.go b/client/container_diff_test.go index 2ee8738592..6fad8767b2 100644 --- a/client/container_diff_test.go +++ b/client/container_diff_test.go @@ -12,6 +12,8 @@ import ( "github.com/docker/docker/api/types/container" "github.com/docker/docker/errdefs" + "gotest.tools/v3/assert" + is "gotest.tools/v3/assert/cmp" ) func TestContainerDiffError(t *testing.T) { @@ -19,28 +21,33 @@ func TestContainerDiffError(t *testing.T) { client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), } _, err := client.ContainerDiff(context.Background(), "nothing") - if !errdefs.IsSystem(err) { - t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) - } + assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) } func TestContainerDiff(t *testing.T) { - expectedURL := "/containers/container_id/changes" + const expectedURL = "/containers/container_id/changes" + + expected := []container.FilesystemChange{ + { + Kind: container.ChangeModify, + Path: "/path/1", + }, + { + Kind: container.ChangeAdd, + Path: "/path/2", + }, + { + Kind: container.ChangeDelete, + Path: "/path/3", + }, + } + client := &Client{ client: newMockClient(func(req *http.Request) (*http.Response, error) { if !strings.HasPrefix(req.URL.Path, expectedURL) { - return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) + return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) } - b, err := json.Marshal([]container.FilesystemChange{ - { - Kind: container.ChangeModify, - Path: "/path/1", - }, - { - Kind: container.ChangeAdd, - Path: "/path/2", - }, - }) + b, err := json.Marshal(expected) if err != nil { return nil, err } @@ -52,10 +59,6 @@ func TestContainerDiff(t *testing.T) { } changes, err := client.ContainerDiff(context.Background(), "container_id") - if err != nil { - t.Fatal(err) - } - if len(changes) != 2 { - t.Fatalf("expected an array of 2 changes, got %v", changes) - } + assert.Check(t, err) + assert.Check(t, is.DeepEqual(changes, expected)) }