tag_test.go 3.1 KB

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