github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/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 "gotest.tools/v3/assert" 16 is "gotest.tools/v3/assert/cmp" 17 ) 18 19 func TestServiceUpdateError(t *testing.T) { 20 client := &Client{ 21 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 22 } 23 24 _, err := client.ServiceUpdate(context.Background(), "service_id", swarm.Version{}, swarm.ServiceSpec{}, types.ServiceUpdateOptions{}) 25 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 26 } 27 28 // TestServiceUpdateConnectionError verifies that connection errors occurring 29 // during API-version negotiation are not shadowed by API-version errors. 30 // 31 // Regression test for https://github.com/docker/cli/issues/4890 32 func TestServiceUpdateConnectionError(t *testing.T) { 33 client, err := NewClientWithOpts(WithAPIVersionNegotiation(), WithHost("tcp://no-such-host.invalid")) 34 assert.NilError(t, err) 35 36 _, err = client.ServiceUpdate(context.Background(), "service_id", swarm.Version{}, swarm.ServiceSpec{}, types.ServiceUpdateOptions{}) 37 assert.Check(t, is.ErrorType(err, IsErrConnectionFailed)) 38 } 39 40 func TestServiceUpdate(t *testing.T) { 41 expectedURL := "/services/service_id/update" 42 43 updateCases := []struct { 44 swarmVersion swarm.Version 45 expectedVersion string 46 }{ 47 { 48 expectedVersion: "0", 49 }, 50 { 51 swarmVersion: swarm.Version{ 52 Index: 0, 53 }, 54 expectedVersion: "0", 55 }, 56 { 57 swarmVersion: swarm.Version{ 58 Index: 10, 59 }, 60 expectedVersion: "10", 61 }, 62 } 63 64 for _, updateCase := range updateCases { 65 client := &Client{ 66 client: newMockClient(func(req *http.Request) (*http.Response, error) { 67 if !strings.HasPrefix(req.URL.Path, expectedURL) { 68 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 69 } 70 if req.Method != http.MethodPost { 71 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 72 } 73 version := req.URL.Query().Get("version") 74 if version != updateCase.expectedVersion { 75 return nil, fmt.Errorf("version not set in URL query properly, expected '%s', got %s", updateCase.expectedVersion, version) 76 } 77 return &http.Response{ 78 StatusCode: http.StatusOK, 79 Body: io.NopCloser(bytes.NewReader([]byte("{}"))), 80 }, nil 81 }), 82 } 83 84 _, err := client.ServiceUpdate(context.Background(), "service_id", updateCase.swarmVersion, swarm.ServiceSpec{}, types.ServiceUpdateOptions{}) 85 if err != nil { 86 t.Fatal(err) 87 } 88 } 89 }