2014-11-20 00:19:16 -05:00
// +build !windows
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"os/exec"
2015-09-11 06:01:47 -04:00
"strings"
"time"
2014-11-20 00:19:16 -05:00
"unicode"
2015-10-19 19:15:24 -04:00
"github.com/docker/docker/pkg/integration/checker"
2015-04-18 12:46:47 -04:00
"github.com/go-check/check"
2014-11-20 00:19:16 -05:00
"github.com/kr/pty"
)
// #5979
2015-04-18 12:46:47 -04:00
func ( s * DockerSuite ) TestEventsRedirectStdout ( c * check . C ) {
since := daemonTime ( c ) . Unix ( )
dockerCmd ( c , "run" , "busybox" , "true" )
2014-11-20 00:19:16 -05:00
file , err := ioutil . TempFile ( "" , "" )
2015-10-19 19:15:24 -04:00
c . Assert ( err , checker . IsNil , check . Commentf ( "could not create temp file" ) )
2014-11-20 00:19:16 -05:00
defer os . Remove ( file . Name ( ) )
2015-04-18 12:46:47 -04:00
command := fmt . Sprintf ( "%s events --since=%d --until=%d > %s" , dockerBinary , since , daemonTime ( c ) . Unix ( ) , file . Name ( ) )
2014-11-20 00:19:16 -05:00
_ , tty , err := pty . Open ( )
2015-10-19 19:15:24 -04:00
c . Assert ( err , checker . IsNil , check . Commentf ( "Could not open pty" ) )
2014-11-20 00:19:16 -05:00
cmd := exec . Command ( "sh" , "-c" , command )
cmd . Stdin = tty
cmd . Stdout = tty
cmd . Stderr = tty
2015-10-19 19:15:24 -04:00
c . Assert ( cmd . Run ( ) , checker . IsNil , check . Commentf ( "run err for command %q" , command ) )
2014-11-20 00:19:16 -05:00
scanner := bufio . NewScanner ( file )
for scanner . Scan ( ) {
2015-04-18 12:46:47 -04:00
for _ , ch := range scanner . Text ( ) {
2015-10-19 19:15:24 -04:00
c . Assert ( unicode . IsControl ( ch ) , checker . False , check . Commentf ( "found control character %v" , [ ] byte ( string ( ch ) ) ) )
2014-11-20 00:19:16 -05:00
}
}
2015-10-19 19:15:24 -04:00
c . Assert ( scanner . Err ( ) , checker . IsNil , check . Commentf ( "Scan err for command %q" , command ) )
2014-11-20 00:19:16 -05:00
}
2015-09-11 06:01:47 -04:00
func ( s * DockerSuite ) TestEventsOOMDisableFalse ( c * check . C ) {
2015-11-10 19:06:13 -05:00
testRequires ( c , DaemonIsLinux , oomControl , memoryLimitSupport , NotGCCGO )
2015-09-11 06:01:47 -04:00
errChan := make ( chan error )
go func ( ) {
defer close ( errChan )
out , exitCode , _ := dockerCmdWithError ( "run" , "--name" , "oomFalse" , "-m" , "10MB" , "busybox" , "sh" , "-c" , "x=a; while true; do x=$x$x$x$x; done" )
if expected := 137 ; exitCode != expected {
errChan <- fmt . Errorf ( "wrong exit code for OOM container: expected %d, got %d (output: %q)" , expected , exitCode , out )
}
} ( )
select {
case err := <- errChan :
2015-10-19 19:15:24 -04:00
c . Assert ( err , checker . IsNil )
2015-09-11 06:01:47 -04:00
case <- time . After ( 30 * time . Second ) :
c . Fatal ( "Timeout waiting for container to die on OOM" )
}
out , _ := dockerCmd ( c , "events" , "--since=0" , "-f" , "container=oomFalse" , fmt . Sprintf ( "--until=%d" , daemonTime ( c ) . Unix ( ) ) )
events := strings . Split ( strings . TrimSuffix ( out , "\n" ) , "\n" )
2015-10-19 19:15:24 -04:00
c . Assert ( len ( events ) , checker . GreaterOrEqualThan , 5 ) //Missing expected event
2015-09-11 06:01:47 -04:00
createEvent := strings . Fields ( events [ len ( events ) - 5 ] )
attachEvent := strings . Fields ( events [ len ( events ) - 4 ] )
startEvent := strings . Fields ( events [ len ( events ) - 3 ] )
oomEvent := strings . Fields ( events [ len ( events ) - 2 ] )
dieEvent := strings . Fields ( events [ len ( events ) - 1 ] )
2015-10-19 19:15:24 -04:00
c . Assert ( createEvent [ len ( createEvent ) - 1 ] , checker . Equals , "create" , check . Commentf ( "event should be create, not %#v" , createEvent ) )
c . Assert ( attachEvent [ len ( attachEvent ) - 1 ] , checker . Equals , "attach" , check . Commentf ( "event should be attach, not %#v" , attachEvent ) )
c . Assert ( startEvent [ len ( startEvent ) - 1 ] , checker . Equals , "start" , check . Commentf ( "event should be start, not %#v" , startEvent ) )
c . Assert ( oomEvent [ len ( oomEvent ) - 1 ] , checker . Equals , "oom" , check . Commentf ( "event should be oom, not %#v" , oomEvent ) )
c . Assert ( dieEvent [ len ( dieEvent ) - 1 ] , checker . Equals , "die" , check . Commentf ( "event should be die, not %#v" , dieEvent ) )
2015-09-11 06:01:47 -04:00
}
func ( s * DockerSuite ) TestEventsOOMDisableTrue ( c * check . C ) {
2015-11-10 19:06:13 -05:00
testRequires ( c , DaemonIsLinux , oomControl , memoryLimitSupport , NotGCCGO )
2015-09-11 06:01:47 -04:00
errChan := make ( chan error )
go func ( ) {
defer close ( errChan )
out , exitCode , _ := dockerCmdWithError ( "run" , "--oom-kill-disable=true" , "--name" , "oomTrue" , "-m" , "10MB" , "busybox" , "sh" , "-c" , "x=a; while true; do x=$x$x$x$x; done" )
if expected := 137 ; exitCode != expected {
errChan <- fmt . Errorf ( "wrong exit code for OOM container: expected %d, got %d (output: %q)" , expected , exitCode , out )
}
} ( )
select {
case err := <- errChan :
2015-10-19 19:15:24 -04:00
c . Assert ( err , checker . IsNil )
2015-09-11 06:01:47 -04:00
case <- time . After ( 20 * time . Second ) :
defer dockerCmd ( c , "kill" , "oomTrue" )
out , _ := dockerCmd ( c , "events" , "--since=0" , "-f" , "container=oomTrue" , fmt . Sprintf ( "--until=%d" , daemonTime ( c ) . Unix ( ) ) )
events := strings . Split ( strings . TrimSuffix ( out , "\n" ) , "\n" )
2015-10-19 19:15:24 -04:00
c . Assert ( len ( events ) , checker . GreaterOrEqualThan , 4 ) //Missing expected event
2015-09-11 06:01:47 -04:00
createEvent := strings . Fields ( events [ len ( events ) - 4 ] )
attachEvent := strings . Fields ( events [ len ( events ) - 3 ] )
startEvent := strings . Fields ( events [ len ( events ) - 2 ] )
oomEvent := strings . Fields ( events [ len ( events ) - 1 ] )
2015-10-19 19:15:24 -04:00
c . Assert ( createEvent [ len ( createEvent ) - 1 ] , checker . Equals , "create" , check . Commentf ( "event should be create, not %#v" , createEvent ) )
c . Assert ( attachEvent [ len ( attachEvent ) - 1 ] , checker . Equals , "attach" , check . Commentf ( "event should be attach, not %#v" , attachEvent ) )
c . Assert ( startEvent [ len ( startEvent ) - 1 ] , checker . Equals , "start" , check . Commentf ( "event should be start, not %#v" , startEvent ) )
c . Assert ( oomEvent [ len ( oomEvent ) - 1 ] , checker . Equals , "oom" , check . Commentf ( "event should be oom, not %#v" , oomEvent ) )
2015-09-11 06:01:47 -04:00
out , _ = dockerCmd ( c , "inspect" , "-f" , "{{.State.Status}}" , "oomTrue" )
2015-10-19 19:15:24 -04:00
c . Assert ( strings . TrimSpace ( out ) , checker . Equals , "running" , check . Commentf ( "container should be still running" ) )
2015-09-11 06:01:47 -04:00
}
}