Browse code

Fix docker search problem

Search terms shouldn't be restricted to only full valid repository
names. It should be perfectly valid to search using a part of a name,
even if it ends with a period, dash or underscore.

Signed-off-by: Hu Keping <hukeping@huawei.com>

Hu Keping authored on 2015/09/22 20:44:40
Showing 4 changed files
... ...
@@ -41,12 +41,13 @@ func (cli *DockerCli) CmdSearch(args ...string) error {
41 41
 
42 42
 	// Resolve the Repository name from fqn to hostname + name
43 43
 	taglessRemote, _ := parsers.ParseRepositoryTag(name)
44
-	repoInfo, err := registry.ParseRepositoryInfo(taglessRemote)
44
+
45
+	indexInfo, err := registry.ParseIndexInfo(taglessRemote)
45 46
 	if err != nil {
46 47
 		return err
47 48
 	}
48 49
 
49
-	rdr, _, err := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, repoInfo.Index, "search")
50
+	rdr, _, err := cli.clientRequestAttemptLogin("GET", "/images/search?"+v.Encode(), nil, nil, indexInfo, "search")
50 51
 	if err != nil {
51 52
 		return err
52 53
 	}
... ...
@@ -89,3 +89,11 @@ func (s *DockerSuite) TestSearchCmdOptions(c *check.C) {
89 89
 		c.Fatalf("failed to search with stars&automated&no-trunc options on the central registry: %s", out)
90 90
 	}
91 91
 }
92
+
93
+// search for repos which start with "ubuntu-" on the central registry
94
+func (s *DockerSuite) TestSearchOnCentralRegistryWithDash(c *check.C) {
95
+	testRequires(c, Network, DaemonIsLinux)
96
+
97
+	out, exitCode := dockerCmd(c, "search", "ubuntu-")
98
+	c.Assert(exitCode, check.Equals, 0, check.Commentf("failed to search on the central registry: %s", out))
99
+}
... ...
@@ -295,14 +295,17 @@ func splitReposName(reposName string) (string, string) {
295 295
 }
296 296
 
297 297
 // NewRepositoryInfo validates and breaks down a repository name into a RepositoryInfo
298
-func (config *ServiceConfig) NewRepositoryInfo(reposName string) (*RepositoryInfo, error) {
298
+func (config *ServiceConfig) NewRepositoryInfo(reposName string, bySearch bool) (*RepositoryInfo, error) {
299 299
 	if err := validateNoSchema(reposName); err != nil {
300 300
 		return nil, err
301 301
 	}
302 302
 
303 303
 	indexName, remoteName := splitReposName(reposName)
304
-	if err := validateRemoteName(remoteName); err != nil {
305
-		return nil, err
304
+
305
+	if !bySearch {
306
+		if err := validateRemoteName(remoteName); err != nil {
307
+			return nil, err
308
+		}
306 309
 	}
307 310
 
308 311
 	repoInfo := &RepositoryInfo{
... ...
@@ -354,7 +357,18 @@ func (repoInfo *RepositoryInfo) GetSearchTerm() string {
354 354
 // ParseRepositoryInfo performs the breakdown of a repository name into a RepositoryInfo, but
355 355
 // lacks registry configuration.
356 356
 func ParseRepositoryInfo(reposName string) (*RepositoryInfo, error) {
357
-	return emptyServiceConfig.NewRepositoryInfo(reposName)
357
+	return emptyServiceConfig.NewRepositoryInfo(reposName, false)
358
+}
359
+
360
+// ParseIndexInfo will use repository name to get back an indexInfo.
361
+func ParseIndexInfo(reposName string) (*IndexInfo, error) {
362
+	indexName, _ := splitReposName(reposName)
363
+
364
+	indexInfo, err := emptyServiceConfig.NewIndexInfo(indexName)
365
+	if err != nil {
366
+		return nil, err
367
+	}
368
+	return indexInfo, nil
358 369
 }
359 370
 
360 371
 // NormalizeLocalName transforms a repository name into a normalize LocalName
... ...
@@ -51,7 +51,8 @@ func (s *Service) Auth(authConfig *cliconfig.AuthConfig) (string, error) {
51 51
 // Search queries the public registry for images matching the specified
52 52
 // search terms, and returns the results.
53 53
 func (s *Service) Search(term string, authConfig *cliconfig.AuthConfig, headers map[string][]string) (*SearchResults, error) {
54
-	repoInfo, err := s.ResolveRepository(term)
54
+
55
+	repoInfo, err := s.ResolveRepositoryBySearch(term)
55 56
 	if err != nil {
56 57
 		return nil, err
57 58
 	}
... ...
@@ -71,7 +72,13 @@ func (s *Service) Search(term string, authConfig *cliconfig.AuthConfig, headers
71 71
 // ResolveRepository splits a repository name into its components
72 72
 // and configuration of the associated registry.
73 73
 func (s *Service) ResolveRepository(name string) (*RepositoryInfo, error) {
74
-	return s.Config.NewRepositoryInfo(name)
74
+	return s.Config.NewRepositoryInfo(name, false)
75
+}
76
+
77
+// ResolveRepositoryBySearch splits a repository name into its components
78
+// and configuration of the associated registry.
79
+func (s *Service) ResolveRepositoryBySearch(name string) (*RepositoryInfo, error) {
80
+	return s.Config.NewRepositoryInfo(name, true)
75 81
 }
76 82
 
77 83
 // ResolveIndex takes indexName and returns index info