2014-02-25 11:17:48 -05:00
package main
import (
2014-04-17 22:24:19 -04:00
"strings"
2015-04-18 12:46:47 -04:00
2018-05-04 17:15:00 -04:00
"github.com/docker/docker/api/types/versions"
2016-12-30 12:23:00 -05:00
"github.com/docker/docker/integration-cli/checker"
2017-03-23 13:35:22 -04:00
"github.com/docker/docker/integration-cli/cli"
2015-04-18 12:46:47 -04:00
"github.com/go-check/check"
2014-02-25 11:17:48 -05:00
)
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestCommitAfterContainerIsDone ( c * testing . T ) {
2017-03-23 13:35:22 -04:00
out := cli . DockerCmd ( c , "run" , "-i" , "-a" , "stdin" , "busybox" , "echo" , "foo" ) . Combined ( )
2014-02-25 11:17:48 -05:00
2015-04-06 09:21:18 -04:00
cleanedContainerID := strings . TrimSpace ( out )
2014-02-25 11:17:48 -05:00
2017-03-23 13:35:22 -04:00
cli . DockerCmd ( c , "wait" , cleanedContainerID )
2014-02-25 11:17:48 -05:00
2017-03-23 13:35:22 -04:00
out = cli . DockerCmd ( c , "commit" , cleanedContainerID ) . Combined ( )
2014-02-25 11:17:48 -05:00
2015-04-06 09:21:18 -04:00
cleanedImageID := strings . TrimSpace ( out )
2014-02-25 11:17:48 -05:00
2017-03-23 13:35:22 -04:00
cli . DockerCmd ( c , "inspect" , cleanedImageID )
2014-06-08 02:37:31 -04:00
}
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestCommitWithoutPause ( c * testing . T ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-07-14 02:35:36 -04:00
out , _ := dockerCmd ( c , "run" , "-i" , "-a" , "stdin" , "busybox" , "echo" , "foo" )
2014-06-08 02:37:31 -04:00
2015-04-06 09:21:18 -04:00
cleanedContainerID := strings . TrimSpace ( out )
2014-06-08 02:37:31 -04:00
2015-07-14 02:35:36 -04:00
dockerCmd ( c , "wait" , cleanedContainerID )
2014-06-08 02:37:31 -04:00
2015-07-14 02:35:36 -04:00
out , _ = dockerCmd ( c , "commit" , "-p=false" , cleanedContainerID )
2014-06-08 02:37:31 -04:00
2015-04-06 09:21:18 -04:00
cleanedImageID := strings . TrimSpace ( out )
2014-06-08 02:37:31 -04:00
2015-07-14 02:35:36 -04:00
dockerCmd ( c , "inspect" , cleanedImageID )
2014-02-25 11:17:48 -05:00
}
2014-04-17 22:24:19 -04:00
2015-02-24 06:28:40 -05:00
//test commit a paused container should not unpause it after commit
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestCommitPausedContainer ( c * testing . T ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-07-14 02:35:36 -04:00
out , _ := dockerCmd ( c , "run" , "-i" , "-d" , "busybox" )
2015-02-24 06:28:40 -05:00
2015-04-06 09:21:18 -04:00
cleanedContainerID := strings . TrimSpace ( out )
2015-02-24 06:28:40 -05:00
2015-07-14 02:35:36 -04:00
dockerCmd ( c , "pause" , cleanedContainerID )
2018-07-09 13:40:34 -04:00
dockerCmd ( c , "commit" , cleanedContainerID )
2015-02-24 06:28:40 -05:00
2016-01-28 09:19:25 -05:00
out = inspectField ( c , cleanedContainerID , "State.Paused" )
2015-10-16 17:48:51 -04:00
// commit should not unpause a paused container
2019-09-09 17:05:55 -04:00
assert . Assert ( c , out , checker . Contains , "true" )
2015-02-24 06:28:40 -05:00
}
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestCommitNewFile ( c * testing . T ) {
2017-05-21 19:24:07 -04:00
dockerCmd ( c , "run" , "--name" , "committer" , "busybox" , "/bin/sh" , "-c" , "echo koye > /foo" )
2014-04-17 22:24:19 -04:00
2017-05-21 19:24:07 -04:00
imageID , _ := dockerCmd ( c , "commit" , "committer" )
2015-10-16 17:48:51 -04:00
imageID = strings . TrimSpace ( imageID )
2014-04-17 22:24:19 -04:00
2015-07-14 02:35:36 -04:00
out , _ := dockerCmd ( c , "run" , imageID , "cat" , "/foo" )
2015-10-16 17:48:51 -04:00
actual := strings . TrimSpace ( out )
2019-09-09 17:05:56 -04:00
assert . Equal ( c , actual , "koye" )
2014-04-17 22:24:19 -04:00
}
2014-03-27 15:16:03 -04:00
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestCommitHardlink ( c * testing . T ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-07-14 02:35:36 -04:00
firstOutput , _ := dockerCmd ( c , "run" , "-t" , "--name" , "hardlinks" , "busybox" , "sh" , "-c" , "touch file1 && ln file1 file2 && ls -di file1 file2" )
2014-09-15 14:45:53 -04:00
2015-07-14 02:35:36 -04:00
chunks := strings . Split ( strings . TrimSpace ( firstOutput ) , " " )
2014-09-15 14:45:53 -04:00
inode := chunks [ 0 ]
2015-10-16 17:48:51 -04:00
chunks = strings . SplitAfterN ( strings . TrimSpace ( firstOutput ) , " " , 2 )
2019-09-09 17:05:55 -04:00
assert . Assert ( c , chunks [ 1 ] , checker . Contains , chunks [ 0 ] , check . Commentf ( "Failed to create hardlink in a container. Expected to find %q in %q" , inode , chunks [ 1 : ] ) )
2014-09-15 14:45:53 -04:00
2015-07-14 02:35:36 -04:00
imageID , _ := dockerCmd ( c , "commit" , "hardlinks" , "hardlinks" )
2015-10-16 17:48:51 -04:00
imageID = strings . TrimSpace ( imageID )
2014-09-15 14:45:53 -04:00
2017-01-17 21:08:31 -05:00
secondOutput , _ := dockerCmd ( c , "run" , "-t" , imageID , "ls" , "-di" , "file1" , "file2" )
2014-09-15 14:45:53 -04:00
2015-07-14 02:35:36 -04:00
chunks = strings . Split ( strings . TrimSpace ( secondOutput ) , " " )
2014-09-15 14:45:53 -04:00
inode = chunks [ 0 ]
2015-10-16 17:48:51 -04:00
chunks = strings . SplitAfterN ( strings . TrimSpace ( secondOutput ) , " " , 2 )
2019-09-09 17:05:55 -04:00
assert . Assert ( c , chunks [ 1 ] , checker . Contains , chunks [ 0 ] , check . Commentf ( "Failed to create hardlink in a container. Expected to find %q in %q" , inode , chunks [ 1 : ] ) )
2014-09-15 14:45:53 -04:00
}
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestCommitTTY ( c * testing . T ) {
2015-07-14 02:35:36 -04:00
dockerCmd ( c , "run" , "-t" , "--name" , "tty" , "busybox" , "/bin/ls" )
2014-03-27 15:16:03 -04:00
2015-07-14 02:35:36 -04:00
imageID , _ := dockerCmd ( c , "commit" , "tty" , "ttytest" )
2015-10-16 17:48:51 -04:00
imageID = strings . TrimSpace ( imageID )
2014-03-27 15:16:03 -04:00
2017-01-17 21:08:31 -05:00
dockerCmd ( c , "run" , imageID , "/bin/ls" )
2014-03-27 15:16:03 -04:00
}
2014-05-19 18:57:29 -04:00
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestCommitWithHostBindMount ( c * testing . T ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-07-14 02:35:36 -04:00
dockerCmd ( c , "run" , "--name" , "bind-commit" , "-v" , "/dev/null:/winning" , "busybox" , "true" )
2014-10-14 14:26:53 -04:00
2015-07-14 02:35:36 -04:00
imageID , _ := dockerCmd ( c , "commit" , "bind-commit" , "bindtest" )
2015-10-16 17:48:51 -04:00
imageID = strings . TrimSpace ( imageID )
2014-05-19 18:57:29 -04:00
2017-01-17 21:08:31 -05:00
dockerCmd ( c , "run" , imageID , "true" )
2014-05-19 18:57:29 -04:00
}
2014-10-01 05:35:44 -04:00
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestCommitChange ( c * testing . T ) {
2015-07-14 02:35:36 -04:00
dockerCmd ( c , "run" , "--name" , "test" , "busybox" , "true" )
2015-02-05 06:26:05 -05:00
2015-07-14 02:35:36 -04:00
imageID , _ := dockerCmd ( c , "commit" ,
2014-10-01 05:35:44 -04:00
"--change" , "EXPOSE 8080" ,
"--change" , "ENV DEBUG true" ,
2015-01-12 14:52:44 -05:00
"--change" , "ENV test 1" ,
2015-02-25 03:04:46 -05:00
"--change" , "ENV PATH /foo" ,
2015-06-04 09:47:55 -04:00
"--change" , "LABEL foo bar" ,
"--change" , "CMD [\"/bin/sh\"]" ,
"--change" , "WORKDIR /opt" ,
"--change" , "ENTRYPOINT [\"/bin/sh\"]" ,
"--change" , "USER testuser" ,
"--change" , "VOLUME /var/lib/docker" ,
"--change" , "ONBUILD /usr/local/bin/python-build --dir /app/src" ,
2014-10-01 05:35:44 -04:00
"test" , "test-commit" )
2015-10-16 17:48:51 -04:00
imageID = strings . TrimSpace ( imageID )
2014-10-01 05:35:44 -04:00
2018-05-04 17:15:00 -04:00
expectedEnv := "[DEBUG=true test=1 PATH=/foo]"
// bug fixed in 1.36, add min APi >= 1.36 requirement
// PR record https://github.com/moby/moby/pull/35582
if versions . GreaterThan ( testEnv . DaemonAPIVersion ( ) , "1.35" ) && testEnv . OSType != "windows" {
// The ordering here is due to `PATH` being overridden from the container's
// ENV. On windows, the container doesn't have a `PATH` ENV variable so
// the ordering is the same as the cli.
expectedEnv = "[PATH=/foo DEBUG=true test=1]"
2017-11-22 16:31:26 -05:00
}
2016-11-16 18:02:27 -05:00
prefix , slash := getPrefixAndSlashFromDaemonPlatform ( )
2017-08-22 18:25:31 -04:00
prefix = strings . ToUpper ( prefix ) // Force C: as that's how WORKDIR is normalized on Windows
2014-10-01 05:35:44 -04:00
expected := map [ string ] string {
2015-03-26 15:43:00 -04:00
"Config.ExposedPorts" : "map[8080/tcp:{}]" ,
2017-11-22 16:31:26 -05:00
"Config.Env" : expectedEnv ,
2015-06-04 09:47:55 -04:00
"Config.Labels" : "map[foo:bar]" ,
2016-02-29 06:28:37 -05:00
"Config.Cmd" : "[/bin/sh]" ,
2016-11-16 18:02:27 -05:00
"Config.WorkingDir" : prefix + slash + "opt" ,
2016-02-29 06:28:37 -05:00
"Config.Entrypoint" : "[/bin/sh]" ,
2015-06-04 09:47:55 -04:00
"Config.User" : "testuser" ,
"Config.Volumes" : "map[/var/lib/docker:{}]" ,
"Config.OnBuild" : "[/usr/local/bin/python-build --dir /app/src]" ,
2014-10-01 05:35:44 -04:00
}
for conf , value := range expected {
2016-01-28 09:19:25 -05:00
res := inspectField ( c , imageID , conf )
2014-10-01 05:35:44 -04:00
if res != value {
2015-04-18 12:46:47 -04:00
c . Errorf ( "%s('%s'), expected %s" , conf , res , value )
2014-10-01 05:35:44 -04:00
}
}
}
2016-12-19 12:56:20 -05:00
2019-09-09 17:05:55 -04:00
func ( s * DockerSuite ) TestCommitChangeLabels ( c * testing . T ) {
2016-12-19 12:56:20 -05:00
dockerCmd ( c , "run" , "--name" , "test" , "--label" , "some=label" , "busybox" , "true" )
imageID , _ := dockerCmd ( c , "commit" ,
"--change" , "LABEL some=label2" ,
"test" , "test-commit" )
imageID = strings . TrimSpace ( imageID )
2019-09-09 17:05:56 -04:00
assert . Equal ( c , inspectField ( c , imageID , "Config.Labels" ) , "map[some:label2]" )
2016-12-19 12:56:20 -05:00
// check that container labels didn't change
2019-09-09 17:05:56 -04:00
assert . Equal ( c , inspectField ( c , "test" , "Config.Labels" ) , "map[some:label]" )
2016-12-19 12:56:20 -05:00
}