github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/integration-cli/docker_cli_tag_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/docker/docker/pkg/integration/checker"
     8  	"github.com/docker/docker/pkg/stringid"
     9  	"github.com/docker/docker/pkg/stringutils"
    10  	"github.com/go-check/check"
    11  )
    12  
    13  // tagging a named image in a new unprefixed repo should work
    14  func (s *DockerSuite) TestTagUnprefixedRepoByName(c *check.C) {
    15  	// Don't attempt to pull on Windows as not in hub. It's installed
    16  	// as an image through .ensure-frozen-images-windows
    17  	if daemonPlatform != "windows" {
    18  		if err := pullImageIfNotExist("busybox:latest"); err != nil {
    19  			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    20  		}
    21  	}
    22  
    23  	dockerCmd(c, "tag", "busybox:latest", "testfoobarbaz")
    24  }
    25  
    26  // tagging an image by ID in a new unprefixed repo should work
    27  func (s *DockerSuite) TestTagUnprefixedRepoByID(c *check.C) {
    28  	imageID, err := inspectField("busybox", "Id")
    29  	c.Assert(err, check.IsNil)
    30  	dockerCmd(c, "tag", imageID, "testfoobarbaz")
    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  	invalidRepos := []string{"fo$z$", "Foo@3cc", "Foo$3", "Foo*3", "Fo^3", "Foo!3", "F)xcz(", "fo%asd"}
    36  
    37  	for _, repo := range invalidRepos {
    38  		out, _, err := dockerCmdWithError("tag", "busybox", repo)
    39  		c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repo, out))
    40  	}
    41  }
    42  
    43  // ensure we don't allow the use of invalid tags; these tag operations should fail
    44  func (s *DockerSuite) TestTagInvalidPrefixedRepo(c *check.C) {
    45  	longTag := stringutils.GenerateRandomAlphaOnlyString(121)
    46  
    47  	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}
    48  
    49  	for _, repotag := range invalidTags {
    50  		out, _, err := dockerCmdWithError("tag", "busybox", repotag)
    51  		c.Assert(err, checker.NotNil, check.Commentf("tag busybox %v should have failed : %v", repotag, out))
    52  	}
    53  }
    54  
    55  // ensure we allow the use of valid tags
    56  func (s *DockerSuite) TestTagValidPrefixedRepo(c *check.C) {
    57  	// Don't attempt to pull on Windows as not in hub. It's installed
    58  	// as an image through .ensure-frozen-images-windows
    59  	if daemonPlatform != "windows" {
    60  		if err := pullImageIfNotExist("busybox:latest"); err != nil {
    61  			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    62  		}
    63  	}
    64  
    65  	validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t"}
    66  
    67  	for _, repo := range validRepos {
    68  		_, _, err := dockerCmdWithError("tag", "busybox:latest", repo)
    69  		if err != nil {
    70  			c.Errorf("tag busybox %v should have worked: %s", repo, err)
    71  			continue
    72  		}
    73  		deleteImages(repo)
    74  	}
    75  }
    76  
    77  // tag an image with an existed tag name without -f option should work
    78  func (s *DockerSuite) TestTagExistedNameWithoutForce(c *check.C) {
    79  	// Don't attempt to pull on Windows as not in hub. It's installed
    80  	// as an image through .ensure-frozen-images-windows
    81  	if daemonPlatform != "windows" {
    82  		if err := pullImageIfNotExist("busybox:latest"); err != nil {
    83  			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
    84  		}
    85  	}
    86  
    87  	dockerCmd(c, "tag", "busybox:latest", "busybox:test")
    88  }
    89  
    90  // tag an image with an existed tag name with -f option should work
    91  func (s *DockerSuite) TestTagExistedNameWithForce(c *check.C) {
    92  	// Don't attempt to pull on Windows as not in hub. It's installed
    93  	// as an image through .ensure-frozen-images-windows
    94  	if daemonPlatform != "windows" {
    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  	}
    99  	dockerCmd(c, "tag", "busybox:latest", "busybox:test")
   100  	dockerCmd(c, "tag", "-f", "busybox:latest", "busybox:test")
   101  }
   102  
   103  func (s *DockerSuite) TestTagWithPrefixHyphen(c *check.C) {
   104  	// TODO Windows CI. This fails on TP4 docker, but has since been fixed.
   105  	// Enable these tests for TP5.
   106  	testRequires(c, DaemonIsLinux)
   107  	// Don't attempt to pull on Windows as not in hub. It's installed
   108  	// as an image through .ensure-frozen-images-windows
   109  	if daemonPlatform != "windows" {
   110  		if err := pullImageIfNotExist("busybox:latest"); err != nil {
   111  			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
   112  		}
   113  	}
   114  	// test repository name begin with '-'
   115  	out, _, err := dockerCmdWithError("tag", "busybox:latest", "-busybox:test")
   116  	c.Assert(err, checker.NotNil, check.Commentf(out))
   117  	c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed"))
   118  
   119  	// test namespace name begin with '-'
   120  	out, _, err = dockerCmdWithError("tag", "busybox:latest", "-test/busybox:test")
   121  	c.Assert(err, checker.NotNil, check.Commentf(out))
   122  	c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed"))
   123  
   124  	// test index name begin with '-'
   125  	out, _, err = dockerCmdWithError("tag", "busybox:latest", "-index:5000/busybox:test")
   126  	c.Assert(err, checker.NotNil, check.Commentf(out))
   127  	c.Assert(out, checker.Contains, "Error parsing reference", check.Commentf("tag a name begin with '-' should failed"))
   128  }
   129  
   130  // ensure tagging using official names works
   131  // ensure all tags result in the same name
   132  func (s *DockerSuite) TestTagOfficialNames(c *check.C) {
   133  	// TODO Windows CI. This fails on TP4 docker, but has since been fixed.
   134  	// Enable these tests for TP5.
   135  	testRequires(c, DaemonIsLinux)
   136  	names := []string{
   137  		"docker.io/busybox",
   138  		"index.docker.io/busybox",
   139  		"library/busybox",
   140  		"docker.io/library/busybox",
   141  		"index.docker.io/library/busybox",
   142  	}
   143  
   144  	for _, name := range names {
   145  		out, exitCode, err := dockerCmdWithError("tag", "busybox:latest", name+":latest")
   146  		if err != nil || exitCode != 0 {
   147  			c.Errorf("tag busybox %v should have worked: %s, %s", name, err, out)
   148  			continue
   149  		}
   150  
   151  		// ensure we don't have multiple tag names.
   152  		out, _, err = dockerCmdWithError("images")
   153  		if err != nil {
   154  			c.Errorf("listing images failed with errors: %v, %s", err, out)
   155  		} else if strings.Contains(out, name) {
   156  			c.Errorf("images should not have listed '%s'", name)
   157  			deleteImages(name + ":latest")
   158  		}
   159  	}
   160  
   161  	for _, name := range names {
   162  		_, exitCode, err := dockerCmdWithError("tag", name+":latest", "fooo/bar:latest")
   163  		if err != nil || exitCode != 0 {
   164  			c.Errorf("tag %v fooo/bar should have worked: %s", name, err)
   165  			continue
   166  		}
   167  		deleteImages("fooo/bar:latest")
   168  	}
   169  }
   170  
   171  // ensure tags can not match digests
   172  func (s *DockerSuite) TestTagMatchesDigest(c *check.C) {
   173  	// TODO Windows CI. This can be enabled for TP5, but will fail on TP4.
   174  	// This is due to the content addressibility changes which are not
   175  	// in the TP4 version of Docker.
   176  	testRequires(c, DaemonIsLinux)
   177  	// Don't attempt to pull on Windows as not in hub. It's installed
   178  	// as an image through .ensure-frozen-images-windows
   179  	if daemonPlatform != "windows" {
   180  		if err := pullImageIfNotExist("busybox:latest"); err != nil {
   181  			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
   182  		}
   183  	}
   184  	digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507"
   185  	// test setting tag fails
   186  	_, _, err := dockerCmdWithError("tag", "busybox:latest", digest)
   187  	if err == nil {
   188  		c.Fatal("digest tag a name should have failed")
   189  	}
   190  	// check that no new image matches the digest
   191  	_, _, err = dockerCmdWithError("inspect", digest)
   192  	if err == nil {
   193  		c.Fatal("inspecting by digest should have failed")
   194  	}
   195  }
   196  
   197  func (s *DockerSuite) TestTagInvalidRepoName(c *check.C) {
   198  	// TODO Windows CI. This can be enabled for TP5, but will fail on the
   199  	// TP4 version of docker.
   200  	testRequires(c, DaemonIsLinux)
   201  	// Don't attempt to pull on Windows as not in hub. It's installed
   202  	// as an image through .ensure-frozen-images-windows
   203  	if daemonPlatform != "windows" {
   204  		if err := pullImageIfNotExist("busybox:latest"); err != nil {
   205  			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
   206  		}
   207  	}
   208  
   209  	// test setting tag fails
   210  	_, _, err := dockerCmdWithError("tag", "busybox:latest", "sha256:sometag")
   211  	if err == nil {
   212  		c.Fatal("tagging with image named \"sha256\" should have failed")
   213  	}
   214  }
   215  
   216  // ensure tags cannot create ambiguity with image ids
   217  func (s *DockerSuite) TestTagTruncationAmbiguity(c *check.C) {
   218  	//testRequires(c, DaemonIsLinux)
   219  	// Don't attempt to pull on Windows as not in hub. It's installed
   220  	// as an image through .ensure-frozen-images-windows
   221  	if daemonPlatform != "windows" {
   222  		if err := pullImageIfNotExist("busybox:latest"); err != nil {
   223  			c.Fatal("couldn't find the busybox:latest image locally and failed to pull it")
   224  		}
   225  	}
   226  	imageID, err := buildImage("notbusybox:latest",
   227  		`FROM busybox
   228  		MAINTAINER dockerio`,
   229  		true)
   230  	if err != nil {
   231  		c.Fatal(err)
   232  	}
   233  	truncatedImageID := stringid.TruncateID(imageID)
   234  	truncatedTag := fmt.Sprintf("notbusybox:%s", truncatedImageID)
   235  
   236  	id, err := inspectField(truncatedTag, "Id")
   237  	if err != nil {
   238  		c.Fatalf("Error inspecting by image id: %s", err)
   239  	}
   240  
   241  	// Ensure inspect by image id returns image for image id
   242  	c.Assert(id, checker.Equals, imageID)
   243  	c.Logf("Built image: %s", imageID)
   244  
   245  	// test setting tag fails
   246  	_, _, err = dockerCmdWithError("tag", "busybox:latest", truncatedTag)
   247  	if err != nil {
   248  		c.Fatalf("Error tagging with an image id: %s", err)
   249  	}
   250  
   251  	id, err = inspectField(truncatedTag, "Id")
   252  	if err != nil {
   253  		c.Fatalf("Error inspecting by image id: %s", err)
   254  	}
   255  
   256  	// Ensure id is imageID and not busybox:latest
   257  	c.Assert(id, checker.Not(checker.Equals), imageID)
   258  }