Signed-off-by: allencloud <allen.sun@daocloud.io>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,55 @@ |
| 0 |
+package client |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bytes" |
|
| 4 |
+ "encoding/json" |
|
| 5 |
+ "fmt" |
|
| 6 |
+ "io/ioutil" |
|
| 7 |
+ "net/http" |
|
| 8 |
+ "strings" |
|
| 9 |
+ "testing" |
|
| 10 |
+ |
|
| 11 |
+ "github.com/docker/docker/api/types" |
|
| 12 |
+ "golang.org/x/net/context" |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 15 |
+func TestDiskUsageError(t *testing.T) {
|
|
| 16 |
+ client := &Client{
|
|
| 17 |
+ client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), |
|
| 18 |
+ } |
|
| 19 |
+ _, err := client.DiskUsage(context.Background()) |
|
| 20 |
+ if err == nil || err.Error() != "Error response from daemon: Server error" {
|
|
| 21 |
+ t.Fatalf("expected a Server Error, got %v", err)
|
|
| 22 |
+ } |
|
| 23 |
+} |
|
| 24 |
+ |
|
| 25 |
+func TestDiskUsage(t *testing.T) {
|
|
| 26 |
+ expectedURL := "/system/df" |
|
| 27 |
+ client := &Client{
|
|
| 28 |
+ client: newMockClient(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 |
+ } |
|
| 32 |
+ |
|
| 33 |
+ du := types.DiskUsage{
|
|
| 34 |
+ LayersSize: int64(100), |
|
| 35 |
+ Images: nil, |
|
| 36 |
+ Containers: nil, |
|
| 37 |
+ Volumes: nil, |
|
| 38 |
+ } |
|
| 39 |
+ |
|
| 40 |
+ b, err := json.Marshal(du) |
|
| 41 |
+ if err != nil {
|
|
| 42 |
+ return nil, err |
|
| 43 |
+ } |
|
| 44 |
+ |
|
| 45 |
+ return &http.Response{
|
|
| 46 |
+ StatusCode: http.StatusOK, |
|
| 47 |
+ Body: ioutil.NopCloser(bytes.NewReader(b)), |
|
| 48 |
+ }, nil |
|
| 49 |
+ }), |
|
| 50 |
+ } |
|
| 51 |
+ if _, err := client.DiskUsage(context.Background()); err != nil {
|
|
| 52 |
+ t.Fatal(err) |
|
| 53 |
+ } |
|
| 54 |
+} |