github.com/tsuna/docker@v1.7.0-rc3/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  	imageID, err := inspectField("busybox", "Id")
    26  	c.Assert(err, check.IsNil)
    27  	tagCmd := exec.Command(dockerBinary, "tag", imageID, "testfoobarbaz")
    28  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
    29  		c.Fatal(out, err)
    30  	}
    31  }
    32  
    33  // ensure we don't allow the use of invalid repository names; these tag operations should fail
    34  func (s *DockerSuite) TestTagInvalidUnprefixedRepo(c *check.C) {
    35  
    36  	invalidRepos := []string{"fo$z$", "Foo@3cc", "Foo$3", "Foo*3", "Fo^3", "Foo!3", "F)xcz(", "fo%asd"}
    37  
    38  	for _, repo := range invalidRepos {
    39  		tagCmd := exec.Command(dockerBinary, "tag", "busybox", repo)
    40  		_, _, err := runCommandWithOutput(tagCmd)
    41  		if err == nil {
    42  			c.Fatalf("tag busybox %v should have failed", repo)
    43  		}
    44  	}
    45  }
    46  
    47  // ensure we don't allow the use of invalid tags; these tag operations should fail
    48  func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) {
    49  	longTag := stringutils.GenerateRandomAlphaOnlyString(121)
    50  
    51  	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}
    52  
    53  	for _, repotag := range invalidTags {
    54  		tagCmd := exec.Command(dockerBinary, "tag", "busybox", repotag)
    55  		_, _, err := runCommandWithOutput(tagCmd)
    56  		if err == nil {
    57  			c.Fatalf("tag busybox %v should have failed", repotag)
    58  		}
    59  	}
    60  }
    61  
    62  // ensure we allow the use of valid tags
    63  func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) {
    64  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
    65  		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    66  	}
    67  
    68  	validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t"}
    69  
    70  	for _, repo := range validRepos {
    71  		tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", repo)
    72  		_, _, err := runCommandWithOutput(tagCmd)
    73  		if err != nil {
    74  			c.Errorf("tag busybox %v should have worked: %s", repo, err)
    75  			continue
    76  		}
    77  		deleteImages(repo)
    78  	}
    79  }
    80  
    81  // tag an image with an existed tag name without -f option should fail
    82  func (s *DockerSuite) TestTagExistedNameWithoutForce(c *check.C) {
    83  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
    84  		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    85  	}
    86  
    87  	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test")
    88  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
    89  		c.Fatal(out, err)
    90  	}
    91  	tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test")
    92  	out, _, err := runCommandWithOutput(tagCmd)
    93  	if err == nil || !strings.Contains(out, "Conflict: Tag test is already set to image") {
    94  		c.Fatal("tag busybox busybox:test should have failed,because busybox:test is existed")
    95  	}
    96  }
    97  
    98  // tag an image with an existed tag name with -f option should work
    99  func (s *DockerSuite) TestTagExistedNameWithForce(c *check.C) {
   100  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
   101  		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
   102  	}
   103  
   104  	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "busybox:test")
   105  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
   106  		c.Fatal(out, err)
   107  	}
   108  	tagCmd = exec.Command(dockerBinary, "tag", "-f", "busybox:latest", "busybox:test")
   109  	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
   110  		c.Fatal(out, err)
   111  	}
   112  }
   113  
   114  func (s *DockerSuite) TestTagWithSuffixHyphen(c *check.C) {
   115  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
   116  		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
   117  	}
   118  	// test repository name begin with '-'
   119  	tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "-busybox:test")
   120  	out, _, err := runCommandWithOutput(tagCmd)
   121  	if err == nil || !strings.Contains(out, "Invalid repository name (-busybox). Cannot begin or end with a hyphen") {
   122  		c.Fatal("tag a name begin with '-' should failed")
   123  	}
   124  	// test namespace name begin with '-'
   125  	tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "-test/busybox:test")
   126  	out, _, err = runCommandWithOutput(tagCmd)
   127  	if err == nil || !strings.Contains(out, "Invalid namespace name (-test). Cannot begin or end with a hyphen") {
   128  		c.Fatal("tag a name begin with '-' should failed")
   129  	}
   130  	// test index name begin wiht '-'
   131  	tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "-index:5000/busybox:test")
   132  	out, _, err = runCommandWithOutput(tagCmd)
   133  	if err == nil || !strings.Contains(out, "Invalid index name (-index:5000). Cannot begin or end with a hyphen") {
   134  		c.Fatal("tag a name begin with '-' should failed")
   135  	}
   136  }
   137  
   138  // ensure tagging using official names works
   139  // ensure all tags result in the same name
   140  func (s *DockerSuite) TestTagOfficialNames(c *check.C) {
   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  			c.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  			c.Errorf("listing images failed with errors: %v, %s", err, out)
   162  		} else if strings.Contains(out, name) {
   163  			c.Errorf("images should not have listed '%s'", name)
   164  			deleteImages(name + ":latest")
   165  		}
   166  	}
   167  
   168  	for _, name := range names {
   169  		tagCmd := exec.Command(dockerBinary, "tag", "-f", name+":latest", "fooo/bar:latest")
   170  		_, exitCode, err := runCommandWithOutput(tagCmd)
   171  		if err != nil || exitCode != 0 {
   172  			c.Errorf("tag %v fooo/bar should have worked: %s", name, err)
   173  			continue
   174  		}
   175  		deleteImages("fooo/bar:latest")
   176  	}
   177  }