Browse code

client/config_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 5 changed files
... ...
@@ -17,28 +17,32 @@ import (
17 17
 )
18 18
 
19 19
 func TestConfigCreateUnsupported(t *testing.T) {
20
-	client := &Client{
21
-		version: "1.29",
22
-		client:  &http.Client{},
23
-	}
24
-	_, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
20
+	client, err := NewClientWithOpts(
21
+		WithVersion("1.29"),
22
+		WithHTTPClient(&http.Client{}),
23
+	)
24
+	assert.NilError(t, err)
25
+
26
+	_, err = client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
25 27
 	assert.Check(t, is.Error(err, `"config create" requires API version 1.30, but the Docker daemon API version is 1.29`))
26 28
 }
27 29
 
28 30
 func TestConfigCreateError(t *testing.T) {
29
-	client := &Client{
30
-		version: "1.30",
31
-		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
32
-	}
33
-	_, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
31
+	client, err := NewClientWithOpts(
32
+		WithVersion("1.30"),
33
+		WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
34
+	)
35
+	assert.NilError(t, err)
36
+
37
+	_, err = client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
34 38
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
35 39
 }
36 40
 
37 41
 func TestConfigCreate(t *testing.T) {
38 42
 	expectedURL := "/v1.30/configs/create"
39
-	client := &Client{
40
-		version: "1.30",
41
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
43
+	client, err := NewClientWithOpts(
44
+		WithVersion("1.30"),
45
+		WithMockClient(func(req *http.Request) (*http.Response, error) {
42 46
 			if !strings.HasPrefix(req.URL.Path, expectedURL) {
43 47
 				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
44 48
 			}
... ...
@@ -56,7 +60,8 @@ func TestConfigCreate(t *testing.T) {
56 56
 				Body:       io.NopCloser(bytes.NewReader(b)),
57 57
 			}, nil
58 58
 		}),
59
-	}
59
+	)
60
+	assert.NilError(t, err)
60 61
 
61 62
 	r, err := client.ConfigCreate(context.Background(), swarm.ConfigSpec{})
62 63
 	assert.NilError(t, err)
... ...
@@ -18,21 +18,23 @@ import (
18 18
 )
19 19
 
20 20
 func TestConfigInspectNotFound(t *testing.T) {
21
-	client := &Client{
22
-		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
23
-	}
21
+	client, err := NewClientWithOpts(
22
+		WithMockClient(errorMock(http.StatusNotFound, "Server error")),
23
+	)
24
+	assert.NilError(t, err)
24 25
 
25
-	_, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
26
+	_, _, err = client.ConfigInspectWithRaw(context.Background(), "unknown")
26 27
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
27 28
 }
28 29
 
29 30
 func TestConfigInspectWithEmptyID(t *testing.T) {
30
-	client := &Client{
31
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
31
+	client, err := NewClientWithOpts(
32
+		WithMockClient(func(req *http.Request) (*http.Response, error) {
32 33
 			return nil, errors.New("should not make request")
33 34
 		}),
34
-	}
35
-	_, _, err := client.ConfigInspectWithRaw(context.Background(), "")
35
+	)
36
+	assert.NilError(t, err)
37
+	_, _, err = client.ConfigInspectWithRaw(context.Background(), "")
36 38
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
37 39
 	assert.Check(t, is.ErrorContains(err, "value is empty"))
38 40
 
... ...
@@ -42,39 +44,43 @@ func TestConfigInspectWithEmptyID(t *testing.T) {
42 42
 }
43 43
 
44 44
 func TestConfigInspectUnsupported(t *testing.T) {
45
-	client := &Client{
46
-		version: "1.29",
47
-		client:  &http.Client{},
48
-	}
49
-	_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
45
+	client, err := NewClientWithOpts(
46
+		WithVersion("1.29"),
47
+		WithHTTPClient(&http.Client{}),
48
+	)
49
+	assert.NilError(t, err)
50
+
51
+	_, _, err = client.ConfigInspectWithRaw(context.Background(), "nothing")
50 52
 	assert.Check(t, is.Error(err, `"config inspect" requires API version 1.30, but the Docker daemon API version is 1.29`))
51 53
 }
52 54
 
53 55
 func TestConfigInspectError(t *testing.T) {
54
-	client := &Client{
55
-		version: "1.30",
56
-		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
57
-	}
56
+	client, err := NewClientWithOpts(
57
+		WithVersion("1.30"),
58
+		WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
59
+	)
60
+	assert.NilError(t, err)
58 61
 
59
-	_, _, err := client.ConfigInspectWithRaw(context.Background(), "nothing")
62
+	_, _, err = client.ConfigInspectWithRaw(context.Background(), "nothing")
60 63
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
61 64
 }
62 65
 
63 66
 func TestConfigInspectConfigNotFound(t *testing.T) {
64
-	client := &Client{
65
-		version: "1.30",
66
-		client:  newMockClient(errorMock(http.StatusNotFound, "Server error")),
67
-	}
67
+	client, err := NewClientWithOpts(
68
+		WithVersion("1.30"),
69
+		WithMockClient(errorMock(http.StatusNotFound, "Server error")),
70
+	)
71
+	assert.NilError(t, err)
68 72
 
69
-	_, _, err := client.ConfigInspectWithRaw(context.Background(), "unknown")
73
+	_, _, err = client.ConfigInspectWithRaw(context.Background(), "unknown")
70 74
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
71 75
 }
72 76
 
73 77
 func TestConfigInspect(t *testing.T) {
74 78
 	expectedURL := "/v1.30/configs/config_id"
75
-	client := &Client{
76
-		version: "1.30",
77
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
79
+	client, err := NewClientWithOpts(
80
+		WithVersion("1.30"),
81
+		WithMockClient(func(req *http.Request) (*http.Response, error) {
78 82
 			if !strings.HasPrefix(req.URL.Path, expectedURL) {
79 83
 				return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL)
80 84
 			}
... ...
@@ -89,7 +95,8 @@ func TestConfigInspect(t *testing.T) {
89 89
 				Body:       io.NopCloser(bytes.NewReader(content)),
90 90
 			}, nil
91 91
 		}),
92
-	}
92
+	)
93
+	assert.NilError(t, err)
93 94
 
94 95
 	configInspect, _, err := client.ConfigInspectWithRaw(context.Background(), "config_id")
95 96
 	assert.NilError(t, err)
... ...
@@ -18,21 +18,24 @@ import (
18 18
 )
19 19
 
20 20
 func TestConfigListUnsupported(t *testing.T) {
21
-	client := &Client{
22
-		version: "1.29",
23
-		client:  &http.Client{},
24
-	}
25
-	_, err := client.ConfigList(context.Background(), ConfigListOptions{})
21
+	client, err := NewClientWithOpts(
22
+		WithVersion("1.29"),
23
+		WithHTTPClient(&http.Client{}),
24
+	)
25
+	assert.NilError(t, err)
26
+
27
+	_, err = client.ConfigList(context.Background(), ConfigListOptions{})
26 28
 	assert.Check(t, is.Error(err, `"config list" requires API version 1.30, but the Docker daemon API version is 1.29`))
27 29
 }
28 30
 
29 31
 func TestConfigListError(t *testing.T) {
30
-	client := &Client{
31
-		version: "1.30",
32
-		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
33
-	}
32
+	client, err := NewClientWithOpts(
33
+		WithVersion("1.30"),
34
+		WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
35
+	)
36
+	assert.NilError(t, err)
34 37
 
35
-	_, err := client.ConfigList(context.Background(), ConfigListOptions{})
38
+	_, err = client.ConfigList(context.Background(), ConfigListOptions{})
36 39
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
37 40
 }
38 41
 
... ...
@@ -62,9 +65,9 @@ func TestConfigList(t *testing.T) {
62 62
 		},
63 63
 	}
64 64
 	for _, listCase := range listCases {
65
-		client := &Client{
66
-			version: "1.30",
67
-			client: newMockClient(func(req *http.Request) (*http.Response, error) {
65
+		client, err := NewClientWithOpts(
66
+			WithVersion("1.30"),
67
+			WithMockClient(func(req *http.Request) (*http.Response, error) {
68 68
 				if !strings.HasPrefix(req.URL.Path, expectedURL) {
69 69
 					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
70 70
 				}
... ...
@@ -91,7 +94,8 @@ func TestConfigList(t *testing.T) {
91 91
 					Body:       io.NopCloser(bytes.NewReader(content)),
92 92
 				}, nil
93 93
 			}),
94
-		}
94
+		)
95
+		assert.NilError(t, err)
95 96
 
96 97
 		configs, err := client.ConfigList(context.Background(), listCase.options)
97 98
 		assert.NilError(t, err)
... ...
@@ -15,21 +15,24 @@ import (
15 15
 )
16 16
 
17 17
 func TestConfigRemoveUnsupported(t *testing.T) {
18
-	client := &Client{
19
-		version: "1.29",
20
-		client:  &http.Client{},
21
-	}
22
-	err := client.ConfigRemove(context.Background(), "config_id")
18
+	client, err := NewClientWithOpts(
19
+		WithVersion("1.29"),
20
+		WithHTTPClient(&http.Client{}),
21
+	)
22
+	assert.NilError(t, err)
23
+
24
+	err = client.ConfigRemove(context.Background(), "config_id")
23 25
 	assert.Check(t, is.Error(err, `"config remove" requires API version 1.30, but the Docker daemon API version is 1.29`))
24 26
 }
25 27
 
26 28
 func TestConfigRemoveError(t *testing.T) {
27
-	client := &Client{
28
-		version: "1.30",
29
-		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
30
-	}
29
+	client, err := NewClientWithOpts(
30
+		WithVersion("1.30"),
31
+		WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
32
+	)
33
+	assert.NilError(t, err)
31 34
 
32
-	err := client.ConfigRemove(context.Background(), "config_id")
35
+	err = client.ConfigRemove(context.Background(), "config_id")
33 36
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
34 37
 
35 38
 	err = client.ConfigRemove(context.Background(), "")
... ...
@@ -44,9 +47,9 @@ func TestConfigRemoveError(t *testing.T) {
44 44
 func TestConfigRemove(t *testing.T) {
45 45
 	expectedURL := "/v1.30/configs/config_id"
46 46
 
47
-	client := &Client{
48
-		version: "1.30",
49
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
47
+	client, err := NewClientWithOpts(
48
+		WithVersion("1.30"),
49
+		WithMockClient(func(req *http.Request) (*http.Response, error) {
50 50
 			if !strings.HasPrefix(req.URL.Path, expectedURL) {
51 51
 				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
52 52
 			}
... ...
@@ -58,8 +61,9 @@ func TestConfigRemove(t *testing.T) {
58 58
 				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
59 59
 			}, nil
60 60
 		}),
61
-	}
61
+	)
62
+	assert.NilError(t, err)
62 63
 
63
-	err := client.ConfigRemove(context.Background(), "config_id")
64
+	err = client.ConfigRemove(context.Background(), "config_id")
64 65
 	assert.NilError(t, err)
65 66
 }
... ...
@@ -16,21 +16,24 @@ import (
16 16
 )
17 17
 
18 18
 func TestConfigUpdateUnsupported(t *testing.T) {
19
-	client := &Client{
20
-		version: "1.29",
21
-		client:  &http.Client{},
22
-	}
23
-	err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
19
+	client, err := NewClientWithOpts(
20
+		WithVersion("1.29"),
21
+		WithHTTPClient(&http.Client{}),
22
+	)
23
+	assert.NilError(t, err)
24
+
25
+	err = client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
24 26
 	assert.Check(t, is.Error(err, `"config update" requires API version 1.30, but the Docker daemon API version is 1.29`))
25 27
 }
26 28
 
27 29
 func TestConfigUpdateError(t *testing.T) {
28
-	client := &Client{
29
-		version: "1.30",
30
-		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
31
-	}
30
+	client, err := NewClientWithOpts(
31
+		WithVersion("1.30"),
32
+		WithMockClient(errorMock(http.StatusInternalServerError, "Server error")),
33
+	)
34
+	assert.NilError(t, err)
32 35
 
33
-	err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
36
+	err = client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
34 37
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
35 38
 
36 39
 	err = client.ConfigUpdate(context.Background(), "", swarm.Version{}, swarm.ConfigSpec{})
... ...
@@ -45,9 +48,9 @@ func TestConfigUpdateError(t *testing.T) {
45 45
 func TestConfigUpdate(t *testing.T) {
46 46
 	expectedURL := "/v1.30/configs/config_id/update"
47 47
 
48
-	client := &Client{
49
-		version: "1.30",
50
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
48
+	client, err := NewClientWithOpts(
49
+		WithVersion("1.30"),
50
+		WithMockClient(func(req *http.Request) (*http.Response, error) {
51 51
 			if !strings.HasPrefix(req.URL.Path, expectedURL) {
52 52
 				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
53 53
 			}
... ...
@@ -59,8 +62,9 @@ func TestConfigUpdate(t *testing.T) {
59 59
 				Body:       io.NopCloser(bytes.NewReader([]byte("body"))),
60 60
 			}, nil
61 61
 		}),
62
-	}
62
+	)
63
+	assert.NilError(t, err)
63 64
 
64
-	err := client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
65
+	err = client.ConfigUpdate(context.Background(), "config_id", swarm.Version{}, swarm.ConfigSpec{})
65 66
 	assert.NilError(t, err)
66 67
 }