github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/image/tag_test.go (about)

     1  package image // import "github.com/Prakhar-Agarwal-byte/moby/integration/image"
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"gotest.tools/v3/assert"
     8  	is "gotest.tools/v3/assert/cmp"
     9  )
    10  
    11  // tagging a named image in a new unprefixed repo should work
    12  func TestTagUnprefixedRepoByNameOrName(t *testing.T) {
    13  	ctx := setupTest(t)
    14  
    15  	client := testEnv.APIClient()
    16  
    17  	// By name
    18  	err := client.ImageTag(ctx, "busybox:latest", "testfoobarbaz")
    19  	assert.NilError(t, err)
    20  
    21  	// By ID
    22  	insp, _, err := client.ImageInspectWithRaw(ctx, "busybox")
    23  	assert.NilError(t, err)
    24  	err = client.ImageTag(ctx, insp.ID, "testfoobarbaz")
    25  	assert.NilError(t, err)
    26  }
    27  
    28  func TestTagUsingDigestAlgorithmAsName(t *testing.T) {
    29  	ctx := setupTest(t)
    30  	client := testEnv.APIClient()
    31  	err := client.ImageTag(ctx, "busybox:latest", "sha256:sometag")
    32  	assert.Check(t, is.ErrorContains(err, "refusing to create an ambiguous tag using digest algorithm as name"))
    33  }
    34  
    35  // ensure we allow the use of valid tags
    36  func TestTagValidPrefixedRepo(t *testing.T) {
    37  	ctx := setupTest(t)
    38  
    39  	client := testEnv.APIClient()
    40  
    41  	validRepos := []string{"fooo/bar", "fooaa/test", "foooo:t", "HOSTNAME.DOMAIN.COM:443/foo/bar"}
    42  
    43  	for _, repo := range validRepos {
    44  		repo := repo
    45  		t.Run(repo, func(t *testing.T) {
    46  			t.Parallel()
    47  			err := client.ImageTag(ctx, "busybox", repo)
    48  			assert.NilError(t, err)
    49  		})
    50  	}
    51  }
    52  
    53  // tag an image with an existed tag name without -f option should work
    54  func TestTagExistedNameWithoutForce(t *testing.T) {
    55  	ctx := setupTest(t)
    56  	client := testEnv.APIClient()
    57  
    58  	err := client.ImageTag(ctx, "busybox:latest", "busybox:test")
    59  	assert.NilError(t, err)
    60  }
    61  
    62  // ensure tagging using official names works
    63  // ensure all tags result in the same name
    64  func TestTagOfficialNames(t *testing.T) {
    65  	ctx := setupTest(t)
    66  	client := testEnv.APIClient()
    67  
    68  	names := []string{
    69  		"docker.io/busybox",
    70  		"index.docker.io/busybox",
    71  		"library/busybox",
    72  		"docker.io/library/busybox",
    73  		"index.docker.io/library/busybox",
    74  	}
    75  
    76  	for _, name := range names {
    77  		name := name
    78  		t.Run("tag from busybox to "+name, func(t *testing.T) {
    79  			err := client.ImageTag(ctx, "busybox", name+":latest")
    80  			assert.NilError(t, err)
    81  
    82  			// ensure we don't have multiple tag names.
    83  			insp, _, err := client.ImageInspectWithRaw(ctx, "busybox")
    84  			assert.NilError(t, err)
    85  			// TODO(vvoland): Not sure what's actually being tested here. Is is still doing anything useful?
    86  			assert.Assert(t, !is.Contains(insp.RepoTags, name)().Success())
    87  
    88  			err = client.ImageTag(ctx, name+":latest", "test-tag-official-names/foobar:latest")
    89  			assert.NilError(t, err)
    90  		})
    91  	}
    92  }
    93  
    94  // ensure tags can not match digests
    95  func TestTagMatchesDigest(t *testing.T) {
    96  	ctx := setupTest(t)
    97  	client := testEnv.APIClient()
    98  
    99  	digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507"
   100  	// test setting tag fails
   101  	err := client.ImageTag(ctx, "busybox:latest", digest)
   102  	assert.Check(t, is.ErrorContains(err, "refusing to create a tag with a digest reference"))
   103  
   104  	// check that no new image matches the digest
   105  	_, _, err = client.ImageInspectWithRaw(ctx, digest)
   106  	assert.Check(t, is.ErrorContains(err, fmt.Sprintf("No such image: %s", digest)))
   107  }