client/system_test: Use functional option to create mock client

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski
2025-08-29 15:17:23 +02:00
parent 407af72993
commit a1e304f76c
3 changed files with 108 additions and 117 deletions

View File

@@ -17,39 +17,37 @@ import (
)
func TestDiskUsageError(t *testing.T) {
client := &Client{
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
}
_, err := client.DiskUsage(context.Background(), DiskUsageOptions{})
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.DiskUsage(context.Background(), DiskUsageOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
func TestDiskUsage(t *testing.T) {
expectedURL := "/system/df"
client := &Client{
client: newMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if !strings.HasPrefix(req.URL.Path, expectedURL) {
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
}
du := system.DiskUsage{
LayersSize: int64(100),
Images: nil,
Containers: nil,
Volumes: nil,
}
du := system.DiskUsage{
LayersSize: int64(100),
Images: nil,
Containers: nil,
Volumes: nil,
}
b, err := json.Marshal(du)
if err != nil {
return nil, err
}
b, err := json.Marshal(du)
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}),
}
_, err := client.DiskUsage(context.Background(), DiskUsageOptions{})
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(b)),
}, nil
}))
assert.NilError(t, err)
_, err = client.DiskUsage(context.Background(), DiskUsageOptions{})
assert.NilError(t, err)
}