github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/image/tag_test.go (about)

     1  package image // import "github.com/demonoid81/moby/integration/image"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/demonoid81/moby/testutil"
     9  	"gotest.tools/v3/assert"
    10  	is "gotest.tools/v3/assert/cmp"
    11  	"gotest.tools/v3/skip"
    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 := testEnv.APIClient()
    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 := testEnv.APIClient()
    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  		assert.Check(t, is.ErrorContains(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  		assert.Check(t, is.ErrorContains(err, "not a valid repository/tag"))
    52  	}
    53  
    54  	// test repository name begin with '-'
    55  	err := client.ImageTag(ctx, "busybox:latest", "-busybox:test")
    56  	assert.Check(t, is.ErrorContains(err, "Error parsing reference"))
    57  
    58  	// test namespace name begin with '-'
    59  	err = client.ImageTag(ctx, "busybox:latest", "-test/busybox:test")
    60  	assert.Check(t, is.ErrorContains(err, "Error parsing reference"))
    61  
    62  	// test index name begin with '-'
    63  	err = client.ImageTag(ctx, "busybox:latest", "-index:5000/busybox:test")
    64  	assert.Check(t, is.ErrorContains(err, "Error parsing reference"))
    65  
    66  	// test setting tag fails
    67  	err = client.ImageTag(ctx, "busybox:latest", "sha256:sometag")
    68  	assert.Check(t, is.ErrorContains(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 := testEnv.APIClient()
    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 := testEnv.APIClient()
    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  	skip.If(t, testEnv.OSType == "windows")
    99  	defer setupTest(t)()
   100  	client := testEnv.APIClient()
   101  	ctx := context.Background()
   102  
   103  	names := []string{
   104  		"docker.io/busybox",
   105  		"index.docker.io/busybox",
   106  		"library/busybox",
   107  		"docker.io/library/busybox",
   108  		"index.docker.io/library/busybox",
   109  	}
   110  
   111  	for _, name := range names {
   112  		err := client.ImageTag(ctx, "busybox", name+":latest")
   113  		assert.NilError(t, err)
   114  
   115  		// ensure we don't have multiple tag names.
   116  		insp, _, err := client.ImageInspectWithRaw(ctx, "busybox")
   117  		assert.NilError(t, err)
   118  		assert.Assert(t, !is.Contains(insp.RepoTags, name)().Success())
   119  	}
   120  
   121  	for _, name := range names {
   122  		err := client.ImageTag(ctx, name+":latest", "fooo/bar:latest")
   123  		assert.NilError(t, err)
   124  	}
   125  }
   126  
   127  // ensure tags can not match digests
   128  func TestTagMatchesDigest(t *testing.T) {
   129  	defer setupTest(t)()
   130  	client := testEnv.APIClient()
   131  	ctx := context.Background()
   132  
   133  	digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507"
   134  	// test setting tag fails
   135  	err := client.ImageTag(ctx, "busybox:latest", digest)
   136  	assert.Check(t, is.ErrorContains(err, "refusing to create a tag with a digest reference"))
   137  
   138  	// check that no new image matches the digest
   139  	_, _, err = client.ImageInspectWithRaw(ctx, digest)
   140  	assert.Check(t, is.ErrorContains(err, fmt.Sprintf("No such image: %s", digest)))
   141  }