Procházet zdrojové kódy

Merge pull request #1943 from dotcloud/1940-prevent_crash_parseNat_build-fix

Abort build if mergeConfig returns an error and fix duplicate error message
Michael Crosby před 11 roky
rodič
revize
4196c704f0
4 změnil soubory, kde provedl 37 přidání a 6 odebrání
  1. 1 2
      api.go
  2. 3 1
      runtime.go
  3. 23 0
      runtime_test.go
  4. 10 3
      utils.go

+ 1 - 2
api.go

@@ -908,8 +908,7 @@ func postBuild(srv *Server, version float64, w http.ResponseWriter, r *http.Requ
 	b := NewBuildFile(srv, utils.NewWriteFlusher(w), !suppressOutput, !noCache, rm)
 	id, err := b.Build(context)
 	if err != nil {
-		fmt.Fprintf(w, "Error build: %s\n", err)
-		return err
+		return fmt.Errorf("Error build: %s", err)
 	}
 	if repoName != "" {
 		srv.runtime.repositories.Set(repoName, tag, id, false)

+ 3 - 1
runtime.go

@@ -283,7 +283,9 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) {
 	}
 
 	if img.Config != nil {
-		MergeConfig(config, img.Config)
+		if err := MergeConfig(config, img.Config); err != nil {
+			return nil, err
+		}
 	}
 
 	if len(config.Entrypoint) != 0 && config.Cmd == nil {

+ 23 - 0
runtime_test.go

@@ -205,6 +205,29 @@ func TestRuntimeCreate(t *testing.T) {
 	if err == nil {
 		t.Fatal("Builder.Create should throw an error when Cmd is empty")
 	}
+
+	config := &Config{
+		Image:     GetTestImage(runtime).ID,
+		Cmd:       []string{"/bin/ls"},
+		PortSpecs: []string{"80"},
+	}
+	container, err = runtime.Create(config)
+
+	image, err := runtime.Commit(container, "testrepo", "testtag", "", "", config)
+	if err != nil {
+		t.Error(err)
+	}
+
+	_, err = runtime.Create(
+		&Config{
+			Image:     image.ID,
+			PortSpecs: []string{"80000:80"},
+		},
+	)
+	if err == nil {
+		t.Fatal("Builder.Create should throw an error when PortSpecs is invalid")
+	}
+
 }
 
 func TestDestroy(t *testing.T) {

+ 10 - 3
utils.go

@@ -65,7 +65,7 @@ func CompareConfig(a, b *Config) bool {
 	return true
 }
 
-func MergeConfig(userConf, imageConf *Config) {
+func MergeConfig(userConf, imageConf *Config) error {
 	if userConf.User == "" {
 		userConf.User = imageConf.User
 	}
@@ -83,9 +83,15 @@ func MergeConfig(userConf, imageConf *Config) {
 	} else {
 		for _, imagePortSpec := range imageConf.PortSpecs {
 			found := false
-			imageNat, _ := parseNat(imagePortSpec)
+			imageNat, err := parseNat(imagePortSpec)
+			if err != nil {
+				return err
+			}
 			for _, userPortSpec := range userConf.PortSpecs {
-				userNat, _ := parseNat(userPortSpec)
+				userNat, err := parseNat(userPortSpec)
+				if err != nil {
+					return err
+				}
 				if imageNat.Proto == userNat.Proto && imageNat.Backend == userNat.Backend {
 					found = true
 				}
@@ -146,6 +152,7 @@ func MergeConfig(userConf, imageConf *Config) {
 			userConf.Volumes[k] = v
 		}
 	}
+	return nil
 }
 
 func parseLxcConfOpts(opts ListOpts) ([]KeyValuePair, error) {