github.com/brandon-bethke-neudesic/moby@v1.13.1/client/secret_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 TestSecretListError(t *testing.T) {
    19  	client := &Client{
    20  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    21  	}
    22  
    23  	_, err := client.SecretList(context.Background(), types.SecretListOptions{})
    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 TestSecretList(t *testing.T) {
    30  	expectedURL := "/secrets"
    31  
    32  	filters := filters.NewArgs()
    33  	filters.Add("label", "label1")
    34  	filters.Add("label", "label2")
    35  
    36  	listCases := []struct {
    37  		options             types.SecretListOptions
    38  		expectedQueryParams map[string]string
    39  	}{
    40  		{
    41  			options: types.SecretListOptions{},
    42  			expectedQueryParams: map[string]string{
    43  				"filters": "",
    44  			},
    45  		},
    46  		{
    47  			options: types.SecretListOptions{
    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.Secret{
    69  					{
    70  						ID: "secret_id1",
    71  					},
    72  					{
    73  						ID: "secret_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  		secrets, err := client.SecretList(context.Background(), listCase.options)
    87  		if err != nil {
    88  			t.Fatal(err)
    89  		}
    90  		if len(secrets) != 2 {
    91  			t.Fatalf("expected 2 secrets, got %v", secrets)
    92  		}
    93  	}
    94  }