gopkg.in/docker/docker.v20@v20.10.27/integration-cli/docker_cli_events_unix_test.go (about) 1 //go:build !windows 2 // +build !windows 3 4 package main 5 6 import ( 7 "bufio" 8 "bytes" 9 "fmt" 10 "os" 11 "os/exec" 12 "strings" 13 "testing" 14 "time" 15 "unicode" 16 17 "github.com/creack/pty" 18 "github.com/docker/docker/integration-cli/cli/build" 19 "golang.org/x/sys/unix" 20 "gotest.tools/v3/assert" 21 is "gotest.tools/v3/assert/cmp" 22 ) 23 24 // #5979 25 func (s *DockerSuite) TestEventsRedirectStdout(c *testing.T) { 26 since := daemonUnixTime(c) 27 dockerCmd(c, "run", "busybox", "true") 28 29 file, err := os.CreateTemp("", "") 30 assert.NilError(c, err, "could not create temp file") 31 defer os.Remove(file.Name()) 32 33 command := fmt.Sprintf("%s events --since=%s --until=%s > %s", dockerBinary, since, daemonUnixTime(c), file.Name()) 34 _, tty, err := pty.Open() 35 assert.NilError(c, err, "Could not open pty") 36 cmd := exec.Command("sh", "-c", command) 37 cmd.Stdin = tty 38 cmd.Stdout = tty 39 cmd.Stderr = tty 40 assert.NilError(c, cmd.Run(), "run err for command %q", command) 41 42 scanner := bufio.NewScanner(file) 43 for scanner.Scan() { 44 for _, ch := range scanner.Text() { 45 assert.Check(c, unicode.IsControl(ch) == false, "found control character %v", []byte(string(ch))) 46 } 47 } 48 assert.NilError(c, scanner.Err(), "Scan err for command %q", command) 49 } 50 51 func (s *DockerSuite) TestEventsOOMDisableFalse(c *testing.T) { 52 testRequires(c, DaemonIsLinux, oomControl, memoryLimitSupport, swapMemorySupport, NotPpc64le) 53 54 errChan := make(chan error, 1) 55 go func() { 56 defer close(errChan) 57 out, exitCode, _ := dockerCmdWithError("run", "--name", "oomFalse", "-m", "10MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done") 58 if expected := 137; exitCode != expected { 59 errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out) 60 } 61 }() 62 select { 63 case err := <-errChan: 64 assert.NilError(c, err) 65 case <-time.After(30 * time.Second): 66 c.Fatal("Timeout waiting for container to die on OOM") 67 } 68 69 out, _ := dockerCmd(c, "events", "--since=0", "-f", "container=oomFalse", "--until", daemonUnixTime(c)) 70 events := strings.Split(strings.TrimSuffix(out, "\n"), "\n") 71 nEvents := len(events) 72 73 assert.Assert(c, nEvents >= 5) 74 assert.Equal(c, parseEventAction(c, events[nEvents-5]), "create") 75 assert.Equal(c, parseEventAction(c, events[nEvents-4]), "attach") 76 assert.Equal(c, parseEventAction(c, events[nEvents-3]), "start") 77 assert.Equal(c, parseEventAction(c, events[nEvents-2]), "oom") 78 assert.Equal(c, parseEventAction(c, events[nEvents-1]), "die") 79 } 80 81 func (s *DockerSuite) TestEventsOOMDisableTrue(c *testing.T) { 82 testRequires(c, DaemonIsLinux, oomControl, memoryLimitSupport, NotArm, swapMemorySupport, NotPpc64le) 83 84 errChan := make(chan error, 1) 85 observer, err := newEventObserver(c) 86 assert.NilError(c, err) 87 err = observer.Start() 88 assert.NilError(c, err) 89 defer observer.Stop() 90 91 go func() { 92 defer close(errChan) 93 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") 94 if expected := 137; exitCode != expected { 95 errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out) 96 } 97 }() 98 99 assert.NilError(c, waitRun("oomTrue")) 100 defer dockerCmdWithResult("kill", "oomTrue") 101 containerID := inspectField(c, "oomTrue", "Id") 102 103 testActions := map[string]chan bool{ 104 "oom": make(chan bool), 105 } 106 107 matcher := matchEventLine(containerID, "container", testActions) 108 processor := processEventMatch(testActions) 109 go observer.Match(matcher, processor) 110 111 select { 112 case <-time.After(20 * time.Second): 113 observer.CheckEventError(c, containerID, "oom", matcher) 114 case <-testActions["oom"]: 115 // ignore, done 116 case errRun := <-errChan: 117 if errRun != nil { 118 c.Fatalf("%v", errRun) 119 } else { 120 c.Fatalf("container should be still running but it's not") 121 } 122 } 123 124 status := inspectField(c, "oomTrue", "State.Status") 125 assert.Equal(c, strings.TrimSpace(status), "running", "container should be still running") 126 } 127 128 // #18453 129 func (s *DockerSuite) TestEventsContainerFilterByName(c *testing.T) { 130 testRequires(c, DaemonIsLinux) 131 cOut, _ := dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top") 132 c1 := strings.TrimSpace(cOut) 133 waitRun("foo") 134 cOut, _ = dockerCmd(c, "run", "--name=bar", "-d", "busybox", "top") 135 c2 := strings.TrimSpace(cOut) 136 waitRun("bar") 137 out, _ := dockerCmd(c, "events", "-f", "container=foo", "--since=0", "--until", daemonUnixTime(c)) 138 assert.Assert(c, strings.Contains(out, c1), out) 139 assert.Assert(c, !strings.Contains(out, c2), out) 140 } 141 142 // #18453 143 func (s *DockerSuite) TestEventsContainerFilterBeforeCreate(c *testing.T) { 144 testRequires(c, DaemonIsLinux) 145 buf := &bytes.Buffer{} 146 cmd := exec.Command(dockerBinary, "events", "-f", "container=foo", "--since=0") 147 cmd.Stdout = buf 148 assert.NilError(c, cmd.Start()) 149 defer cmd.Wait() 150 defer cmd.Process.Kill() 151 152 // Sleep for a second to make sure we are testing the case where events are listened before container starts. 153 time.Sleep(time.Second) 154 id, _ := dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top") 155 cID := strings.TrimSpace(id) 156 for i := 0; ; i++ { 157 out := buf.String() 158 if strings.Contains(out, cID) { 159 break 160 } 161 if i > 30 { 162 c.Fatalf("Missing event of container (foo, %v), got %q", cID, out) 163 } 164 time.Sleep(500 * time.Millisecond) 165 } 166 } 167 168 func (s *DockerSuite) TestVolumeEvents(c *testing.T) { 169 testRequires(c, DaemonIsLinux) 170 171 since := daemonUnixTime(c) 172 173 // Observe create/mount volume actions 174 dockerCmd(c, "volume", "create", "test-event-volume-local") 175 dockerCmd(c, "run", "--name", "test-volume-container", "--volume", "test-event-volume-local:/foo", "-d", "busybox", "true") 176 waitRun("test-volume-container") 177 178 // Observe unmount/destroy volume actions 179 dockerCmd(c, "rm", "-f", "test-volume-container") 180 dockerCmd(c, "volume", "rm", "test-event-volume-local") 181 182 until := daemonUnixTime(c) 183 out, _ := dockerCmd(c, "events", "--since", since, "--until", until) 184 events := strings.Split(strings.TrimSpace(out), "\n") 185 assert.Assert(c, len(events) > 4) 186 187 volumeEvents := eventActionsByIDAndType(c, events, "test-event-volume-local", "volume") 188 assert.Equal(c, len(volumeEvents), 5) 189 assert.Equal(c, volumeEvents[0], "create") 190 assert.Equal(c, volumeEvents[1], "create") 191 assert.Equal(c, volumeEvents[2], "mount") 192 assert.Equal(c, volumeEvents[3], "unmount") 193 assert.Equal(c, volumeEvents[4], "destroy") 194 } 195 196 func (s *DockerSuite) TestNetworkEvents(c *testing.T) { 197 testRequires(c, DaemonIsLinux) 198 199 since := daemonUnixTime(c) 200 201 // Observe create/connect network actions 202 dockerCmd(c, "network", "create", "test-event-network-local") 203 dockerCmd(c, "run", "--name", "test-network-container", "--net", "test-event-network-local", "-d", "busybox", "true") 204 waitRun("test-network-container") 205 206 // Observe disconnect/destroy network actions 207 dockerCmd(c, "rm", "-f", "test-network-container") 208 dockerCmd(c, "network", "rm", "test-event-network-local") 209 210 until := daemonUnixTime(c) 211 out, _ := dockerCmd(c, "events", "--since", since, "--until", until) 212 events := strings.Split(strings.TrimSpace(out), "\n") 213 assert.Assert(c, len(events) > 4) 214 215 netEvents := eventActionsByIDAndType(c, events, "test-event-network-local", "network") 216 assert.Equal(c, len(netEvents), 4) 217 assert.Equal(c, netEvents[0], "create") 218 assert.Equal(c, netEvents[1], "connect") 219 assert.Equal(c, netEvents[2], "disconnect") 220 assert.Equal(c, netEvents[3], "destroy") 221 } 222 223 func (s *DockerSuite) TestEventsContainerWithMultiNetwork(c *testing.T) { 224 testRequires(c, DaemonIsLinux) 225 226 // Observe create/connect network actions 227 dockerCmd(c, "network", "create", "test-event-network-local-1") 228 dockerCmd(c, "network", "create", "test-event-network-local-2") 229 dockerCmd(c, "run", "--name", "test-network-container", "--net", "test-event-network-local-1", "-td", "busybox", "sh") 230 waitRun("test-network-container") 231 dockerCmd(c, "network", "connect", "test-event-network-local-2", "test-network-container") 232 233 since := daemonUnixTime(c) 234 235 dockerCmd(c, "stop", "-t", "1", "test-network-container") 236 237 until := daemonUnixTime(c) 238 out, _ := dockerCmd(c, "events", "--since", since, "--until", until, "-f", "type=network") 239 netEvents := strings.Split(strings.TrimSpace(out), "\n") 240 241 // received two network disconnect events 242 assert.Equal(c, len(netEvents), 2) 243 assert.Assert(c, strings.Contains(netEvents[0], "disconnect")) 244 assert.Assert(c, strings.Contains(netEvents[1], "disconnect")) 245 246 // both networks appeared in the network event output 247 assert.Assert(c, strings.Contains(out, "test-event-network-local-1")) 248 assert.Assert(c, strings.Contains(out, "test-event-network-local-2")) 249 } 250 251 func (s *DockerSuite) TestEventsStreaming(c *testing.T) { 252 testRequires(c, DaemonIsLinux) 253 254 observer, err := newEventObserver(c) 255 assert.NilError(c, err) 256 err = observer.Start() 257 assert.NilError(c, err) 258 defer observer.Stop() 259 260 out, _ := dockerCmd(c, "run", "-d", "busybox:latest", "true") 261 containerID := strings.TrimSpace(out) 262 263 testActions := map[string]chan bool{ 264 "create": make(chan bool, 1), 265 "start": make(chan bool, 1), 266 "die": make(chan bool, 1), 267 "destroy": make(chan bool, 1), 268 } 269 270 matcher := matchEventLine(containerID, "container", testActions) 271 processor := processEventMatch(testActions) 272 go observer.Match(matcher, processor) 273 274 select { 275 case <-time.After(5 * time.Second): 276 observer.CheckEventError(c, containerID, "create", matcher) 277 case <-testActions["create"]: 278 // ignore, done 279 } 280 281 select { 282 case <-time.After(5 * time.Second): 283 observer.CheckEventError(c, containerID, "start", matcher) 284 case <-testActions["start"]: 285 // ignore, done 286 } 287 288 select { 289 case <-time.After(5 * time.Second): 290 observer.CheckEventError(c, containerID, "die", matcher) 291 case <-testActions["die"]: 292 // ignore, done 293 } 294 295 dockerCmd(c, "rm", containerID) 296 297 select { 298 case <-time.After(5 * time.Second): 299 observer.CheckEventError(c, containerID, "destroy", matcher) 300 case <-testActions["destroy"]: 301 // ignore, done 302 } 303 } 304 305 func (s *DockerSuite) TestEventsImageUntagDelete(c *testing.T) { 306 testRequires(c, DaemonIsLinux) 307 308 observer, err := newEventObserver(c) 309 assert.NilError(c, err) 310 err = observer.Start() 311 assert.NilError(c, err) 312 defer observer.Stop() 313 314 name := "testimageevents" 315 buildImageSuccessfully(c, name, build.WithDockerfile(`FROM scratch 316 MAINTAINER "docker"`)) 317 imageID := getIDByName(c, name) 318 assert.NilError(c, deleteImages(name)) 319 320 testActions := map[string]chan bool{ 321 "untag": make(chan bool, 1), 322 "delete": make(chan bool, 1), 323 } 324 325 matcher := matchEventLine(imageID, "image", testActions) 326 processor := processEventMatch(testActions) 327 go observer.Match(matcher, processor) 328 329 select { 330 case <-time.After(10 * time.Second): 331 observer.CheckEventError(c, imageID, "untag", matcher) 332 case <-testActions["untag"]: 333 // ignore, done 334 } 335 336 select { 337 case <-time.After(10 * time.Second): 338 observer.CheckEventError(c, imageID, "delete", matcher) 339 case <-testActions["delete"]: 340 // ignore, done 341 } 342 } 343 344 func (s *DockerSuite) TestEventsFilterVolumeAndNetworkType(c *testing.T) { 345 testRequires(c, DaemonIsLinux) 346 347 since := daemonUnixTime(c) 348 349 dockerCmd(c, "network", "create", "test-event-network-type") 350 dockerCmd(c, "volume", "create", "test-event-volume-type") 351 352 out, _ := dockerCmd(c, "events", "--filter", "type=volume", "--filter", "type=network", "--since", since, "--until", daemonUnixTime(c)) 353 events := strings.Split(strings.TrimSpace(out), "\n") 354 assert.Assert(c, len(events) >= 2, out) 355 356 networkActions := eventActionsByIDAndType(c, events, "test-event-network-type", "network") 357 volumeActions := eventActionsByIDAndType(c, events, "test-event-volume-type", "volume") 358 359 assert.Equal(c, volumeActions[0], "create") 360 assert.Equal(c, networkActions[0], "create") 361 } 362 363 func (s *DockerSuite) TestEventsFilterVolumeID(c *testing.T) { 364 testRequires(c, DaemonIsLinux) 365 366 since := daemonUnixTime(c) 367 368 dockerCmd(c, "volume", "create", "test-event-volume-id") 369 out, _ := dockerCmd(c, "events", "--filter", "volume=test-event-volume-id", "--since", since, "--until", daemonUnixTime(c)) 370 events := strings.Split(strings.TrimSpace(out), "\n") 371 assert.Equal(c, len(events), 1) 372 373 assert.Equal(c, len(events), 1) 374 assert.Assert(c, strings.Contains(events[0], "test-event-volume-id")) 375 assert.Assert(c, strings.Contains(events[0], "driver=local")) 376 } 377 378 func (s *DockerSuite) TestEventsFilterNetworkID(c *testing.T) { 379 testRequires(c, DaemonIsLinux) 380 381 since := daemonUnixTime(c) 382 383 dockerCmd(c, "network", "create", "test-event-network-local") 384 out, _ := dockerCmd(c, "events", "--filter", "network=test-event-network-local", "--since", since, "--until", daemonUnixTime(c)) 385 events := strings.Split(strings.TrimSpace(out), "\n") 386 assert.Equal(c, len(events), 1) 387 assert.Assert(c, strings.Contains(events[0], "test-event-network-local")) 388 assert.Assert(c, strings.Contains(events[0], "type=bridge")) 389 } 390 391 func (s *DockerDaemonSuite) TestDaemonEvents(c *testing.T) { 392 393 // daemon config file 394 configFilePath := "test.json" 395 defer os.Remove(configFilePath) 396 397 daemonConfig := `{"labels":["foo=bar"]}` 398 err := os.WriteFile(configFilePath, []byte(daemonConfig), 0644) 399 assert.NilError(c, err) 400 s.d.Start(c, "--config-file="+configFilePath) 401 402 info := s.d.Info(c) 403 404 daemonConfig = `{"max-concurrent-downloads":1,"labels":["bar=foo"], "shutdown-timeout": 10}` 405 err = os.WriteFile(configFilePath, []byte(daemonConfig), 0644) 406 assert.NilError(c, err) 407 408 assert.NilError(c, s.d.Signal(unix.SIGHUP)) 409 time.Sleep(3 * time.Second) 410 411 out, err := s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c)) 412 assert.NilError(c, err) 413 414 // only check for values known (daemon ID/name) or explicitly set above, 415 // otherwise just check for names being present. 416 expectedSubstrings := []string{ 417 " daemon reload " + info.ID + " ", 418 "(allow-nondistributable-artifacts=[", 419 " cluster-advertise=, ", 420 " cluster-store=, ", 421 " cluster-store-opts=", 422 " debug=true, ", 423 " default-ipc-mode=", 424 " default-runtime=", 425 " default-shm-size=", 426 " insecure-registries=[", 427 " labels=[\"bar=foo\"], ", 428 " live-restore=", 429 " max-concurrent-downloads=1, ", 430 " max-concurrent-uploads=5, ", 431 " name=" + info.Name, 432 " registry-mirrors=[", 433 " runtimes=", 434 " shutdown-timeout=10)", 435 } 436 437 for _, s := range expectedSubstrings { 438 assert.Check(c, is.Contains(out, s)) 439 } 440 } 441 442 func (s *DockerDaemonSuite) TestDaemonEventsWithFilters(c *testing.T) { 443 444 // daemon config file 445 configFilePath := "test.json" 446 defer os.Remove(configFilePath) 447 448 daemonConfig := `{"labels":["foo=bar"]}` 449 err := os.WriteFile(configFilePath, []byte(daemonConfig), 0644) 450 assert.NilError(c, err) 451 s.d.Start(c, "--config-file="+configFilePath) 452 453 info := s.d.Info(c) 454 455 assert.NilError(c, s.d.Signal(unix.SIGHUP)) 456 time.Sleep(3 * time.Second) 457 458 out, err := s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", info.ID)) 459 assert.NilError(c, err) 460 assert.Assert(c, strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID))) 461 462 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", info.ID)) 463 assert.NilError(c, err) 464 assert.Assert(c, strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID))) 465 466 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "daemon=foo") 467 assert.NilError(c, err) 468 assert.Assert(c, !strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID))) 469 470 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "type=daemon") 471 assert.NilError(c, err) 472 assert.Assert(c, strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID))) 473 474 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "type=container") 475 assert.NilError(c, err) 476 assert.Assert(c, !strings.Contains(out, fmt.Sprintf("daemon reload %s", info.ID))) 477 }