Browse code

More informative error message on name collisions

This is the proposed fix for #2506. It provides a more complete message
with regards to name collisions including informing of the opposing
containers ID.

I have included a test to ensure that the correct short id is displayed
to make the message easier to understand.

Darren Coxall authored on 2013/11/11 06:50:34
Showing 3 changed files
... ...
@@ -44,6 +44,7 @@ Daniel Nordberg <dnordberg@gmail.com>
44 44
 Daniel Robinson <gottagetmac@gmail.com>
45 45
 Daniel Von Fange <daniel@leancoder.com>
46 46
 Daniel YC Lin <dlin.tw@gmail.com>
47
+Darren Coxall <darren@darrencoxall.com>
47 48
 David Calavera <david.calavera@gmail.com>
48 49
 David Sissitka <me@dsissitka.com>
49 50
 Deni Bertovic <deni@kset.org>
... ...
@@ -231,6 +231,18 @@ func TestRuntimeCreate(t *testing.T) {
231 231
 		t.Errorf("Exists() returned false for a newly created container")
232 232
 	}
233 233
 
234
+	// Test that conflict error displays correct details
235
+	testContainer, _, _ := runtime.Create(
236
+		&docker.Config{
237
+			Image: GetTestImage(runtime).ID,
238
+			Cmd:   []string{"ls", "-al"},
239
+		},
240
+		"conflictname",
241
+	)
242
+	if _, _, err := runtime.Create(&docker.Config{Image: GetTestImage(runtime).ID, Cmd: []string{"ls", "-al"}}, testContainer.Name); err == nil || !strings.Contains(err.Error(), utils.TruncateID(testContainer.ID)) {
243
+		t.Fatalf("Name conflict error doesn't include the correct short id. Message was: %s", err.Error())
244
+	}
245
+
234 246
 	// Make sure create with bad parameters returns an error
235 247
 	if _, _, err = runtime.Create(&docker.Config{Image: GetTestImage(runtime).ID}, ""); err == nil {
236 248
 		t.Fatal("Builder.Create should throw an error when Cmd is missing")
... ...
@@ -402,7 +402,8 @@ func (runtime *Runtime) Create(config *Config, name string) (*Container, []strin
402 402
 	// Set the enitity in the graph using the default name specified
403 403
 	if _, err := runtime.containerGraph.Set(name, id); err != nil {
404 404
 		if strings.HasSuffix(err.Error(), "name are not unique") {
405
-			return nil, nil, fmt.Errorf("Conflict, %s already exists.", name)
405
+			conflictingContainer, _ := runtime.GetByName(name)
406
+			return nil, nil, fmt.Errorf("Conflict, The name %s is already assigned to %s. You have to delete (or rename) that container to be able to assign %s to a container again.", name, utils.TruncateID(conflictingContainer.ID), name)
406 407
 		}
407 408
 		return nil, nil, err
408 409
 	}