github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/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 func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) { 90 // Don't attempt to pull on Windows as not in hub. It's installed 91 // as an image through .ensure-frozen-images-windows 92 if daemonPlatform != "windows" { 93 if err := pullImageIfNotExist("busybox:latest"); err != nil { 94 c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") 95 } 96 } 97 // test repository name begin with '-' 98 out, _, err := dockerCmdWithError("tag", "busybox:latest", "-busybox:test") 99 c.Assert(err, checker.NotNil, check.Commentf(out)) 100 c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed")) 101 102 // test namespace name begin with '-' 103 out, _, err = dockerCmdWithError("tag", "busybox:latest", "-test/busybox:test") 104 c.Assert(err, checker.NotNil, check.Commentf(out)) 105 c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed")) 106 107 // test index name begin with '-' 108 out, _, err = dockerCmdWithError("tag", "busybox:latest", "-index:5000/busybox:test") 109 c.Assert(err, checker.NotNil, check.Commentf(out)) 110 c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed")) 111 } 112 113 // ensure tagging using official names works 114 // ensure all tags result in the same name 115 func (s *DockerSuite) TestTagOfficialNames(c *check.C) { 116 names := []string{ 117 "docker.io/busybox", 118 "index.docker.io/busybox", 119 "library/busybox", 120 "docker.io/library/busybox", 121 "index.docker.io/library/busybox", 122 } 123 124 for _, name := range names { 125 out, exitCode, err := dockerCmdWithError("tag", "busybox:latest", name+":latest") 126 if err != nil || exitCode != 0 { 127 c.Errorf("tag busybox %v should have worked: %s, %s", name, err, out) 128 continue 129 } 130 131 // ensure we don't have multiple tag names. 132 out, _, err = dockerCmdWithError("images") 133 if err != nil { 134 c.Errorf("listing images failed with errors: %v, %s", err, out) 135 } else if strings.Contains(out, name) { 136 c.Errorf("images should not have listed '%s'", name) 137 deleteImages(name + ":latest") 138 } 139 } 140 141 for _, name := range names { 142 _, exitCode, err := dockerCmdWithError("tag", name+":latest", "fooo/bar:latest") 143 if err != nil || exitCode != 0 { 144 c.Errorf("tag %v fooo/bar should have worked: %s", name, err) 145 continue 146 } 147 deleteImages("fooo/bar:latest") 148 } 149 } 150 151 // ensure tags can not match digests 152 func (s *DockerSuite) TestTagMatchesDigest(c *check.C) { 153 // Don't attempt to pull on Windows as not in hub. It's installed 154 // as an image through .ensure-frozen-images-windows 155 if daemonPlatform != "windows" { 156 if err := pullImageIfNotExist("busybox:latest"); err != nil { 157 c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") 158 } 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 // Don't attempt to pull on Windows as not in hub. It's installed 175 // as an image through .ensure-frozen-images-windows 176 if daemonPlatform != "windows" { 177 if err := pullImageIfNotExist("busybox:latest"); err != nil { 178 c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") 179 } 180 } 181 182 // test setting tag fails 183 _, _, err := dockerCmdWithError("tag", "busybox:latest", "sha256:sometag") 184 if err == nil { 185 c.Fatal("tagging with image named \"sha256\" should have failed") 186 } 187 } 188 189 // ensure tags cannot create ambiguity with image ids 190 func (s *DockerSuite) TestTagTruncationAmbiguity(c *check.C) { 191 //testRequires(c, DaemonIsLinux) 192 // Don't attempt to pull on Windows as not in hub. It's installed 193 // as an image through .ensure-frozen-images-windows 194 if daemonPlatform != "windows" { 195 if err := pullImageIfNotExist("busybox:latest"); err != nil { 196 c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") 197 } 198 } 199 imageID, err := buildImage("notbusybox:latest", 200 `FROM busybox 201 MAINTAINER dockerio`, 202 true) 203 if err != nil { 204 c.Fatal(err) 205 } 206 truncatedImageID := stringid.TruncateID(imageID) 207 truncatedTag := fmt.Sprintf("notbusybox:%s", truncatedImageID) 208 209 id := inspectField(c, truncatedTag, "Id") 210 211 // Ensure inspect by image id returns image for image id 212 c.Assert(id, checker.Equals, imageID) 213 c.Logf("Built image: %s", imageID) 214 215 // test setting tag fails 216 _, _, err = dockerCmdWithError("tag", "busybox:latest", truncatedTag) 217 if err != nil { 218 c.Fatalf("Error tagging with an image id: %s", err) 219 } 220 221 id = inspectField(c, truncatedTag, "Id") 222 223 // Ensure id is imageID and not busybox:latest 224 c.Assert(id, checker.Not(checker.Equals), imageID) 225 }