github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/integration-cli/docker_cli_tag_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/docker/docker/integration-cli/checker" 8 "github.com/docker/docker/pkg/stringid" 9 "github.com/docker/docker/pkg/stringutils" 10 "github.com/go-check/check" 11 ) 12 13 // tagging a named image in a new unprefixed repo should work 14 func (s *DockerSuite) TestTagUnprefixedRepoByName(c *check.C) { 15 dockerCmd(c, "tag", "busybox:latest", "testfoobarbaz") 16 } 17 18 // tagging an image by ID in a new unprefixed repo should work 19 func (s *DockerSuite) TestTagUnprefixedRepoByID(c *check.C) { 20 imageID := inspectField(c, "busybox", "Id") 21 dockerCmd(c, "tag", imageID, "testfoobarbaz") 22 } 23 24 // ensure we don't allow the use of invalid repository names; these tag operations should fail 25 func (s *DockerSuite) TestTagInvalidUnprefixedRepo(c *check.C) { 26 invalidRepos := []string{"fo$z$", "Foo@3cc", "Foo$3", "Foo*3", "Fo^3", "Foo!3", "F)xcz(", "fo%asd", "FOO/bar"} 27 28 for _, repo := range invalidRepos { 29 out, _, err := dockerCmdWithError("tag", "busybox", repo) 30 c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repo, out)) 31 } 32 } 33 34 // ensure we don't allow the use of invalid tags; these tag operations should fail 35 func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) { 36 longTag := stringutils.GenerateRandomAlphaOnlyString(121) 37 38 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} 39 40 for _, repotag := range invalidTags { 41 out, _, err := dockerCmdWithError("tag", "busybox", repotag) 42 c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repotag, out)) 43 } 44 } 45 46 // ensure we allow the use of valid tags 47 func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) { 48 validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t", "HOSTNAME.DOMAIN.COM:443/foo/bar"} 49 50 for _, repo := range validRepos { 51 _, _, err := dockerCmdWithError("tag", "busybox:latest", repo) 52 if err != nil { 53 c.Errorf("tag busybox %v should have worked: %s", repo, err) 54 continue 55 } 56 deleteImages(repo) 57 } 58 } 59 60 // tag an image with an existed tag name without -f option should work 61 func (s *DockerSuite) TestTagExistedNameWithoutForce(c *check.C) { 62 dockerCmd(c, "tag", "busybox:latest", "busybox:test") 63 } 64 65 func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) { 66 // test repository name begin with '-' 67 out, _, err := dockerCmdWithError("tag", "busybox:latest", "-busybox:test") 68 c.Assert(err, checker.NotNil, check.Commentf(out)) 69 c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed")) 70 71 // test namespace name begin with '-' 72 out, _, err = dockerCmdWithError("tag", "busybox:latest", "-test/busybox:test") 73 c.Assert(err, checker.NotNil, check.Commentf(out)) 74 c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed")) 75 76 // test index name begin with '-' 77 out, _, err = dockerCmdWithError("tag", "busybox:latest", "-index:5000/busybox:test") 78 c.Assert(err, checker.NotNil, check.Commentf(out)) 79 c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed")) 80 } 81 82 // ensure tagging using official names works 83 // ensure all tags result in the same name 84 func (s *DockerSuite) TestTagOfficialNames(c *check.C) { 85 names := []string{ 86 "docker.io/busybox", 87 "index.docker.io/busybox", 88 "library/busybox", 89 "docker.io/library/busybox", 90 "index.docker.io/library/busybox", 91 } 92 93 for _, name := range names { 94 out, exitCode, err := dockerCmdWithError("tag", "busybox:latest", name+":latest") 95 if err != nil || exitCode != 0 { 96 c.Errorf("tag busybox %v should have worked: %s, %s", name, err, out) 97 continue 98 } 99 100 // ensure we don't have multiple tag names. 101 out, _, err = dockerCmdWithError("images") 102 if err != nil { 103 c.Errorf("listing images failed with errors: %v, %s", err, out) 104 } else if strings.Contains(out, name) { 105 c.Errorf("images should not have listed '%s'", name) 106 deleteImages(name + ":latest") 107 } 108 } 109 110 for _, name := range names { 111 _, exitCode, err := dockerCmdWithError("tag", name+":latest", "fooo/bar:latest") 112 if err != nil || exitCode != 0 { 113 c.Errorf("tag %v fooo/bar should have worked: %s", name, err) 114 continue 115 } 116 deleteImages("fooo/bar:latest") 117 } 118 } 119 120 // ensure tags can not match digests 121 func (s *DockerSuite) TestTagMatchesDigest(c *check.C) { 122 digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507" 123 // test setting tag fails 124 _, _, err := dockerCmdWithError("tag", "busybox:latest", digest) 125 if err == nil { 126 c.Fatal("digest tag a name should have failed") 127 } 128 // check that no new image matches the digest 129 _, _, err = dockerCmdWithError("inspect", digest) 130 if err == nil { 131 c.Fatal("inspecting by digest should have failed") 132 } 133 } 134 135 func (s *DockerSuite) TestTagInvalidRepoName(c *check.C) { 136 // test setting tag fails 137 _, _, err := dockerCmdWithError("tag", "busybox:latest", "sha256:sometag") 138 if err == nil { 139 c.Fatal("tagging with image named \"sha256\" should have failed") 140 } 141 } 142 143 // ensure tags cannot create ambiguity with image ids 144 func (s *DockerSuite) TestTagTruncationAmbiguity(c *check.C) { 145 imageID, err := buildImage("notbusybox:latest", 146 `FROM busybox 147 MAINTAINER dockerio`, 148 true) 149 if err != nil { 150 c.Fatal(err) 151 } 152 truncatedImageID := stringid.TruncateID(imageID) 153 truncatedTag := fmt.Sprintf("notbusybox:%s", truncatedImageID) 154 155 id := inspectField(c, truncatedTag, "Id") 156 157 // Ensure inspect by image id returns image for image id 158 c.Assert(id, checker.Equals, imageID) 159 c.Logf("Built image: %s", imageID) 160 161 // test setting tag fails 162 _, _, err = dockerCmdWithError("tag", "busybox:latest", truncatedTag) 163 if err != nil { 164 c.Fatalf("Error tagging with an image id: %s", err) 165 } 166 167 id = inspectField(c, truncatedTag, "Id") 168 169 // Ensure id is imageID and not busybox:latest 170 c.Assert(id, checker.Not(checker.Equals), imageID) 171 }