Browse code

client/system_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 3 changed files
... ...
@@ -17,39 +17,37 @@ import (
17 17
 )
18 18
 
19 19
 func TestDiskUsageError(t *testing.T) {
20
-	client := &Client{
21
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
22
-	}
23
-	_, err := client.DiskUsage(context.Background(), DiskUsageOptions{})
20
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
21
+	assert.NilError(t, err)
22
+	_, err = client.DiskUsage(context.Background(), DiskUsageOptions{})
24 23
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
25 24
 }
26 25
 
27 26
 func TestDiskUsage(t *testing.T) {
28 27
 	expectedURL := "/system/df"
29
-	client := &Client{
30
-		client: newMockClient(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
-			}
28
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
29
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
30
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
31
+		}
34 32
 
35
-			du := system.DiskUsage{
36
-				LayersSize: int64(100),
37
-				Images:     nil,
38
-				Containers: nil,
39
-				Volumes:    nil,
40
-			}
33
+		du := system.DiskUsage{
34
+			LayersSize: int64(100),
35
+			Images:     nil,
36
+			Containers: nil,
37
+			Volumes:    nil,
38
+		}
41 39
 
42
-			b, err := json.Marshal(du)
43
-			if err != nil {
44
-				return nil, err
45
-			}
40
+		b, err := json.Marshal(du)
41
+		if err != nil {
42
+			return nil, err
43
+		}
46 44
 
47
-			return &http.Response{
48
-				StatusCode: http.StatusOK,
49
-				Body:       io.NopCloser(bytes.NewReader(b)),
50
-			}, nil
51
-		}),
52
-	}
53
-	_, err := client.DiskUsage(context.Background(), DiskUsageOptions{})
45
+		return &http.Response{
46
+			StatusCode: http.StatusOK,
47
+			Body:       io.NopCloser(bytes.NewReader(b)),
48
+		}, nil
49
+	}))
50
+	assert.NilError(t, err)
51
+	_, err = client.DiskUsage(context.Background(), DiskUsageOptions{})
54 52
 	assert.NilError(t, err)
55 53
 }
... ...
@@ -37,21 +37,19 @@ func TestEventsErrorInOptions(t *testing.T) {
37 37
 		},
38 38
 	}
39 39
 	for _, tc := range errorCases {
40
-		client := &Client{
41
-			client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
42
-		}
40
+		client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
41
+		assert.NilError(t, err)
43 42
 		_, errs := client.Events(context.Background(), tc.options)
44
-		err := <-errs
43
+		err = <-errs
45 44
 		assert.Check(t, is.ErrorContains(err, tc.expectedError))
46 45
 	}
47 46
 }
48 47
 
49 48
 func TestEventsErrorFromServer(t *testing.T) {
50
-	client := &Client{
51
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
52
-	}
49
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
50
+	assert.NilError(t, err)
53 51
 	_, errs := client.Events(context.Background(), EventsListOptions{})
54
-	err := <-errs
52
+	err = <-errs
55 53
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
56 54
 }
57 55
 
... ...
@@ -110,33 +108,32 @@ func TestEvents(t *testing.T) {
110 110
 	}
111 111
 
112 112
 	for _, eventsCase := range eventsCases {
113
-		client := &Client{
114
-			client: newMockClient(func(req *http.Request) (*http.Response, error) {
115
-				if !strings.HasPrefix(req.URL.Path, expectedURL) {
116
-					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
117
-				}
118
-				query := req.URL.Query()
113
+		client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
114
+			if !strings.HasPrefix(req.URL.Path, expectedURL) {
115
+				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
116
+			}
117
+			query := req.URL.Query()
119 118
 
120
-				for key, expected := range eventsCase.expectedQueryParams {
121
-					actual := query.Get(key)
122
-					if actual != expected {
123
-						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
124
-					}
119
+			for key, expected := range eventsCase.expectedQueryParams {
120
+				actual := query.Get(key)
121
+				if actual != expected {
122
+					return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
125 123
 				}
124
+			}
126 125
 
127
-				buffer := new(bytes.Buffer)
126
+			buffer := new(bytes.Buffer)
128 127
 
129
-				for _, e := range eventsCase.events {
130
-					b, _ := json.Marshal(e)
131
-					buffer.Write(b)
132
-				}
128
+			for _, e := range eventsCase.events {
129
+				b, _ := json.Marshal(e)
130
+				buffer.Write(b)
131
+			}
133 132
 
134
-				return &http.Response{
135
-					StatusCode: http.StatusOK,
136
-					Body:       io.NopCloser(buffer),
137
-				}, nil
138
-			}),
139
-		}
133
+			return &http.Response{
134
+				StatusCode: http.StatusOK,
135
+				Body:       io.NopCloser(buffer),
136
+			}, nil
137
+		}))
138
+		assert.NilError(t, err)
140 139
 
