integration-cli/docker_cli_tag_test.go
6db32fde
 package main
 
 import (
4352da78
 	"fmt"
c496f241
 	"strings"
b80fae73
 
72269acf
 	"github.com/docker/docker/pkg/integration/checker"
4352da78
 	"github.com/docker/docker/pkg/stringid"
b80fae73
 	"github.com/docker/docker/pkg/stringutils"
dc944ea7
 	"github.com/go-check/check"
6db32fde
 )
 
 // tagging a named image in a new unprefixed repo should work
dc944ea7
 func (s *DockerSuite) TestTagUnprefixedRepoByName(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
dad47680
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
dc944ea7
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
6db32fde
 	}
 
eef6eda7
 	dockerCmd(c, "tag", "busybox:latest", "testfoobarbaz")
6db32fde
 }
 
 // tagging an image by ID in a new unprefixed repo should work
dc944ea7
 func (s *DockerSuite) TestTagUnprefixedRepoByID(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
74f8a4ec
 	imageID, err := inspectField("busybox", "Id")
 	c.Assert(err, check.IsNil)
eef6eda7
 	dockerCmd(c, "tag", imageID, "testfoobarbaz")
6db32fde
 }
 
46eb4140
 // ensure we don't allow the use of invalid repository names; these tag operations should fail
dc944ea7
 func (s *DockerSuite) TestTagInvalidUnprefixedRepo(c *check.C) {
46eb4140
 	invalidRepos := []string{"fo$z$", "Foo@3cc", "Foo$3", "Foo*3", "Fo^3", "Foo!3", "F)xcz(", "fo%asd"}
6db32fde
 
 	for _, repo := range invalidRepos {
72269acf
 		out, _, err := dockerCmdWithError("tag", "busybox", repo)
 		c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repo, out))
46eb4140
 	}
 }
 
 // ensure we don't allow the use of invalid tags; these tag operations should fail
dc944ea7
 func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) {
ae907e7a
 	longTag := stringutils.GenerateRandomAlphaOnlyString(121)
46eb4140
 
ae907e7a
 	invalidTags := []string{"repo:fo$z$", "repo:Foo@3cc", "repo:Foo$3", "repo:Foo*3", "repo:Fo^3", "repo:Foo!3", "repo:%goodbye", "repo:#hashtagit", "repo:F)xcz(", "repo:-foo", "repo:..", longTag}
46eb4140
 
 	for _, repotag := range invalidTags {
72269acf
 		out, _, err := dockerCmdWithError("tag", "busybox", repotag)
 		c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repotag, out))
6db32fde
 	}
 }
 
 // ensure we allow the use of valid tags
dc944ea7
 func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
dad47680
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
dc944ea7
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
6db32fde
 	}
 
985fae25
 	validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t"}
6db32fde
 
 	for _, repo := range validRepos {
693ba98c
 		_, _, err := dockerCmdWithError("tag", "busybox:latest", repo)
6db32fde
 		if err != nil {
dc944ea7
 			c.Errorf("tag busybox %v should have worked: %s", repo, err)
6db32fde
 			continue
 		}
e0927447
 		deleteImages(repo)
6db32fde
 	}
 }
c496f241
 
8d4fe141
 // tag an image with an existed tag name without -f option should work
dc944ea7
 func (s *DockerSuite) TestTagExistedNameWithoutForce(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
c496f241
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
dc944ea7
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
c496f241
 	}
 
eef6eda7
 	dockerCmd(c, "tag", "busybox:latest", "busybox:test")
c496f241
 }
 
 // tag an image with an existed tag name with -f option should work
dc944ea7
 func (s *DockerSuite) TestTagExistedNameWithForce(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
c496f241
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
dc944ea7
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
c496f241
 	}
 
eef6eda7
 	dockerCmd(c, "tag", "busybox:latest", "busybox:test")
 	dockerCmd(c, "tag", "-f", "busybox:latest", "busybox:test")
c496f241
 }
568f86eb
 
b8301005
 func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
e00cfbb6
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
 	}
72269acf
 
e00cfbb6
 	// test repository name begin with '-'
693ba98c
 	out, _, err := dockerCmdWithError("tag", "busybox:latest", "-busybox:test")
72269acf
 	c.Assert(err, checker.NotNil, check.Commentf(out))
15d84a3a
 	c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed"))
72269acf
 
e00cfbb6
 	// test namespace name begin with '-'
693ba98c
 	out, _, err = dockerCmdWithError("tag", "busybox:latest", "-test/busybox:test")
72269acf
 	c.Assert(err, checker.NotNil, check.Commentf(out))
