github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/client/container_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/errdefs"
    16  )
    17  
    18  func TestContainerListError(t *testing.T) {
    19  	client := &Client{
    20  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    21  	}
    22  	_, err := client.ContainerList(context.Background(), types.ContainerListOptions{})
    23  	if !errdefs.IsSystem(err) {
    24  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    25  	}
    26  }
    27  
    28  func TestContainerList(t *testing.T) {
    29  	expectedURL := "/containers/json"
    30  	expectedFilters := `{"before":{"container":true},"label":{"label1":true,"label2":true}}`
    31  	client := &Client{
    32  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    33  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    34  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    35  			}
    36  			query := req.URL.Query()
    37  			all := query.Get("all")
    38  			if all != "1" {
    39  				return nil, fmt.Errorf("all not set in URL query properly. Expected '1', got %s", all)
    40  			}
    41  			limit := query.Get("limit")
    42  			if limit != "0" {
    43  				return nil, fmt.Errorf("limit should have not be present in query. Expected '0', got %s", limit)
    44  			}
    45  			since := query.Get("since")
    46  			if since != "container" {
    47  				return nil, fmt.Errorf("since not set in URL query properly. Expected 'container', got %s", since)
    48  			}
    49  			before := query.Get("before")
    50  			if before != "" {
    51  				return nil, fmt.Errorf("before should have not be present in query, go %s", before)
    52  			}
    53  			size := query.Get("size")
    54  			if size != "1" {
    55  				return nil, fmt.Errorf("size not set in URL query properly. Expected '1', got %s", size)
    56  			}
    57  			filters := query.Get("filters")
    58  			if filters != expectedFilters {
    59  				return nil, fmt.Errorf("expected filters incoherent '%v' with actual filters %v", expectedFilters, filters)
    60  			}
    61  
    62  			b, err := json.Marshal([]types.Container{
    63  				{
    64  					ID: "container_id1",
    65  				},
    66  				{
    67  					ID: "container_id2",
    68  				},
    69  			})
    70  			if err != nil {
    71  				return nil, err
    72  			}
    73  
    74  			return &http.Response{
    75  				StatusCode: http.StatusOK,
    76  				Body:       ioutil.NopCloser(bytes.NewReader(b)),
    77  			}, nil
    78  		}),
    79  	}
    80  
    81  	filters := filters.NewArgs()
    82  	filters.Add("label", "label1")
    83  	filters.Add("label", "label2")
    84  	filters.Add("before", "container")
    85  	containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{
    86  		Size:    true,
    87  		All:     true,
    88  		Since:   "container",
    89  		Filters: filters,
    90  	})
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	if len(containers) != 2 {
    95  		t.Fatalf("expected 2 containers, got %v", containers)
    96  	}
    97  }