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