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