github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/service_update_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types" 13 "github.com/docker/docker/api/types/swarm" 14 ) 15 16 func TestServiceUpdateError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 19 } 20 21 _, err := client.ServiceUpdate(context.Background(), "service_id", swarm.Version{}, swarm.ServiceSpec{}, types.ServiceUpdateOptions{}) 22 if err == nil || err.Error() != "Error response from daemon: Server error" { 23 t.Fatalf("expected a Server Error, got %v", err) 24 } 25 } 26 27 func TestServiceUpdate(t *testing.T) { 28 expectedURL := "/services/service_id/update" 29 30 updateCases := []struct { 31 swarmVersion swarm.Version 32 expectedVersion string 33 }{ 34 { 35 expectedVersion: "0", 36 }, 37 { 38 swarmVersion: swarm.Version{ 39 Index: 0, 40 }, 41 expectedVersion: "0", 42 }, 43 { 44 swarmVersion: swarm.Version{ 45 Index: 10, 46 }, 47 expectedVersion: "10", 48 }, 49 } 50 51 for _, updateCase := range updateCases { 52 client := &Client{ 53 client: newMockClient(func(req *http.Request) (*http.Response, error) { 54 if !strings.HasPrefix(req.URL.Path, expectedURL) { 55 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 56 } 57 if req.Method != "POST" { 58 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 59 } 60 version := req.URL.Query().Get("version") 61 if version != updateCase.expectedVersion { 62 return nil, fmt.Errorf("version not set in URL query properly, expected '%s', got %s", updateCase.expectedVersion, version) 63 } 64 return &http.Response{ 65 StatusCode: http.StatusOK, 66 Body: ioutil.NopCloser(bytes.NewReader([]byte("{}"))), 67 }, nil 68 }), 69 } 70 71 _, err := client.ServiceUpdate(context.Background(), "service_id", updateCase.swarmVersion, swarm.ServiceSpec{}, types.ServiceUpdateOptions{}) 72 if err != nil { 73 t.Fatal(err) 74 } 75 } 76 }