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