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