Browse Source

Merge pull request #8511 from coolljt0725/fix_tag

Fix tag an existed tag name of a repository
Michael Crosby 10 years ago
parent
commit
b90ab68f2a
3 changed files with 44 additions and 4 deletions
  1. 1 1
      builder/job.go
  2. 3 3
      graph/tags.go
  3. 40 0
      integration-cli/docker_cli_tag_test.go

+ 1 - 1
builder/job.go

@@ -124,7 +124,7 @@ func (b *BuilderJob) CmdBuild(job *engine.Job) engine.Status {
 	}
 
 	if repoName != "" {
-		b.Daemon.Repositories().Set(repoName, tag, id, false)
+		b.Daemon.Repositories().Set(repoName, tag, id, true)
 	}
 	return engine.StatusOK
 }

+ 3 - 3
graph/tags.go

@@ -218,11 +218,11 @@ func (store *TagStore) Set(repoName, tag, imageName string, force bool) error {
 	var repo Repository
 	if r, exists := store.Repositories[repoName]; exists {
 		repo = r
+		if old, exists := store.Repositories[repoName][tag]; exists && !force {
+			return fmt.Errorf("Conflict: Tag %s is already set to image %s, if you want to replace it, please use -f option", tag, old)
+		}
 	} else {
 		repo = make(map[string]string)
-		if old, exists := store.Repositories[repoName]; exists && !force {
-			return fmt.Errorf("Conflict: Tag %s:%s is already set to %s", repoName, tag, old)
-		}
 		store.Repositories[repoName] = repo
 	}
 	repo[tag] = img.ID

+ 40 - 0
integration-cli/docker_cli_tag_test.go

@@ -3,6 +3,7 @@ package main
 import (
 	"fmt"
 	"os/exec"
+	"strings"
 	"testing"
 )
 
@@ -92,3 +93,42 @@ func TestTagValidPrefixedRepo(t *testing.T) {
 		logDone(logMessage)
 	}
 }
+
+// tag an image with an existed tag name without -f option should fail
+func TestTagExistedNameWithoutForce(t *testing.T) {
+	if err := pullImageIfNotExist("busybox:latest"); err != nil {
+		t.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
+	}
+
+	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test")
+	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
+		t.Fatal(out, err)
+	}
+	tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test")
+	out, _, err := runCommandWithOutput(tagCmd)
+	if err == nil || !strings.Contains(out, "Conflict: Tag test is already set to image") {
+		t.Fatal("tag busybox busybox:test should have failed,because busybox:test is existed")
+	}
+	deleteImages("busybox:test")
+
+	logDone("tag - busybox with an existed tag name without -f option --> must fail")
+}
+
+// tag an image with an existed tag name with -f option should work
+func TestTagExistedNameWithForce(t *testing.T) {
+	if err := pullImageIfNotExist("busybox:latest"); err != nil {
+		t.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
+	}
+
+	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test")
+	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
+		t.Fatal(out, err)
+	}
+	tagCmd = exec.Command(dockerBinary, "tag", "-f", "busybox:latest", "busybox:test")
+	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
+		t.Fatal(out, err)
+	}
+	deleteImages("busybox:test")
+
+	logDone("tag - busybox with an existed tag name with -f option work")
+}