15d84a3a
 	c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed"))
72269acf
 
51462327
 	// test index name begin with '-'
693ba98c
 	out, _, err = dockerCmdWithError("tag", "busybox:latest", "-index:5000/busybox:test")
72269acf
 	c.Assert(err, checker.NotNil, check.Commentf(out))
15d84a3a
 	c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed"))
e00cfbb6
 }
 
568f86eb
 // ensure tagging using official names works
 // ensure all tags result in the same name
dc944ea7
 func (s *DockerSuite) TestTagOfficialNames(c *check.C) {
f9a3558a
 	testRequires(c, DaemonIsLinux)
568f86eb
 	names := []string{
 		"docker.io/busybox",
 		"index.docker.io/busybox",
 		"library/busybox",
 		"docker.io/library/busybox",
 		"index.docker.io/library/busybox",
 	}
 
 	for _, name := range names {
8d4fe141
 		out, exitCode, err := dockerCmdWithError("tag", "busybox:latest", name+":latest")
568f86eb
 		if err != nil || exitCode != 0 {
dc944ea7
 			c.Errorf("tag busybox %v should have worked: %s, %s", name, err, out)
568f86eb
 			continue
 		}
 
 		// ensure we don't have multiple tag names.
693ba98c
 		out, _, err = dockerCmdWithError("images")
568f86eb
 		if err != nil {
dc944ea7
 			c.Errorf("listing images failed with errors: %v, %s", err, out)
568f86eb
 		} else if strings.Contains(out, name) {
dc944ea7
 			c.Errorf("images should not have listed '%s'", name)
568f86eb
 			deleteImages(name + ":latest")
 		}
 	}
 
 	for _, name := range names {
8d4fe141
 		_, exitCode, err := dockerCmdWithError("tag", name+":latest", "fooo/bar:latest")
568f86eb
 		if err != nil || exitCode != 0 {
dc944ea7
 			c.Errorf("tag %v fooo/bar should have worked: %s", name, err)
568f86eb
 			continue
 		}
 		deleteImages("fooo/bar:latest")
 	}
 }
d08ca5c2
 
 // ensure tags can not match digests
 func (s *DockerSuite) TestTagMatchesDigest(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
 	}
 	digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507"
 	// test setting tag fails
8d4fe141
 	_, _, err := dockerCmdWithError("tag", "busybox:latest", digest)
d08ca5c2
 	if err == nil {
 		c.Fatal("digest tag a name should have failed")
 	}
 	// check that no new image matches the digest
 	_, _, err = dockerCmdWithError("inspect", digest)
 	if err == nil {
 		c.Fatal("inspecting by digest should have failed")
 	}
 }
4352da78
 
 func (s *DockerSuite) TestTagInvalidRepoName(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
 	}
 
 	// test setting tag fails
8d4fe141
 	_, _, err := dockerCmdWithError("tag", "busybox:latest", "sha256:sometag")
4352da78
 	if err == nil {
 		c.Fatal("tagging with image named \"sha256\" should have failed")
 	}
 }
 
 // ensure tags cannot create ambiguity with image ids
 func (s *DockerSuite) TestTagTruncationAmbiguity(c *check.C) {
 	testRequires(c, DaemonIsLinux)
 	if err := pullImageIfNotExist("busybox:latest"); err != nil {
 		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
 	}
 
 	imageID, err := buildImage("notbusybox:latest",
 		`FROM busybox
 		MAINTAINER dockerio`,
 		true)
 	if err != nil {
 		c.Fatal(err)
 	}
 	truncatedImageID := stringid.TruncateID(imageID)
 	truncatedTag := fmt.Sprintf("notbusybox:%s", truncatedImageID)
 
 	id, err := inspectField(truncatedTag, "Id")
 	if err != nil {
 		c.Fatalf("Error inspecting by image id: %s", err)
 	}
 
 	// Ensure inspect by image id returns image for image id
 	c.Assert(id, checker.Equals, imageID)
 	c.Logf("Built image: %s", imageID)
 
 	// test setting tag fails
8d4fe141
 	_, _, err = dockerCmdWithError("tag", "busybox:latest", truncatedTag)
4352da78
 	if err != nil {
 		c.Fatalf("Error tagging with an image id: %s", err)
 	}
 
 	id, err = inspectField(truncatedTag, "Id")
 	if err != nil {
 		c.Fatalf("Error inspecting by image id: %s", err)
 	}
 
 	// Ensure id is imageID and not busybox:latest
 	c.Assert(id, checker.Not(checker.Equals), imageID)
 }