github.com/lacework-dev/go-moby@v20.10.12+incompatible/client/task_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  )
    18  
    19  func TestTaskListError(t *testing.T) {
    20  	client := &Client{
    21  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    22  	}
    23  
    24  	_, err := client.TaskList(context.Background(), types.TaskListOptions{})
    25  	if !errdefs.IsSystem(err) {
    26  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    27  	}
    28  }
    29  
    30  func TestTaskList(t *testing.T) {
    31  	expectedURL := "/tasks"
    32  
    33  	filters := filters.NewArgs()
    34  	filters.Add("label", "label1")
    35  	filters.Add("label", "label2")
    36  
    37  	listCases := []struct {
    38  		options             types.TaskListOptions
    39  		expectedQueryParams map[string]string
    40  	}{
    41  		{
    42  			options: types.TaskListOptions{},
    43  			expectedQueryParams: map[string]string{
    44  				"filters": "",
    45  			},
    46  		},
    47  		{
    48  			options: types.TaskListOptions{
    49  				Filters: filters,
    50  			},
    51  			expectedQueryParams: map[string]string{
    52  				"filters": `{"label":{"label1":true,"label2":true}}`,
    53  			},
    54  		},
    55  	}
    56  	for _, listCase := range listCases {
    57  		client := &Client{
    58  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    59  				if !strings.HasPrefix(req.URL.Path, expectedURL) {
    60  					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    61  				}
    62  				query := req.URL.Query()
    63  				for key, expected := range listCase.expectedQueryParams {
    64  					actual := query.Get(key)
    65  					if actual != expected {
    66  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
    67  					}
    68  				}
    69  				content, err := json.Marshal([]swarm.Task{
    70  					{
    71  						ID: "task_id1",
    72  					},
    73  					{
    74  						ID: "task_id2",
    75  					},
    76  				})
    77  				if err != nil {
    78  					return nil, err
    79  				}
    80  				return &http.Response{
    81  					StatusCode: http.StatusOK,
    82  					Body:       ioutil.NopCloser(bytes.NewReader(content)),
    83  				}, nil
    84  			}),
    85  		}
    86  
    87  		tasks, err := client.TaskList(context.Background(), listCase.options)
    88  		if err != nil {
    89  			t.Fatal(err)
    90  		}
    91  		if len(tasks) != 2 {
    92  			t.Fatalf("expected 2 tasks, got %v", tasks)
    93  		}
    94  	}
    95  }