github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_tag_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/pkg/integration/checker"
     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  	testRequires(c, DaemonIsLinux)
    14  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
    15  		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    16  	}
    17  
    18  	dockerCmd(c, "tag", "busybox:latest", "testfoobarbaz")
    19  }
    20  
    21  // tagging an image by ID in a new unprefixed repo should work
    22  func (s *DockerSuite) TestTagUnprefixedRepoByID(c *check.C) {
    23  	testRequires(c, DaemonIsLinux)
    24  	imageID, err := inspectField("busybox", "Id")
    25  	c.Assert(err, check.IsNil)
    26  	dockerCmd(c, "tag", imageID, "testfoobarbaz")
    27  }
    28  
    29  // ensure we don't allow the use of invalid repository names; these tag operations should fail
    30  func (s *DockerSuite) TestTagInvalidUnprefixedRepo(c *check.C) {
    31  	invalidRepos := []string{"fo$z$", "Foo@3cc", "Foo$3", "Foo*3", "Fo^3", "Foo!3", "F)xcz(", "fo%asd"}
    32  
    33  	for _, repo := range invalidRepos {
    34  		out, _, err := dockerCmdWithError("tag", "busybox", repo)
    35  		c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repo, out))
    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  		out, _, err := dockerCmdWithError("tag", "busybox", repotag)
    47  		c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repotag, out))
    48  	}
    49  }
    50  
    51  // ensure we allow the use of valid tags
    52  func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) {
    53  	testRequires(c, DaemonIsLinux)
    54  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
    55  		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    56  	}
    57  
    58  	validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t"}
    59  
    60  	for _, repo := range validRepos {
    61  		_, _, err := dockerCmdWithError("tag", "busybox:latest", repo)
    62  		if err != nil {
    63  			c.Errorf("tag busybox %v should have worked: %s", repo, err)
    64  			continue
    65  		}
    66  		deleteImages(repo)
    67  	}
    68  }
    69  
    70  // tag an image with an existed tag name without -f option should fail
    71  func (s *DockerSuite) TestTagExistedNameWithoutForce(c *check.C) {
    72  	testRequires(c, DaemonIsLinux)
    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("tag", "busybox:latest", "busybox:test")
    79  
    80  	c.Assert(err, checker.NotNil, check.Commentf(out))
    81  	c.Assert(out, checker.Contains, "Conflict: Tag busybox:test is already set to image", check.Commentf("tag busybox busybox:test should have failed,because busybox:test is existed"))
    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  	testRequires(c, DaemonIsLinux)
    87  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
    88  		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    89  	}
    90  
    91  	dockerCmd(c, "tag", "busybox:latest", "busybox:test")
    92  	dockerCmd(c, "tag", "-f", "busybox:latest", "busybox:test")
    93  }
    94  
    95  func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) {
    96  	testRequires(c, DaemonIsLinux)
    97  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
    98  		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    99  	}
   100  
   101  	// test repository name begin with '-'
   102  	out, _, err := dockerCmdWithError("tag", "busybox:latest", "-busybox:test")
   103  	c.Assert(err, checker.NotNil, check.Commentf(out))
   104  	c.Assert(out, checker.Contains, "repository name component must match", check.Commentf("tag a name begin with '-' should failed"))
   105  
   106  	// test namespace name begin with '-'
   107  	out, _, err = dockerCmdWithError("tag", "busybox:latest", "-test/busybox:test")
   108  	c.Assert(err, checker.NotNil, check.Commentf(out))
   109  	c.Assert(out, checker.Contains, "repository name component must match", check.Commentf("tag a name begin with '-' should failed"))
   110  
   111  	// test index name begin with '-'
   112  	out, _, err = dockerCmdWithError("tag", "busybox:latest", "-index:5000/busybox:test")
   113  	c.Assert(err, checker.NotNil, check.Commentf(out))
   114  	c.Assert(out, checker.Contains, "Invalid index name (-index:5000). Cannot begin or end with a hyphen", check.Commentf("tag a name begin with '-' should failed"))
   115  }
   116  
   117  // ensure tagging using official names works
   118  // ensure all tags result in the same name
   119  func (s *DockerSuite) TestTagOfficialNames(c *check.C) {
   120  	testRequires(c, DaemonIsLinux)
   121  	names := []string{
   122  		"docker.io/busybox",
   123  		"index.docker.io/busybox",
   124  		"library/busybox",
   125  		"docker.io/library/busybox",
   126  		"index.docker.io/library/busybox",
   127  	}
   128  
   129  	for _, name := range names {
   130  		out, exitCode, err := dockerCmdWithError("tag", "-f", "busybox:latest", name+":latest")
   131  		if err != nil || exitCode != 0 {
   132  			c.Errorf("tag busybox %v should have worked: %s, %s", name, err, out)
   133  			continue
   134  		}
   135  
   136  		// ensure we don't have multiple tag names.
   137  		out, _, err = dockerCmdWithError("images")
   138  		if err != nil {
   139  			c.Errorf("listing images failed with errors: %v, %s", err, out)
   140  		} else if strings.Contains(out, name) {
   141  			c.Errorf("images should not have listed '%s'", name)
   142  			deleteImages(name + ":latest")
   143  		}
   144  	}
   145  
   146  	for _, name := range names {
   147  		_, exitCode, err := dockerCmdWithError("tag", "-f", name+":latest", "fooo/bar:latest")
   148  		if err != nil || exitCode != 0 {
   149  			c.Errorf("tag %v fooo/bar should have worked: %s", name, err)
   150  			continue
   151  		}
   152  		deleteImages("fooo/bar:latest")
   153  	}
   154  }
   155  
   156  // ensure tags can not match digests
   157  func (s *DockerSuite) TestTagMatchesDigest(c *check.C) {
   158  	testRequires(c, DaemonIsLinux)
   159  	if err := pullImageIfNotExist("busybox:latest"); err != nil {
   160  		c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
   161  	}
   162  	digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507"
   163  	// test setting tag fails
   164  	_, _, err := dockerCmdWithError("tag", "-f", "busybox:latest", digest)
   165  	if err == nil {
   166  		c.Fatal("digest tag a name should have failed")
   167  	}
   168  	// check that no new image matches the digest
   169  	_, _, err = dockerCmdWithError("inspect", digest)
   170  	if err == nil {
   171  		c.Fatal("inspecting by digest should have failed")
   172  	}
   173  }