github.com/glycerine/docker@v1.8.2/integration-cli/docker_cli_tag_test.go (about)

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