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