1package client // import "github.com/docker/docker/client" 2 3import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "io/ioutil" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/errdefs" 15) 16 17func TestInfoServerError(t *testing.T) { 18 client := &Client{ 19 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 20 } 21 _, err := client.Info(context.Background()) 22 if !errdefs.IsSystem(err) { 23 t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) 24 } 25} 26 27func 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: ioutil.NopCloser(bytes.NewReader([]byte("invalid json"))), 33 }, nil 34 }), 35 } 36 _, err := client.Info(context.Background()) 37 if err == nil || !strings.Contains(err.Error(), "invalid character") { 38 t.Fatalf("expected a 'invalid character' error, got %v", err) 39 } 40} 41 42func TestInfo(t *testing.T) { 43 expectedURL := "/info" 44 client := &Client{ 45 client: newMockClient(func(req *http.Request) (*http.Response, error) { 46 if !strings.HasPrefix(req.URL.Path, expectedURL) { 47 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 48 } 49 info := &types.Info{ 50 ID: "daemonID", 51 Containers: 3, 52 } 53 b, err := json.Marshal(info) 54 if err != nil { 55 return nil, err 56 } 57 58 return &http.Response{ 59 StatusCode: http.StatusOK, 60 Body: ioutil.NopCloser(bytes.NewReader(b)), 61 }, nil 62 }), 63 } 64 65 info, err := client.Info(context.Background()) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 if info.ID != "daemonID" { 71 t.Fatalf("expected daemonID, got %s", info.ID) 72 } 73 74 if info.Containers != 3 { 75 t.Fatalf("expected 3 containers, got %d", info.Containers) 76 } 77} 78