github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/integration-cli/docker_cli_tag_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/integration-cli/checker"
     7  	"github.com/docker/docker/internal/testutil"
     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  	dockerCmd(c, "tag", "busybox:latest", "testfoobarbaz")
    14  }
    15  
    16  // tagging an image by ID in a new unprefixed repo should work
    17  func (s *DockerSuite) TestTagUnprefixedRepoByID(c *check.C) {
    18  	imageID := inspectField(c, "busybox", "Id")
    19  	dockerCmd(c, "tag", imageID, "testfoobarbaz")
    20  }
    21  
    22  // ensure we don't allow the use of invalid repository names; these tag operations should fail
    23  func (s *DockerSuite) TestTagInvalidUnprefixedRepo(c *check.C) {
    24  	invalidRepos := []string{"fo$z$", "Foo@3cc", "Foo$3", "Foo*3", "Fo^3", "Foo!3", "F)xcz(", "fo%asd", "FOO/bar"}
    25  
    26  	for _, repo := range invalidRepos {
    27  		out, _, err := dockerCmdWithError("tag", "busybox", repo)
    28  		c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repo, out))
    29  	}
    30  }
    31  
    32  // ensure we don't allow the use of invalid tags; these tag operations should fail
    33  func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) {
    34  	longTag := testutil.GenerateRandomAlphaOnlyString(121)
    35  
    36  	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}
    37  
    38  	for _, repotag := range invalidTags {
    39  		out, _, err := dockerCmdWithError("tag", "busybox", repotag)
    40  		c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repotag, out))
    41  	}
    42  }
    43  
    44  // ensure we allow the use of valid tags
    45  func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) {
    46  	validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t", "HOSTNAME.DOMAIN.COM:443/foo/bar"}
    47  
    48  	for _, repo := range validRepos {
    49  		_, _, err := dockerCmdWithError("tag", "busybox:latest", repo)
    50  		if err != nil {
    51  			c.Errorf("tag busybox %v should have worked: %s", repo, err)
    52  			continue
    53  		}
    54  		deleteImages(repo)
    55  	}
    56  }
    57  
    58  // tag an image with an existed tag name without -f option should work
    59  func (s *DockerSuite) TestTagExistedNameWithoutForce(c *check.C) {
    60  	dockerCmd(c, "tag", "busybox:latest", "busybox:test")
    61  }
    62  
    63  func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) {
    64  	// test repository name begin with '-'
    65  	out, _, err := dockerCmdWithError("tag", "busybox:latest", "-busybox:test")
    66  	c.Assert(err, checker.NotNil, check.Commentf(out))
    67  	c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed"))
    68  
    69  	// test namespace name begin with '-'
    70  	out, _, err = dockerCmdWithError("tag", "busybox:latest", "-test/busybox:test")
    71  	c.Assert(err, checker.NotNil, check.Commentf(out))
    72  	c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed"))
    73  
    74  	// test index name begin with '-'
    75  	out, _, err = dockerCmdWithError("tag", "busybox:latest", "-index:5000/busybox:test")
    76  	c.Assert(err, checker.NotNil, check.Commentf(out))
    77  	c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed"))
    78  }
    79  
    80  // ensure tagging using official names works
    81  // ensure all tags result in the same name
    82  func (s *DockerSuite) TestTagOfficialNames(c *check.C) {
    83  	names := []string{
    84  		"docker.io/busybox",
    85  		"index.docker.io/busybox",
    86  		"library/busybox",
    87  		"docker.io/library/busybox",
    88  		"index.docker.io/library/busybox",
    89  	}
    90  
    91  	for _, name := range names {
    92  		out, exitCode, err := dockerCmdWithError("tag", "busybox:latest", name+":latest")
    93  		if err != nil || exitCode != 0 {
    94  			c.Errorf("tag busybox %v should have worked: %s, %s", name, err, out)
    95  			continue
    96  		}
    97  
    98  		// ensure we don't have multiple tag names.
    99  		out, _, err = dockerCmdWithError("images")
   100  		if err != nil {
   101  			c.Errorf("listing images failed with errors: %v, %s", err, out)
   102  		} else if strings.Contains(out, name) {
   103  			c.Errorf("images should not have listed '%s'", name)
   104  			deleteImages(name + ":latest")
   105  		}
   106  	}
   107  
   108  	for _, name := range names {
   109  		_, exitCode, err := dockerCmdWithError("tag", name+":latest", "fooo/bar:latest")
   110  		if err != nil || exitCode != 0 {
   111  			c.Errorf("tag %v fooo/bar should have worked: %s", name, err)
   112  			continue
   113  		}
   114  		deleteImages("fooo/bar:latest")
   115  	}
   116  }
   117  
   118  // ensure tags can not match digests
   119  func (s *DockerSuite) TestTagMatchesDigest(c *check.C) {
   120  	digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507"
   121  	// test setting tag fails
   122  	_, _, err := dockerCmdWithError("tag", "busybox:latest", digest)
   123  	if err == nil {
   124  		c.Fatal("digest tag a name should have failed")
   125  	}
   126  	// check that no new image matches the digest
   127  	_, _, err = dockerCmdWithError("inspect", digest)
   128  	if err == nil {
   129  		c.Fatal("inspecting by digest should have failed")
   130  	}
   131  }
   132  
   133  func (s *DockerSuite) TestTagInvalidRepoName(c *check.C) {
   134  	// test setting tag fails
   135  	_, _, err := dockerCmdWithError("tag", "busybox:latest", "sha256:sometag")
   136  	if err == nil {
   137  		c.Fatal("tagging with image named \"sha256\" should have failed")
   138  	}
   139  }