github.com/moby/docker@v26.1.3+incompatible/client/node_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"
     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 TestNodeListError(t *testing.T) {
    22  	client := &Client{
    23  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    24  	}
    25  
    26  	_, err := client.NodeList(context.Background(), types.NodeListOptions{})
    27  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    28  }
    29  
    30  func TestNodeList(t *testing.T) {
    31  	const expectedURL = "/nodes"
    32  
    33  	listCases := []struct {
    34  		options             types.NodeListOptions
    35  		expectedQueryParams map[string]string
    36  	}{
    37  		{
    38  			options: types.NodeListOptions{},
    39  			expectedQueryParams: map[string]string{
    40  				"filters": "",
    41  			},
    42  		},
    43  		{
    44  			options: types.NodeListOptions{
    45  				Filters: filters.NewArgs(
    46  					filters.Arg("label", "label1"),
    47  					filters.Arg("label", "label2"),
    48  				),
    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.Node{
    69  					{
    70  						ID: "node_id1",
    71  					},
    72  					{
    73  						ID: "node_id2",
    74  					},
    75  				})
    76  				if err != nil {
    77  					return nil, err
    78  				}
    79  				return &http.Response{
    80  					StatusCode: http.StatusOK,
    81  					Body:       io.NopCloser(bytes.NewReader(content)),
    82  				}, nil
    83  			}),
    84  		}
    85  
    86  		nodes, err := client.NodeList(context.Background(), listCase.options)
    87  		if err != nil {
    88  			t.Fatal(err)
    89  		}
    90  		if len(nodes) != 2 {
    91  			t.Fatalf("expected 2 nodes, got %v", nodes)
    92  		}
    93  	}
    94  }