Browse code

client/volume_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:23
Showing 5 changed files
... ...
@@ -17,41 +17,39 @@ import (
17 17
 )
18 18
 
19 19
 func TestVolumeCreateError(t *testing.T) {
20
-	client := &Client{
21
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
22
-	}
20
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
21
+	assert.NilError(t, err)
23 22
 
24
-	_, err := client.VolumeCreate(context.Background(), volume.CreateOptions{})
23
+	_, err = client.VolumeCreate(context.Background(), volume.CreateOptions{})
25 24
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
26 25
 }
27 26
 
28 27
 func TestVolumeCreate(t *testing.T) {
29 28
 	expectedURL := "/volumes/create"
30 29
 
31
-	client := &Client{
32
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
33
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
34
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
35
-			}
30
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
31
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
32
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
33
+		}
36 34
 
37
-			if req.Method != http.MethodPost {
38
-				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
39
-			}
35
+		if req.Method != http.MethodPost {
36
+			return nil, fmt.Errorf("expected POST method, got %s", req.Method)
37
+		}
40 38
 
41
-			content, err := json.Marshal(volume.Volume{
42
-				Name:       "volume",
43
-				Driver:     "local",
44
-				Mountpoint: "mountpoint",
45
-			})
46
-			if err != nil {
47
-				return nil, err
48
-			}
49
-			return &http.Response{
50
-				StatusCode: http.StatusOK,
51
-				Body:       io.NopCloser(bytes.NewReader(content)),
52
-			}, nil
53
-		}),
54
-	}
39
+		content, err := json.Marshal(volume.Volume{
40
+			Name:       "volume",
41
+			Driver:     "local",
42
+			Mountpoint: "mountpoint",
43
+		})
44
+		if err != nil {
45
+			return nil, err
46
+		}
47
+		return &http.Response{
48
+			StatusCode: http.StatusOK,
49
+			Body:       io.NopCloser(bytes.NewReader(content)),
50
+		}, nil
51
+	}))
52
+	assert.NilError(t, err)
55 53
 
56 54
 	vol, err := client.VolumeCreate(context.Background(), volume.CreateOptions{
57 55
 		Name:   "myvolume",
... ...
@@ -18,30 +18,27 @@ import (
18 18
 )
19 19
 
20 20
 func TestVolumeInspectError(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.VolumeInspect(context.Background(), "nothing")
24
+	_, err = client.VolumeInspect(context.Background(), "nothing")
26 25
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
27 26
 }
28 27
 
29 28
 func TestVolumeInspectNotFound(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.VolumeInspect(context.Background(), "unknown")
32
+	_, err = client.VolumeInspect(context.Background(), "unknown")
35 33
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
36 34
 }
37 35
 
38 36
 func TestVolumeInspectWithEmptyID(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.VolumeInspectWithRaw(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.VolumeInspectWithRaw(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
 
... ...
@@ -58,24 +55,23 @@ func TestVolumeInspect(t *testing.T) {
58 58
 		Mountpoint: "mountpoint",
59 59
 	}
60 60
 
61
-	client := &Client{
62
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
63
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
64
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
65
-			}
66
-			if req.Method != http.MethodGet {
67
-				return nil, fmt.Errorf("expected GET method, got %s", req.Method)
68
-			}
69
-			content, err := json.Marshal(expected)
70
-			if err != nil {
71
-				return nil, err
72
-			}
73
-			return &http.Response{
74
-				StatusCode: http.StatusOK,
75
-				Body:       io.NopCloser(bytes.NewReader(content)),
76
-			}, nil
77
-		}),
78
-	}
61
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
62
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
63
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
64
+		}
65
+		if req.Method != http.MethodGet {
66
+			return nil, fmt.Errorf("expected GET method, got %s", req.Method)
67
+		}
68
+		content, err := json.Marshal(expected)
69
+		if err != nil {
70
+			return nil, err
71
+		}
72
+		return &http.Response{
73
+			StatusCode: http.StatusOK,
74
+			Body:       io.NopCloser(bytes.NewReader(content)),
75
+		}, nil
76
+	}))
77
+	assert.NilError(t, err)
79 78
 
80 79
 	vol, err := client.VolumeInspect(context.Background(), "volume_id")
81 80
 	assert.NilError(t, err)
... ...
@@ -18,11 +18,10 @@ import (
18 18
 )
19 19
 
20 20
 func TestVolumeListError(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.VolumeList(context.Background(), VolumeListOptions{})
24
+	_, err = client.VolumeList(context.Background(), VolumeListOptions{})
26 25
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
27 26
 }
28 27
 