141 140
 		messages, errs := client.Events(context.Background(), eventsCase.options)
142 141
 
... ...
@@ -17,48 +17,45 @@ import (
17 17
 )
18 18
 
19 19
 func TestInfoServerError(t *testing.T) {
20
-	client := &Client{
21
-		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
22
-	}
23
-	_, err := client.Info(context.Background())
20
+	client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
21
+	assert.NilError(t, err)
22
+	_, err = client.Info(context.Background())
24 23
 	assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
25 24
 }
26 25
 
27 26
 func TestInfoInvalidResponseJSONError(t *testing.T) {
28
-	client := &Client{
29
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
30
-			return &http.Response{
31
-				StatusCode: http.StatusOK,
32
-				Body:       io.NopCloser(bytes.NewReader([]byte("invalid json"))),
33
-			}, nil
34
-		}),
35
-	}
36
-	_, err := client.Info(context.Background())
27
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
28
+		return &http.Response{
29
+			StatusCode: http.StatusOK,
30
+			Body:       io.NopCloser(bytes.NewReader([]byte("invalid json"))),
31
+		}, nil
32
+	}))
33
+	assert.NilError(t, err)
34
+	_, err = client.Info(context.Background())
37 35
 	assert.Check(t, is.ErrorContains(err, "invalid character"))
38 36
 }
39 37
 
40 38
 func TestInfo(t *testing.T) {
41 39
 	expectedURL := "/info"
42
-	client := &Client{
43
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
44
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
45
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
46
-			}
47
-			info := &system.Info{
48
-				ID:         "daemonID",
49
-				Containers: 3,
50
-			}
51
-			b, err := json.Marshal(info)
52
-			if err != nil {
53
-				return nil, err
54
-			}
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
+		info := &system.Info{
45
+			ID:         "daemonID",
46
+			Containers: 3,
47
+		}
48
+		b, err := json.Marshal(info)
49
+		if err != nil {
50
+			return nil, err
51
+		}
55 52
 
56
-			return &http.Response{
57
-				StatusCode: http.StatusOK,
58
-				Body:       io.NopCloser(bytes.NewReader(b)),
59
-			}, nil
60
-		}),
61
-	}
53
+		return &http.Response{
54
+			StatusCode: http.StatusOK,
55
+			Body:       io.NopCloser(bytes.NewReader(b)),
56
+		}, nil
57
+	}))
58
+	assert.NilError(t, err)
62 59
 
63 60
 	info, err := client.Info(context.Background())
64 61
 	assert.NilError(t, err)
... ...
@@ -69,36 +66,35 @@ func TestInfo(t *testing.T) {
69 69
 
70 70
 func TestInfoWithDiscoveredDevices(t *testing.T) {
71 71
 	expectedURL := "/info"
72
-	client := &Client{
73
-		client: newMockClient(func(req *http.Request) (*http.Response, error) {
74
-			if !strings.HasPrefix(req.URL.Path, expectedURL) {
75
-				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
76
-			}
77
-			info := &system.Info{
78
-				ID:         "daemonID",
79
-				Containers: 3,
80
-				DiscoveredDevices: []system.DeviceInfo{
81
-					{
82
-						Source: "cdi",
83
-						ID:     "vendor.com/gpu=0",
84
-					},
85
-					{
86
-						Source: "cdi",
87
-						ID:     "vendor.com/gpu=1",
88
-					},
72
+	client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
73
+		if !strings.HasPrefix(req.URL.Path, expectedURL) {
74
+			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
75
+		}
76
+		info := &system.Info{
77
+			ID:         "daemonID",
78
+			Containers: 3,
79
+			DiscoveredDevices: []system.DeviceInfo{
80
+				{
81
+					Source: "cdi",
82
+					ID:     "vendor.com/gpu=0",
89 83
 				},
90
-			}
91
-			b, err := json.Marshal(info)
92
-			if err != nil {
93
-				return nil, err
94
-			}
84
+				{
85
+					Source: "cdi",
86
+					ID:     "vendor.com/gpu=1",
87
+				},
88
+			},
89
+		}
90
+		b, err := json.Marshal(info)
91
+		if err != nil {
92
+			return nil, err
93
+		}
95 94
 
96
-			return &http.Response{
97
-				StatusCode: http.StatusOK,
98
-				Body:       io.NopCloser(bytes.NewReader(b)),
99
-			}, nil
100
-		}),
101
-	}
95
+		return &http.Response{
96
+			StatusCode: http.StatusOK,
97
+			Body:       io.NopCloser(bytes.NewReader(b)),
98
+		}, nil
99
+	}))
100
+	assert.NilError(t, err)
102 101
 
103 102
 	info, err := client.Info(context.Background())
104 103
 	assert.NilError(t, err)