瀏覽代碼

Merge pull request #12649 from jlhawn/fix_pull_err_explosion

Correctly format API error on image pull
Doug Davis 10 年之前
父節點
當前提交
5ea8dc376c
共有 2 個文件被更改,包括 25 次插入20 次删除
  1. 18 18
      api/server/server.go
  2. 7 2
      integration-cli/docker_cli_pull_test.go

+ 18 - 18
api/server/server.go

@@ -738,6 +738,15 @@ func (s *Server) postImagesCreate(eng *engine.Engine, version version.Version, w
 		}
 		}
 	}
 	}
 
 
+	var (
+		opErr   error
+		useJSON = version.GreaterThan("1.0")
+	)
+
+	if useJSON {
+		w.Header().Set("Content-Type", "application/json")
+	}
+
 	if image != "" { //pull
 	if image != "" { //pull
 		if tag == "" {
 		if tag == "" {
 			image, tag = parsers.ParseRepositoryTag(image)
 			image, tag = parsers.ParseRepositoryTag(image)
@@ -754,17 +763,10 @@ func (s *Server) postImagesCreate(eng *engine.Engine, version version.Version, w
 			MetaHeaders: metaHeaders,
 			MetaHeaders: metaHeaders,
 			AuthConfig:  authConfig,
 			AuthConfig:  authConfig,
 			OutStream:   utils.NewWriteFlusher(w),
 			OutStream:   utils.NewWriteFlusher(w),
-		}
-		if version.GreaterThan("1.0") {
-			imagePullConfig.Json = true
-			w.Header().Set("Content-Type", "application/json")
-		} else {
-			imagePullConfig.Json = false
+			Json:        useJSON,
 		}
 		}
 
 
-		if err := s.daemon.Repositories().Pull(image, tag, imagePullConfig); err != nil {
-			return err
-		}
+		opErr = s.daemon.Repositories().Pull(image, tag, imagePullConfig)
 	} else { //import
 	} else { //import
 		if tag == "" {
 		if tag == "" {
 			repo, tag = parsers.ParseRepositoryTag(repo)
 			repo, tag = parsers.ParseRepositoryTag(repo)
@@ -775,12 +777,7 @@ func (s *Server) postImagesCreate(eng *engine.Engine, version version.Version, w
 			Changes:   r.Form["changes"],
 			Changes:   r.Form["changes"],
 			InConfig:  r.Body,
 			InConfig:  r.Body,
 			OutStream: utils.NewWriteFlusher(w),
 			OutStream: utils.NewWriteFlusher(w),
-		}
-		if version.GreaterThan("1.0") {
-			imageImportConfig.Json = true
-			w.Header().Set("Content-Type", "application/json")
-		} else {
-			imageImportConfig.Json = false
+			Json:      useJSON,
 		}
 		}
 
 
 		newConfig, err := builder.BuildFromConfig(s.daemon, &runconfig.Config{}, imageImportConfig.Changes)
 		newConfig, err := builder.BuildFromConfig(s.daemon, &runconfig.Config{}, imageImportConfig.Changes)
@@ -789,9 +786,12 @@ func (s *Server) postImagesCreate(eng *engine.Engine, version version.Version, w
 		}
 		}
 		imageImportConfig.ContainerConfig = newConfig
 		imageImportConfig.ContainerConfig = newConfig
 
 
-		if err := s.daemon.Repositories().Import(src, repo, tag, imageImportConfig); err != nil {
-			return err
-		}
+		opErr = s.daemon.Repositories().Import(src, repo, tag, imageImportConfig)
+	}
+
+	if opErr != nil {
+		sf := streamformatter.NewStreamFormatter(useJSON)
+		return fmt.Errorf(string(sf.FormatError(opErr)))
 	}
 	}
 
 
 	return nil
 	return nil

+ 7 - 2
integration-cli/docker_cli_pull_test.go

@@ -98,8 +98,13 @@ func (s *DockerSuite) TestPullImageFromCentralRegistry(c *check.C) {
 
 
 // pulling a non-existing image from the central registry should return a non-zero exit code
 // pulling a non-existing image from the central registry should return a non-zero exit code
 func (s *DockerSuite) TestPullNonExistingImage(c *check.C) {
 func (s *DockerSuite) TestPullNonExistingImage(c *check.C) {
-	pullCmd := exec.Command(dockerBinary, "pull", "fooblahblah1234")
-	if out, _, err := runCommandWithOutput(pullCmd); err == nil {
+	testRequires(c, Network)
+
+	name := "sadfsadfasdf"
+	pullCmd := exec.Command(dockerBinary, "pull", name)
+	out, _, err := runCommandWithOutput(pullCmd)
+
+	if err == nil || !strings.Contains(out, fmt.Sprintf("Error: image library/%s:latest not found", name)) {
 		c.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
 		c.Fatalf("expected non-zero exit status when pulling non-existing image: %s", out)
 	}
 	}
 }
 }