github.com/rish1988/moby@v25.0.2+incompatible/client/image_search_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/registry"
    16  	"github.com/docker/docker/errdefs"
    17  	"gotest.tools/v3/assert"
    18  	is "gotest.tools/v3/assert/cmp"
    19  )
    20  
    21  func TestImageSearchAnyError(t *testing.T) {
    22  	client := &Client{
    23  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    24  	}
    25  	_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{})
    26  	assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
    27  }
    28  
    29  func TestImageSearchStatusUnauthorizedError(t *testing.T) {
    30  	client := &Client{
    31  		client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
    32  	}
    33  	_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{})
    34  	assert.Check(t, is.ErrorType(err, errdefs.IsUnauthorized))
    35  }
    36  
    37  func TestImageSearchWithUnauthorizedErrorAndPrivilegeFuncError(t *testing.T) {
    38  	client := &Client{
    39  		client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
    40  	}
    41  	privilegeFunc := func() (string, error) {
    42  		return "", fmt.Errorf("Error requesting privilege")
    43  	}
    44  	_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
    45  		PrivilegeFunc: privilegeFunc,
    46  	})
    47  	if err == nil || err.Error() != "Error requesting privilege" {
    48  		t.Fatalf("expected an error requesting privilege, got %v", err)
    49  	}
    50  }
    51  
    52  func TestImageSearchWithUnauthorizedErrorAndAnotherUnauthorizedError(t *testing.T) {
    53  	client := &Client{
    54  		client: newMockClient(errorMock(http.StatusUnauthorized, "Unauthorized error")),
    55  	}
    56  	privilegeFunc := func() (string, error) {
    57  		return "a-auth-header", nil
    58  	}
    59  	_, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
    60  		PrivilegeFunc: privilegeFunc,
    61  	})
    62  	assert.Check(t, is.ErrorType(err, errdefs.IsUnauthorized))
    63  }
    64  
    65  func TestImageSearchWithPrivilegedFuncNoError(t *testing.T) {
    66  	expectedURL := "/images/search"
    67  	client := &Client{
    68  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    69  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    70  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    71  			}
    72  			auth := req.Header.Get(registry.AuthHeader)
    73  			if auth == "NotValid" {
    74  				return &http.Response{
    75  					StatusCode: http.StatusUnauthorized,
    76  					Body:       io.NopCloser(bytes.NewReader([]byte("Invalid credentials"))),
    77  				}, nil
    78  			}
    79  			if auth != "IAmValid" {
    80  				return nil, fmt.Errorf("invalid auth header: expected 'IAmValid', got %s", auth)
    81  			}
    82  			query := req.URL.Query()
    83  			term := query.Get("term")
    84  			if term != "some-image" {
    85  				return nil, fmt.Errorf("term not set in URL query properly. Expected 'some-image', got %s", term)
    86  			}
    87  			content, err := json.Marshal([]registry.SearchResult{
    88  				{
    89  					Name: "anything",
    90  				},
    91  			})
    92  			if err != nil {
    93  				return nil, err
    94  			}
    95  			return &http.Response{
    96  				StatusCode: http.StatusOK,
    97  				Body:       io.NopCloser(bytes.NewReader(content)),
    98  			}, nil
    99  		}),
   100  	}
   101  	privilegeFunc := func() (string, error) {
   102  		return "IAmValid", nil
   103  	}
   104  	results, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
   105  		RegistryAuth:  "NotValid",
   106  		PrivilegeFunc: privilegeFunc,
   107  	})
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	if len(results) != 1 {
   112  		t.Fatalf("expected 1 result, got %v", results)
   113  	}
   114  }
   115  
   116  func TestImageSearchWithoutErrors(t *testing.T) {
   117  	const expectedURL = "/images/search"
   118  	const expectedFilters = `{"is-automated":{"true":true},"stars":{"3":true}}`
   119  
   120  	client := &Client{
   121  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
   122  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
   123  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
   124  			}
   125  			query := req.URL.Query()
   126  			term := query.Get("term")
   127  			if term != "some-image" {
   128  				return nil, fmt.Errorf("term not set in URL query properly. Expected 'some-image', got %s", term)
   129  			}
   130  			fltrs := query.Get("filters")
   131  			if fltrs != expectedFilters {
   132  				return nil, fmt.Errorf("filters not set in URL query properly. Expected '%s', got %s", expectedFilters, fltrs)
   133  			}
   134  			content, err := json.Marshal([]registry.SearchResult{
   135  				{
   136  					Name: "anything",
   137  				},
   138  			})
   139  			if err != nil {
   140  				return nil, err
   141  			}
   142  			return &http.Response{
   143  				StatusCode: http.StatusOK,
   144  				Body:       io.NopCloser(bytes.NewReader(content)),
   145  			}, nil
   146  		}),
   147  	}
   148  	results, err := client.ImageSearch(context.Background(), "some-image", types.ImageSearchOptions{
   149  		Filters: filters.NewArgs(
   150  			filters.Arg("is-automated", "true"),
   151  			filters.Arg("stars", "3"),
   152  		),
   153  	})
   154  	if err != nil {
   155  		t.Fatal(err)
   156  	}
   157  	if len(results) != 1 {
   158  		t.Fatalf("expected a result, got %v", results)
   159  	}
   160  }