github.com/skf/moby@v1.13.1/client/service_list_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     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/filters"
    14  	"github.com/docker/docker/api/types/swarm"
    15  	"golang.org/x/net/context"
    16  )
    17  
    18  func TestServiceListError(t *testing.T) {
    19  	client := &Client{
    20  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    21  	}
    22  
    23  	_, err := client.ServiceList(context.Background(), types.ServiceListOptions{})
    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 TestServiceList(t *testing.T) {
    30  	expectedURL := "/services"
    31  
    32  	filters := filters.NewArgs()
    33  	filters.Add("label", "label1")
    34  	filters.Add("label", "label2")
    35  
    36  	listCases := []struct {
    37  		options             types.ServiceListOptions
    38  		expectedQueryParams map[string]string
    39  	}{
    40  		{
    41  			options: types.ServiceListOptions{},
    42  			expectedQueryParams: map[string]string{
    43  				"filters": "",
    44  			},
    45  		},
    46  		{
    47  			options: types.ServiceListOptions{
    48  				Filters: filters,
    49  			},
    50  			expectedQueryParams: map[string]string{
    51  				"filters": `{"label":{"label1":true,"label2":true}}`,
    52  			},
    53  		},
    54  	}
    55  	for _, listCase := range listCases {
    56  		client := &Client{
    57  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    58  				if !strings.HasPrefix(req.URL.Path, expectedURL) {
    59  					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    60  				}
    61  				query := req.URL.Query()
    62  				for key, expected := range listCase.expectedQueryParams {
    63  					actual := query.Get(key)
    64  					if actual != expected {
    65  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
    66  					}
    67  				}
    68  				content, err := json.Marshal([]swarm.Service{
    69  					{
    70  						ID: "service_id1",
    71  					},
    72  					{
    73  						ID: "service_id2",
    74  					},
    75  				})
    76  				if err != nil {
    77  					return nil, err
    78  				}
    79  				return &http.Response{
    80  					StatusCode: http.StatusOK,
    81  					Body:       ioutil.NopCloser(bytes.NewReader(content)),
    82  				}, nil
    83  			}),
    84  		}
    85  
    86  		services, err := client.ServiceList(context.Background(), listCase.options)
    87  		if err != nil {
    88  			t.Fatal(err)
    89  		}
    90  		if len(services) != 2 {
    91  			t.Fatalf("expected 2 services, got %v", services)
    92  		}
    93  	}
    94  }