... ...
@@ -52,33 +51,32 @@ func TestVolumeList(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
-				actualFilters := query.Get("filters")
62
-				if actualFilters != listCase.expectedFilters {
63
-					return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", listCase.expectedFilters, actualFilters)
64
-				}
65
-				content, err := json.Marshal(volume.ListResponse{
66
-					Volumes: []*volume.Volume{
67
-						{
68
-							Name:   "volume",
69
-							Driver: "local",
70
-						},
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
+			actualFilters := query.Get("filters")
61
+			if actualFilters != listCase.expectedFilters {
62
+				return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", listCase.expectedFilters, actualFilters)
63
+			}
64
+			content, err := json.Marshal(volume.ListResponse{
65
+				Volumes: []*volume.Volume{
66
+					{
67
+						Name:   "volume",
68
+						Driver: "local",
71 69
 					},
72
-				})
73
-				if err != nil {
74
-					return nil, err
75
-				}
76
-				return &http.Response{
77
-					StatusCode: http.StatusOK,
78
-					Body:       io.NopCloser(bytes.NewReader(content)),
79
-				}, nil
80
-			}),
81
-		}
70
+				},
71
+			})
72
+			if err != nil {
73
+				return nil, err
74
+			}
75
+			return &http.Response{
76
+				StatusCode: http.StatusOK,
77
+				Body:       io.NopCloser(bytes.NewReader(content)),
78
+			}, nil
79
+		}))
80
+		assert.NilError(t, err)
82 81
 
83 82
 		volumeResponse, err := client.VolumeList(context.Background(), VolumeListOptions{Filters: listCase.filters})
84 83
 		assert.NilError(t, err)
... ...
@@ -15,11 +15,10 @@ import (
15 15
 )
16 16
 
17 17
 func TestVolumeRemoveError(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.VolumeRemove(context.Background(), "volume_id", false)
21
+	err = client.VolumeRemove(context.Background(), "volume_id", false)
23 22
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
24 23
 
25 24
 	err = client.VolumeRemove(context.Background(), "", false)
... ...
@@ -46,21 +45,20 @@ func TestVolumeRemoveConnectionError(t *testing.T) {
46 46
 func TestVolumeRemove(t *testing.T) {
47 47
 	expectedURL := "/volumes/volume_id"
48 48
 
49
-	client := &Client{
50
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
51
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
52
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
53
-			}
54
-			if req.Method != http.MethodDelete {
55
-				return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
56
-			}
57
-			return &http.Response{
58
-				StatusCode: http.StatusOK,
59
-				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
60
-			}, nil
61
-		}),
62
-	}
49
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
50
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
51
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
52
+		}
53
+		if req.Method != http.MethodDelete {
54
+			return nil, fmt.Errorf("expected DELETE method, got %s", req.Method)
55
+		}
56
+		return &http.Response{
57
+			StatusCode: http.StatusOK,
58
+			Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
59
+		}, nil
60
+	}))
61
+	assert.NilError(t, err)
63 62
 
64
-	err := client.VolumeRemove(context.Background(), "volume_id", false)
63
+	err = client.VolumeRemove(context.Background(), "volume_id", false)
65 64
 	assert.NilError(t, err)
66 65
 }
... ...
@@ -17,11 +17,10 @@ import (
17 17
 )
18 18
 
19 19
 func TestVolumeUpdateError(t *testing.T) {
20
-	client := &Client{
21
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
22
-	}
20
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
21
+	assert.NilError(t, err)
23 22
 
24
-	err := client.VolumeUpdate(context.Background(), "volume", swarm.Version{}, volumetypes.UpdateOptions{})
23
+	err = client.VolumeUpdate(context.Background(), "volume", swarm.Version{}, volumetypes.UpdateOptions{})
25 24
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
26 25
 
27 26
 	err = client.VolumeUpdate(context.Background(), "", swarm.Version{}, volumetypes.UpdateOptions{})
... ...
@@ -37,24 +36,23 @@ func TestVolumeUpdate(t *testing.T) {
37 37
 	expectedURL := "/volumes/test1"
38 38
 	expectedVersion := "version=10"
39 39
 
40
-	client := &Client{
41
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
42
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
43
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
44
-			}
45
-			if req.Method != http.MethodPut {
46
-				return nil, fmt.Errorf("expected PUT method, got %s", req.Method)
47
-			}
48
-			if !strings.Contains(req.URL.RawQuery, expectedVersion) {
49
-				return nil, fmt.Errorf("expected query to contain '%s', got '%s'", expectedVersion, req.URL.RawQuery)
50
-			}
51
-			return &http.Response{
52
-				StatusCode: http.StatusOK,
53
-				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
54
-			}, nil
55
-		}),
56
-	}
57
-
58
-	err := client.VolumeUpdate(context.Background(), "test1", swarm.Version{Index: uint64(10)}, volumetypes.UpdateOptions{})
40
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
41
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
42
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
43
+		}
44
+		if req.Method != http.MethodPut {
45
+			return nil, fmt.Errorf("expected PUT method, got %s", req.Method)
46
+		}
47
+		if !strings.Contains(req.URL.RawQuery, expectedVersion) {
48
+			return nil, fmt.Errorf("expected query to contain '%s', got '%s'", expectedVersion, req.URL.RawQuery)
49
+		}
50
+		return &http.Response{
51
+			StatusCode: http.StatusOK,
52
+			Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
53
+		}, nil
54
+	}))
55
+	assert.NilError(t, err)
56
+
57
+	err = client.VolumeUpdate(context.Background(), "test1", swarm.Version{Index: uint64(10)}, volumetypes.UpdateOptions{})
59 58
 	assert.NilError(t, err)
60 59
 }