Browse code

client: slightly improve ContainerDiff tests

- use gotest.tools for asserting
- check result returned

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2023/05/04 04:25:07
Showing 1 changed files
... ...
@@ -12,6 +12,8 @@ import (
12 12
 
13 13
 	"github.com/docker/docker/api/types/container"
14 14
 	"github.com/docker/docker/errdefs"
15
+	"gotest.tools/v3/assert"
16
+	is "gotest.tools/v3/assert/cmp"
15 17
 )
16 18
 
17 19
 func TestContainerDiffError(t *testing.T) {
... ...
@@ -19,28 +21,33 @@ func TestContainerDiffError(t *testing.T) {
19 19
 		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
20 20
 	}
21 21
 	_, err := client.ContainerDiff(context.Background(), "nothing")
22
-	if !errdefs.IsSystem(err) {
23
-		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
24
-	}
22
+	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
25 23
 }
26 24
 
27 25
 func TestContainerDiff(t *testing.T) {
28
-	expectedURL := "/containers/container_id/changes"
26
+	const expectedURL = "/containers/container_id/changes"
27
+
28
+	expected := []container.FilesystemChange{
29
+		{
30
+			Kind: container.ChangeModify,
31
+			Path: "/path/1",
32
+		},
33
+		{
34
+			Kind: container.ChangeAdd,
35
+			Path: "/path/2",
36
+		},
37
+		{
38
+			Kind: container.ChangeDelete,
39
+			Path: "/path/3",
40
+		},
41
+	}
42
+
29 43
 	client := &Client{
30 44
 		client: newMockClient(func(req *http.Request) (*http.Response, error) {
31 45
 			if !strings.HasPrefix(req.URL.Path, expectedURL) {
32
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
46
+				return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
33 47
 			}
34
-			b, err := json.Marshal([]container.FilesystemChange{
35
-				{
36
-					Kind: container.ChangeModify,
37
-					Path: "/path/1",
38
-				},
39
-				{
40
-					Kind: container.ChangeAdd,
41
-					Path: "/path/2",
42
-				},
43
-			})
48
+			b, err := json.Marshal(expected)
44 49
 			if err != nil {
45 50
 				return nil, err
46 51
 			}
... ...
@@ -52,10 +59,6 @@ func TestContainerDiff(t *testing.T) {
52 52
 	}
53 53
 
54 54
 	changes, err := client.ContainerDiff(context.Background(), "container_id")
55
-	if err != nil {
56
-		t.Fatal(err)
57
-	}
58
-	if len(changes) != 2 {
59
-		t.Fatalf("expected an array of 2 changes, got %v", changes)
60
-	}
55
+	assert.Check(t, err)
56
+	assert.Check(t, is.DeepEqual(changes, expected))
61 57
 }