github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/client/image_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  	"golang.org/x/net/context"
    15  )
    16  
    17  func TestImageListError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  
    22  	_, err := client.ImageList(context.Background(), types.ImageListOptions{})
    23  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    24  		t.Fatalf("expected a Server Error, got %v", err)
    25  	}
    26  }
    27  
    28  func TestImageList(t *testing.T) {
    29  	expectedURL := "/images/json"
    30  
    31  	noDanglingfilters := filters.NewArgs()
    32  	noDanglingfilters.Add("dangling", "false")
    33  
    34  	filters := filters.NewArgs()
    35  	filters.Add("label", "label1")
    36  	filters.Add("label", "label2")
    37  	filters.Add("dangling", "true")
    38  
    39  	listCases := []struct {
    40  		options             types.ImageListOptions
    41  		expectedQueryParams map[string]string
    42  	}{
    43  		{
    44  			options: types.ImageListOptions{},
    45  			expectedQueryParams: map[string]string{
    46  				"all":     "",
    47  				"filter":  "",
    48  				"filters": "",
    49  			},
    50  		},
    51  		{
    52  			options: types.ImageListOptions{
    53  				Filters: filters,
    54  			},
    55  			expectedQueryParams: map[string]string{
    56  				"all":     "",
    57  				"filter":  "",
    58  				"filters": `{"dangling":{"true":true},"label":{"label1":true,"label2":true}}`,
    59  			},
    60  		},
    61  		{
    62  			options: types.ImageListOptions{
    63  				Filters: noDanglingfilters,
    64  			},
    65  			expectedQueryParams: map[string]string{
    66  				"all":     "",
    67  				"filter":  "",
    68  				"filters": `{"dangling":{"false":true}}`,
    69  			},
    70  		},
    71  	}
    72  	for _, listCase := range listCases {
    73  		client := &Client{
    74  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    75  				if !strings.HasPrefix(req.URL.Path, expectedURL) {
    76  					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    77  				}
    78  				query := req.URL.Query()
    79  				for key, expected := range listCase.expectedQueryParams {
    80  					actual := query.Get(key)
    81  					if actual != expected {
    82  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
    83  					}
    84  				}
    85  				content, err := json.Marshal([]types.ImageSummary{
    86  					{
    87  						ID: "image_id2",
    88  					},
    89  					{
    90  						ID: "image_id2",
    91  					},
    92  				})
    93  				if err != nil {
    94  					return nil, err
    95  				}
    96  				return &http.Response{
    97  					StatusCode: http.StatusOK,
    98  					Body:       ioutil.NopCloser(bytes.NewReader(content)),
    99  				}, nil
   100  			}),
   101  		}
   102  
   103  		images, err := client.ImageList(context.Background(), listCase.options)
   104  		if err != nil {
   105  			t.Fatal(err)
   106  		}
   107  		if len(images) != 2 {
   108  			t.Fatalf("expected 2 images, got %v", images)
   109  		}
   110  	}
   111  }
   112  
   113  func TestImageListApiBefore125(t *testing.T) {
   114  	expectedFilter := "image:tag"
   115  	client := &Client{
   116  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
   117  			query := req.URL.Query()
   118  			actualFilter := query.Get("filter")
   119  			if actualFilter != expectedFilter {
   120  				return nil, fmt.Errorf("filter not set in URL query properly. Expected '%s', got %s", expectedFilter, actualFilter)
   121  			}
   122  			actualFilters := query.Get("filters")
   123  			if actualFilters != "" {
   124  				return nil, fmt.Errorf("filters should have not been present, were with value: %s", actualFilters)
   125  			}
   126  			content, err := json.Marshal([]types.ImageSummary{
   127  				{
   128  					ID: "image_id2",
   129  				},
   130  				{
   131  					ID: "image_id2",
   132  				},
   133  			})
   134  			if err != nil {
   135  				return nil, err
   136  			}
   137  			return &http.Response{
   138  				StatusCode: http.StatusOK,
   139  				Body:       ioutil.NopCloser(bytes.NewReader(content)),
   140  			}, nil
   141  		}),
   142  		version: "1.24",
   143  	}
   144  
   145  	filters := filters.NewArgs()
   146  	filters.Add("reference", "image:tag")
   147  
   148  	options := types.ImageListOptions{
   149  		Filters: filters,
   150  	}
   151  
   152  	images, err := client.ImageList(context.Background(), options)
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  	if len(images) != 2 {
   157  		t.Fatalf("expected 2 images, got %v", images)
   158  	}
   159  }