github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/integration-cli/docker_cli_events_unix_test.go (about) 1 // +build !windows 2 3 package main 4 5 import ( 6 "bufio" 7 "bytes" 8 "fmt" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 "strings" 13 "time" 14 "unicode" 15 16 "github.com/docker/docker/integration-cli/checker" 17 "github.com/docker/docker/integration-cli/cli/build" 18 "github.com/go-check/check" 19 "github.com/kr/pty" 20 "golang.org/x/sys/unix" 21 ) 22 23 // #5979 24 func (s *DockerSuite) TestEventsRedirectStdout(c *check.C) { 25 since := daemonUnixTime(c) 26 dockerCmd(c, "run", "busybox", "true") 27 28 file, err := ioutil.TempFile("", "") 29 c.Assert(err, checker.IsNil, check.Commentf("could not create temp file")) 30 defer os.Remove(file.Name()) 31 32 command := fmt.Sprintf("%s events --since=%s --until=%s > %s", dockerBinary, since, daemonUnixTime(c), file.Name()) 33 _, tty, err := pty.Open() 34 c.Assert(err, checker.IsNil, check.Commentf("Could not open pty")) 35 cmd := exec.Command("sh", "-c", command) 36 cmd.Stdin = tty 37 cmd.Stdout = tty 38 cmd.Stderr = tty 39 c.Assert(cmd.Run(), checker.IsNil, check.Commentf("run err for command %q", command)) 40 41 scanner := bufio.NewScanner(file) 42 for scanner.Scan() { 43 for _, ch := range scanner.Text() { 44 c.Assert(unicode.IsControl(ch), checker.False, check.Commentf("found control character %v", []byte(string(ch)))) 45 } 46 } 47 c.Assert(scanner.Err(), checker.IsNil, check.Commentf("Scan err for command %q", command)) 48 49 } 50 51 func (s *DockerSuite) TestEventsOOMDisableFalse(c *check.C) { 52 testRequires(c, DaemonIsLinux, oomControl, memoryLimitSupport, swapMemorySupport) 53 54 errChan := make(chan error) 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 c.Assert(err, checker.IsNil) 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 c.Assert(nEvents, checker.GreaterOrEqualThan, 5) //Missing expected event 74 c.Assert(parseEventAction(c, events[nEvents-5]), checker.Equals, "create") 75 c.Assert(parseEventAction(c, events[nEvents-4]), checker.Equals, "attach") 76 c.Assert(parseEventAction(c, events[nEvents-3]), checker.Equals, "start") 77 c.Assert(parseEventAction(c, events[nEvents-2]), checker.Equals, "oom") 78 c.Assert(parseEventAction(c, events[nEvents-1]), checker.Equals, "die") 79 } 80 81 func (s *DockerSuite) TestEventsOOMDisableTrue(c *check.C) { 82 testRequires(c, DaemonIsLinux, oomControl, memoryLimitSupport, NotArm, swapMemorySupport) 83 84 errChan := make(chan error) 85 observer, err := newEventObserver(c) 86 c.Assert(err, checker.IsNil) 87 err = observer.Start() 88 c.Assert(err, checker.IsNil) 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 c.Assert(waitRun("oomTrue"), checker.IsNil) 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 c.Assert(strings.TrimSpace(status), checker.Equals, "running", check.Commentf("container should be still running")) 126 } 127 128 // #18453 129 func (s *DockerSuite) TestEventsContainerFilterByName(c *check.C) { 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 c.Assert(out, checker.Contains, c1, check.Commentf(out)) 139 c.Assert(out, checker.Not(checker.Contains), c2, check.Commentf(out)) 140 } 141 142 // #18453 143 func (s *DockerSuite) TestEventsContainerFilterBeforeCreate(c *check.C) { 144 testRequires(c, DaemonIsLinux) 145 buf := &bytes.Buffer{} 146 cmd := exec.Command(dockerBinary, "events", "-f", "container=foo", "--since=0") 147 cmd.Stdout = buf 148 c.Assert(cmd.Start(), check.IsNil) 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 *check.C) { 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 c.Assert(len(events), checker.GreaterThan, 4) 186 187 volumeEvents := eventActionsByIDAndType(c, events, "test-event-volume-local", "volume") 188 c.Assert(volumeEvents, checker.HasLen, 4) 189 c.Assert(volumeEvents[0], checker.Equals, "create") 190 c.Assert(volumeEvents[1], checker.Equals, "mount") 191 c.Assert(volumeEvents[2], checker.Equals, "unmount") 192 c.Assert(volumeEvents[3], checker.Equals, "destroy") 193 } 194 195 func (s *DockerSuite) TestNetworkEvents(c *check.C) { 196 testRequires(c, DaemonIsLinux) 197 198 since := daemonUnixTime(c) 199 200 // Observe create/connect network actions 201 dockerCmd(c, "network", "create", "test-event-network-local") 202 dockerCmd(c, "run", "--name", "test-network-container", "--net", "test-event-network-local", "-d", "busybox", "true") 203 waitRun("test-network-container") 204 205 // Observe disconnect/destroy network actions 206 dockerCmd(c, "rm", "-f", "test-network-container") 207 dockerCmd(c, "network", "rm", "test-event-network-local") 208 209 until := daemonUnixTime(c) 210 out, _ := dockerCmd(c, "events", "--since", since, "--until", until) 211 events := strings.Split(strings.TrimSpace(out), "\n") 212 c.Assert(len(events), checker.GreaterThan, 4) 213 214 netEvents := eventActionsByIDAndType(c, events, "test-event-network-local", "network") 215 c.Assert(netEvents, checker.HasLen, 4) 216 c.Assert(netEvents[0], checker.Equals, "create") 217 c.Assert(netEvents[1], checker.Equals, "connect") 218 c.Assert(netEvents[2], checker.Equals, "disconnect") 219 c.Assert(netEvents[3], checker.Equals, "destroy") 220 } 221 222 func (s *DockerSuite) TestEventsContainerWithMultiNetwork(c *check.C) { 223 testRequires(c, DaemonIsLinux) 224 225 // Observe create/connect network actions 226 dockerCmd(c, "network", "create", "test-event-network-local-1") 227 dockerCmd(c, "network", "create", "test-event-network-local-2") 228 dockerCmd(c, "run", "--name", "test-network-container", "--net", "test-event-network-local-1", "-td", "busybox", "sh") 229 waitRun("test-network-container") 230 dockerCmd(c, "network", "connect", "test-event-network-local-2", "test-network-container") 231 232 since := daemonUnixTime(c) 233 234 dockerCmd(c, "stop", "-t", "1", "test-network-container") 235 236 until := daemonUnixTime(c) 237 out, _ := dockerCmd(c, "events", "--since", since, "--until", until, "-f", "type=network") 238 netEvents := strings.Split(strings.TrimSpace(out), "\n") 239 240 // received two network disconnect events 241 c.Assert(len(netEvents), checker.Equals, 2) 242 c.Assert(netEvents[0], checker.Contains, "disconnect") 243 c.Assert(netEvents[1], checker.Contains, "disconnect") 244 245 //both networks appeared in the network event output 246 c.Assert(out, checker.Contains, "test-event-network-local-1") 247 c.Assert(out, checker.Contains, "test-event-network-local-2") 248 } 249 250 func (s *DockerSuite) TestEventsStreaming(c *check.C) { 251 testRequires(c, DaemonIsLinux) 252 253 observer, err := newEventObserver(c) 254 c.Assert(err, checker.IsNil) 255 err = observer.Start() 256 c.Assert(err, checker.IsNil) 257 defer observer.Stop() 258 259 out, _ := dockerCmd(c, "run", "-d", "busybox:latest", "true") 260 containerID := strings.TrimSpace(out) 261 262 testActions := map[string]chan bool{ 263 "create": make(chan bool, 1), 264 "start": make(chan bool, 1), 265 "die": make(chan bool, 1), 266 "destroy": make(chan bool, 1), 267 } 268 269 matcher := matchEventLine(containerID, "container", testActions) 270 processor := processEventMatch(testActions) 271 go observer.Match(matcher, processor) 272 273 select { 274 case <-time.After(5 * time.Second): 275 observer.CheckEventError(c, containerID, "create", matcher) 276 case <-testActions["create"]: 277 // ignore, done 278 } 279 280 select { 281 case <-time.After(5 * time.Second): 282 observer.CheckEventError(c, containerID, "start", matcher) 283 case <-testActions["start"]: 284 // ignore, done 285 } 286 287 select { 288 case <-time.After(5 * time.Second): 289 observer.CheckEventError(c, containerID, "die", matcher) 290 case <-testActions["die"]: 291 // ignore, done 292 } 293 294 dockerCmd(c, "rm", containerID) 295 296 select { 297 case <-time.After(5 * time.Second): 298 observer.CheckEventError(c, containerID, "destroy", matcher) 299 case <-testActions["destroy"]: 300 // ignore, done 301 } 302 } 303 304 func (s *DockerSuite) TestEventsImageUntagDelete(c *check.C) { 305 testRequires(c, DaemonIsLinux) 306 307 observer, err := newEventObserver(c) 308 c.Assert(err, checker.IsNil) 309 err = observer.Start() 310 c.Assert(err, checker.IsNil) 311 defer observer.Stop() 312 313 name := "testimageevents" 314 buildImageSuccessfully(c, name, build.WithDockerfile(`FROM scratch 315 MAINTAINER "docker"`)) 316 imageID := getIDByName(c, name) 317 c.Assert(deleteImages(name), checker.IsNil) 318 319 testActions := map[string]chan bool{ 320 "untag": make(chan bool, 1), 321 "delete": make(chan bool, 1), 322 } 323 324 matcher := matchEventLine(imageID, "image", testActions) 325 processor := processEventMatch(testActions) 326 go observer.Match(matcher, processor) 327 328 select { 329 case <-time.After(10 * time.Second): 330 observer.CheckEventError(c, imageID, "untag", matcher) 331 case <-testActions["untag"]: 332 // ignore, done 333 } 334 335 select { 336 case <-time.After(10 * time.Second): 337 observer.CheckEventError(c, imageID, "delete", matcher) 338 case <-testActions["delete"]: 339 // ignore, done 340 } 341 } 342 343 func (s *DockerSuite) TestEventsFilterVolumeAndNetworkType(c *check.C) { 344 testRequires(c, DaemonIsLinux) 345 346 since := daemonUnixTime(c) 347 348 dockerCmd(c, "network", "create", "test-event-network-type") 349 dockerCmd(c, "volume", "create", "test-event-volume-type") 350 351 out, _ := dockerCmd(c, "events", "--filter", "type=volume", "--filter", "type=network", "--since", since, "--until", daemonUnixTime(c)) 352 events := strings.Split(strings.TrimSpace(out), "\n") 353 c.Assert(len(events), checker.GreaterOrEqualThan, 2, check.Commentf(out)) 354 355 networkActions := eventActionsByIDAndType(c, events, "test-event-network-type", "network") 356 volumeActions := eventActionsByIDAndType(c, events, "test-event-volume-type", "volume") 357 358 c.Assert(volumeActions[0], checker.Equals, "create") 359 c.Assert(networkActions[0], checker.Equals, "create") 360 } 361 362 func (s *DockerSuite) TestEventsFilterVolumeID(c *check.C) { 363 testRequires(c, DaemonIsLinux) 364 365 since := daemonUnixTime(c) 366 367 dockerCmd(c, "volume", "create", "test-event-volume-id") 368 out, _ := dockerCmd(c, "events", "--filter", "volume=test-event-volume-id", "--since", since, "--until", daemonUnixTime(c)) 369 events := strings.Split(strings.TrimSpace(out), "\n") 370 c.Assert(events, checker.HasLen, 1) 371 372 c.Assert(events[0], checker.Contains, "test-event-volume-id") 373 c.Assert(events[0], checker.Contains, "driver=local") 374 } 375 376 func (s *DockerSuite) TestEventsFilterNetworkID(c *check.C) { 377 testRequires(c, DaemonIsLinux) 378 379 since := daemonUnixTime(c) 380 381 dockerCmd(c, "network", "create", "test-event-network-local") 382 out, _ := dockerCmd(c, "events", "--filter", "network=test-event-network-local", "--since", since, "--until", daemonUnixTime(c)) 383 events := strings.Split(strings.TrimSpace(out), "\n") 384 c.Assert(events, checker.HasLen, 1) 385 386 c.Assert(events[0], checker.Contains, "test-event-network-local") 387 c.Assert(events[0], checker.Contains, "type=bridge") 388 } 389 390 func (s *DockerDaemonSuite) TestDaemonEvents(c *check.C) { 391 testRequires(c, SameHostDaemon, DaemonIsLinux) 392 393 // daemon config file 394 configFilePath := "test.json" 395 configFile, err := os.Create(configFilePath) 396 c.Assert(err, checker.IsNil) 397 defer os.Remove(configFilePath) 398 399 daemonConfig := `{"labels":["foo=bar"]}` 400 fmt.Fprintf(configFile, "%s", daemonConfig) 401 configFile.Close() 402 s.d.Start(c, fmt.Sprintf("--config-file=%s", configFilePath)) 403 404 // Get daemon ID 405 out, err := s.d.Cmd("info") 406 c.Assert(err, checker.IsNil) 407 daemonID := "" 408 daemonName := "" 409 for _, line := range strings.Split(out, "\n") { 410 if strings.HasPrefix(line, "ID: ") { 411 daemonID = strings.TrimPrefix(line, "ID: ") 412 } else if strings.HasPrefix(line, "Name: ") { 413 daemonName = strings.TrimPrefix(line, "Name: ") 414 } 415 } 416 c.Assert(daemonID, checker.Not(checker.Equals), "") 417 418 configFile, err = os.Create(configFilePath) 419 c.Assert(err, checker.IsNil) 420 daemonConfig = `{"max-concurrent-downloads":1,"labels":["bar=foo"], "shutdown-timeout": 10}` 421 fmt.Fprintf(configFile, "%s", daemonConfig) 422 configFile.Close() 423 424 c.Assert(s.d.Signal(unix.SIGHUP), checker.IsNil) 425 426 time.Sleep(3 * time.Second) 427 428 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c)) 429 c.Assert(err, checker.IsNil) 430 431 // only check for values known (daemon ID/name) or explicitly set above, 432 // otherwise just check for names being present. 433 expectedSubstrings := []string{ 434 " daemon reload " + daemonID + " ", 435 "(allow-nondistributable-artifacts=[", 436 " cluster-advertise=, ", 437 " cluster-store=, ", 438 " cluster-store-opts={", 439 " debug=true, ", 440 " default-ipc-mode=", 441 " default-runtime=", 442 " default-shm-size=", 443 " insecure-registries=[", 444 " labels=[\"bar=foo\"], ", 445 " live-restore=", 446 " max-concurrent-downloads=1, ", 447 " max-concurrent-uploads=5, ", 448 " name=" + daemonName, 449 " registry-mirrors=[", 450 " runtimes=", 451 " shutdown-timeout=10)", 452 } 453 454 for _, s := range expectedSubstrings { 455 c.Assert(out, checker.Contains, s) 456 } 457 } 458 459 func (s *DockerDaemonSuite) TestDaemonEventsWithFilters(c *check.C) { 460 testRequires(c, SameHostDaemon, DaemonIsLinux) 461 462 // daemon config file 463 configFilePath := "test.json" 464 configFile, err := os.Create(configFilePath) 465 c.Assert(err, checker.IsNil) 466 defer os.Remove(configFilePath) 467 468 daemonConfig := `{"labels":["foo=bar"]}` 469 fmt.Fprintf(configFile, "%s", daemonConfig) 470 configFile.Close() 471 s.d.Start(c, fmt.Sprintf("--config-file=%s", configFilePath)) 472 473 // Get daemon ID 474 out, err := s.d.Cmd("info") 475 c.Assert(err, checker.IsNil) 476 daemonID := "" 477 daemonName := "" 478 for _, line := range strings.Split(out, "\n") { 479 if strings.HasPrefix(line, "ID: ") { 480 daemonID = strings.TrimPrefix(line, "ID: ") 481 } else if strings.HasPrefix(line, "Name: ") { 482 daemonName = strings.TrimPrefix(line, "Name: ") 483 } 484 } 485 c.Assert(daemonID, checker.Not(checker.Equals), "") 486 487 c.Assert(s.d.Signal(unix.SIGHUP), checker.IsNil) 488 489 time.Sleep(3 * time.Second) 490 491 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", daemonID)) 492 c.Assert(err, checker.IsNil) 493 c.Assert(out, checker.Contains, fmt.Sprintf("daemon reload %s", daemonID)) 494 495 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("daemon=%s", daemonName)) 496 c.Assert(err, checker.IsNil) 497 c.Assert(out, checker.Contains, fmt.Sprintf("daemon reload %s", daemonID)) 498 499 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "daemon=foo") 500 c.Assert(err, checker.IsNil) 501 c.Assert(out, checker.Not(checker.Contains), fmt.Sprintf("daemon reload %s", daemonID)) 502 503 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "type=daemon") 504 c.Assert(err, checker.IsNil) 505 c.Assert(out, checker.Contains, fmt.Sprintf("daemon reload %s", daemonID)) 506 507 out, err = s.d.Cmd("events", "--since=0", "--until", daemonUnixTime(c), "--filter", "type=container") 508 c.Assert(err, checker.IsNil) 509 c.Assert(out, checker.Not(checker.Contains), fmt.Sprintf("daemon reload %s", daemonID)) 510 }