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