github.com/rish1988/moby@v25.0.2+incompatible/client/disk_usage_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "io" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/errdefs" 15 "gotest.tools/v3/assert" 16 is "gotest.tools/v3/assert/cmp" 17 ) 18 19 func TestDiskUsageError(t *testing.T) { 20 client := &Client{ 21 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 22 } 23 _, err := client.DiskUsage(context.Background(), types.DiskUsageOptions{}) 24 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 25 } 26 27 func TestDiskUsage(t *testing.T) { 28 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 } 34 35 du := types.DiskUsage{ 36 LayersSize: int64(100), 37 Images: nil, 38 Containers: nil, 39 Volumes: nil, 40 } 41 42 b, err := json.Marshal(du) 43 if err != nil { 44 return nil, err 45 } 46 47 return &http.Response{ 48 StatusCode: http.StatusOK, 49 Body: io.NopCloser(bytes.NewReader(b)), 50 }, nil 51 }), 52 } 53 if _, err := client.DiskUsage(context.Background(), types.DiskUsageOptions{}); err != nil { 54 t.Fatal(err) 55 } 56 }