github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/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  				All:       true,
    54  				MatchName: "image_name",
    55  			},
    56  			expectedQueryParams: map[string]string{
    57  				"all":     "1",
    58  				"filter":  "image_name",
    59  				"filters": "",
    60  			},
    61  		},
    62  		{
    63  			options: types.ImageListOptions{
    64  				Filters: filters,
    65  			},
    66  			expectedQueryParams: map[string]string{
    67  				"all":     "",
    68  				"filter":  "",
    69  				"filters": `{"dangling":{"true":true},"label":{"label1":true,"label2":true}}`,
    70  			},
    71  		},
    72  		{
    73  			options: types.ImageListOptions{
    74  				Filters: noDanglingfilters,
    75  			},
    76  			expectedQueryParams: map[string]string{
    77  				"all":     "",
    78  				"filter":  "",
    79  				"filters": `{"dangling":{"false":true}}`,
    80  			},
    81  		},
    82  	}
    83  	for _, listCase := range listCases {
    84  		client := &Client{
    85  			client: newMockClient(func(req *http.Request) (*http.Response, error) {
    86  				if !strings.HasPrefix(req.URL.Path, expectedURL) {
    87  					return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    88  				}
    89  				query := req.URL.Query()
    90  				for key, expected := range listCase.expectedQueryParams {
    91  					actual := query.Get(key)
    92  					if actual != expected {
    93  						return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
    94  					}
    95  				}
    96  				content, err := json.Marshal([]types.ImageSummary{
    97  					{
    98  						ID: "image_id2",
    99  					},
   100  					{
   101  						ID: "image_id2",
   102  					},
   103  				})
   104  				if err != nil {
   105  					return nil, err
   106  				}
   107  				return &http.Response{
   108  					StatusCode: http.StatusOK,
   109  					Body:       ioutil.NopCloser(bytes.NewReader(content)),
   110  				}, nil
   111  			}),
   112  		}
   113  
   114  		images, err := client.ImageList(context.Background(), listCase.options)
   115  		if err != nil {
   116  			t.Fatal(err)
   117  		}
   118  		if len(images) != 2 {
   119  			t.Fatalf("expected 2 images, got %v", images)
   120  		}
   121  	}
   122  }