2014-02-25 16:17:48 +00:00
package main
import (
2014-10-20 03:10:31 +00:00
"strings"
2015-03-24 11:25:26 +00:00
2015-10-08 19:42:41 +00:00
"github.com/docker/docker/pkg/integration/checker"
2015-03-24 11:25:26 +00:00
"github.com/docker/docker/pkg/stringutils"
2015-04-18 16:46:47 +00:00
"github.com/go-check/check"
2014-02-25 16:17:48 +00:00
)
// tagging a named image in a new unprefixed repo should work
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestTagUnprefixedRepoByName ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-06-30 19:42:09 +00:00
if err := pullImageIfNotExist ( "busybox:latest" ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "couldn't find the busybox:latest image locally and failed to pull it" )
2014-02-25 16:17:48 +00:00
}
2015-07-20 06:44:22 +00:00
dockerCmd ( c , "tag" , "busybox:latest" , "testfoobarbaz" )
2014-02-25 16:17:48 +00:00
}
// tagging an image by ID in a new unprefixed repo should work
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestTagUnprefixedRepoByID ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-05-18 02:06:13 +00:00
imageID , err := inspectField ( "busybox" , "Id" )
c . Assert ( err , check . IsNil )
2015-07-20 06:44:22 +00:00
dockerCmd ( c , "tag" , imageID , "testfoobarbaz" )
2014-02-25 16:17:48 +00:00
}
2014-09-11 14:32:44 +00:00
// ensure we don't allow the use of invalid repository names; these tag operations should fail
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestTagInvalidUnprefixedRepo ( c * check . C ) {
2014-09-11 14:32:44 +00:00
invalidRepos := [ ] string { "fo$z$" , "Foo@3cc" , "Foo$3" , "Foo*3" , "Fo^3" , "Foo!3" , "F)xcz(" , "fo%asd" }
2014-02-25 16:17:48 +00:00
for _ , repo := range invalidRepos {
2015-10-08 19:42:41 +00:00
out , _ , err := dockerCmdWithError ( "tag" , "busybox" , repo )
c . Assert ( err , checker . NotNil , check . Commentf ( "tag busybox %v should have failed : %v" , repo , out ) )
2014-09-11 14:32:44 +00:00
}
}
// ensure we don't allow the use of invalid tags; these tag operations should fail
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestTagInvalidPrefixedRepo ( c * check . C ) {
2015-03-26 01:40:23 +00:00
longTag := stringutils . GenerateRandomAlphaOnlyString ( 121 )
2014-09-11 14:32:44 +00:00
2015-03-26 01:40:23 +00:00
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 }
2014-09-11 14:32:44 +00:00
for _ , repotag := range invalidTags {
2015-10-08 19:42:41 +00:00
out , _ , err := dockerCmdWithError ( "tag" , "busybox" , repotag )
c . Assert ( err , checker . NotNil , check . Commentf ( "tag busybox %v should have failed : %v" , repotag , out ) )
2014-02-25 16:17:48 +00:00
}
}
// ensure we allow the use of valid tags
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestTagValidPrefixedRepo ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-06-30 19:42:09 +00:00
if err := pullImageIfNotExist ( "busybox:latest" ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "couldn't find the busybox:latest image locally and failed to pull it" )
2014-02-25 16:17:48 +00:00
}
2014-09-15 22:17:50 +00:00
validRepos := [ ] string { "fooo/bar" , "fooaa/test" , "foooo:t" }
2014-02-25 16:17:48 +00:00
for _ , repo := range validRepos {
2015-07-27 18:13:25 +00:00
_ , _ , err := dockerCmdWithError ( "tag" , "busybox:latest" , repo )
2014-02-25 16:17:48 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Errorf ( "tag busybox %v should have worked: %s" , repo , err )
2014-02-25 16:17:48 +00:00
continue
}
2014-04-04 00:22:32 +00:00
deleteImages ( repo )
2014-02-25 16:17:48 +00:00
}
}
2014-10-20 03:10:31 +00:00
// tag an image with an existed tag name without -f option should fail
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestTagExistedNameWithoutForce ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-10-20 03:10:31 +00:00
if err := pullImageIfNotExist ( "busybox:latest" ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "couldn't find the busybox:latest image locally and failed to pull it" )
2014-10-20 03:10:31 +00:00
}
2015-07-20 06:44:22 +00:00
dockerCmd ( c , "tag" , "busybox:latest" , "busybox:test" )
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "tag" , "busybox:latest" , "busybox:test" )
2015-10-08 19:42:41 +00:00
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
c . Assert ( out , checker . Contains , "Conflict: Tag busybox:test is already set to image" , check . Commentf ( "tag busybox busybox:test should have failed,because busybox:test is existed" ) )
2014-10-20 03:10:31 +00:00
}
// tag an image with an existed tag name with -f option should work
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestTagExistedNameWithForce ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-10-20 03:10:31 +00:00
if err := pullImageIfNotExist ( "busybox:latest" ) ; err != nil {
2015-04-18 16:46:47 +00:00
c . Fatal ( "couldn't find the busybox:latest image locally and failed to pull it" )
2014-10-20 03:10:31 +00:00
}
2015-07-20 06:44:22 +00:00
dockerCmd ( c , "tag" , "busybox:latest" , "busybox:test" )
dockerCmd ( c , "tag" , "-f" , "busybox:latest" , "busybox:test" )
2014-10-20 03:10:31 +00:00
}
2014-10-07 01:54:52 +00:00
2015-05-29 14:22:33 +00:00
func ( s * DockerSuite ) TestTagWithPrefixHyphen ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2015-05-13 06:23:13 +00:00
if err := pullImageIfNotExist ( "busybox:latest" ) ; err != nil {
c . Fatal ( "couldn't find the busybox:latest image locally and failed to pull it" )
}
2015-10-08 19:42:41 +00:00
2015-05-13 06:23:13 +00:00
// test repository name begin with '-'
2015-07-27 18:13:25 +00:00
out , _ , err := dockerCmdWithError ( "tag" , "busybox:latest" , "-busybox:test" )
2015-10-08 19:42:41 +00:00
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
c . Assert ( out , checker . Contains , "repository name component must match" , check . Commentf ( "tag a name begin with '-' should failed" ) )
2015-05-13 06:23:13 +00:00
// test namespace name begin with '-'
2015-07-27 18:13:25 +00:00
out , _ , err = dockerCmdWithError ( "tag" , "busybox:latest" , "-test/busybox:test" )
2015-10-08 19:42:41 +00:00
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
c . Assert ( out , checker . Contains , "repository name component must match" , check . Commentf ( "tag a name begin with '-' should failed" ) )
2015-08-07 22:24:18 +00:00
// test index name begin with '-'
2015-07-27 18:13:25 +00:00
out , _ , err = dockerCmdWithError ( "tag" , "busybox:latest" , "-index:5000/busybox:test" )
2015-10-08 19:42:41 +00:00
c . Assert ( err , checker . NotNil , check . Commentf ( out ) )
c . Assert ( out , checker . Contains , "Invalid index name (-index:5000). Cannot begin or end with a hyphen" , check . Commentf ( "tag a name begin with '-' should failed" ) )
2015-05-13 06:23:13 +00:00
}
2014-10-07 01:54:52 +00:00
// ensure tagging using official names works
// ensure all tags result in the same name
2015-04-18 16:46:47 +00:00
func ( s * DockerSuite ) TestTagOfficialNames ( c * check . C ) {
2015-08-28 17:36:42 +00:00
testRequires ( c , DaemonIsLinux )
2014-10-07 01:54:52 +00:00
names := [ ] string {
"docker.io/busybox" ,
"index.docker.io/busybox" ,
"library/busybox" ,
"docker.io/library/busybox" ,
"index.docker.io/library/busybox" ,
}
for _ , name := range names {
2015-07-27 18:13:25 +00:00
out , exitCode , err := dockerCmdWithError ( "tag" , "-f" , "busybox:latest" , name + ":latest" )
2014-10-07 01:54:52 +00:00
if err != nil || exitCode != 0 {
2015-04-18 16:46:47 +00:00
c . Errorf ( "tag busybox %v should have worked: %s, %s" , name , err , out )
2014-10-07 01:54:52 +00:00
continue
}
// ensure we don't have multiple tag names.
2015-07-27 18:13:25 +00:00
out , _ , err = dockerCmdWithError ( "images" )
2014-10-07 01:54:52 +00:00
if err != nil {
2015-04-18 16:46:47 +00:00
c . Errorf ( "listing images failed with errors: %v, %s" , err , out )
2014-10-07 01:54:52 +00:00
} else if strings . Contains ( out , name ) {
2015-04-18 16:46:47 +00:00
c . Errorf ( "images should not have listed '%s'" , name )
2014-10-07 01:54:52 +00:00
deleteImages ( name + ":latest" )
}
}
for _ , name := range names {
2015-07-27 18:13:25 +00:00
_ , exitCode , err := dockerCmdWithError ( "tag" , "-f" , name + ":latest" , "fooo/bar:latest" )
2014-10-07 01:54:52 +00:00
if err != nil || exitCode != 0 {
2015-04-18 16:46:47 +00:00
c . Errorf ( "tag %v fooo/bar should have worked: %s" , name , err )
2014-10-07 01:54:52 +00:00
continue
}
deleteImages ( "fooo/bar:latest" )
}
}
2015-09-14 17:20:11 +00:00
// ensure tags can not match digests
func ( s * DockerSuite ) TestTagMatchesDigest ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
if err := pullImageIfNotExist ( "busybox:latest" ) ; err != nil {
c . Fatal ( "couldn't find the busybox:latest image locally and failed to pull it" )
}
digest := "busybox@sha256:abcdef76720241213f5303bda7704ec4c2ef75613173910a56fb1b6e20251507"
// test setting tag fails
_ , _ , err := dockerCmdWithError ( "tag" , "-f" , "busybox:latest" , digest )
if err == nil {
c . Fatal ( "digest tag a name should have failed" )
}
// check that no new image matches the digest
_ , _ , err = dockerCmdWithError ( "inspect" , digest )
if err == nil {
c . Fatal ( "inspecting by digest should have failed" )
}
}