tag_test.go 4.4 KB

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