2014-07-17 04:29:52 -04:00
package main
import (
2015-01-23 07:17:05 -05:00
"fmt"
2015-08-14 18:00:48 -04:00
"io/ioutil"
"os"
"path/filepath"
2015-11-09 09:37:24 -05:00
"sort"
2015-01-23 07:17:05 -05:00
"strconv"
2014-07-17 04:29:52 -04:00
"strings"
2014-09-12 03:45:50 -04:00
"time"
2015-04-18 12:46:47 -04:00
2016-12-30 12:23:00 -05:00
"github.com/docker/docker/integration-cli/checker"
2017-03-27 11:12:48 -04:00
"github.com/docker/docker/integration-cli/cli"
2017-03-23 13:35:22 -04:00
"github.com/docker/docker/integration-cli/cli/build"
2015-08-20 03:57:15 -04:00
"github.com/docker/docker/pkg/stringid"
2015-11-09 09:37:24 -05:00
"github.com/go-check/check"
2017-08-23 17:01:29 -04:00
"github.com/gotestyourself/gotestyourself/icmd"
2014-07-17 04:29:52 -04:00
)
2015-08-26 04:16:53 -04:00
func ( s * DockerSuite ) TestPsListContainersBase ( c * check . C ) {
2017-04-16 17:39:30 -04:00
out := runSleepingContainer ( c , "-d" )
2015-04-06 09:21:18 -04:00
firstID := strings . TrimSpace ( out )
2014-07-17 04:29:52 -04:00
2017-04-16 17:39:30 -04:00
out = runSleepingContainer ( c , "-d" )
2015-04-06 09:21:18 -04:00
secondID := strings . TrimSpace ( out )
2014-07-17 04:29:52 -04:00
// not long running
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "run" , "-d" , "busybox" , "true" )
2015-04-06 09:21:18 -04:00
thirdID := strings . TrimSpace ( out )
2014-07-17 04:29:52 -04:00
2017-04-16 17:39:30 -04:00
out = runSleepingContainer ( c , "-d" )
2015-04-06 09:21:18 -04:00
fourthID := strings . TrimSpace ( out )
2014-07-17 04:29:52 -04:00
2015-04-08 19:20:42 -04:00
// make sure the second is running
2015-10-14 03:03:09 -04:00
c . Assert ( waitRun ( secondID ) , checker . IsNil )
2015-04-08 19:20:42 -04:00
2014-07-17 04:29:52 -04:00
// make sure third one is not running
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "wait" , thirdID )
2014-07-17 04:29:52 -04:00
2015-04-08 19:20:42 -04:00
// make sure the forth is running
2015-10-14 03:03:09 -04:00
c . Assert ( waitRun ( fourthID ) , checker . IsNil )
2015-04-08 19:20:42 -04:00
2014-07-17 04:29:52 -04:00
// all
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-a" )
2015-10-14 03:03:09 -04:00
c . Assert ( assertContainerList ( out , [ ] string { fourthID , thirdID , secondID , firstID } ) , checker . Equals , true , check . Commentf ( "ALL: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
// running
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" )
2015-10-14 03:03:09 -04:00
c . Assert ( assertContainerList ( out , [ ] string { fourthID , secondID , firstID } ) , checker . Equals , true , check . Commentf ( "RUNNING: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
// limit
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-n=2" , "-a" )
2014-07-17 04:29:52 -04:00
expected := [ ] string { fourthID , thirdID }
2015-10-14 03:03:09 -04:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "LIMIT & ALL: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-n=2" )
2015-10-14 03:03:09 -04:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "LIMIT: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-11-05 02:08:00 -05:00
// filter since
out , _ = dockerCmd ( c , "ps" , "-f" , "since=" + firstID , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { fourthID , thirdID , secondID }
2016-02-09 03:06:29 -05:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter & ALL: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-11-05 02:08:00 -05:00
out , _ = dockerCmd ( c , "ps" , "-f" , "since=" + firstID )
2016-02-09 03:06:29 -05:00
expected = [ ] string { fourthID , secondID }
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2016-02-19 01:07:16 -05:00
out , _ = dockerCmd ( c , "ps" , "-f" , "since=" + thirdID )
expected = [ ] string { fourthID }
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter: Container list is not in the correct order: \n%s" , out ) )
2015-11-05 02:08:00 -05:00
// filter before
2016-02-09 03:06:29 -05:00
out , _ = dockerCmd ( c , "ps" , "-f" , "before=" + fourthID , "-a" )
expected = [ ] string { thirdID , secondID , firstID }
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "BEFORE filter & ALL: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2016-02-09 03:06:29 -05:00
out , _ = dockerCmd ( c , "ps" , "-f" , "before=" + fourthID )
expected = [ ] string { secondID , firstID }
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "BEFORE filter: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2016-02-19 01:07:16 -05:00
out , _ = dockerCmd ( c , "ps" , "-f" , "before=" + thirdID )
expected = [ ] string { secondID , firstID }
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter: Container list is not in the correct order: \n%s" , out ) )
2015-11-05 02:08:00 -05:00
// filter since & before
out , _ = dockerCmd ( c , "ps" , "-f" , "since=" + firstID , "-f" , "before=" + fourthID , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { thirdID , secondID }
2016-02-09 03:06:29 -05:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter, BEFORE filter & ALL: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-11-05 02:08:00 -05:00
out , _ = dockerCmd ( c , "ps" , "-f" , "since=" + firstID , "-f" , "before=" + fourthID )
2016-02-09 03:06:29 -05:00
expected = [ ] string { secondID }
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter, BEFORE filter: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-11-05 02:08:00 -05:00
// filter since & limit
out , _ = dockerCmd ( c , "ps" , "-f" , "since=" + firstID , "-n=2" , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { fourthID , thirdID }
2016-02-09 03:06:29 -05:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter, LIMIT & ALL: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-11-05 02:08:00 -05:00
out , _ = dockerCmd ( c , "ps" , "-f" , "since=" + firstID , "-n=2" )
2016-02-09 03:06:29 -05:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter, LIMIT: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-11-05 02:08:00 -05:00
// filter before & limit
out , _ = dockerCmd ( c , "ps" , "-f" , "before=" + fourthID , "-n=1" , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { thirdID }
2016-02-09 03:06:29 -05:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-11-05 02:08:00 -05:00
out , _ = dockerCmd ( c , "ps" , "-f" , "before=" + fourthID , "-n=1" )
2016-02-09 03:06:29 -05:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "BEFORE filter, LIMIT: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-11-05 02:08:00 -05:00
// filter since & filter before & limit
out , _ = dockerCmd ( c , "ps" , "-f" , "since=" + firstID , "-f" , "before=" + fourthID , "-n=1" , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { thirdID }
2016-02-09 03:06:29 -05:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter, BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s" , out ) )
2014-07-17 04:29:52 -04:00
2015-11-05 02:08:00 -05:00
out , _ = dockerCmd ( c , "ps" , "-f" , "since=" + firstID , "-f" , "before=" + fourthID , "-n=1" )
2016-02-09 03:06:29 -05:00
c . Assert ( assertContainerList ( out , expected ) , checker . Equals , true , check . Commentf ( "SINCE filter, BEFORE filter, LIMIT: Container list is not in the correct order: \n%s" , out ) )
}
2014-07-17 04:29:52 -04:00
func assertContainerList ( out string , expected [ ] string ) bool {
lines := strings . Split ( strings . Trim ( out , "\n " ) , "\n" )
2016-02-09 03:06:29 -05:00
2014-07-17 04:29:52 -04:00
if len ( lines ) - 1 != len ( expected ) {
return false
}
2014-10-06 10:26:55 -04:00
containerIDIndex := strings . Index ( lines [ 0 ] , "CONTAINER ID" )
2014-07-17 04:29:52 -04:00
for i := 0 ; i < len ( expected ) ; i ++ {
2014-10-06 10:26:55 -04:00
foundID := lines [ i + 1 ] [ containerIDIndex : containerIDIndex + 12 ]
2014-07-17 04:29:52 -04:00
if foundID != expected [ i ] [ : 12 ] {
return false
}
}
return true
}
2014-09-12 03:45:50 -04:00
2016-09-28 06:45:30 -04:00
// FIXME(vdemeester) Move this into a unit test in daemon package
2016-03-14 23:54:12 -04:00
func ( s * DockerSuite ) TestPsListContainersInvalidFilterName ( c * check . C ) {
out , _ , err := dockerCmdWithError ( "ps" , "-f" , "invalidFilter=test" )
c . Assert ( err , checker . NotNil )
c . Assert ( out , checker . Contains , "Invalid filter" )
}
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersSize ( c * check . C ) {
2016-01-22 23:44:46 -05:00
// Problematic on Windows as it doesn't report the size correctly @swernli
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "-d" , "busybox" )
2015-05-19 14:33:59 -04:00
baseOut , _ := dockerCmd ( c , "ps" , "-s" , "-n=1" )
2015-03-25 21:40:23 -04:00
baseLines := strings . Split ( strings . Trim ( baseOut , "\n " ) , "\n" )
baseSizeIndex := strings . Index ( baseLines [ 0 ] , "SIZE" )
baseFoundsize := baseLines [ 1 ] [ baseSizeIndex : ]
2017-02-07 15:58:56 -05:00
baseBytes , err := strconv . Atoi ( strings . Split ( baseFoundsize , "B" ) [ 0 ] )
2015-10-14 03:03:09 -04:00
c . Assert ( err , checker . IsNil )
2015-01-23 07:17:05 -05:00
2014-09-12 03:45:50 -04:00
name := "test_size"
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name" , name , "busybox" , "sh" , "-c" , "echo 1 > test" )
2017-01-16 05:30:14 -05:00
id := getIDByName ( c , name )
2014-09-12 03:45:50 -04:00
2017-01-16 10:39:12 -05:00
var result * icmd . Result
2015-05-19 14:33:59 -04:00
2014-09-12 03:45:50 -04:00
wait := make ( chan struct { } )
go func ( ) {
2017-01-16 10:39:12 -05:00
result = icmd . RunCommand ( dockerBinary , "ps" , "-s" , "-n=1" )
2014-09-12 03:45:50 -04:00
close ( wait )
} ( )
select {
case <- wait :
case <- time . After ( 3 * time . Second ) :
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Calling \"docker ps -s\" timed out!" )
2014-09-12 03:45:50 -04:00
}
2017-01-16 10:39:12 -05:00
result . Assert ( c , icmd . Success )
lines := strings . Split ( strings . Trim ( result . Combined ( ) , "\n " ) , "\n" )
2015-10-14 03:03:09 -04:00
c . Assert ( lines , checker . HasLen , 2 , check . Commentf ( "Expected 2 lines for 'ps -s -n=1' output, got %d" , len ( lines ) ) )
2014-09-12 03:45:50 -04:00
sizeIndex := strings . Index ( lines [ 0 ] , "SIZE" )
idIndex := strings . Index ( lines [ 0 ] , "CONTAINER ID" )
foundID := lines [ 1 ] [ idIndex : idIndex + 12 ]
2015-10-14 03:03:09 -04:00
c . Assert ( foundID , checker . Equals , id [ : 12 ] , check . Commentf ( "Expected id %s, got %s" , id [ : 12 ] , foundID ) )
2017-02-07 15:58:56 -05:00
expectedSize := fmt . Sprintf ( "%dB" , ( 2 + baseBytes ) )
2014-09-12 03:45:50 -04:00
foundSize := lines [ 1 ] [ sizeIndex : ]
2015-10-14 03:03:09 -04:00
c . Assert ( foundSize , checker . Contains , expectedSize , check . Commentf ( "Expected size %q, got %q" , expectedSize , foundSize ) )
2014-09-26 19:25:50 -04:00
}
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterStatus ( c * check . C ) {
2014-09-26 19:25:50 -04:00
// start exited container
2017-03-27 11:12:48 -04:00
out := cli . DockerCmd ( c , "run" , "-d" , "busybox" ) . Combined ( )
2015-04-06 09:21:18 -04:00
firstID := strings . TrimSpace ( out )
2014-09-26 19:25:50 -04:00
2015-11-11 12:51:36 -05:00
// make sure the exited container is not running
2017-03-27 11:12:48 -04:00
cli . DockerCmd ( c , "wait" , firstID )
2014-09-26 19:25:50 -04:00
// start running container
2017-03-27 11:12:48 -04:00
out = cli . DockerCmd ( c , "run" , "-itd" , "busybox" ) . Combined ( )
2015-04-06 09:21:18 -04:00
secondID := strings . TrimSpace ( out )
2014-09-26 19:25:50 -04:00
// filter containers by exited
2017-03-27 11:12:48 -04:00
out = cli . DockerCmd ( c , "ps" , "--no-trunc" , "-q" , "--filter=status=exited" ) . Combined ( )
2014-09-26 19:25:50 -04:00
containerOut := strings . TrimSpace ( out )
2015-11-11 12:51:36 -05:00
c . Assert ( containerOut , checker . Equals , firstID )
2014-09-26 19:25:50 -04:00
2017-03-27 11:12:48 -04:00
out = cli . DockerCmd ( c , "ps" , "-a" , "--no-trunc" , "-q" , "--filter=status=running" ) . Combined ( )
2014-09-26 19:25:50 -04:00
containerOut = strings . TrimSpace ( out )
2015-11-11 12:51:36 -05:00
c . Assert ( containerOut , checker . Equals , secondID )
2014-09-26 19:25:50 -04:00
2017-03-27 11:12:48 -04:00
result := cli . Docker ( cli . Args ( "ps" , "-a" , "-q" , "--filter=status=rubbish" ) , cli . WithTimeout ( time . Second * 60 ) )
2017-08-23 17:01:29 -04:00
result . Assert ( c , icmd . Expected {
2016-08-04 12:57:34 -04:00
ExitCode : 1 ,
2017-07-19 10:20:13 -04:00
Err : "Invalid filter 'status=rubbish'" ,
2016-08-04 12:57:34 -04:00
} )
2015-06-30 17:40:27 -04:00
2016-01-22 23:44:46 -05:00
// Windows doesn't support pausing of containers
2017-01-13 11:23:28 -05:00
if testEnv . DaemonPlatform ( ) != "windows" {
2016-01-22 23:44:46 -05:00
// pause running container
2017-03-27 11:12:48 -04:00
out = cli . DockerCmd ( c , "run" , "-itd" , "busybox" ) . Combined ( )
2016-01-22 23:44:46 -05:00
pausedID := strings . TrimSpace ( out )
2017-03-27 11:12:48 -04:00
cli . DockerCmd ( c , "pause" , pausedID )
2016-01-22 23:44:46 -05:00
// make sure the container is unpaused to let the daemon stop it properly
2017-03-27 11:12:48 -04:00
defer func ( ) { cli . DockerCmd ( c , "unpause" , pausedID ) } ( )
2016-01-22 23:44:46 -05:00
2017-03-27 11:12:48 -04:00
out = cli . DockerCmd ( c , "ps" , "--no-trunc" , "-q" , "--filter=status=paused" ) . Combined ( )
2016-01-22 23:44:46 -05:00
containerOut = strings . TrimSpace ( out )
c . Assert ( containerOut , checker . Equals , pausedID )
}
2014-09-12 03:45:50 -04:00
}
2014-10-13 02:12:44 -04:00
2016-07-15 14:21:19 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterHealth ( c * check . C ) {
// Test legacy no health check
2017-04-16 17:39:30 -04:00
out := runSleepingContainer ( c , "--name=none_legacy" )
2016-07-15 14:21:19 -04:00
containerID := strings . TrimSpace ( out )
2017-04-11 15:18:30 -04:00
cli . WaitRun ( c , containerID )
2016-07-15 14:21:19 -04:00
2017-04-11 15:18:30 -04:00
out = cli . DockerCmd ( c , "ps" , "-q" , "-l" , "--no-trunc" , "--filter=health=none" ) . Combined ( )
2016-07-15 14:21:19 -04:00
containerOut := strings . TrimSpace ( out )
c . Assert ( containerOut , checker . Equals , containerID , check . Commentf ( "Expected id %s, got %s for legacy none filter, output: %q" , containerID , containerOut , out ) )
// Test no health check specified explicitly
2017-04-16 17:39:30 -04:00
out = runSleepingContainer ( c , "--name=none" , "--no-healthcheck" )
2016-07-15 14:21:19 -04:00
containerID = strings . TrimSpace ( out )
2017-04-11 15:18:30 -04:00
cli . WaitRun ( c , containerID )
2016-07-15 14:21:19 -04:00
2017-04-11 15:18:30 -04:00
out = cli . DockerCmd ( c , "ps" , "-q" , "-l" , "--no-trunc" , "--filter=health=none" ) . Combined ( )
2016-07-15 14:21:19 -04:00
containerOut = strings . TrimSpace ( out )
c . Assert ( containerOut , checker . Equals , containerID , check . Commentf ( "Expected id %s, got %s for none filter, output: %q" , containerID , containerOut , out ) )
// Test failing health check
2017-04-16 17:39:30 -04:00
out = runSleepingContainer ( c , "--name=failing_container" , "--health-cmd=exit 1" , "--health-interval=1s" )
2016-07-15 14:21:19 -04:00
containerID = strings . TrimSpace ( out )
waitForHealthStatus ( c , "failing_container" , "starting" , "unhealthy" )
2017-04-11 15:18:30 -04:00
out = cli . DockerCmd ( c , "ps" , "-q" , "--no-trunc" , "--filter=health=unhealthy" ) . Combined ( )
2016-07-15 14:21:19 -04:00
containerOut = strings . TrimSpace ( out )
c . Assert ( containerOut , checker . Equals , containerID , check . Commentf ( "Expected containerID %s, got %s for unhealthy filter, output: %q" , containerID , containerOut , out ) )
// Check passing healthcheck
2017-04-16 17:39:30 -04:00
out = runSleepingContainer ( c , "--name=passing_container" , "--health-cmd=exit 0" , "--health-interval=1s" )
2016-07-15 14:21:19 -04:00
containerID = strings . TrimSpace ( out )
waitForHealthStatus ( c , "passing_container" , "starting" , "healthy" )
2017-04-11 15:18:30 -04:00
out = cli . DockerCmd ( c , "ps" , "-q" , "--no-trunc" , "--filter=health=healthy" ) . Combined ( )
2016-07-15 14:21:19 -04:00
containerOut = strings . TrimSpace ( out )
c . Assert ( containerOut , checker . Equals , containerID , check . Commentf ( "Expected containerID %s, got %s for healthy filter, output: %q" , containerID , containerOut , out ) )
}
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterID ( c * check . C ) {
2014-10-13 02:12:44 -04:00
// start container
2015-05-19 14:33:59 -04:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" )
2015-04-06 09:21:18 -04:00
firstID := strings . TrimSpace ( out )
2014-10-13 02:12:44 -04:00
// start another container
2016-01-26 23:16:36 -05:00
runSleepingContainer ( c )
2014-10-13 02:12:44 -04:00
// filter containers by id
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-a" , "-q" , "--filter=id=" + firstID )
2014-10-13 02:12:44 -04:00
containerOut := strings . TrimSpace ( out )
2015-10-14 03:03:09 -04:00
c . Assert ( containerOut , checker . Equals , firstID [ : 12 ] , check . Commentf ( "Expected id %s, got %s for exited filter, output: %q" , firstID [ : 12 ] , containerOut , out ) )
2014-10-13 02:12:44 -04:00
}
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterName ( c * check . C ) {
2014-10-13 02:12:44 -04:00
// start container
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name=a_name_to_match" , "busybox" )
2017-01-16 05:30:14 -05:00
id := getIDByName ( c , "a_name_to_match" )
2014-10-13 02:12:44 -04:00
// start another container
2016-01-26 23:16:36 -05:00
runSleepingContainer ( c , "--name=b_name_to_match" )
2014-10-13 02:12:44 -04:00
// filter containers by name
2016-02-28 05:47:37 -05:00
out , _ := dockerCmd ( c , "ps" , "-a" , "-q" , "--filter=name=a_name_to_match" )
2014-10-13 02:12:44 -04:00
containerOut := strings . TrimSpace ( out )
2016-02-28 05:47:37 -05:00
c . Assert ( containerOut , checker . Equals , id [ : 12 ] , check . Commentf ( "Expected id %s, got %s for exited filter, output: %q" , id [ : 12 ] , containerOut , out ) )
2014-10-13 02:12:44 -04:00
}
2014-11-03 13:50:16 -05:00
2015-08-20 03:57:15 -04:00
// Test for the ancestor filter for ps.
// There is also the same test but with image:tag@digest in docker_cli_by_digest_test.go
//
// What the test setups :
// - Create 2 image based on busybox using the same repository but different tags
// - Create an image based on the previous image (images_ps_filter_test2)
// - Run containers for each of those image (busybox, images_ps_filter_test1, images_ps_filter_test2)
// - Filter them out :P
func ( s * DockerSuite ) TestPsListContainersFilterAncestorImage ( c * check . C ) {
// Build images
imageName1 := "images_ps_filter_test1"
2017-03-23 13:35:22 -04:00
buildImageSuccessfully ( c , imageName1 , build . WithDockerfile ( ` FROM busybox
2017-01-16 05:30:14 -05:00
LABEL match me 1 ` ) )
imageID1 := getIDByName ( c , imageName1 )
2015-08-20 03:57:15 -04:00
imageName1Tagged := "images_ps_filter_test1:tag"
2017-03-23 13:35:22 -04:00
buildImageSuccessfully ( c , imageName1Tagged , build . WithDockerfile ( ` FROM busybox
2017-01-16 05:30:14 -05:00
LABEL match me 1 tagged ` ) )
imageID1Tagged := getIDByName ( c , imageName1Tagged )
2015-08-20 03:57:15 -04:00
imageName2 := "images_ps_filter_test2"
2017-03-23 13:35:22 -04:00
buildImageSuccessfully ( c , imageName2 , build . WithDockerfile ( fmt . Sprintf ( ` FROM % s
2017-01-16 05:30:14 -05:00
LABEL match me 2 ` , imageName1 ) ) )
imageID2 := getIDByName ( c , imageName2 )
2015-08-20 03:57:15 -04:00
// start containers
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name=first" , "busybox" , "echo" , "hello" )
2017-01-16 05:30:14 -05:00
firstID := getIDByName ( c , "first" )
2015-08-20 03:57:15 -04:00
// start another container
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name=second" , "busybox" , "echo" , "hello" )
2017-01-16 05:30:14 -05:00
secondID := getIDByName ( c , "second" )
2015-08-20 03:57:15 -04:00
// start third container
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name=third" , imageName1 , "echo" , "hello" )
2017-01-16 05:30:14 -05:00
thirdID := getIDByName ( c , "third" )
2015-08-20 03:57:15 -04:00
// start fourth container
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name=fourth" , imageName1Tagged , "echo" , "hello" )
2017-01-16 05:30:14 -05:00
fourthID := getIDByName ( c , "fourth" )
2015-08-20 03:57:15 -04:00
// start fifth container
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name=fifth" , imageName2 , "echo" , "hello" )
2017-01-16 05:30:14 -05:00
fifthID := getIDByName ( c , "fifth" )
2015-08-20 03:57:15 -04:00
var filterTestSuite = [ ] struct {
filterName string
expectedIDs [ ] string
} {
// non existent stuff
{ "nonexistent" , [ ] string { } } ,
{ "nonexistent:tag" , [ ] string { } } ,
// image
{ "busybox" , [ ] string { firstID , secondID , thirdID , fourthID , fifthID } } ,
{ imageName1 , [ ] string { thirdID , fifthID } } ,
{ imageName2 , [ ] string { fifthID } } ,
// image:tag
{ fmt . Sprintf ( "%s:latest" , imageName1 ) , [ ] string { thirdID , fifthID } } ,
{ imageName1Tagged , [ ] string { fourthID } } ,
// short-id
{ stringid . TruncateID ( imageID1 ) , [ ] string { thirdID , fifthID } } ,
{ stringid . TruncateID ( imageID2 ) , [ ] string { fifthID } } ,
// full-id
{ imageID1 , [ ] string { thirdID , fifthID } } ,
{ imageID1Tagged , [ ] string { fourthID } } ,
{ imageID2 , [ ] string { fifthID } } ,
}
2016-02-28 05:47:37 -05:00
var out string
2015-08-20 03:57:15 -04:00
for _ , filter := range filterTestSuite {
out , _ = dockerCmd ( c , "ps" , "-a" , "-q" , "--no-trunc" , "--filter=ancestor=" + filter . filterName )
checkPsAncestorFilterOutput ( c , out , filter . filterName , filter . expectedIDs )
}
// Multiple ancestor filter
out , _ = dockerCmd ( c , "ps" , "-a" , "-q" , "--no-trunc" , "--filter=ancestor=" + imageName2 , "--filter=ancestor=" + imageName1Tagged )
checkPsAncestorFilterOutput ( c , out , imageName2 + "," + imageName1Tagged , [ ] string { fourthID , fifthID } )
}
func checkPsAncestorFilterOutput ( c * check . C , out string , filterName string , expectedIDs [ ] string ) {
actualIDs := [ ] string { }
if out != "" {
actualIDs = strings . Split ( out [ : len ( out ) - 1 ] , "\n" )
}
sort . Strings ( actualIDs )
sort . Strings ( expectedIDs )
2015-10-14 03:03:09 -04:00
c . Assert ( actualIDs , checker . HasLen , len ( expectedIDs ) , check . Commentf ( "Expected filtered container(s) for %s ancestor filter to be %v:%v, got %v:%v" , filterName , len ( expectedIDs ) , expectedIDs , len ( actualIDs ) , actualIDs ) )
2015-08-20 03:57:15 -04:00
if len ( expectedIDs ) > 0 {
same := true
for i := range expectedIDs {
if actualIDs [ i ] != expectedIDs [ i ] {
c . Logf ( "%s, %s" , actualIDs [ i ] , expectedIDs [ i ] )
same = false
break
}
}
2015-10-14 03:03:09 -04:00
c . Assert ( same , checker . Equals , true , check . Commentf ( "Expected filtered container(s) for %s ancestor filter to be %v, got %v" , filterName , expectedIDs , actualIDs ) )
2015-08-20 03:57:15 -04:00
}
}
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterLabel ( c * check . C ) {
2015-01-06 19:04:10 -05:00
// start container
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name=first" , "-l" , "match=me" , "-l" , "second=tag" , "busybox" )
2017-01-16 05:30:14 -05:00
firstID := getIDByName ( c , "first" )
2015-01-06 19:04:10 -05:00
// start another container
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name=second" , "-l" , "match=me too" , "busybox" )
2017-01-16 05:30:14 -05:00
secondID := getIDByName ( c , "second" )
2015-01-06 19:04:10 -05:00
// start third container
2016-02-28 05:47:37 -05:00
dockerCmd ( c , "run" , "--name=third" , "-l" , "nomatch=me" , "busybox" )
2017-01-16 05:30:14 -05:00
thirdID := getIDByName ( c , "third" )
2015-01-06 19:04:10 -05:00
// filter containers by exact match
2016-02-28 05:47:37 -05:00
out , _ := dockerCmd ( c , "ps" , "-a" , "-q" , "--no-trunc" , "--filter=label=match=me" )
2015-01-06 19:04:10 -05:00
containerOut := strings . TrimSpace ( out )
2015-10-14 03:03:09 -04:00
c . Assert ( containerOut , checker . Equals , firstID , check . Commentf ( "Expected id %s, got %s for exited filter, output: %q" , firstID , containerOut , out ) )
2015-01-06 19:04:10 -05:00
2015-03-09 01:18:59 -04:00
// filter containers by two labels
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-a" , "-q" , "--no-trunc" , "--filter=label=match=me" , "--filter=label=second=tag" )
2015-03-09 01:18:59 -04:00
containerOut = strings . TrimSpace ( out )
2015-10-14 03:03:09 -04:00
c . Assert ( containerOut , checker . Equals , firstID , check . Commentf ( "Expected id %s, got %s for exited filter, output: %q" , firstID , containerOut , out ) )
2015-03-09 01:18:59 -04:00
// filter containers by two labels, but expect not found because of AND behavior
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-a" , "-q" , "--no-trunc" , "--filter=label=match=me" , "--filter=label=second=tag-no" )
2015-03-09 01:18:59 -04:00
containerOut = strings . TrimSpace ( out )
2015-10-14 03:03:09 -04:00
c . Assert ( containerOut , checker . Equals , "" , check . Commentf ( "Expected nothing, got %s for exited filter, output: %q" , containerOut , out ) )
2015-03-09 01:18:59 -04:00
2015-01-06 19:04:10 -05:00
// filter containers by exact key
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-a" , "-q" , "--no-trunc" , "--filter=label=match" )
2015-01-06 19:04:10 -05:00
containerOut = strings . TrimSpace ( out )
2015-10-14 03:03:09 -04:00
c . Assert ( containerOut , checker . Contains , firstID )
c . Assert ( containerOut , checker . Contains , secondID )
c . Assert ( containerOut , checker . Not ( checker . Contains ) , thirdID )
2015-01-06 19:04:10 -05:00
}
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterExited ( c * check . C ) {
2016-01-26 23:16:36 -05:00
runSleepingContainer ( c , "--name=sleep" )
2015-01-08 17:51:47 -05:00
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "run" , "--name" , "zero1" , "busybox" , "true" )
2017-01-16 05:30:14 -05:00
firstZero := getIDByName ( c , "zero1" )
2014-11-03 13:50:16 -05:00
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "run" , "--name" , "zero2" , "busybox" , "true" )
2017-01-16 05:30:14 -05:00
secondZero := getIDByName ( c , "zero2" )
2014-11-03 13:50:16 -05:00
2015-10-14 03:03:09 -04:00
out , _ , err := dockerCmdWithError ( "run" , "--name" , "nonzero1" , "busybox" , "false" )
c . Assert ( err , checker . NotNil , check . Commentf ( "Should fail." , out , err ) )
2015-05-19 14:33:59 -04:00
2017-01-16 05:30:14 -05:00
firstNonZero := getIDByName ( c , "nonzero1" )
2014-11-03 13:50:16 -05:00
2015-10-14 03:03:09 -04:00
out , _ , err = dockerCmdWithError ( "run" , "--name" , "nonzero2" , "busybox" , "false" )
c . Assert ( err , checker . NotNil , check . Commentf ( "Should fail." , out , err ) )
2017-01-16 05:30:14 -05:00
secondNonZero := getIDByName ( c , "nonzero2" )
2014-11-03 13:50:16 -05:00
// filter containers by exited=0
2015-10-14 03:03:09 -04:00
out , _ = dockerCmd ( c , "ps" , "-a" , "-q" , "--no-trunc" , "--filter=exited=0" )
2014-11-03 13:50:16 -05:00
ids := strings . Split ( strings . TrimSpace ( out ) , "\n" )
2015-10-14 03:03:09 -04:00
c . Assert ( ids , checker . HasLen , 2 , check . Commentf ( "Should be 2 zero exited containers got %d: %s" , len ( ids ) , out ) )
c . Assert ( ids [ 0 ] , checker . Equals , secondZero , check . Commentf ( "First in list should be %q, got %q" , secondZero , ids [ 0 ] ) )
c . Assert ( ids [ 1 ] , checker . Equals , firstZero , check . Commentf ( "Second in list should be %q, got %q" , firstZero , ids [ 1 ] ) )
2014-11-03 13:50:16 -05:00
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-a" , "-q" , "--no-trunc" , "--filter=exited=1" )
2014-11-03 13:50:16 -05:00
ids = strings . Split ( strings . TrimSpace ( out ) , "\n" )
2015-10-14 03:03:09 -04:00
c . Assert ( ids , checker . HasLen , 2 , check . Commentf ( "Should be 2 zero exited containers got %d" , len ( ids ) ) )
c . Assert ( ids [ 0 ] , checker . Equals , secondNonZero , check . Commentf ( "First in list should be %q, got %q" , secondNonZero , ids [ 0 ] ) )
c . Assert ( ids [ 1 ] , checker . Equals , firstNonZero , check . Commentf ( "Second in list should be %q, got %q" , firstNonZero , ids [ 1 ] ) )
2015-01-08 17:51:47 -05:00
2014-11-03 13:50:16 -05:00
}
2015-02-05 17:55:08 -05:00
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsRightTagName ( c * check . C ) {
2016-01-22 23:44:46 -05:00
// TODO Investigate further why this fails on Windows to Windows CI
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-02-05 17:55:08 -05:00
tag := "asybox:shmatest"
2015-07-14 02:35:36 -04:00
dockerCmd ( c , "tag" , "busybox" , tag )
2015-02-05 17:55:08 -05:00
var id1 string
2017-04-16 17:39:30 -04:00
out := runSleepingContainer ( c )
2015-07-14 02:35:36 -04:00
id1 = strings . TrimSpace ( string ( out ) )
2015-02-05 17:55:08 -05:00
var id2 string
2017-04-16 17:39:30 -04:00
out = runSleepingContainerInImage ( c , tag )
2015-07-14 02:35:36 -04:00
id2 = strings . TrimSpace ( string ( out ) )
2015-04-01 10:08:00 -04:00
var imageID string
2016-01-28 09:19:25 -05:00
out = inspectField ( c , "busybox" , "Id" )
2015-07-14 02:35:36 -04:00
imageID = strings . TrimSpace ( string ( out ) )
2015-04-01 10:08:00 -04:00
var id3 string
2017-04-16 17:39:30 -04:00
out = runSleepingContainerInImage ( c , imageID )
2015-07-14 02:35:36 -04:00
id3 = strings . TrimSpace ( string ( out ) )
2015-04-01 10:08:00 -04:00
2015-07-14 02:35:36 -04:00
out , _ = dockerCmd ( c , "ps" , "--no-trunc" )
2015-02-05 17:55:08 -05:00
lines := strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
// skip header
lines = lines [ 1 : ]
2015-10-14 03:03:09 -04:00
c . Assert ( lines , checker . HasLen , 3 , check . Commentf ( "There should be 3 running container, got %d" , len ( lines ) ) )
2015-02-05 17:55:08 -05:00
for _ , line := range lines {
f := strings . Fields ( line )
switch f [ 0 ] {
case id1 :
2015-10-14 03:03:09 -04:00
c . Assert ( f [ 1 ] , checker . Equals , "busybox" , check . Commentf ( "Expected %s tag for id %s, got %s" , "busybox" , id1 , f [ 1 ] ) )
2015-02-05 17:55:08 -05:00
case id2 :
2015-10-14 03:03:09 -04:00
c . Assert ( f [ 1 ] , checker . Equals , tag , check . Commentf ( "Expected %s tag for id %s, got %s" , tag , id2 , f [ 1 ] ) )
2015-04-01 10:08:00 -04:00
case id3 :
2015-10-14 03:03:09 -04:00
c . Assert ( f [ 1 ] , checker . Equals , imageID , check . Commentf ( "Expected %s imageID for id %s, got %s" , tag , id3 , f [ 1 ] ) )
2015-02-05 17:55:08 -05:00
default :
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Unexpected id %s, expected %s and %s and %s" , f [ 0 ] , id1 , id2 , id3 )
2015-02-05 17:55:08 -05:00
}
}
}
2015-01-15 14:03:46 -05:00
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsLinkedWithNoTrunc ( c * check . C ) {
2016-01-22 23:44:46 -05:00
// Problematic on Windows as it doesn't support links as of Jan 2016
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2016-01-26 23:16:36 -05:00
runSleepingContainer ( c , "--name=first" )
runSleepingContainer ( c , "--name=second" , "--link=first:first" )
2015-07-14 02:35:36 -04:00
out , _ := dockerCmd ( c , "ps" , "--no-trunc" )
2015-01-15 14:03:46 -05:00
lines := strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
// strip header
lines = lines [ 1 : ]
expected := [ ] string { "second" , "first,second/first" }
var names [ ] string
for _ , l := range lines {
fields := strings . Fields ( l )
names = append ( names , fields [ len ( fields ) - 1 ] )
}
2015-10-14 03:03:09 -04:00
c . Assert ( expected , checker . DeepEquals , names , check . Commentf ( "Expected array: %v, got: %v" , expected , names ) )
2015-01-15 14:03:46 -05:00
}
2015-02-16 14:08:32 -05:00
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsGroupPortRange ( c * check . C ) {
2016-01-22 23:44:46 -05:00
// Problematic on Windows as it doesn't support port ranges as of Jan 2016
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2016-05-17 06:23:37 -04:00
portRange := "3850-3900"
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "run" , "-d" , "--name" , "porttest" , "-p" , portRange + ":" + portRange , "busybox" , "top" )
2015-02-16 14:08:32 -05:00
2015-05-19 14:33:59 -04:00
out , _ := dockerCmd ( c , "ps" )
2015-02-16 14:08:32 -05:00
2015-10-14 03:03:09 -04:00
c . Assert ( string ( out ) , checker . Contains , portRange , check . Commentf ( "docker ps output should have had the port range %q: %s" , portRange , string ( out ) ) )
2015-02-16 14:08:32 -05:00
}
2015-05-19 01:48:44 -04:00
func ( s * DockerSuite ) TestPsWithSize ( c * check . C ) {
2016-01-22 23:44:46 -05:00
// Problematic on Windows as it doesn't report the size correctly @swernli
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-07-14 02:35:36 -04:00
dockerCmd ( c , "run" , "-d" , "--name" , "sizetest" , "busybox" , "top" )
out , _ := dockerCmd ( c , "ps" , "--size" )
2015-10-14 03:03:09 -04:00
c . Assert ( out , checker . Contains , "virtual" , check . Commentf ( "docker ps with --size should show virtual size of container" ) )
2015-05-19 01:48:44 -04:00
}
2015-05-20 17:51:58 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterCreated ( c * check . C ) {
// create a container
2015-07-14 02:35:36 -04:00
out , _ := dockerCmd ( c , "create" , "busybox" )
2015-05-20 17:51:58 -04:00
cID := strings . TrimSpace ( out )
shortCID := cID [ : 12 ]
// Make sure it DOESN'T show up w/o a '-a' for normal 'ps'
2015-07-14 02:35:36 -04:00
out , _ = dockerCmd ( c , "ps" , "-q" )
2015-10-14 03:03:09 -04:00
c . Assert ( out , checker . Not ( checker . Contains ) , shortCID , check . Commentf ( "Should have not seen '%s' in ps output:\n%s" , shortCID , out ) )
2015-05-20 17:51:58 -04:00
// Make sure it DOES show up as 'Created' for 'ps -a'
2015-07-14 02:35:36 -04:00
out , _ = dockerCmd ( c , "ps" , "-a" )
2015-05-20 17:51:58 -04:00
hits := 0
for _ , line := range strings . Split ( out , "\n" ) {
if ! strings . Contains ( line , shortCID ) {
continue
}
hits ++
2015-10-14 03:03:09 -04:00
c . Assert ( line , checker . Contains , "Created" , check . Commentf ( "Missing 'Created' on '%s'" , line ) )
2015-05-20 17:51:58 -04:00
}
2015-10-14 03:03:09 -04:00
c . Assert ( hits , checker . Equals , 1 , check . Commentf ( "Should have seen '%s' in ps -a output once:%d\n%s" , shortCID , hits , out ) )
2015-05-20 17:51:58 -04:00
// filter containers by 'create' - note, no -a needed
2015-07-14 02:35:36 -04:00
out , _ = dockerCmd ( c , "ps" , "-q" , "-f" , "status=created" )
2015-05-20 17:51:58 -04:00
containerOut := strings . TrimSpace ( out )
2015-10-14 03:03:09 -04:00
c . Assert ( cID , checker . HasPrefix , containerOut )
2015-05-20 17:51:58 -04:00
}
2015-07-17 00:03:16 -04:00
func ( s * DockerSuite ) TestPsFormatMultiNames ( c * check . C ) {
2016-01-22 23:44:46 -05:00
// Problematic on Windows as it doesn't support link as of Jan 2016
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-07-17 00:03:16 -04:00
//create 2 containers and link them
dockerCmd ( c , "run" , "--name=child" , "-d" , "busybox" , "top" )
dockerCmd ( c , "run" , "--name=parent" , "--link=child:linkedone" , "-d" , "busybox" , "top" )
//use the new format capabilities to only list the names and --no-trunc to get all names
out , _ := dockerCmd ( c , "ps" , "--format" , "{{.Names}}" , "--no-trunc" )
lines := strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
expected := [ ] string { "parent" , "child,parent/linkedone" }
var names [ ] string
2016-10-13 12:34:19 -04:00
names = append ( names , lines ... )
2015-10-14 03:03:09 -04:00
c . Assert ( expected , checker . DeepEquals , names , check . Commentf ( "Expected array with non-truncated names: %v, got: %v" , expected , names ) )
2015-07-17 00:03:16 -04:00
//now list without turning off truncation and make sure we only get the non-link names
out , _ = dockerCmd ( c , "ps" , "--format" , "{{.Names}}" )
lines = strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
expected = [ ] string { "parent" , "child" }
var truncNames [ ] string
2016-10-13 12:34:19 -04:00
truncNames = append ( truncNames , lines ... )
2015-10-14 03:03:09 -04:00
c . Assert ( expected , checker . DeepEquals , truncNames , check . Commentf ( "Expected array with truncated names: %v, got: %v" , expected , truncNames ) )
2016-03-26 14:29:57 -04:00
}
// Test for GitHub issue #21772
func ( s * DockerSuite ) TestPsNamesMultipleTime ( c * check . C ) {
runSleepingContainer ( c , "--name=test1" )
runSleepingContainer ( c , "--name=test2" )
2015-07-17 00:03:16 -04:00
2016-03-26 14:29:57 -04:00
//use the new format capabilities to list the names twice
out , _ := dockerCmd ( c , "ps" , "--format" , "{{.Names}} {{.Names}}" )
lines := strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
expected := [ ] string { "test2 test2" , "test1 test1" }
var names [ ] string
2016-10-13 12:34:19 -04:00
names = append ( names , lines ... )
2016-03-26 14:29:57 -04:00
c . Assert ( expected , checker . DeepEquals , names , check . Commentf ( "Expected array with names displayed twice: %v, got: %v" , expected , names ) )
2015-07-17 00:03:16 -04:00
}
2015-07-23 16:23:24 -04:00
func ( s * DockerSuite ) TestPsFormatHeaders ( c * check . C ) {
// make sure no-container "docker ps" still prints the header row
out , _ := dockerCmd ( c , "ps" , "--format" , "table {{.ID}}" )
2015-10-14 03:03:09 -04:00
c . Assert ( out , checker . Equals , "CONTAINER ID\n" , check . Commentf ( ` Expected 'CONTAINER ID\n', got %v ` , out ) )
2015-07-23 16:23:24 -04:00
// verify that "docker ps" with a container still prints the header row also
2016-01-26 23:16:36 -05:00
runSleepingContainer ( c , "--name=test" )
2015-07-23 16:23:24 -04:00
out , _ = dockerCmd ( c , "ps" , "--format" , "table {{.Names}}" )
2015-10-14 03:03:09 -04:00
c . Assert ( out , checker . Equals , "NAMES\ntest\n" , check . Commentf ( ` Expected 'NAMES\ntest\n', got %v ` , out ) )
2015-07-23 16:23:24 -04:00
}
2015-08-14 18:00:48 -04:00
func ( s * DockerSuite ) TestPsDefaultFormatAndQuiet ( c * check . C ) {
config := ` {
2015-12-18 08:03:41 -05:00
"psFormat" : "default {{ .ID }}"
2015-08-14 18:00:48 -04:00
} `
d , err := ioutil . TempDir ( "" , "integration-cli-" )
2015-10-14 03:03:09 -04:00
c . Assert ( err , checker . IsNil )
2015-08-14 18:00:48 -04:00
defer os . RemoveAll ( d )
err = ioutil . WriteFile ( filepath . Join ( d , "config.json" ) , [ ] byte ( config ) , 0644 )
2015-10-14 03:03:09 -04:00
c . Assert ( err , checker . IsNil )
2015-08-14 18:00:48 -04:00
2017-04-16 17:39:30 -04:00
out := runSleepingContainer ( c , "--name=test" )
2015-08-14 18:00:48 -04:00
id := strings . TrimSpace ( out )
out , _ = dockerCmd ( c , "--config" , d , "ps" , "-q" )
2015-10-14 03:03:09 -04:00
c . Assert ( id , checker . HasPrefix , strings . TrimSpace ( out ) , check . Commentf ( "Expected to print only the container id, got %v\n" , out ) )
2015-08-14 18:00:48 -04:00
}
2015-04-30 21:13:18 -04:00
// Test for GitHub issue #12595
func ( s * DockerSuite ) TestPsImageIDAfterUpdate ( c * check . C ) {
2016-01-22 23:44:46 -05:00
// TODO: Investigate why this fails on Windows to Windows CI further.
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-04-30 21:13:18 -04:00
originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
2017-01-05 06:38:34 -05:00
icmd . RunCommand ( dockerBinary , "tag" , "busybox:latest" , originalImageName ) . Assert ( c , icmd . Success )
2015-04-30 21:13:18 -04:00
2017-01-16 05:30:14 -05:00
originalImageID := getIDByName ( c , originalImageName )
2015-04-30 21:13:18 -04:00
2017-01-05 06:38:34 -05:00
result := icmd . RunCommand ( dockerBinary , append ( [ ] string { "run" , "-d" , originalImageName } , sleepCommandForDaemonPlatform ( ) ... ) ... )
result . Assert ( c , icmd . Success )
containerID := strings . TrimSpace ( result . Combined ( ) )
2015-04-30 21:13:18 -04:00
2017-01-05 06:38:34 -05:00
result = icmd . RunCommand ( dockerBinary , "ps" , "--no-trunc" )
result . Assert ( c , icmd . Success )
2015-04-30 21:13:18 -04:00
2017-01-05 06:38:34 -05:00
lines := strings . Split ( strings . TrimSpace ( string ( result . Combined ( ) ) ) , "\n" )
2015-04-30 21:13:18 -04:00
// skip header
lines = lines [ 1 : ]
2015-10-14 03:03:09 -04:00
c . Assert ( len ( lines ) , checker . Equals , 1 )
2015-04-30 21:13:18 -04:00
for _ , line := range lines {
f := strings . Fields ( line )
2015-10-14 03:03:09 -04:00
c . Assert ( f [ 1 ] , checker . Equals , originalImageName )
2015-04-30 21:13:18 -04:00
}
2017-01-05 06:38:34 -05:00
icmd . RunCommand ( dockerBinary , "commit" , containerID , updatedImageName ) . Assert ( c , icmd . Success )
icmd . RunCommand ( dockerBinary , "tag" , updatedImageName , originalImageName ) . Assert ( c , icmd . Success )
2015-04-30 21:13:18 -04:00
2017-01-05 06:38:34 -05:00
result = icmd . RunCommand ( dockerBinary , "ps" , "--no-trunc" )
result . Assert ( c , icmd . Success )
2015-04-30 21:13:18 -04:00
2017-01-05 06:38:34 -05:00
lines = strings . Split ( strings . TrimSpace ( string ( result . Combined ( ) ) ) , "\n" )
2015-04-30 21:13:18 -04:00
// skip header
lines = lines [ 1 : ]
2015-10-14 03:03:09 -04:00
c . Assert ( len ( lines ) , checker . Equals , 1 )
2015-04-30 21:13:18 -04:00
for _ , line := range lines {
f := strings . Fields ( line )
2015-10-14 03:03:09 -04:00
c . Assert ( f [ 1 ] , checker . Equals , originalImageID )
2015-04-30 21:13:18 -04:00
}
}
2016-01-20 20:09:11 -05:00
func ( s * DockerSuite ) TestPsNotShowPortsOfStoppedContainer ( c * check . C ) {
2016-01-21 21:48:21 -05:00
testRequires ( c , DaemonIsLinux )
2016-01-20 20:09:11 -05:00
dockerCmd ( c , "run" , "--name=foo" , "-d" , "-p" , "5000:5000" , "busybox" , "top" )
c . Assert ( waitRun ( "foo" ) , checker . IsNil )
out , _ := dockerCmd ( c , "ps" )
lines := strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
expected := "0.0.0.0:5000->5000/tcp"
fields := strings . Fields ( lines [ 1 ] )
c . Assert ( fields [ len ( fields ) - 2 ] , checker . Equals , expected , check . Commentf ( "Expected: %v, got: %v" , expected , fields [ len ( fields ) - 2 ] ) )
dockerCmd ( c , "kill" , "foo" )
dockerCmd ( c , "wait" , "foo" )
out , _ = dockerCmd ( c , "ps" , "-l" )
lines = strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
fields = strings . Fields ( lines [ 1 ] )
c . Assert ( fields [ len ( fields ) - 2 ] , checker . Not ( checker . Equals ) , expected , check . Commentf ( "Should not got %v" , expected ) )
}
2016-02-03 17:46:01 -05:00
func ( s * DockerSuite ) TestPsShowMounts ( c * check . C ) {
prefix , slash := getPrefixAndSlashFromDaemonPlatform ( )
mp := prefix + slash + "test"
2016-06-14 18:42:30 -04:00
dockerCmd ( c , "volume" , "create" , "ps-volume-test" )
2016-03-16 19:15:04 -04:00
// volume mount containers
2016-02-03 17:46:01 -05:00
runSleepingContainer ( c , "--name=volume-test-1" , "--volume" , "ps-volume-test:" + mp )
c . Assert ( waitRun ( "volume-test-1" ) , checker . IsNil )
runSleepingContainer ( c , "--name=volume-test-2" , "--volume" , mp )
c . Assert ( waitRun ( "volume-test-2" ) , checker . IsNil )
2016-03-16 19:15:04 -04:00
// bind mount container
var bindMountSource string
var bindMountDestination string
2016-12-16 09:13:23 -05:00
if DaemonIsWindows ( ) {
2016-03-16 19:15:04 -04:00
bindMountSource = "c:\\"
bindMountDestination = "c:\\t"
} else {
bindMountSource = "/tmp"
bindMountDestination = "/t"
}
runSleepingContainer ( c , "--name=bind-mount-test" , "-v" , bindMountSource + ":" + bindMountDestination )
c . Assert ( waitRun ( "bind-mount-test" ) , checker . IsNil )
2016-02-03 17:46:01 -05:00
out , _ := dockerCmd ( c , "ps" , "--format" , "{{.Names}} {{.Mounts}}" )
lines := strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
2016-03-16 19:15:04 -04:00
c . Assert ( lines , checker . HasLen , 3 )
2016-02-03 17:46:01 -05:00
fields := strings . Fields ( lines [ 0 ] )
c . Assert ( fields , checker . HasLen , 2 )
2016-03-16 19:15:04 -04:00
c . Assert ( fields [ 0 ] , checker . Equals , "bind-mount-test" )
c . Assert ( fields [ 1 ] , checker . Equals , bindMountSource )
fields = strings . Fields ( lines [ 1 ] )
c . Assert ( fields , checker . HasLen , 2 )
2016-02-03 17:46:01 -05:00
2017-05-21 19:24:07 -04:00
anonymousVolumeID := fields [ 1 ]
2016-02-03 17:46:01 -05:00
2016-03-16 19:15:04 -04:00
fields = strings . Fields ( lines [ 2 ] )
2016-02-03 17:46:01 -05:00
c . Assert ( fields [ 1 ] , checker . Equals , "ps-volume-test" )
// filter by volume name
out , _ = dockerCmd ( c , "ps" , "--format" , "{{.Names}} {{.Mounts}}" , "--filter" , "volume=ps-volume-test" )
lines = strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
c . Assert ( lines , checker . HasLen , 1 )
fields = strings . Fields ( lines [ 0 ] )
c . Assert ( fields [ 1 ] , checker . Equals , "ps-volume-test" )
// empty results filtering by unknown volume
out , _ = dockerCmd ( c , "ps" , "--format" , "{{.Names}} {{.Mounts}}" , "--filter" , "volume=this-volume-should-not-exist" )
c . Assert ( strings . TrimSpace ( string ( out ) ) , checker . HasLen , 0 )
// filter by mount destination
out , _ = dockerCmd ( c , "ps" , "--format" , "{{.Names}} {{.Mounts}}" , "--filter" , "volume=" + mp )
lines = strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
c . Assert ( lines , checker . HasLen , 2 )
fields = strings . Fields ( lines [ 0 ] )
2017-05-21 19:24:07 -04:00
c . Assert ( fields [ 1 ] , checker . Equals , anonymousVolumeID )
2016-02-03 17:46:01 -05:00
fields = strings . Fields ( lines [ 1 ] )
c . Assert ( fields [ 1 ] , checker . Equals , "ps-volume-test" )
2016-03-16 19:15:04 -04:00
// filter by bind mount source
out , _ = dockerCmd ( c , "ps" , "--format" , "{{.Names}} {{.Mounts}}" , "--filter" , "volume=" + bindMountSource )
lines = strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
c . Assert ( lines , checker . HasLen , 1 )
fields = strings . Fields ( lines [ 0 ] )
c . Assert ( fields , checker . HasLen , 2 )
c . Assert ( fields [ 0 ] , checker . Equals , "bind-mount-test" )
c . Assert ( fields [ 1 ] , checker . Equals , bindMountSource )
// filter by bind mount destination
out , _ = dockerCmd ( c , "ps" , "--format" , "{{.Names}} {{.Mounts}}" , "--filter" , "volume=" + bindMountDestination )
lines = strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
c . Assert ( lines , checker . HasLen , 1 )
fields = strings . Fields ( lines [ 0 ] )
c . Assert ( fields , checker . HasLen , 2 )
c . Assert ( fields [ 0 ] , checker . Equals , "bind-mount-test" )
c . Assert ( fields [ 1 ] , checker . Equals , bindMountSource )
2016-02-03 17:46:01 -05:00
// empty results filtering by unknown mount point
out , _ = dockerCmd ( c , "ps" , "--format" , "{{.Names}} {{.Mounts}}" , "--filter" , "volume=" + prefix + slash + "this-path-was-never-mounted" )
c . Assert ( strings . TrimSpace ( string ( out ) ) , checker . HasLen , 0 )
}
2016-04-14 05:43:15 -04:00
func ( s * DockerSuite ) TestPsFormatSize ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
runSleepingContainer ( c )
out , _ := dockerCmd ( c , "ps" , "--format" , "table {{.Size}}" )
lines := strings . Split ( out , "\n" )
c . Assert ( lines [ 1 ] , checker . Not ( checker . Equals ) , "0 B" , check . Commentf ( "Should not display a size of 0 B" ) )
out , _ = dockerCmd ( c , "ps" , "--size" , "--format" , "table {{.Size}}" )
lines = strings . Split ( out , "\n" )
c . Assert ( lines [ 0 ] , checker . Equals , "SIZE" , check . Commentf ( "Should only have one size column" ) )
out , _ = dockerCmd ( c , "ps" , "--size" , "--format" , "raw" )
lines = strings . Split ( out , "\n" )
c . Assert ( lines [ 8 ] , checker . HasPrefix , "size:" , check . Commentf ( "Size should be appended on a newline" ) )
}
2016-05-27 15:20:31 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterNetwork ( c * check . C ) {
2016-06-05 20:04:33 -04:00
// TODO default network on Windows is not called "bridge", and creating a
// custom network fails on Windows fails with "Error response from daemon: plugin not found")
testRequires ( c , DaemonIsLinux )
// create some containers
runSleepingContainer ( c , "--net=bridge" , "--name=onbridgenetwork" )
runSleepingContainer ( c , "--net=none" , "--name=onnonenetwork" )
// Filter docker ps on non existing network
out , _ := dockerCmd ( c , "ps" , "--filter" , "network=doesnotexist" )
containerOut := strings . TrimSpace ( string ( out ) )
lines := strings . Split ( containerOut , "\n" )
// skip header
lines = lines [ 1 : ]
// ps output should have no containers
c . Assert ( lines , checker . HasLen , 0 )
2016-05-27 15:20:31 -04:00
// Filter docker ps on network bridge
out , _ = dockerCmd ( c , "ps" , "--filter" , "network=bridge" )
2016-06-05 20:04:33 -04:00
containerOut = strings . TrimSpace ( string ( out ) )
2016-05-27 15:20:31 -04:00
2016-06-05 20:04:33 -04:00
lines = strings . Split ( containerOut , "\n" )
2016-05-27 15:20:31 -04:00
// skip header
lines = lines [ 1 : ]
// ps output should have only one container
c . Assert ( lines , checker . HasLen , 1 )
// Making sure onbridgenetwork is on the output
2016-06-05 20:04:33 -04:00
c . Assert ( containerOut , checker . Contains , "onbridgenetwork" , check . Commentf ( "Missing the container on network\n" ) )
2016-05-27 15:20:31 -04:00
// Filter docker ps on networks bridge and none
out , _ = dockerCmd ( c , "ps" , "--filter" , "network=bridge" , "--filter" , "network=none" )
containerOut = strings . TrimSpace ( string ( out ) )
lines = strings . Split ( containerOut , "\n" )
// skip header
lines = lines [ 1 : ]
//ps output should have both the containers
c . Assert ( lines , checker . HasLen , 2 )
// Making sure onbridgenetwork and onnonenetwork is on the output
2016-06-05 20:04:33 -04:00
c . Assert ( containerOut , checker . Contains , "onnonenetwork" , check . Commentf ( "Missing the container on none network\n" ) )
c . Assert ( containerOut , checker . Contains , "onbridgenetwork" , check . Commentf ( "Missing the container on bridge network\n" ) )
nwID , _ := dockerCmd ( c , "network" , "inspect" , "--format" , "{{.ID}}" , "bridge" )
// Filter by network ID
out , _ = dockerCmd ( c , "ps" , "--filter" , "network=" + nwID )
containerOut = strings . TrimSpace ( string ( out ) )
c . Assert ( containerOut , checker . Contains , "onbridgenetwork" )
2016-11-22 15:02:46 -05:00
// Filter by partial network ID
partialnwID := string ( nwID [ 0 : 4 ] )
out , _ = dockerCmd ( c , "ps" , "--filter" , "network=" + partialnwID )
containerOut = strings . TrimSpace ( string ( out ) )
lines = strings . Split ( containerOut , "\n" )
// skip header
lines = lines [ 1 : ]
// ps output should have only one container
c . Assert ( lines , checker . HasLen , 1 )
// Making sure onbridgenetwork is on the output
c . Assert ( containerOut , checker . Contains , "onbridgenetwork" , check . Commentf ( "Missing the container on network\n" ) )
2016-05-27 15:20:31 -04:00
}
2016-08-03 22:02:39 -04:00
func ( s * DockerSuite ) TestPsByOrder ( c * check . C ) {
name1 := "xyz-abc"
2017-04-16 17:39:30 -04:00
out := runSleepingContainer ( c , "--name" , name1 )
2016-08-03 22:02:39 -04:00
container1 := strings . TrimSpace ( out )
name2 := "xyz-123"
2017-04-16 17:39:30 -04:00
out = runSleepingContainer ( c , "--name" , name2 )
2016-08-03 22:02:39 -04:00
container2 := strings . TrimSpace ( out )
name3 := "789-abc"
2017-04-16 17:39:30 -04:00
out = runSleepingContainer ( c , "--name" , name3 )
2016-08-03 22:02:39 -04:00
name4 := "789-123"
2017-04-16 17:39:30 -04:00
out = runSleepingContainer ( c , "--name" , name4 )
2016-08-03 22:02:39 -04:00
// Run multiple time should have the same result
2017-04-16 17:39:30 -04:00
out = cli . DockerCmd ( c , "ps" , "--no-trunc" , "-q" , "-f" , "name=xyz" ) . Combined ( )
2016-08-03 22:02:39 -04:00
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , fmt . Sprintf ( "%s\n%s" , container2 , container1 ) )
// Run multiple time should have the same result
2017-04-16 17:39:30 -04:00
out = cli . DockerCmd ( c , "ps" , "--no-trunc" , "-q" , "-f" , "name=xyz" ) . Combined ( )
2016-08-03 22:02:39 -04:00
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , fmt . Sprintf ( "%s\n%s" , container2 , container1 ) )
}
2016-09-28 19:33:45 -04:00
func ( s * DockerSuite ) TestPsFilterMissingArgErrorCode ( c * check . C ) {
_ , errCode , _ := dockerCmdWithError ( "ps" , "--filter" )
c . Assert ( errCode , checker . Equals , 125 )
}
2017-01-19 13:17:56 -05:00
// Test case for 30291
func ( s * DockerSuite ) TestPsFormatTemplateWithArg ( c * check . C ) {
runSleepingContainer ( c , "-d" , "--name" , "top" , "--label" , "some.label=label.foo-bar" )
out , _ := dockerCmd ( c , "ps" , "--format" , ` {{ .Names }} {{ .Label "some.label" }} ` )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , "top label.foo-bar" )
}
2016-10-15 18:53:06 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterPorts ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
out , _ := dockerCmd ( c , "run" , "-d" , "--publish=80" , "busybox" , "top" )
id1 := strings . TrimSpace ( out )
out , _ = dockerCmd ( c , "run" , "-d" , "--expose=8080" , "busybox" , "top" )
id2 := strings . TrimSpace ( out )
out , _ = dockerCmd ( c , "ps" , "--no-trunc" , "-q" )
c . Assert ( strings . TrimSpace ( out ) , checker . Contains , id1 )
c . Assert ( strings . TrimSpace ( out ) , checker . Contains , id2 )
out , _ = dockerCmd ( c , "ps" , "--no-trunc" , "-q" , "--filter" , "publish=80-8080/udp" )
c . Assert ( strings . TrimSpace ( out ) , checker . Not ( checker . Equals ) , id1 )
c . Assert ( strings . TrimSpace ( out ) , checker . Not ( checker . Equals ) , id2 )
out , _ = dockerCmd ( c , "ps" , "--no-trunc" , "-q" , "--filter" , "expose=8081" )
c . Assert ( strings . TrimSpace ( out ) , checker . Not ( checker . Equals ) , id1 )
c . Assert ( strings . TrimSpace ( out ) , checker . Not ( checker . Equals ) , id2 )
out , _ = dockerCmd ( c , "ps" , "--no-trunc" , "-q" , "--filter" , "publish=80-81" )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , id1 )
c . Assert ( strings . TrimSpace ( out ) , checker . Not ( checker . Equals ) , id2 )
out , _ = dockerCmd ( c , "ps" , "--no-trunc" , "-q" , "--filter" , "expose=80/tcp" )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , id1 )
c . Assert ( strings . TrimSpace ( out ) , checker . Not ( checker . Equals ) , id2 )
out , _ = dockerCmd ( c , "ps" , "--no-trunc" , "-q" , "--filter" , "expose=8080/tcp" )
c . Assert ( strings . TrimSpace ( out ) , checker . Not ( checker . Equals ) , id1 )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , id2 )
}
2017-07-14 05:25:09 -04:00
func ( s * DockerSuite ) TestPsNotShowLinknamesOfDeletedContainer ( c * check . C ) {
testRequires ( c , DaemonIsLinux )
dockerCmd ( c , "create" , "--name=aaa" , "busybox" , "top" )
dockerCmd ( c , "create" , "--name=bbb" , "--link=aaa" , "busybox" , "top" )
out , _ := dockerCmd ( c , "ps" , "--no-trunc" , "-a" , "--format" , "{{.Names}}" )
lines := strings . Split ( strings . TrimSpace ( string ( out ) ) , "\n" )
expected := [ ] string { "bbb" , "aaa,bbb/aaa" }
var names [ ] string
names = append ( names , lines ... )
c . Assert ( expected , checker . DeepEquals , names , check . Commentf ( "Expected array with non-truncated names: %v, got: %v" , expected , names ) )
dockerCmd ( c , "rm" , "bbb" )
out , _ = dockerCmd ( c , "ps" , "--no-trunc" , "-a" , "--format" , "{{.Names}}" )
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , "aaa" )
}