github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/service_inspect_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/ioutil" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/api/types/swarm" 15 "github.com/pkg/errors" 16 ) 17 18 func TestServiceInspectError(t *testing.T) { 19 client := &Client{ 20 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 21 } 22 23 _, _, err := client.ServiceInspectWithRaw(context.Background(), "nothing", types.ServiceInspectOptions{}) 24 if err == nil || err.Error() != "Error response from daemon: Server error" { 25 t.Fatalf("expected a Server Error, got %v", err) 26 } 27 } 28 29 func TestServiceInspectServiceNotFound(t *testing.T) { 30 client := &Client{ 31 client: newMockClient(errorMock(http.StatusNotFound, "Server error")), 32 } 33 34 _, _, err := client.ServiceInspectWithRaw(context.Background(), "unknown", types.ServiceInspectOptions{}) 35 if err == nil || !IsErrNotFound(err) { 36 t.Fatalf("expected a serviceNotFoundError error, got %v", err) 37 } 38 } 39 40 func TestServiceInspectWithEmptyID(t *testing.T) { 41 client := &Client{ 42 client: newMockClient(func(req *http.Request) (*http.Response, error) { 43 return nil, errors.New("should not make request") 44 }), 45 } 46 _, _, err := client.ServiceInspectWithRaw(context.Background(), "", types.ServiceInspectOptions{}) 47 if !IsErrNotFound(err) { 48 t.Fatalf("Expected NotFoundError, got %v", err) 49 } 50 } 51 52 func TestServiceInspect(t *testing.T) { 53 expectedURL := "/services/service_id" 54 client := &Client{ 55 client: newMockClient(func(req *http.Request) (*http.Response, error) { 56 if !strings.HasPrefix(req.URL.Path, expectedURL) { 57 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 58 } 59 content, err := json.Marshal(swarm.Service{ 60 ID: "service_id", 61 }) 62 if err != nil { 63 return nil, err 64 } 65 return &http.Response{ 66 StatusCode: http.StatusOK, 67 Body: ioutil.NopCloser(bytes.NewReader(content)), 68 }, nil 69 }), 70 } 71 72 serviceInspect, _, err := client.ServiceInspectWithRaw(context.Background(), "service_id", types.ServiceInspectOptions{}) 73 if err != nil { 74 t.Fatal(err) 75 } 76 if serviceInspect.ID != "service_id" { 77 t.Fatalf("expected `service_id`, got %s", serviceInspect.ID) 78 } 79 }