github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/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  // ensure tagging using official names works
   120  // ensure all tags result in the same name
   121  func (s *DockerSuite) TestTagOfficialNames(c *check.C) {
   122  	names := []string{
   123  		"docker.io/busybox",
   124  		"index.docker.io/busybox",
   125  		"library/busybox",
   126  		"docker.io/library/busybox",
   127  		"index.docker.io/library/busybox",
   128  	}
   129  
   130  	for _, name := range names {
   131  		tagCmd := exec.Command(dockerBinary, "tag", "-f", "busybox:latest", name+":latest")
   132  		out, exitCode, err := runCommandWithOutput(tagCmd)
   133  		if err != nil || exitCode != 0 {
   134  			c.Errorf("tag busybox %v should have worked: %s, %s", name, err, out)
   135  			continue
   136  		}
   137  
   138  		// ensure we don't have multiple tag names.
   139  		imagesCmd := exec.Command(dockerBinary, "images")
   140  		out, _, err = runCommandWithOutput(imagesCmd)
   141  		if err != nil {
   142  			c.Errorf("listing images failed with errors: %v, %s", err, out)
   143  		} else if strings.Contains(out, name) {
   144  			c.Errorf("images should not have listed '%s'", name)
   145  			deleteImages(name + ":latest")
   146  		}
   147  	}
   148  
   149  	for _, name := range names {
   150  		tagCmd := exec.Command(dockerBinary, "tag", "-f", name+":latest", "fooo/bar:latest")
   151  		_, exitCode, err := runCommandWithOutput(tagCmd)
   152  		if err != nil || exitCode != 0 {
   153  			c.Errorf("tag %v fooo/bar should have worked: %s", name, err)
   154  			continue
   155  		}
   156  		deleteImages("fooo/bar:latest")
   157  	}
   158  }