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"
2014-07-17 04:29:52 -04:00
"os/exec"
2015-08-14 18:00:48 -04:00
"path/filepath"
2015-01-15 14:03:46 -05:00
"reflect"
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
"github.com/go-check/check"
2015-08-20 03:57:15 -04:00
"sort"
"github.com/docker/docker/pkg/stringid"
2014-07-17 04:29:52 -04:00
)
2015-08-26 04:16:53 -04:00
func ( s * DockerSuite ) TestPsListContainersBase ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-05-19 14:33:59 -04:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "top" )
2015-04-06 09:21:18 -04:00
firstID := strings . TrimSpace ( out )
2014-07-17 04:29:52 -04:00
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "run" , "-d" , "busybox" , "top" )
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
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "run" , "-d" , "busybox" , "top" )
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-08-11 03:41:11 -04:00
c . Assert ( waitRun ( secondID ) , check . 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-08-11 03:41:11 -04:00
c . Assert ( waitRun ( fourthID ) , check . 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" )
2014-07-17 04:29:52 -04:00
if ! assertContainerList ( out , [ ] string { fourthID , thirdID , secondID , firstID } ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "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" )
2014-07-17 04:29:52 -04:00
if ! assertContainerList ( out , [ ] string { fourthID , secondID , firstID } ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "RUNNING: Container list is not in the correct order: \n%s" , out )
2014-07-17 04:29:52 -04:00
}
// from here all flag '-a' is ignored
// 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 }
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "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" )
2014-07-17 04:29:52 -04:00
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "LIMIT: Container list is not in the correct order: \n%s" , out )
2014-07-17 04:29:52 -04:00
}
// since
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "--since" , firstID , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { fourthID , thirdID , secondID }
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "SINCE & 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" , "--since" , firstID )
2014-07-17 04:29:52 -04:00
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "SINCE: Container list is not in the correct order: \n%s" , out )
2014-07-17 04:29:52 -04:00
}
// before
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "--before" , thirdID , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { secondID , firstID }
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "BEFORE & 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" , "--before" , thirdID )
2014-07-17 04:29:52 -04:00
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "BEFORE: Container list is not in the correct order: \n%s" , out )
2014-07-17 04:29:52 -04:00
}
// since & before
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "--since" , firstID , "--before" , fourthID , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { thirdID , secondID }
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "SINCE, BEFORE & 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" , "--since" , firstID , "--before" , fourthID )
2014-07-17 04:29:52 -04:00
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "SINCE, BEFORE: Container list is not in the correct order: \n%s" , out )
2014-07-17 04:29:52 -04:00
}
// since & limit
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "--since" , firstID , "-n=2" , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { fourthID , thirdID }
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "SINCE, 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" , "--since" , firstID , "-n=2" )
2014-07-17 04:29:52 -04:00
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "SINCE, LIMIT: Container list is not in the correct order: \n%s" , out )
2014-07-17 04:29:52 -04:00
}
// before & limit
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "--before" , fourthID , "-n=1" , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { thirdID }
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "BEFORE, 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" , "--before" , fourthID , "-n=1" )
2014-07-17 04:29:52 -04:00
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "BEFORE, LIMIT: 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" , "--since" , firstID , "--before" , fourthID , "-n=1" , "-a" )
2014-07-17 04:29:52 -04:00
expected = [ ] string { thirdID }
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "SINCE, BEFORE, 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" , "--since" , firstID , "--before" , fourthID , "-n=1" )
2014-07-17 04:29:52 -04:00
if ! assertContainerList ( out , expected ) {
2015-08-26 04:16:53 -04:00
c . Errorf ( "SINCE, BEFORE, 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" )
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
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersSize ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "run" , "-d" , "busybox" , "echo" , "hello" )
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 : ]
baseBytes , err := strconv . Atoi ( strings . Split ( baseFoundsize , " " ) [ 0 ] )
2015-01-23 07:17:05 -05:00
if err != nil {
2015-04-18 12:46:47 -04:00
c . Fatal ( err )
2015-01-23 07:17:05 -05:00
}
2014-09-12 03:45:50 -04:00
name := "test_size"
2015-05-19 14:33:59 -04:00
out , _ := dockerCmd ( c , "run" , "--name" , name , "busybox" , "sh" , "-c" , "echo 1 > test" )
2014-09-12 03:45:50 -04:00
id , err := getIDByName ( name )
if err != nil {
2015-04-18 12:46:47 -04:00
c . Fatal ( err )
2014-09-12 03:45:50 -04:00
}
2015-05-19 14:33:59 -04:00
runCmd := exec . Command ( dockerBinary , "ps" , "-s" , "-n=1" )
2014-09-12 03:45:50 -04:00
wait := make ( chan struct { } )
go func ( ) {
out , _ , err = runCommandWithOutput ( runCmd )
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
}
2014-10-14 16:04:37 -04:00
if err != nil {
2015-04-18 12:46:47 -04:00
c . Fatal ( out , err )
2014-10-14 16:04:37 -04:00
}
2014-09-12 03:45:50 -04:00
lines := strings . Split ( strings . Trim ( out , "\n " ) , "\n" )
2015-04-02 17:44:53 -04:00
if len ( lines ) != 2 {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Expected 2 lines for 'ps -s -n=1' output, got %d" , len ( lines ) )
2015-04-02 17:44:53 -04:00
}
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 ]
if foundID != id [ : 12 ] {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Expected id %s, got %s" , id [ : 12 ] , foundID )
2014-09-12 03:45:50 -04:00
}
2015-03-25 21:40:23 -04:00
expectedSize := fmt . Sprintf ( "%d B" , ( 2 + baseBytes ) )
2014-09-12 03:45:50 -04:00
foundSize := lines [ 1 ] [ sizeIndex : ]
2015-05-19 01:48:44 -04:00
if ! strings . Contains ( foundSize , expectedSize ) {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Expected size %q, got %q" , expectedSize , foundSize )
2014-09-12 03:45:50 -04:00
}
2014-09-26 19:25:50 -04:00
}
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterStatus ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2014-09-26 19:25:50 -04:00
// FIXME: this should test paused, but it makes things hang and its wonky
// this is because paused containers can't be controlled by signals
2015-02-20 01:56:02 -05:00
2014-09-26 19:25:50 -04:00
// start exited 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-09-26 19:25:50 -04:00
// make sure the exited cintainer is not running
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "wait" , firstID )
2014-09-26 19:25:50 -04:00
// start running container
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "run" , "-itd" , "busybox" )
2015-04-06 09:21:18 -04:00
secondID := strings . TrimSpace ( out )
2014-09-26 19:25:50 -04:00
// filter containers by exited
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-q" , "--filter=status=exited" )
2014-09-26 19:25:50 -04:00
containerOut := strings . TrimSpace ( out )
if containerOut != firstID [ : 12 ] {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Expected id %s, got %s for exited filter, output: %q" , firstID [ : 12 ] , containerOut , out )
2014-09-26 19:25:50 -04:00
}
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "ps" , "-a" , "-q" , "--filter=status=running" )
2014-09-26 19:25:50 -04:00
containerOut = strings . TrimSpace ( out )
if containerOut != secondID [ : 12 ] {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Expected id %s, got %s for running filter, output: %q" , secondID [ : 12 ] , containerOut , out )
2014-09-26 19:25:50 -04:00
}
2015-06-30 17:40:27 -04:00
out , _ , _ = dockerCmdWithTimeout ( time . Second * 60 , "ps" , "-a" , "-q" , "--filter=status=rubbish" )
if ! strings . Contains ( out , "Unrecognised filter value for status" ) {
c . Fatalf ( "Expected error response due to invalid status filter output: %q" , out )
}
2014-09-12 03:45:50 -04:00
}
2014-10-13 02:12:44 -04:00
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterID ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
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
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "run" , "-d" , "busybox" , "top" )
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 )
if containerOut != firstID [ : 12 ] {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "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 ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2014-10-13 02:12:44 -04:00
// start container
2015-05-19 14:33:59 -04:00
out , _ := dockerCmd ( c , "run" , "-d" , "--name=a_name_to_match" , "busybox" )
2015-04-06 09:21:18 -04:00
firstID := strings . TrimSpace ( out )
2014-10-13 02:12:44 -04:00
// start another container
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "run" , "-d" , "--name=b_name_to_match" , "busybox" , "top" )
2014-10-13 02:12:44 -04:00
// filter containers by name
2015-05-19 14:33:59 -04: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 )
if containerOut != firstID [ : 12 ] {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Expected id %s, got %s for exited filter, output: %q" , firstID [ : 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 ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-08-20 03:57:15 -04:00
// Build images
imageName1 := "images_ps_filter_test1"
imageID1 , err := buildImage ( imageName1 ,
` FROM busybox
LABEL match me 1 ` , true )
c . Assert ( err , check . IsNil )
imageName1Tagged := "images_ps_filter_test1:tag"
imageID1Tagged , err := buildImage ( imageName1Tagged ,
` FROM busybox
LABEL match me 1 tagged ` , true )
c . Assert ( err , check . IsNil )
imageName2 := "images_ps_filter_test2"
imageID2 , err := buildImage ( imageName2 ,
fmt . Sprintf ( ` FROM % s
LABEL match me 2 ` , imageName1 ) , true )
c . Assert ( err , check . IsNil )
// start containers
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "echo" , "hello" )
firstID := strings . TrimSpace ( out )
// start another container
out , _ = dockerCmd ( c , "run" , "-d" , "busybox" , "echo" , "hello" )
secondID := strings . TrimSpace ( out )
// start third container
out , _ = dockerCmd ( c , "run" , "-d" , imageName1 , "echo" , "hello" )
thirdID := strings . TrimSpace ( out )
// start fourth container
out , _ = dockerCmd ( c , "run" , "-d" , imageName1Tagged , "echo" , "hello" )
fourthID := strings . TrimSpace ( out )
// start fifth container
out , _ = dockerCmd ( c , "run" , "-d" , imageName2 , "echo" , "hello" )
fifthID := strings . TrimSpace ( out )
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 } } ,
}
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 )
if len ( actualIDs ) != len ( expectedIDs ) {
c . Fatalf ( "Expected filtered container(s) for %s ancestor filter to be %v:%v, got %v:%v" , filterName , len ( expectedIDs ) , expectedIDs , len ( actualIDs ) , actualIDs )
}
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
}
}
if ! same {
c . Fatalf ( "Expected filtered container(s) for %s ancestor filter to be %v, got %v" , filterName , expectedIDs , actualIDs )
}
}
}
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterLabel ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-01-06 19:04:10 -05:00
// start container
2015-05-19 14:33:59 -04:00
out , _ := dockerCmd ( c , "run" , "-d" , "-l" , "match=me" , "-l" , "second=tag" , "busybox" )
2015-04-06 09:21:18 -04:00
firstID := strings . TrimSpace ( out )
2015-01-06 19:04:10 -05:00
// start another container
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "run" , "-d" , "-l" , "match=me too" , "busybox" )
2015-04-06 09:21:18 -04:00
secondID := strings . TrimSpace ( out )
2015-01-06 19:04:10 -05:00
// start third container
2015-05-19 14:33:59 -04:00
out , _ = dockerCmd ( c , "run" , "-d" , "-l" , "nomatch=me" , "busybox" )
2015-04-06 09:21:18 -04:00
thirdID := strings . TrimSpace ( out )
2015-01-06 19:04:10 -05:00
// filter containers by exact match
2015-05-19 14:33:59 -04: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 )
if containerOut != firstID {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "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 )
if containerOut != firstID {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "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 )
if containerOut != "" {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "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 )
if ( ! strings . Contains ( containerOut , firstID ) || ! strings . Contains ( containerOut , secondID ) ) || strings . Contains ( containerOut , thirdID ) {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Expected ids %s,%s, got %s for exited filter, output: %q" , firstID , secondID , containerOut , out )
2015-01-06 19:04:10 -05:00
}
}
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterExited ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "run" , "-d" , "--name" , "top" , "busybox" , "top" )
2015-01-08 17:51:47 -05:00
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "run" , "--name" , "zero1" , "busybox" , "true" )
2014-11-03 13:50:16 -05:00
firstZero , err := getIDByName ( "zero1" )
if err != nil {
2015-04-18 12:46:47 -04:00
c . Fatal ( err )
2014-11-03 13:50:16 -05:00
}
2015-05-19 14:33:59 -04:00
dockerCmd ( c , "run" , "--name" , "zero2" , "busybox" , "true" )
2014-11-03 13:50:16 -05:00
secondZero , err := getIDByName ( "zero2" )
if err != nil {
2015-04-18 12:46:47 -04:00
c . Fatal ( err )
2014-11-03 13:50:16 -05:00
}
2015-07-27 14:13:25 -04:00
if out , _ , err := dockerCmdWithError ( "run" , "--name" , "nonzero1" , "busybox" , "false" ) ; err == nil {
2015-04-18 12:46:47 -04:00
c . Fatal ( "Should fail." , out , err )
2014-11-03 13:50:16 -05:00
}
2015-05-19 14:33:59 -04:00
2014-11-03 13:50:16 -05:00
firstNonZero , err := getIDByName ( "nonzero1" )
if err != nil {
2015-04-18 12:46:47 -04:00
c . Fatal ( err )
2014-11-03 13:50:16 -05:00
}
2015-07-27 14:13:25 -04:00
if out , _ , err := dockerCmdWithError ( "run" , "--name" , "nonzero2" , "busybox" , "false" ) ; err == nil {
2015-04-18 12:46:47 -04:00
c . Fatal ( "Should fail." , out , err )
2014-11-03 13:50:16 -05:00
}
secondNonZero , err := getIDByName ( "nonzero2" )
if err != nil {
2015-04-18 12:46:47 -04:00
c . Fatal ( err )
2014-11-03 13:50:16 -05:00
}
// filter containers by exited=0
2015-05-19 14:33:59 -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" )
if len ( ids ) != 2 {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Should be 2 zero exited containers got %d: %s" , len ( ids ) , out )
2014-11-03 13:50:16 -05:00
}
if ids [ 0 ] != secondZero {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "First in list should be %q, got %q" , secondZero , ids [ 0 ] )
2014-11-03 13:50:16 -05:00
}
if ids [ 1 ] != firstZero {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "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" )
if len ( ids ) != 2 {
2015-04-27 16:33:30 -04:00
c . Fatalf ( "Should be 2 zero exited containers got %d" , len ( ids ) )
2014-11-03 13:50:16 -05:00
}
if ids [ 0 ] != secondNonZero {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "First in list should be %q, got %q" , secondNonZero , ids [ 0 ] )
2014-11-03 13:50:16 -05:00
}
if ids [ 1 ] != firstNonZero {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Second in list should be %q, got %q" , firstNonZero , ids [ 1 ] )
2014-11-03 13:50:16 -05:00
}
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 ) {
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
2015-07-14 02:35:36 -04:00
out , _ := dockerCmd ( c , "run" , "-d" , "busybox" , "top" )
id1 = strings . TrimSpace ( string ( out ) )
2015-02-05 17:55:08 -05:00
var id2 string
2015-07-14 02:35:36 -04:00
out , _ = dockerCmd ( c , "run" , "-d" , tag , "top" )
id2 = strings . TrimSpace ( string ( out ) )
2015-04-01 10:08:00 -04:00
var imageID string
2015-07-14 02:35:36 -04:00
out , _ = dockerCmd ( c , "inspect" , "-f" , "{{.Id}}" , "busybox" )
imageID = strings . TrimSpace ( string ( out ) )
2015-04-01 10:08:00 -04:00
var id3 string
2015-07-14 02:35:36 -04:00
out , _ = dockerCmd ( c , "run" , "-d" , imageID , "top" )
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-04-01 10:08:00 -04:00
if len ( lines ) != 3 {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "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-04-01 10:08:00 -04:00
if f [ 1 ] != "busybox" {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Expected %s tag for id %s, got %s" , "busybox" , id1 , f [ 1 ] )
2015-02-05 17:55:08 -05:00
}
case id2 :
if f [ 1 ] != tag {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "Expected %s tag for id %s, got %s" , tag , id2 , f [ 1 ] )
2015-04-01 10:08:00 -04:00
}
case id3 :
if f [ 1 ] != imageID {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "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 ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-07-14 02:35:36 -04:00
dockerCmd ( c , "run" , "--name=first" , "-d" , "busybox" , "top" )
dockerCmd ( c , "run" , "--name=second" , "--link=first:first" , "-d" , "busybox" , "top" )
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 ] )
}
if ! reflect . DeepEqual ( expected , names ) {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "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 ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-04-02 11:26:29 -04:00
portRange := "3800-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
// check that the port range is in the output
if ! strings . Contains ( string ( out ) , portRange ) {
2015-04-18 12:46:47 -04:00
c . Fatalf ( "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 ) {
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-05-19 01:48:44 -04:00
if ! strings . Contains ( out , "virtual" ) {
c . Fatalf ( "docker ps with --size should show virtual size of container" )
}
}
2015-05-20 17:51:58 -04:00
func ( s * DockerSuite ) TestPsListContainersFilterCreated ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-05-20 17:51:58 -04:00
// 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-05-20 17:51:58 -04:00
if strings . Contains ( out , shortCID ) {
c . Fatalf ( "Should have not seen '%s' in ps output:\n%s" , shortCID , out )
}
// 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 ++
if ! strings . Contains ( line , "Created" ) {
c . Fatalf ( "Missing 'Created' on '%s'" , line )
}
}
if hits != 1 {
c . Fatalf ( "Should have seen '%s' in ps -a output once:%d\n%s" , shortCID , hits , out )
}
// 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 )
if ! strings . HasPrefix ( cID , containerOut ) {
c . Fatalf ( "Expected id %s, got %s for filter, out: %s" , cID , containerOut , out )
}
}
2015-07-17 00:03:16 -04:00
func ( s * DockerSuite ) TestPsFormatMultiNames ( c * check . C ) {
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
for _ , l := range lines {
names = append ( names , l )
}
if ! reflect . DeepEqual ( expected , names ) {
c . Fatalf ( "Expected array with non-truncated names: %v, got: %v" , expected , names )
}
//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
for _ , l := range lines {
truncNames = append ( truncNames , l )
}
if ! reflect . DeepEqual ( expected , truncNames ) {
c . Fatalf ( "Expected array with truncated names: %v, got: %v" , expected , truncNames )
}
}
2015-07-23 16:23:24 -04:00
func ( s * DockerSuite ) TestPsFormatHeaders ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-07-23 16:23:24 -04:00
// make sure no-container "docker ps" still prints the header row
out , _ := dockerCmd ( c , "ps" , "--format" , "table {{.ID}}" )
if out != "CONTAINER ID\n" {
c . Fatalf ( ` Expected 'CONTAINER ID\n', got %v ` , out )
}
// verify that "docker ps" with a container still prints the header row also
dockerCmd ( c , "run" , "--name=test" , "-d" , "busybox" , "top" )
out , _ = dockerCmd ( c , "ps" , "--format" , "table {{.Names}}" )
if out != "NAMES\ntest\n" {
c . Fatalf ( ` Expected 'NAMES\ntest\n', got %v ` , out )
}
}
2015-08-14 18:00:48 -04:00
func ( s * DockerSuite ) TestPsDefaultFormatAndQuiet ( c * check . C ) {
2015-08-28 13:36:42 -04:00
testRequires ( c , DaemonIsLinux )
2015-08-14 18:00:48 -04:00
config := ` {
"psFormat" : "{{ .ID }} default"
} `
d , err := ioutil . TempDir ( "" , "integration-cli-" )
c . Assert ( err , check . IsNil )
defer os . RemoveAll ( d )
err = ioutil . WriteFile ( filepath . Join ( d , "config.json" ) , [ ] byte ( config ) , 0644 )
c . Assert ( err , check . IsNil )
out , _ := dockerCmd ( c , "run" , "--name=test" , "-d" , "busybox" , "top" )
id := strings . TrimSpace ( out )
out , _ = dockerCmd ( c , "--config" , d , "ps" , "-q" )
if ! strings . HasPrefix ( id , strings . TrimSpace ( out ) ) {
c . Fatalf ( "Expected to print only the container id, got %v\n" , out )
}
}
2015-04-30 21:13:18 -04:00
// Test for GitHub issue #12595
func ( s * DockerSuite ) TestPsImageIDAfterUpdate ( c * check . C ) {
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"
runCmd := exec . Command ( dockerBinary , "tag" , "busybox:latest" , originalImageName )
out , _ , err := runCommandWithOutput ( runCmd )
c . Assert ( err , check . IsNil )
originalImageID , err := getIDByName ( originalImageName )
c . Assert ( err , check . IsNil )
runCmd = exec . Command ( dockerBinary , "run" , "-d" , originalImageName , "top" )
out , _ , err = runCommandWithOutput ( runCmd )
c . Assert ( err , check . IsNil )
containerID := strings . TrimSpace ( out )
linesOut , err := exec . Command ( dockerBinary , "ps" , "--no-trunc" ) . CombinedOutput ( )
c . Assert ( err , check . IsNil )
lines := strings . Split ( strings . TrimSpace ( string ( linesOut ) ) , "\n" )
// skip header
lines = lines [ 1 : ]
c . Assert ( len ( lines ) , check . Equals , 1 )
for _ , line := range lines {
f := strings . Fields ( line )
c . Assert ( f [ 1 ] , check . Equals , originalImageName )
}
runCmd = exec . Command ( dockerBinary , "commit" , containerID , updatedImageName )
out , _ , err = runCommandWithOutput ( runCmd )
c . Assert ( err , check . IsNil )
runCmd = exec . Command ( dockerBinary , "tag" , "-f" , updatedImageName , originalImageName )
out , _ , err = runCommandWithOutput ( runCmd )
c . Assert ( err , check . IsNil )
linesOut , err = exec . Command ( dockerBinary , "ps" , "--no-trunc" ) . CombinedOutput ( )
c . Assert ( err , check . IsNil )
lines = strings . Split ( strings . TrimSpace ( string ( linesOut ) ) , "\n" )
// skip header
lines = lines [ 1 : ]
c . Assert ( len ( lines ) , check . Equals , 1 )
for _ , line := range lines {
f := strings . Fields ( line )
c . Assert ( f [ 1 ] , check . Equals , originalImageID )
}
}