github.com/olljanat/moby@v1.13.1/client/service_update_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"golang.org/x/net/context"
    12  
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/api/types/swarm"
    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 err == nil || err.Error() != "Error response from daemon: Server error" {
    24  		t.Fatalf("expected a Server Error, got %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 != "POST" {
    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:       ioutil.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  }