github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration/image/tag_test.go (about)

     1  package image // import "github.com/docker/docker/integration/image"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/integration/internal/request"
     9  	"github.com/docker/docker/internal/testutil"
    10  	"github.com/gotestyourself/gotestyourself/assert"
    11  	is "github.com/gotestyourself/gotestyourself/assert/cmp"
    12  )
    13  
    14  // tagging a named image in a new unprefixed repo should work
    15  func TestTagUnprefixedRepoByNameOrName(t *testing.T) {
    16  	defer setupTest(t)()
    17  	client := request.NewAPIClient(t)
    18  	ctx := context.Background()
    19  
    20  	// By name
    21  	err := client.ImageTag(ctx, "busybox:latest", "testfoobarbaz")
    22  	assert.NilError(t, err)
    23  
    24  	// By ID
    25  	insp, _, err := client.ImageInspectWithRaw(ctx, "busybox")
    26  	assert.NilError(t, err)
    27  	err = client.ImageTag(ctx, insp.ID, "testfoobarbaz")
    28  	assert.NilError(t, err)
    29  }
    30  
    31  // ensure we don't allow the use of invalid repository names or tags; these tag operations should fail
    32  // TODO (yongtang): Migrate to unit tests
    33  func TestTagInvalidReference(t *testing.T) {
    34  	defer setupTest(t)()
    35  	client := request.NewAPIClient(t)
    36  	ctx := context.Background()
    37  
    38  	invalidRepos := []string{"fo$z$", "Foo@3cc", "Foo$3", "Foo*3", "Fo^3", "Foo!3", "F)xcz(", "fo%asd", "FOO/bar"}
    39  
    40  	for _, repo := range invalidRepos {
    41  		err := client.ImageTag(ctx, "busybox", repo)
    42  		testutil.ErrorContains(t, err, "not a valid repository/tag")
    43  	}
    44  
    45  	longTag := testutil.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  		err := client.ImageTag(ctx, "busybox", repotag)
    51  		testutil.ErrorContains(t, err, "not a valid repository/tag")
    52  	}
    53  
    54  	// test repository name begin with '-'
    55  	err := client.ImageTag(ctx, "busybox:latest", "-busybox:test")
    56  	testutil.ErrorContains(t, err, "Error parsing reference")
    57  
    58  	// test namespace name begin with '-'
    59  	err = client.ImageTag(ctx, "busybox:latest", "-test/busybox:test")
    60  	testutil.ErrorContains(t, err, "Error parsing reference")
    61  
    62  	// test index name begin with '-'
    63  	err = client.ImageTag(ctx, "busybox:latest", "-index:5000/busybox:test")
    64  	testutil.ErrorContains(t, err, "Error parsing reference")
    65  
    66  	// test setting tag fails
    67  	err = client.ImageTag(ctx, "busybox:latest", "sha256:sometag")
    68  	testutil.ErrorContains(t, err, "refusing to create an ambiguous tag using digest algorithm as name")
    69  }
    70  
    71  // ensure we allow the use of valid tags
    72  func TestTagValidPrefixedRepo(t *testing.T) {
    73  	defer setupTest(t)()
    74  	client := request.NewAPIClient(t)
    75  	ctx := context.Background()
    76  
    77  	validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t", "HOSTNAME.DOMAIN.COM:443/foo/bar"}
    78  
    79  	for _, repo := range validRepos {
    80  		err := client.ImageTag(ctx, "busybox", repo)
    81  		assert.NilError(t, err)
    82  	}
    83  }
    84  
    85  // tag an image with an existed tag name without -f option should work
    86  func TestTagExistedNameWithoutForce(t *testing.T) {
    87  	defer setupTest(t)()
    88  	client := request.NewAPIClient(t)
    89  	ctx := context.Background()
    90  
    91  	err := client.ImageTag(ctx, "busybox:latest", "busybox:test")
    92  	assert.NilError(t, err)
    93  }
    94  
    95  // ensure tagging using official names works
    96  // ensure all tags result in the same name
    97  func TestTagOfficialNames(t *testing.T) {
    98  	defer setupTest(t)()
    99  	client := request.NewAPIClient(t)
   100  	ctx := context.Background()
   101  
   102  	names := []string{
   103  		"docker.io/busybox",
   104  		"index.docker.io/busybox",
   105  		"library/busybox",
   106  		"docker.io/library/busybox",
   107  		"index.docker.io/library/busybox",
   108  	}
   109  
   110  	for _, name := range names {
   111  		err := client.ImageTag(ctx, "busybox", name+":latest")
   112  		assert.NilError(t, err)
   113  
   114  		// ensure we don't have multiple tag names.
   115  		insp, _, err := client.ImageInspectWithRaw(ctx, "busybox")
   116  		assert.NilError(t, err)
   117  		assert.Assert(t, !is.Contains(insp.RepoTags, name)().Success())
   118  	}
   119  
   120  	for _, name := range names {
   121  		err := client.ImageTag(ctx, name+":latest", "fooo/bar:latest")
   122  		assert.NilError(t, err)
   123  	}
   124  }
   125  
   126  // ensure tags can not match digests
   127  func TestTagMatchesDigest(t *testing.T) {
   128  	defer setupTest(t)()
   129  	client := request.NewAPIClient(t)
   130  	ctx := context.Background()
   131  
   132  	digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507"
   133  	// test setting tag fails
   134  	err := client.ImageTag(ctx, "busybox:latest", digest)
   135  	testutil.ErrorContains(t, err, "refusing to create a tag with a digest reference")
   136  	// check that no new image matches the digest
   137  	_, _, err = client.ImageInspectWithRaw(ctx, digest)
   138  	testutil.ErrorContains(t, err, fmt.Sprintf("No such image: %s", digest))
   139  }