2021-08-23 09:14:53 -04:00
//go:build !windows
2014-11-20 00:19:16 -05:00
// +build !windows
package main
import (
"bufio"
2016-08-25 13:29:10 -04:00
"bytes"
2014-11-20 00:19:16 -05:00
"fmt"
"os"
"os/exec"
2015-09-11 06:01:47 -04:00
"strings"
2019-09-09 17:06:12 -04:00
"testing"
2015-09-11 06:01:47 -04:00
"time"
2014-11-20 00:19:16 -05:00
"unicode"
2019-07-29 19:59:08 -04:00
"github.com/creack/pty"
2017-03-23 13:35:22 -04:00
"github.com/docker/docker/integration-cli/cli/build"
2017-05-23 10:22:32 -04:00
"golang.org/x/sys/unix"
2020-02-07 08:39:24 -05:00
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
2014-11-20 00:19:16 -05:00
)
// #5979
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsRedirectStdout ( c * testing . T ) {
2016-04-11 14:52:34 -04:00
since := daemonUnixTime ( c )
2015-04-18 12:46:47 -04:00
dockerCmd ( c , "run" , "busybox" , "true" )
2014-11-20 00:19:16 -05:00
2021-08-24 06:10:50 -04:00
file , err := os . CreateTemp ( "" , "" )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err , "could not create temp file" )
2014-11-20 00:19:16 -05:00
defer os . Remove ( file . Name ( ) )
2016-04-11 14:52:34 -04:00
command := fmt . Sprintf ( "%s events --since=%s --until=%s > %s" , dockerBinary , since , daemonUnixTime ( c ) , file . Name ( ) )
2014-11-20 00:19:16 -05:00
_ , tty , err := pty . Open ( )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err , "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
2019-04-04 09:23:19 -04:00
assert . NilError ( c , cmd . Run ( ) , "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 ( ) {
2019-04-04 09:23:19 -04:00
assert . Check ( c , unicode . IsControl ( ch ) == false , "found control character %v" , [ ] byte ( string ( ch ) ) )
2014-11-20 00:19:16 -05:00
}
}
2019-04-04 09:23:19 -04:00
assert . NilError ( c , scanner . Err ( ) , "Scan err for command %q" , command )
2014-11-20 00:19:16 -05:00
}
2015-09-11 06:01:47 -04:00
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsOOMDisableFalse ( c * testing . T ) {
2018-03-08 17:57:23 -05:00
testRequires ( c , DaemonIsLinux , oomControl , memoryLimitSupport , swapMemorySupport , NotPpc64le )
2015-09-11 06:01:47 -04:00
2020-02-25 17:13:25 -05:00
errChan := make ( chan error , 1 )
2015-09-11 06:01:47 -04:00
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 :
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2015-09-11 06:01:47 -04:00
case <- time . After ( 30 * time . Second ) :
c . Fatal ( "Timeout waiting for container to die on OOM" )
}
2016-04-11 14:52:34 -04:00
out , _ := dockerCmd ( c , "events" , "--since=0" , "-f" , "container=oomFalse" , "--until" , daemonUnixTime ( c ) )
2015-09-11 06:01:47 -04:00
events := strings . Split ( strings . TrimSuffix ( out , "\n" ) , "\n" )
2015-12-21 17:55:23 -05:00
nEvents := len ( events )
2019-04-04 09:23:19 -04:00
assert . Assert ( c , nEvents >= 5 )
assert . Equal ( c , parseEventAction ( c , events [ nEvents - 5 ] ) , "create" )
assert . Equal ( c , parseEventAction ( c , events [ nEvents - 4 ] ) , "attach" )
assert . Equal ( c , parseEventAction ( c , events [ nEvents - 3 ] ) , "start" )
assert . Equal ( c , parseEventAction ( c , events [ nEvents - 2 ] ) , "oom" )
assert . Equal ( c , parseEventAction ( c , events [ nEvents - 1 ] ) , "die" )
2015-09-11 06:01:47 -04:00
}
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsOOMDisableTrue ( c * testing . T ) {
2018-03-08 17:57:23 -05:00
testRequires ( c , DaemonIsLinux , oomControl , memoryLimitSupport , NotArm , swapMemorySupport , NotPpc64le )
2015-09-11 06:01:47 -04:00
2020-02-25 17:13:25 -05:00
errChan := make ( chan error , 1 )
2016-01-23 22:27:39 -05:00
observer , err := newEventObserver ( c )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2016-01-23 22:27:39 -05:00
err = observer . Start ( )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2016-01-23 22:27:39 -05:00
defer observer . Stop ( )
2015-09-11 06:01:47 -04:00
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 )
}
} ( )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , waitRun ( "oomTrue" ) )
2017-09-05 11:19:57 -04:00
defer dockerCmdWithResult ( "kill" , "oomTrue" )
2016-01-28 09:19:25 -05:00
containerID := inspectField ( c , "oomTrue" , "Id" )
2015-09-11 06:01:47 -04:00
2016-01-23 22:27:39 -05:00
testActions := map [ string ] chan bool {
"oom" : make ( chan bool ) ,
}
2015-09-11 06:01:47 -04:00
2016-01-23 22:27:39 -05:00
matcher := matchEventLine ( containerID , "container" , testActions )
processor := processEventMatch ( testActions )
go observer . Match ( matcher , processor )
select {
case <- time . After ( 20 * time . Second ) :
observer . CheckEventError ( c , containerID , "oom" , matcher )
case <- testActions [ "oom" ] :
2017-03-23 13:35:22 -04:00
// ignore, done
2016-01-23 22:27:39 -05:00
case errRun := <- errChan :
if errRun != nil {
c . Fatalf ( "%v" , errRun )
} else {
c . Fatalf ( "container should be still running but it's not" )
}
2015-09-11 06:01:47 -04:00
}
2016-01-23 22:27:39 -05:00
2016-01-28 09:19:25 -05:00
status := inspectField ( c , "oomTrue" , "State.Status" )
2019-04-04 09:23:19 -04:00
assert . Equal ( c , strings . TrimSpace ( status ) , "running" , "container should be still running" )
2015-09-11 06:01:47 -04:00
}
2015-12-07 08:33:16 -05:00
// #18453
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsContainerFilterByName ( c * testing . T ) {
2015-12-07 08:33:16 -05:00
testRequires ( c , DaemonIsLinux )
2015-12-21 17:55:23 -05:00
cOut , _ := dockerCmd ( c , "run" , "--name=foo" , "-d" , "busybox" , "top" )
c1 := strings . TrimSpace ( cOut )
waitRun ( "foo" )
cOut , _ = dockerCmd ( c , "run" , "--name=bar" , "-d" , "busybox" , "top" )
c2 := strings . TrimSpace ( cOut )
waitRun ( "bar" )
2016-04-11 14:52:34 -04:00
out , _ := dockerCmd ( c , "events" , "-f" , "container=foo" , "--since=0" , "--until" , daemonUnixTime ( c ) )
2019-04-04 09:23:19 -04:00
assert . Assert ( c , strings . Contains ( out , c1 ) , out )
assert . Assert ( c , ! strings . Contains ( out , c2 ) , out )
2015-12-07 08:33:16 -05:00
}
// #18453
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsContainerFilterBeforeCreate ( c * testing . T ) {
2015-12-07 08:33:16 -05:00
testRequires ( c , DaemonIsLinux )
2016-08-25 13:29:10 -04:00
buf := & bytes . Buffer { }
cmd := exec . Command ( dockerBinary , "events" , "-f" , "container=foo" , "--since=0" )
cmd . Stdout = buf
2019-04-04 09:23:19 -04:00
assert . NilError ( c , cmd . Start ( ) )
2016-08-25 13:29:10 -04:00
defer cmd . Wait ( )
defer cmd . Process . Kill ( )
// Sleep for a second to make sure we are testing the case where events are listened before container starts.
time . Sleep ( time . Second )
id , _ := dockerCmd ( c , "run" , "--name=foo" , "-d" , "busybox" , "top" )
2015-12-07 08:33:16 -05:00
cID := strings . TrimSpace ( id )
2016-08-25 13:29:10 -04:00
for i := 0 ; ; i ++ {
out := buf . String ( )
if strings . Contains ( out , cID ) {
break
}
if i > 30 {
c . Fatalf ( "Missing event of container (foo, %v), got %q" , cID , out )
}
time . Sleep ( 500 * time . Millisecond )
}
2015-12-07 08:33:16 -05:00
}
2015-12-21 19:45:31 -05:00
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestVolumeEvents ( c * testing . T ) {
2015-12-21 19:45:31 -05:00
testRequires ( c , DaemonIsLinux )
2016-04-11 14:52:34 -04:00
since := daemonUnixTime ( c )
2015-12-21 19:45:31 -05:00
// Observe create/mount volume actions
2016-06-14 18:42:30 -04:00
dockerCmd ( c , "volume" , "create" , "test-event-volume-local" )
2015-12-21 19:45:31 -05:00
dockerCmd ( c , "run" , "--name" , "test-volume-container" , "--volume" , "test-event-volume-local:/foo" , "-d" , "busybox" , "true" )
waitRun ( "test-volume-container" )
// Observe unmount/destroy volume actions
dockerCmd ( c , "rm" , "-f" , "test-volume-container" )
dockerCmd ( c , "volume" , "rm" , "test-event-volume-local" )
2016-04-11 14:52:34 -04:00
until := daemonUnixTime ( c )
out , _ := dockerCmd ( c , "events" , "--since" , since , "--until" , until )
2015-12-21 19:45:31 -05:00
events := strings . Split ( strings . TrimSpace ( out ) , "\n" )
volumes: only send "create" event when actually creating volume
The VolumesService did not have information wether or not a volume
was _created_ or if a volume already existed in the driver, and
the existing volume was used.
As a result, multiple "create" events could be generated for the
same volume. For example:
1. Run `docker events` in a shell to start listening for events
2. Create a volume:
docker volume create myvolume
3. Start a container that uses that volume:
docker run -dit -v myvolume:/foo busybox
4. Check the events that were generated:
2021-02-15T18:49:55.874621004+01:00 volume create myvolume (driver=local)
2021-02-15T18:50:11.442759052+01:00 volume create myvolume (driver=local)
2021-02-15T18:50:11.487104176+01:00 container create 45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1 (image=busybox, name=gracious_hypatia)
2021-02-15T18:50:11.519288102+01:00 network connect a19f6bb8d44ff84d478670fa4e34c5bf5305f42786294d3d90e790ac74b6d3e0 (container=45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1, name=bridge, type=bridge)
2021-02-15T18:50:11.526407799+01:00 volume mount myvolume (container=45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1, destination=/foo, driver=local, propagation=, read/write=true)
2021-02-15T18:50:11.864134043+01:00 container start 45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1 (image=busybox, name=gracious_hypatia)
5. Notice that a "volume create" event is created twice;
- once when `docker volume create` was ran
- once when `docker run ...` was ran
This patch moves the generation of (most) events to the volume _store_, and only
generates an event if the volume did not yet exist.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-02-15 12:45:15 -05:00
assert . Assert ( c , len ( events ) > 3 )
2015-12-21 19:45:31 -05:00
volumeEvents := eventActionsByIDAndType ( c , events , "test-event-volume-local" , "volume" )
volumes: only send "create" event when actually creating volume
The VolumesService did not have information wether or not a volume
was _created_ or if a volume already existed in the driver, and
the existing volume was used.
As a result, multiple "create" events could be generated for the
same volume. For example:
1. Run `docker events` in a shell to start listening for events
2. Create a volume:
docker volume create myvolume
3. Start a container that uses that volume:
docker run -dit -v myvolume:/foo busybox
4. Check the events that were generated:
2021-02-15T18:49:55.874621004+01:00 volume create myvolume (driver=local)
2021-02-15T18:50:11.442759052+01:00 volume create myvolume (driver=local)
2021-02-15T18:50:11.487104176+01:00 container create 45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1 (image=busybox, name=gracious_hypatia)
2021-02-15T18:50:11.519288102+01:00 network connect a19f6bb8d44ff84d478670fa4e34c5bf5305f42786294d3d90e790ac74b6d3e0 (container=45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1, name=bridge, type=bridge)
2021-02-15T18:50:11.526407799+01:00 volume mount myvolume (container=45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1, destination=/foo, driver=local, propagation=, read/write=true)
2021-02-15T18:50:11.864134043+01:00 container start 45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1 (image=busybox, name=gracious_hypatia)
5. Notice that a "volume create" event is created twice;
- once when `docker volume create` was ran
- once when `docker run ...` was ran
This patch moves the generation of (most) events to the volume _store_, and only
generates an event if the volume did not yet exist.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-02-15 12:45:15 -05:00
assert . Equal ( c , len ( volumeEvents ) , 4 )
2019-04-04 09:23:19 -04:00
assert . Equal ( c , volumeEvents [ 0 ] , "create" )
volumes: only send "create" event when actually creating volume
The VolumesService did not have information wether or not a volume
was _created_ or if a volume already existed in the driver, and
the existing volume was used.
As a result, multiple "create" events could be generated for the
same volume. For example:
1. Run `docker events` in a shell to start listening for events
2. Create a volume:
docker volume create myvolume
3. Start a container that uses that volume:
docker run -dit -v myvolume:/foo busybox
4. Check the events that were generated:
2021-02-15T18:49:55.874621004+01:00 volume create myvolume (driver=local)
2021-02-15T18:50:11.442759052+01:00 volume create myvolume (driver=local)
2021-02-15T18:50:11.487104176+01:00 container create 45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1 (image=busybox, name=gracious_hypatia)
2021-02-15T18:50:11.519288102+01:00 network connect a19f6bb8d44ff84d478670fa4e34c5bf5305f42786294d3d90e790ac74b6d3e0 (container=45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1, name=bridge, type=bridge)
2021-02-15T18:50:11.526407799+01:00 volume mount myvolume (container=45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1, destination=/foo, driver=local, propagation=, read/write=true)
2021-02-15T18:50:11.864134043+01:00 container start 45112157c8b1382626bf5e01ef18445a4c680f3846c5e32d01775dddee8ca6d1 (image=busybox, name=gracious_hypatia)
5. Notice that a "volume create" event is created twice;
- once when `docker volume create` was ran
- once when `docker run ...` was ran
This patch moves the generation of (most) events to the volume _store_, and only
generates an event if the volume did not yet exist.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-02-15 12:45:15 -05:00
assert . Equal ( c , volumeEvents [ 1 ] , "mount" )
assert . Equal ( c , volumeEvents [ 2 ] , "unmount" )
assert . Equal ( c , volumeEvents [ 3 ] , "destroy" )
2015-12-21 19:45:31 -05:00
}
2015-12-21 23:35:30 -05:00
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestNetworkEvents ( c * testing . T ) {
2015-12-21 23:35:30 -05:00
testRequires ( c , DaemonIsLinux )
2016-04-11 14:52:34 -04:00
since := daemonUnixTime ( c )
2015-12-21 23:35:30 -05:00
// Observe create/connect network actions
dockerCmd ( c , "network" , "create" , "test-event-network-local" )
dockerCmd ( c , "run" , "--name" , "test-network-container" , "--net" , "test-event-network-local" , "-d" , "busybox" , "true" )
waitRun ( "test-network-container" )
// Observe disconnect/destroy network actions
dockerCmd ( c , "rm" , "-f" , "test-network-container" )
dockerCmd ( c , "network" , "rm" , "test-event-network-local" )
2016-04-11 14:52:34 -04:00
until := daemonUnixTime ( c )
out , _ := dockerCmd ( c , "events" , "--since" , since , "--until" , until )
2015-12-21 23:35:30 -05:00
events := strings . Split ( strings . TrimSpace ( out ) , "\n" )
2019-04-04 09:23:19 -04:00
assert . Assert ( c , len ( events ) > 4 )
2015-12-21 23:35:30 -05:00
netEvents := eventActionsByIDAndType ( c , events , "test-event-network-local" , "network" )
2019-04-04 09:23:19 -04:00
assert . Equal ( c , len ( netEvents ) , 4 )
assert . Equal ( c , netEvents [ 0 ] , "create" )
assert . Equal ( c , netEvents [ 1 ] , "connect" )
assert . Equal ( c , netEvents [ 2 ] , "disconnect" )
assert . Equal ( c , netEvents [ 3 ] , "destroy" )
2015-12-21 23:35:30 -05:00
}
2015-12-28 15:15:34 -05:00
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsContainerWithMultiNetwork ( c * testing . T ) {
2016-06-08 10:36:42 -04:00
testRequires ( c , DaemonIsLinux )
// Observe create/connect network actions
dockerCmd ( c , "network" , "create" , "test-event-network-local-1" )
dockerCmd ( c , "network" , "create" , "test-event-network-local-2" )
dockerCmd ( c , "run" , "--name" , "test-network-container" , "--net" , "test-event-network-local-1" , "-td" , "busybox" , "sh" )
waitRun ( "test-network-container" )
dockerCmd ( c , "network" , "connect" , "test-event-network-local-2" , "test-network-container" )
since := daemonUnixTime ( c )
dockerCmd ( c , "stop" , "-t" , "1" , "test-network-container" )
until := daemonUnixTime ( c )
out , _ := dockerCmd ( c , "events" , "--since" , since , "--until" , until , "-f" , "type=network" )
netEvents := strings . Split ( strings . TrimSpace ( out ) , "\n" )
2016-06-11 02:51:09 -04:00
// received two network disconnect events
2019-04-04 09:23:19 -04:00
assert . Equal ( c , len ( netEvents ) , 2 )
assert . Assert ( c , strings . Contains ( netEvents [ 0 ] , "disconnect" ) )
assert . Assert ( c , strings . Contains ( netEvents [ 1 ] , "disconnect" ) )
2016-06-11 02:51:09 -04:00
2019-11-27 09:36:45 -05:00
// both networks appeared in the network event output
2019-04-04 09:23:19 -04:00
assert . Assert ( c , strings . Contains ( out , "test-event-network-local-1" ) )
assert . Assert ( c , strings . Contains ( out , "test-event-network-local-2" ) )
2016-06-08 10:36:42 -04:00
}
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsStreaming ( c * testing . T ) {
2015-12-28 15:15:34 -05:00
testRequires ( c , DaemonIsLinux )
observer , err := newEventObserver ( c )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2015-12-28 15:15:34 -05:00
err = observer . Start ( )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2015-12-28 15:15:34 -05:00
defer observer . Stop ( )
out , _ := dockerCmd ( c , "run" , "-d" , "busybox:latest" , "true" )
containerID := strings . TrimSpace ( out )
testActions := map [ string ] chan bool {
2016-02-18 16:10:29 -05:00
"create" : make ( chan bool , 1 ) ,
"start" : make ( chan bool , 1 ) ,
"die" : make ( chan bool , 1 ) ,
"destroy" : make ( chan bool , 1 ) ,
2015-12-28 15:15:34 -05:00
}
matcher := matchEventLine ( containerID , "container" , testActions )
2016-01-20 17:36:39 -05:00
processor := processEventMatch ( testActions )
go observer . Match ( matcher , processor )
2015-12-28 15:15:34 -05:00
select {
case <- time . After ( 5 * time . Second ) :
observer . CheckEventError ( c , containerID , "create" , matcher )
case <- testActions [ "create" ] :
// ignore, done
}
select {
case <- time . After ( 5 * time . Second ) :
observer . CheckEventError ( c , containerID , "start" , matcher )
case <- testActions [ "start" ] :
// ignore, done
}
select {
case <- time . After ( 5 * time . Second ) :
observer . CheckEventError ( c , containerID , "die" , matcher )
case <- testActions [ "die" ] :
// ignore, done
}
dockerCmd ( c , "rm" , containerID )
select {
case <- time . After ( 5 * time . Second ) :
observer . CheckEventError ( c , containerID , "destroy" , matcher )
case <- testActions [ "destroy" ] :
// ignore, done
}
}
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsImageUntagDelete ( c * testing . T ) {
2015-12-28 15:15:34 -05:00
testRequires ( c , DaemonIsLinux )
observer , err := newEventObserver ( c )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2015-12-28 15:15:34 -05:00
err = observer . Start ( )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2015-12-28 15:15:34 -05:00
defer observer . Stop ( )
name := "testimageevents"
2017-03-23 13:35:22 -04:00
buildImageSuccessfully ( c , name , build . WithDockerfile ( ` FROM scratch
2017-01-16 05:30:14 -05:00
MAINTAINER "docker" ` ) )
imageID := getIDByName ( c , name )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , deleteImages ( name ) )
2015-12-28 15:15:34 -05:00
testActions := map [ string ] chan bool {
2016-02-18 16:10:29 -05:00
"untag" : make ( chan bool , 1 ) ,
"delete" : make ( chan bool , 1 ) ,
2015-12-28 15:15:34 -05:00
}
matcher := matchEventLine ( imageID , "image" , testActions )
2016-01-20 17:36:39 -05:00
processor := processEventMatch ( testActions )
go observer . Match ( matcher , processor )
2015-12-28 15:15:34 -05:00
select {
case <- time . After ( 10 * time . Second ) :
observer . CheckEventError ( c , imageID , "untag" , matcher )
case <- testActions [ "untag" ] :
// ignore, done
}
select {
case <- time . After ( 10 * time . Second ) :
observer . CheckEventError ( c , imageID , "delete" , matcher )
case <- testActions [ "delete" ] :
// ignore, done
}
}
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsFilterVolumeAndNetworkType ( c * testing . T ) {
2015-12-28 15:15:34 -05:00
testRequires ( c , DaemonIsLinux )
2016-04-11 14:52:34 -04:00
since := daemonUnixTime ( c )
2015-12-28 15:15:34 -05:00
dockerCmd ( c , "network" , "create" , "test-event-network-type" )
2016-06-14 18:42:30 -04:00
dockerCmd ( c , "volume" , "create" , "test-event-volume-type" )
2015-12-28 15:15:34 -05:00
2016-04-11 14:52:34 -04:00
out , _ := dockerCmd ( c , "events" , "--filter" , "type=volume" , "--filter" , "type=network" , "--since" , since , "--until" , daemonUnixTime ( c ) )
2015-12-28 15:15:34 -05:00
events := strings . Split ( strings . TrimSpace ( out ) , "\n" )
2019-04-04 09:23:19 -04:00
assert . Assert ( c , len ( events ) >= 2 , out )
2015-12-28 15:15:34 -05:00
networkActions := eventActionsByIDAndType ( c , events , "test-event-network-type" , "network" )
volumeActions := eventActionsByIDAndType ( c , events , "test-event-volume-type" , "volume" )
2019-04-04 09:23:19 -04:00
assert . Equal ( c , volumeActions [ 0 ] , "create" )
assert . Equal ( c , networkActions [ 0 ] , "create" )
2015-12-28 15:15:34 -05:00
}
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsFilterVolumeID ( c * testing . T ) {
2015-12-28 15:15:34 -05:00
testRequires ( c , DaemonIsLinux )
2016-04-11 14:52:34 -04:00
since := daemonUnixTime ( c )
2015-12-28 15:15:34 -05:00
2016-06-14 18:42:30 -04:00
dockerCmd ( c , "volume" , "create" , "test-event-volume-id" )
2016-04-11 14:52:34 -04:00
out , _ := dockerCmd ( c , "events" , "--filter" , "volume=test-event-volume-id" , "--since" , since , "--until" , daemonUnixTime ( c ) )
2015-12-28 15:15:34 -05:00
events := strings . Split ( strings . TrimSpace ( out ) , "\n" )
2019-04-04 09:23:19 -04:00
assert . Equal ( c , len ( events ) , 1 )
2015-12-28 15:15:34 -05:00
2019-04-04 09:23:19 -04:00
assert . Equal ( c , len ( events ) , 1 )
assert . Assert ( c , strings . Contains ( events [ 0 ] , "test-event-volume-id" ) )
assert . Assert ( c , strings . Contains ( events [ 0 ] , "driver=local" ) )
2015-12-28 15:15:34 -05:00
}
2022-06-16 17:32:10 -04:00
func ( s * DockerCLIEventSuite ) TestEventsFilterNetworkID ( c * testing . T ) {
2015-12-28 15:15:34 -05:00
testRequires ( c , DaemonIsLinux )
2016-04-11 14:52:34 -04:00
since := daemonUnixTime ( c )
2015-12-28 15:15:34 -05:00
dockerCmd ( c , "network" , "create" , "test-event-network-local" )
2016-04-11 14:52:34 -04:00
out , _ := dockerCmd ( c , "events" , "--filter" , "network=test-event-network-local" , "--since" , since , "--until" , daemonUnixTime ( c ) )
2015-12-28 15:15:34 -05:00
events := strings . Split ( strings . TrimSpace ( out ) , "\n" )
2019-04-04 09:23:19 -04:00
assert . Equal ( c , len ( events ) , 1 )
assert . Assert ( c , strings . Contains ( events [ 0 ] , "test-event-network-local" ) )
assert . Assert ( c , strings . Contains ( events [ 0 ] , "type=bridge" ) )
2015-12-28 15:15:34 -05:00
}
2016-05-08 19:11:34 -04:00
2019-09-09 17:05:55 -04:00
func ( s * DockerDaemonSuite ) TestDaemonEvents ( c * testing . T ) {
2016-05-08 19:11:34 -04:00
// daemon config file
configFilePath := "test.json"
defer os . Remove ( configFilePath )
daemonConfig := ` { "labels":["foo=bar"]} `
2021-08-24 06:10:50 -04:00
err := os . WriteFile ( configFilePath , [ ] byte ( daemonConfig ) , 0644 )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2019-10-07 15:21:54 -04:00
s . d . Start ( c , "--config-file=" + configFilePath )
info := s . d . Info ( c )
2016-05-08 19:11:34 -04:00
2016-05-26 17:07:30 -04:00
daemonConfig = ` { "max-concurrent-downloads":1,"labels":["bar=foo"], "shutdown-timeout": 10} `
2021-08-24 06:10:50 -04:00
err = os . WriteFile ( configFilePath , [ ] byte ( daemonConfig ) , 0644 )
2019-10-07 15:21:54 -04:00
assert . NilError ( c , err )
2016-05-08 19:11:34 -04:00
2019-04-04 09:23:19 -04:00
assert . NilError ( c , s . d . Signal ( unix . SIGHUP ) )
2016-05-08 19:11:34 -04:00
time . Sleep ( 3 * time . Second )
2019-10-07 15:21:54 -04:00
out , err := s . d . Cmd ( "events" , "--since=0" , "--until" , daemonUnixTime ( c ) )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2016-05-23 17:49:50 -04:00
2017-07-19 14:24:54 -04:00
// only check for values known (daemon ID/name) or explicitly set above,
// otherwise just check for names being present.
expectedSubstrings := [ ] string {
2019-10-07 15:21:54 -04:00
" daemon reload " + info . ID + " " ,
2017-07-19 14:24:54 -04:00
"(allow-nondistributable-artifacts=[" ,
" debug=true, " ,
" default-ipc-mode=" ,
" default-runtime=" ,
" default-shm-size=" ,
" insecure-registries=[" ,
" labels=[\"bar=foo\"], " ,
" live-restore=" ,
" max-concurrent-downloads=1, " ,
" max-concurrent-uploads=5, " ,
2019-10-07 15:21:54 -04:00
" name=" + info . Name ,
2017-07-19 14:24:54 -04:00
" registry-mirrors=[" ,
" runtimes=" ,
" shutdown-timeout=10)" ,
}
for _ , s := range expectedSubstrings {
2019-10-09 12:54:17 -04:00
assert . Check ( c , is . Contains ( out , s ) )
2017-07-19 14:24:54 -04:00
}
2016-05-08 19:15:33 -04:00
}
2019-09-09 17:05:55 -04:00
func ( s * DockerDaemonSuite ) TestDaemonEventsWithFilters ( c * testing . T ) {
2016-05-08 19:15:33 -04:00
// daemon config file
configFilePath := "test.json"
defer os . Remove ( configFilePath )
daemonConfig := ` { "labels":["foo=bar"]} `
2021-08-24 06:10:50 -04:00
err := os . WriteFile ( configFilePath , [ ] byte ( daemonConfig ) , 0644 )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2019-10-07 15:21:54 -04:00
s . d . Start ( c , "--config-file=" + configFilePath )
info := s . d . Info ( c )
2016-05-08 19:15:33 -04:00
2019-10-07 15:21:54 -04:00
assert . NilError ( c , s . d . Signal ( unix . SIGHUP ) )
2016-05-08 19:15:33 -04:00
time . Sleep ( 3 * time . Second )
2019-10-07 15:21:54 -04:00
out , err := s . d . Cmd ( "events" , "--since=0" , "--until" , daemonUnixTime ( c ) , "--filter" , fmt . Sprintf ( "daemon=%s" , info . ID ) )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2019-10-07 15:21:54 -04:00
assert . Assert ( c , strings . Contains ( out , fmt . Sprintf ( "daemon reload %s" , info . ID ) ) )
2016-05-08 19:15:33 -04:00
2019-10-07 15:21:54 -04:00
out , err = s . d . Cmd ( "events" , "--since=0" , "--until" , daemonUnixTime ( c ) , "--filter" , fmt . Sprintf ( "daemon=%s" , info . ID ) )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2019-10-07 15:21:54 -04:00
assert . Assert ( c , strings . Contains ( out , fmt . Sprintf ( "daemon reload %s" , info . ID ) ) )
2016-05-08 19:15:33 -04:00
out , err = s . d . Cmd ( "events" , "--since=0" , "--until" , daemonUnixTime ( c ) , "--filter" , "daemon=foo" )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2019-10-07 15:21:54 -04:00
assert . Assert ( c , ! strings . Contains ( out , fmt . Sprintf ( "daemon reload %s" , info . ID ) ) )
2016-05-08 19:15:33 -04:00
out , err = s . d . Cmd ( "events" , "--since=0" , "--until" , daemonUnixTime ( c ) , "--filter" , "type=daemon" )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2019-10-07 15:21:54 -04:00
assert . Assert ( c , strings . Contains ( out , fmt . Sprintf ( "daemon reload %s" , info . ID ) ) )
2016-05-08 19:15:33 -04:00
out , err = s . d . Cmd ( "events" , "--since=0" , "--until" , daemonUnixTime ( c ) , "--filter" , "type=container" )
2019-04-04 09:23:19 -04:00
assert . NilError ( c , err )
2019-10-07 15:21:54 -04:00
assert . Assert ( c , ! strings . Contains ( out , fmt . Sprintf ( "daemon reload %s" , info . ID ) ) )
2016-05-08 19:11:34 -04:00
}