Browse code

client/node_test: Use functional option to create mock client

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2025/08/29 22:17:22
Showing 4 changed files
... ...
@@ -18,30 +18,27 @@ import (
18 18
 )
19 19
 
20 20
 func TestNodeInspectError(t *testing.T) {
21
-	client := &Client{
22
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
23
-	}
21
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
22
+	assert.NilError(t, err)
24 23
 
25
-	_, _, err := client.NodeInspectWithRaw(context.Background(), "nothing")
24
+	_, _, err = client.NodeInspectWithRaw(context.Background(), "nothing")
26 25
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
27 26
 }
28 27
 
29 28
 func TestNodeInspectNodeNotFound(t *testing.T) {
30
-	client := &Client{
31
-		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
32
-	}
29
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusNotFound, "Server error")))
30
+	assert.NilError(t, err)
33 31
 
34
-	_, _, err := client.NodeInspectWithRaw(context.Background(), "unknown")
32
+	_, _, err = client.NodeInspectWithRaw(context.Background(), "unknown")
35 33
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
36 34
 }
37 35
 
38 36
 func TestNodeInspectWithEmptyID(t *testing.T) {
39
-	client := &Client{
40
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
41
-			return nil, errors.New("should not make request")
42
-		}),
43
-	}
44
-	_, _, err := client.NodeInspectWithRaw(context.Background(), "")
37
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
38
+		return nil, errors.New("should not make request")
39
+	}))
40
+	assert.NilError(t, err)
41
+	_, _, err = client.NodeInspectWithRaw(context.Background(), "")
45 42
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
46 43
 	assert.Check(t, is.ErrorContains(err, "value is empty"))
47 44
 
... ...
@@ -52,23 +49,22 @@ func TestNodeInspectWithEmptyID(t *testing.T) {
52 52
 
53 53
 func TestNodeInspect(t *testing.T) {
54 54
 	expectedURL := "/nodes/node_id"
55
-	client := &Client{
56
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
57
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
58
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
59
-			}
60
-			content, err := json.Marshal(swarm.Node{
61
-				ID: "node_id",
62
-			})
63
-			if err != nil {
64
-				return nil, err
65
-			}
66
-			return &http.Response{
67
-				StatusCode: http.StatusOK,
68
-				Body:       io.NopCloser(bytes.NewReader(content)),
69
-			}, nil
70
-		}),
71
-	}
55
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
56
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
57
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
58
+		}
59
+		content, err := json.Marshal(swarm.Node{
60
+			ID: "node_id",
61
+		})
62
+		if err != nil {
63
+			return nil, err
64
+		}
65
+		return &http.Response{
66
+			StatusCode: http.StatusOK,
67
+			Body:       io.NopCloser(bytes.NewReader(content)),
68
+		}, nil
69
+	}))
70
+	assert.NilError(t, err)
72 71
 
73 72
 	nodeInspect, _, err := client.NodeInspectWithRaw(context.Background(), "node_id")
74 73
 	assert.NilError(t, err)
... ...
@@ -18,11 +18,10 @@ import (
18 18
 )
19 19
 
20 20
 func TestNodeListError(t *testing.T) {
21
-	client := &Client{
22
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
23
-	}
21
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
22
+	assert.NilError(t, err)
24 23
 
25
-	_, err := client.NodeList(context.Background(), NodeListOptions{})
24
+	_, err = client.NodeList(context.Background(), NodeListOptions{})
26 25
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
27 26
 }
28 27
 
... ...
@@ -52,35 +51,34 @@ func TestNodeList(t *testing.T) {
52 52
 		},
53 53
 	}
54 54
 	for _, listCase := range listCases {
55
-		client := &Client{
56
-			client: newMockClient(func(req *http.Request) (*http.Response, error) {
57
-				if !strings.HasPrefix(req.URL.Path, expectedURL) {
58
-					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
59
-				}
60
-				query := req.URL.Query()
61
-				for key, expected := range listCase.expectedQueryParams {
62
-					actual := query.Get(key)
63
-					if actual != expected {
64
-						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
65
-					}
55
+		client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
56
+			if !strings.HasPrefix(req.URL.Path, expectedURL) {
57
+				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
58
+			}
59
+			query := req.URL.Query()
60
+			for key, expected := range listCase.expectedQueryParams {
61
+				actual := query.Get(key)
62
+				if actual != expected {
63
+					return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
66 64
 				}
67
-				content, err := json.Marshal([]swarm.Node{
68
-					{
69
-						ID: "node_id1",
70
-					},
71
-					{
72
-						ID: "node_id2",
73
-					},
74
-				})
75
-				if err != nil {
76
-					return nil, err
77
-				}
78
-				return &http.Response{
79
-					StatusCode: http.StatusOK,
80
-					Body:       io.NopCloser(bytes.NewReader(content)),
81
-				}, nil
82
-			}),
83
-		}
65
+			}
66
+			content, err := json.Marshal([]swarm.Node{
67
+				{
68
+					ID: "node_id1",
69
+				},
70
+				{
71
+					ID: "node_id2",
72
+				},
73
+			})
74
+			if err != nil {
75
+				return nil, err
76
+			}
77
+			return &http.Response{
78
+				StatusCode: http.StatusOK,
79
+				Body:       io.NopCloser(bytes.NewReader(content)),
80
+			}, nil
81
+		}))
82
+		assert.NilError(t, err)
84 83
 
