github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/config_list_test.go (about)

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