github.com/portworx/docker@v1.12.1/daemon/search_test.go (about)

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"golang.org/x/net/context"
     9  
    10  	"github.com/docker/docker/registry"
    11  	"github.com/docker/engine-api/types"
    12  	registrytypes "github.com/docker/engine-api/types/registry"
    13  )
    14  
    15  type FakeService struct {
    16  	registry.DefaultService
    17  
    18  	shouldReturnError bool
    19  
    20  	term    string
    21  	results []registrytypes.SearchResult
    22  }
    23  
    24  func (s *FakeService) Search(ctx context.Context, term string, limit int, authConfig *types.AuthConfig, userAgent string, headers map[string][]string) (*registrytypes.SearchResults, error) {
    25  	if s.shouldReturnError {
    26  		return nil, fmt.Errorf("Search unknown error")
    27  	}
    28  	return &registrytypes.SearchResults{
    29  		Query:      s.term,
    30  		NumResults: len(s.results),
    31  		Results:    s.results,
    32  	}, nil
    33  }
    34  
    35  func TestSearchRegistryForImagesErrors(t *testing.T) {
    36  	errorCases := []struct {
    37  		filtersArgs       string
    38  		shouldReturnError bool
    39  		expectedError     string
    40  	}{
    41  		{
    42  			expectedError:     "Search unknown error",
    43  			shouldReturnError: true,
    44  		},
    45  		{
    46  			filtersArgs:   "invalid json",
    47  			expectedError: "invalid character 'i' looking for beginning of value",
    48  		},
    49  		{
    50  			filtersArgs:   `{"type":{"custom":true}}`,
    51  			expectedError: "Invalid filter 'type'",
    52  		},
    53  		{
    54  			filtersArgs:   `{"is-automated":{"invalid":true}}`,
    55  			expectedError: "Invalid filter 'is-automated=[invalid]'",
    56  		},
    57  		{
    58  			filtersArgs:   `{"is-automated":{"true":true,"false":true}}`,
    59  			expectedError: "Invalid filter 'is-automated",
    60  		},
    61  		{
    62  			filtersArgs:   `{"is-official":{"invalid":true}}`,
    63  			expectedError: "Invalid filter 'is-official=[invalid]'",
    64  		},
    65  		{
    66  			filtersArgs:   `{"is-official":{"true":true,"false":true}}`,
    67  			expectedError: "Invalid filter 'is-official",
    68  		},
    69  		{
    70  			filtersArgs:   `{"stars":{"invalid":true}}`,
    71  			expectedError: "Invalid filter 'stars=invalid'",
    72  		},
    73  		{
    74  			filtersArgs:   `{"stars":{"1":true,"invalid":true}}`,
    75  			expectedError: "Invalid filter 'stars=invalid'",
    76  		},
    77  	}
    78  	for index, e := range errorCases {
    79  		daemon := &Daemon{
    80  			RegistryService: &FakeService{
    81  				shouldReturnError: e.shouldReturnError,
    82  			},
    83  		}
    84  		_, err := daemon.SearchRegistryForImages(context.Background(), e.filtersArgs, "term", 25, nil, map[string][]string{})
    85  		if err == nil {
    86  			t.Errorf("%d: expected an error, got nothing", index)
    87  		}
    88  		if !strings.Contains(err.Error(), e.expectedError) {
    89  			t.Errorf("%d: expected error to contain %s, got %s", index, e.expectedError, err.Error())
    90  		}
    91  	}
    92  }
    93  
    94  func TestSearchRegistryForImages(t *testing.T) {
    95  	term := "term"
    96  	successCases := []struct {
    97  		filtersArgs     string
    98  		registryResults []registrytypes.SearchResult
    99  		expectedResults []registrytypes.SearchResult
   100  	}{
   101  		{
   102  			filtersArgs:     "",
   103  			registryResults: []registrytypes.SearchResult{},
   104  			expectedResults: []registrytypes.SearchResult{},
   105  		},
   106  		{
   107  			filtersArgs: "",
   108  			registryResults: []registrytypes.SearchResult{
   109  				{
   110  					Name:        "name",
   111  					Description: "description",
   112  				},
   113  			},
   114  			expectedResults: []registrytypes.SearchResult{
   115  				{
   116  					Name:        "name",
   117  					Description: "description",
   118  				},
   119  			},
   120  		},
   121  		{
   122  			filtersArgs: `{"is-automated":{"true":true}}`,
   123  			registryResults: []registrytypes.SearchResult{
   124  				{
   125  					Name:        "name",
   126  					Description: "description",
   127  				},
   128  			},
   129  			expectedResults: []registrytypes.SearchResult{},
   130  		},
   131  		{
   132  			filtersArgs: `{"is-automated":{"true":true}}`,
   133  			registryResults: []registrytypes.SearchResult{
   134  				{
   135  					Name:        "name",
   136  					Description: "description",
   137  					IsAutomated: true,
   138  				},
   139  			},
   140  			expectedResults: []registrytypes.SearchResult{
   141  				{
   142  					Name:        "name",
   143  					Description: "description",
   144  					IsAutomated: true,
   145  				},
   146  			},
   147  		},
   148  		{
   149  			filtersArgs: `{"is-automated":{"false":true}}`,
   150  			registryResults: []registrytypes.SearchResult{
   151  				{
   152  					Name:        "name",
   153  					Description: "description",
   154  					IsAutomated: true,
   155  				},
   156  			},
   157  			expectedResults: []registrytypes.SearchResult{},
   158  		},
   159  		{
   160  			filtersArgs: `{"is-automated":{"false":true}}`,
   161  			registryResults: []registrytypes.SearchResult{
   162  				{
   163  					Name:        "name",
   164  					Description: "description",
   165  					IsAutomated: false,
   166  				},
   167  			},
   168  			expectedResults: []registrytypes.SearchResult{
   169  				{
   170  					Name:        "name",
   171  					Description: "description",
   172  					IsAutomated: false,
   173  				},
   174  			},
   175  		},
   176  		{
   177  			filtersArgs: `{"is-official":{"true":true}}`,
   178  			registryResults: []registrytypes.SearchResult{
   179  				{
   180  					Name:        "name",
   181  					Description: "description",
   182  				},
   183  			},
   184  			expectedResults: []registrytypes.SearchResult{},
   185  		},
   186  		{
   187  			filtersArgs: `{"is-official":{"true":true}}`,
   188  			registryResults: []registrytypes.SearchResult{
   189  				{
   190  					Name:        "name",
   191  					Description: "description",
   192  					IsOfficial:  true,
   193  				},
   194  			},
   195  			expectedResults: []registrytypes.SearchResult{
   196  				{
   197  					Name:        "name",
   198  					Description: "description",
   199  					IsOfficial:  true,
   200  				},
   201  			},
   202  		},
   203  		{
   204  			filtersArgs: `{"is-official":{"false":true}}`,
   205  			registryResults: []registrytypes.SearchResult{
   206  				{
   207  					Name:        "name",
   208  					Description: "description",
   209  					IsOfficial:  true,
   210  				},
   211  			},
   212  			expectedResults: []registrytypes.SearchResult{},
   213  		},
   214  		{
   215  			filtersArgs: `{"is-official":{"false":true}}`,
   216  			registryResults: []registrytypes.SearchResult{
   217  				{
   218  					Name:        "name",
   219  					Description: "description",
   220  					IsOfficial:  false,
   221  				},
   222  			},
   223  			expectedResults: []registrytypes.SearchResult{
   224  				{
   225  					Name:        "name",
   226  					Description: "description",
   227  					IsOfficial:  false,
   228  				},
   229  			},
   230  		},
   231  		{
   232  			filtersArgs: `{"stars":{"0":true}}`,
   233  			registryResults: []registrytypes.SearchResult{
   234  				{
   235  					Name:        "name",
   236  					Description: "description",
   237  					StarCount:   0,
   238  				},
   239  			},
   240  			expectedResults: []registrytypes.SearchResult{
   241  				{
   242  					Name:        "name",
   243  					Description: "description",
   244  					StarCount:   0,
   245  				},
   246  			},
   247  		},
   248  		{
   249  			filtersArgs: `{"stars":{"1":true}}`,
   250  			registryResults: []registrytypes.SearchResult{
   251  				{
   252  					Name:        "name",
   253  					Description: "description",
   254  					StarCount:   0,
   255  				},
   256  			},
   257  			expectedResults: []registrytypes.SearchResult{},
   258  		},
   259  		{
   260  			filtersArgs: `{"stars":{"1":true}}`,
   261  			registryResults: []registrytypes.SearchResult{
   262  				{
   263  					Name:        "name0",
   264  					Description: "description0",
   265  					StarCount:   0,
   266  				},
   267  				{
   268  					Name:        "name1",
   269  					Description: "description1",
   270  					StarCount:   1,
   271  				},
   272  			},
   273  			expectedResults: []registrytypes.SearchResult{
   274  				{
   275  					Name:        "name1",
   276  					Description: "description1",
   277  					StarCount:   1,
   278  				},
   279  			},
   280  		},
   281  		{
   282  			filtersArgs: `{"stars":{"1":true}, "is-official":{"true":true}, "is-automated":{"true":true}}`,
   283  			registryResults: []registrytypes.SearchResult{
   284  				{
   285  					Name:        "name0",
   286  					Description: "description0",
   287  					StarCount:   0,
   288  					IsOfficial:  true,
   289  					IsAutomated: true,
   290  				},
   291  				{
   292  					Name:        "name1",
   293  					Description: "description1",
   294  					StarCount:   1,
   295  					IsOfficial:  false,
   296  					IsAutomated: true,
   297  				},
   298  				{
   299  					Name:        "name2",
   300  					Description: "description2",
   301  					StarCount:   1,
   302  					IsOfficial:  true,
   303  					IsAutomated: false,
   304  				},
   305  				{
   306  					Name:        "name3",
   307  					Description: "description3",
   308  					StarCount:   2,
   309  					IsOfficial:  true,
   310  					IsAutomated: true,
   311  				},
   312  			},
   313  			expectedResults: []registrytypes.SearchResult{
   314  				{
   315  					Name:        "name3",
   316  					Description: "description3",
   317  					StarCount:   2,
   318  					IsOfficial:  true,
   319  					IsAutomated: true,
   320  				},
   321  			},
   322  		},
   323  	}
   324  	for index, s := range successCases {
   325  		daemon := &Daemon{
   326  			RegistryService: &FakeService{
   327  				term:    term,
   328  				results: s.registryResults,
   329  			},
   330  		}
   331  		results, err := daemon.SearchRegistryForImages(context.Background(), s.filtersArgs, term, 25, nil, map[string][]string{})
   332  		if err != nil {
   333  			t.Errorf("%d: %v", index, err)
   334  		}
   335  		if results.Query != term {
   336  			t.Errorf("%d: expected Query to be %s, got %s", index, term, results.Query)
   337  		}
   338  		if results.NumResults != len(s.expectedResults) {
   339  			t.Errorf("%d: expected NumResults to be %d, got %d", index, len(s.expectedResults), results.NumResults)
   340  		}
   341  		for _, result := range results.Results {
   342  			found := false
   343  			for _, expectedResult := range s.expectedResults {
   344  				if expectedResult.Name == result.Name &&
   345  					expectedResult.Description == result.Description &&
   346  					expectedResult.IsAutomated == result.IsAutomated &&
   347  					expectedResult.IsOfficial == result.IsOfficial &&
   348  					expectedResult.StarCount == result.StarCount {
   349  					found = true
   350  				}
   351  			}
   352  			if !found {
   353  				t.Errorf("%d: expected results %v, got %v", index, s.expectedResults, results.Results)
   354  			}
   355  		}
   356  	}
   357  }