github.com/squaremo/docker@v1.3.2-0.20150516120342-42cfc9554972/integration-cli/docker_cli_tag_test.go (about) 1 package main 2 3 import ( 4 "os/exec" 5 "strings" 6 7 "github.com/docker/docker/pkg/stringutils" 8 "github.com/go-check/check" 9 ) 10 11 // tagging a named image in a new unprefixed repo should work 12 func (s *DockerSuite) TestTagUnprefixedRepoByName(c *check.C) { 13 if err := pullImageIfNotExist("busybox:latest"); err != nil { 14 c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") 15 } 16 17 tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "testfoobarbaz") 18 if out, _, err := runCommandWithOutput(tagCmd); err != nil { 19 c.Fatal(out, err) 20 } 21 } 22 23 // tagging an image by ID in a new unprefixed repo should work 24 func (s *DockerSuite) TestTagUnprefixedRepoByID(c *check.C) { 25 getIDCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox") 26 out, _, err := runCommandWithOutput(getIDCmd) 27 if err != nil { 28 c.Fatalf("failed to get the image ID of busybox: %s, %v", out, err) 29 } 30 31 cleanedImageID := strings.TrimSpace(out) 32 tagCmd := exec.Command(dockerBinary, "tag", cleanedImageID, "testfoobarbaz") 33 if out, _, err = runCommandWithOutput(tagCmd); err != nil { 34 c.Fatal(out, err) 35 } 36 } 37 38 // ensure we don't allow the use of invalid repository names; these tag operations should fail 39 func (s *DockerSuite) TestTagInvalidUnprefixedRepo(c *check.C) { 40 41 invalidRepos := []string{"fo$z$", "Foo@3cc", "Foo$3", "Foo*3", "Fo^3", "Foo!3", "F)xcz(", "fo%asd"} 42 43 for _, repo := range invalidRepos { 44 tagCmd := exec.Command(dockerBinary, "tag", "busybox", repo) 45 _, _, err := runCommandWithOutput(tagCmd) 46 if err == nil { 47 c.Fatalf("tag busybox %v should have failed", repo) 48 } 49 } 50 } 51 52 // ensure we don't allow the use of invalid tags; these tag operations should fail 53 func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) { 54 longTag := stringutils.GenerateRandomAlphaOnlyString(121) 55 56 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} 57 58 for _, repotag := range invalidTags { 59 tagCmd := exec.Command(dockerBinary, "tag", "busybox", repotag) 60 _, _, err := runCommandWithOutput(tagCmd) 61 if err == nil { 62 c.Fatalf("tag busybox %v should have failed", repotag) 63 } 64 } 65 } 66 67 // ensure we allow the use of valid tags 68 func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) { 69 if err := pullImageIfNotExist("busybox:latest"); err != nil { 70 c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") 71 } 72 73 validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t"} 74 75 for _, repo := range validRepos { 76 tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", repo) 77 _, _, err := runCommandWithOutput(tagCmd) 78 if err != nil { 79 c.Errorf("tag busybox %v should have worked: %s", repo, err) 80 continue 81 } 82 deleteImages(repo) 83 } 84 } 85 86 // tag an image with an existed tag name without -f option should fail 87 func (s *DockerSuite) TestTagExistedNameWithoutForce(c *check.C) { 88 if err := pullImageIfNotExist("busybox:latest"); err != nil { 89 c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") 90 } 91 92 tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test") 93 if out, _, err := runCommandWithOutput(tagCmd); err != nil { 94 c.Fatal(out, err) 95 } 96 tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test") 97 out, _, err := runCommandWithOutput(tagCmd) 98 if err == nil || !strings.Contains(out, "Conflict: Tag test is already set to image") { 99 c.Fatal("tag busybox busybox:test should have failed,because busybox:test is existed") 100 } 101 } 102 103 // tag an image with an existed tag name with -f option should work 104 func (s *DockerSuite) TestTagExistedNameWithForce(c *check.C) { 105 if err := pullImageIfNotExist("busybox:latest"); err != nil { 106 c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") 107 } 108 109 tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test") 110 if out, _, err := runCommandWithOutput(tagCmd); err != nil { 111 c.Fatal(out, err) 112 } 113 tagCmd = exec.Command(dockerBinary, "tag", "-f", "busybox:latest", "busybox:test") 114 if out, _, err := runCommandWithOutput(tagCmd); err != nil { 115 c.Fatal(out, err) 116 } 117 } 118 119 func (s *DockerSuite) TestTagWithSuffixHyphen(c *check.C) { 120 if err := pullImageIfNotExist("busybox:latest"); err != nil { 121 c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") 122 } 123 // test repository name begin with '-' 124 tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "-busybox:test") 125 out, _, err := runCommandWithOutput(tagCmd) 126 if err == nil || !strings.Contains(out, "Invalid repository name (-busybox). Cannot begin or end with a hyphen") { 127 c.Fatal("tag a name begin with '-' should failed") 128 } 129 // test namespace name begin with '-' 130 tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "-test/busybox:test") 131 out, _, err = runCommandWithOutput(tagCmd) 132 if err == nil || !strings.Contains(out, "Invalid namespace name (-test). Cannot begin or end with a hyphen") { 133 c.Fatal("tag a name begin with '-' should failed") 134 } 135 // test index name begin wiht '-' 136 tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "-index:5000/busybox:test") 137 out, _, err = runCommandWithOutput(tagCmd) 138 if err == nil || !strings.Contains(out, "Invalid index name (-index:5000). Cannot begin or end with a hyphen") { 139 c.Fatal("tag a name begin with '-' should failed") 140 } 141 } 142 143 // ensure tagging using official names works 144 // ensure all tags result in the same name 145 func (s *DockerSuite) TestTagOfficialNames(c *check.C) { 146 names := []string{ 147 "docker.io/busybox", 148 "index.docker.io/busybox", 149 "library/busybox", 150 "docker.io/library/busybox", 151 "index.docker.io/library/busybox", 152 } 153 154 for _, name := range names { 155 tagCmd := exec.Command(dockerBinary, "tag", "-f", "busybox:latest", name+":latest") 156 out, exitCode, err := runCommandWithOutput(tagCmd) 157 if err != nil || exitCode != 0 { 158 c.Errorf("tag busybox %v should have worked: %s, %s", name, err, out) 159 continue 160 } 161 162 // ensure we don't have multiple tag names. 163 imagesCmd := exec.Command(dockerBinary, "images") 164 out, _, err = runCommandWithOutput(imagesCmd) 165 if err != nil { 166 c.Errorf("listing images failed with errors: %v, %s", err, out) 167 } else if strings.Contains(out, name) { 168 c.Errorf("images should not have listed '%s'", name) 169 deleteImages(name + ":latest") 170 } 171 } 172 173 for _, name := range names { 174 tagCmd := exec.Command(dockerBinary, "tag", "-f", name+":latest", "fooo/bar:latest") 175 _, exitCode, err := runCommandWithOutput(tagCmd) 176 if err != nil || exitCode != 0 { 177 c.Errorf("tag %v fooo/bar should have worked: %s", name, err) 178 continue 179 } 180 deleteImages("fooo/bar:latest") 181 } 182 }