85 84
 		nodes, err := client.NodeList(context.Background(), listCase.options)
86 85
 		assert.NilError(t, err)
... ...
@@ -15,11 +15,10 @@ import (
15 15
 )
16 16
 
17 17
 func TestNodeRemoveError(t *testing.T) {
18
-	client := &Client{
19
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
20
-	}
18
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
19
+	assert.NilError(t, err)
21 20
 
22
-	err := client.NodeRemove(context.Background(), "node_id", NodeRemoveOptions{Force: false})
21
+	err = client.NodeRemove(context.Background(), "node_id", NodeRemoveOptions{Force: false})
23 22
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
24 23
 
25 24
 	err = client.NodeRemove(context.Background(), "", NodeRemoveOptions{Force: false})
... ...
@@ -48,27 +47,26 @@ func TestNodeRemove(t *testing.T) {
48 48
 	}
49 49
 
50 50
 	for _, removeCase := range removeCases {
51
-		client := &Client{
52
-			client: newMockClient(func(req *http.Request) (*http.Response, error) {
53
-				if !strings.HasPrefix(req.URL.Path, expectedURL) {
54
-					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
55
-				}
56
-				if req.Method != http.MethodDelete {
57
-					return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
58
-				}
59
-				force := req.URL.Query().Get("force")
60
-				if force != removeCase.expectedForce {
61
-					return nil, fmt.Errorf("force not set in URL query properly. expected '%s', got %s", removeCase.expectedForce, force)
62
-				}
51
+		client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
52
+			if !strings.HasPrefix(req.URL.Path, expectedURL) {
53
+				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
54
+			}
55
+			if req.Method != http.MethodDelete {
56
+				return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
57
+			}
58
+			force := req.URL.Query().Get("force")
59
+			if force != removeCase.expectedForce {
60
+				return nil, fmt.Errorf("force not set in URL query properly. expected '%s', got %s", removeCase.expectedForce, force)
61
+			}
63 62
 
64
-				return &http.Response{
65
-					StatusCode: http.StatusOK,
66
-					Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
67
-				}, nil
68
-			}),
69
-		}
63
+			return &http.Response{
64
+				StatusCode: http.StatusOK,
65
+				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
66
+			}, nil
67
+		}))
68
+		assert.NilError(t, err)
70 69
 
71
-		err := client.NodeRemove(context.Background(), "node_id", NodeRemoveOptions{Force: removeCase.force})
70
+		err = client.NodeRemove(context.Background(), "node_id", NodeRemoveOptions{Force: removeCase.force})
72 71
 		assert.NilError(t, err)
73 72
 	}
74 73
 }
... ...
@@ -16,11 +16,10 @@ import (
16 16
 )
17 17
 
18 18
 func TestNodeUpdateError(t *testing.T) {
19
-	client := &Client{
20
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
21
-	}
19
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
20
+	assert.NilError(t, err)
22 21
 
23
-	err := client.NodeUpdate(context.Background(), "node_id", swarm.Version{}, swarm.NodeSpec{})
22
+	err = client.NodeUpdate(context.Background(), "node_id", swarm.Version{}, swarm.NodeSpec{})
24 23
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
25 24
 
26 25
 	err = client.NodeUpdate(context.Background(), "", swarm.Version{}, swarm.NodeSpec{})
... ...
@@ -35,21 +34,20 @@ func TestNodeUpdateError(t *testing.T) {
35 35
 func TestNodeUpdate(t *testing.T) {
36 36
 	expectedURL := "/nodes/node_id/update"
37 37
 
38
-	client := &Client{
39
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
40
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
41
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
42
-			}
43
-			if req.Method != http.MethodPost {
44
-				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
45
-			}
46
-			return &http.Response{
47
-				StatusCode: http.StatusOK,
48
-				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
49
-			}, nil
50
-		}),
51
-	}
52
-
53
-	err := client.NodeUpdate(context.Background(), "node_id", swarm.Version{}, swarm.NodeSpec{})
38
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
39
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
40
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
41
+		}
42
+		if req.Method != http.MethodPost {
43
+			return nil, fmt.Errorf("expected POST method, got %s", req.Method)
44
+		}
45
+		return &http.Response{
46
+			StatusCode: http.StatusOK,
47
+			Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
48
+		}, nil
49
+	}))
50
+	assert.NilError(t, err)
51
+
52
+	err = client.NodeUpdate(context.Background(), "node_id", swarm.Version{}, swarm.NodeSpec{})
54 53
 	assert.NilError(t, err)
55 54
 }