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