github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/client/config_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  	"github.com/stretchr/testify/assert"
    16  	"golang.org/x/net/context"
    17  )
    18  
    19  func TestConfigListUnsupported(t *testing.T) {
    20  	client := &Client{
    21  		version: "1.29",
    22  		client:  &http.Client{},
    23  	}
    24  	_, err := client.ConfigList(context.Background(), types.ConfigListOptions{})
    25  	assert.EqualError(t, err, `"config list" requires API version 1.30, but the Docker daemon API version is 1.29`)
    26  }
    27  
    28  func TestConfigListError(t *testing.T) {
    29  	client := &Client{
    30  		version: "1.30",
    31  		client:  newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    32  	}
    33  
    34  	_, err := client.ConfigList(context.Background(), types.ConfigListOptions{})
    35  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    36  		t.Fatalf("expected a Server Error, got %v", err)
    37  	}
    38  }
    39  
    40  func TestConfigList(t *testing.T) {
    41  	expectedURL := "/v1.30/configs"
    42  
    43  	filters := filters.NewArgs()
    44  	filters.Add("label", "label1")
    45  	filters.Add("label", "label2")
    46  
    47  	listCases := []struct {
    48  		options             types.ConfigListOptions
    49  		expectedQueryParams map[string]string
    50  	}{
    51  		{
    52  			options: types.ConfigListOptions{},
    53  			expectedQueryParams: map[string]string{
    54  				"filters": "",
    55  			},
    56  		},
    57  		{
    58  			options: types.ConfigListOptions{
    59  				Filters: filters,
    60  			},
    61  			expectedQueryParams: map[string]string{
    62  				"filters": `{"label":{"label1":true,"label2":true}}`,
    63  			},
    64  		},
    65  	}
    66  	for _, listCase := range listCases {
    67  		client := &Client{
    68  			version: "1.30",
    69  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    70  				if !strings.HasPrefix(req.URL.Path, expectedURL) {
    71  					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    72  				}
    73  				query := req.URL.Query()
    74  				for key, expected := range listCase.expectedQueryParams {
    75  					actual := query.Get(key)
    76  					if actual != expected {
    77  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
    78  					}
    79  				}
    80  				content, err := json.Marshal([]swarm.Config{
    81  					{
    82  						ID: "config_id1",
    83  					},
    84  					{
    85  						ID: "config_id2",
    86  					},
    87  				})
    88  				if err != nil {
    89  					return nil, err
    90  				}
    91  				return &http.Response{
    92  					StatusCode: http.StatusOK,
    93  					Body:       ioutil.NopCloser(bytes.NewReader(content)),
    94  				}, nil
    95  			}),
    96  		}
    97  
    98  		configs, err := client.ConfigList(context.Background(), listCase.options)
    99  		if err != nil {
   100  			t.Fatal(err)
   101  		}
   102  		if len(configs) != 2 {
   103  			t.Fatalf("expected 2 configs, got %v", configs)
   104  		}
   105  	}
   106  }