From 6047debf8540dd878041c8f68e70eeaf6a6e07f5 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Tue, 30 Sep 2014 16:57:17 +0800 Subject: [PATCH 1/2] Fix the bug of tag a existed tag name of a repository. Signed-off-by: Lei Jitang Upstream-commit: 5e6f16e34264fa81205c8becbdcd401823261056 Component: engine --- components/engine/builder/job.go | 2 +- components/engine/graph/tags.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/engine/builder/job.go b/components/engine/builder/job.go index 555232c9ae..ae501acb5d 100644 --- a/components/engine/builder/job.go +++ b/components/engine/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 } diff --git a/components/engine/graph/tags.go b/components/engine/graph/tags.go index 31c65ced5c..6e4e63148a 100644 --- a/components/engine/graph/tags.go +++ b/components/engine/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 From 621ba20f0eb3e098732fe7456573999123b99c82 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Mon, 20 Oct 2014 11:10:31 +0800 Subject: [PATCH 2/2] Add docker tag tests. Signed-off-by: Lei Jitang Upstream-commit: c496f24157afd81c9a26f5746175236485e97fa7 Component: engine --- .../integration-cli/docker_cli_tag_test.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/components/engine/integration-cli/docker_cli_tag_test.go b/components/engine/integration-cli/docker_cli_tag_test.go index 815416f208..00228c0963 100644 --- a/components/engine/integration-cli/docker_cli_tag_test.go +++ b/components/engine/integration-cli/docker_cli_tag_test.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os/exec" + "strings" "testing" ) @@ -88,3 +89,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") +}