github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/integration-cli/docker_cli_events_test.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "os" 9 "os/exec" 10 "strings" 11 "sync" 12 "time" 13 14 "github.com/docker/docker/daemon/events/testutils" 15 "github.com/docker/docker/pkg/integration/checker" 16 "github.com/go-check/check" 17 ) 18 19 func (s *DockerSuite) TestEventsTimestampFormats(c *check.C) { 20 name := "events-time-format-test" 21 22 // Start stopwatch, generate an event 23 start := daemonTime(c) 24 time.Sleep(1100 * time.Millisecond) // so that first event occur in different second from since (just for the case) 25 dockerCmd(c, "run", "--rm", "--name", name, "busybox", "true") 26 time.Sleep(1100 * time.Millisecond) // so that until > since 27 end := daemonTime(c) 28 29 // List of available time formats to --since 30 unixTs := func(t time.Time) string { return fmt.Sprintf("%v", t.Unix()) } 31 rfc3339 := func(t time.Time) string { return t.Format(time.RFC3339) } 32 duration := func(t time.Time) string { return time.Now().Sub(t).String() } 33 34 // --since=$start must contain only the 'untag' event 35 for _, f := range []func(time.Time) string{unixTs, rfc3339, duration} { 36 since, until := f(start), f(end) 37 out, _ := dockerCmd(c, "events", "--since="+since, "--until="+until) 38 events := strings.Split(out, "\n") 39 events = events[:len(events)-1] 40 41 nEvents := len(events) 42 c.Assert(nEvents, checker.GreaterOrEqualThan, 5) //Missing expected event 43 containerEvents := eventActionsByIDAndType(c, events, name, "container") 44 c.Assert(containerEvents, checker.HasLen, 5, check.Commentf("events: %v", events)) 45 46 c.Assert(containerEvents[0], checker.Equals, "create", check.Commentf(out)) 47 c.Assert(containerEvents[1], checker.Equals, "attach", check.Commentf(out)) 48 c.Assert(containerEvents[2], checker.Equals, "start", check.Commentf(out)) 49 c.Assert(containerEvents[3], checker.Equals, "die", check.Commentf(out)) 50 c.Assert(containerEvents[4], checker.Equals, "destroy", check.Commentf(out)) 51 } 52 } 53 54 func (s *DockerSuite) TestEventsUntag(c *check.C) { 55 image := "busybox" 56 dockerCmd(c, "tag", image, "utest:tag1") 57 dockerCmd(c, "tag", image, "utest:tag2") 58 dockerCmd(c, "rmi", "utest:tag1") 59 dockerCmd(c, "rmi", "utest:tag2") 60 eventsCmd := exec.Command(dockerBinary, "events", "--since=1") 61 out, exitCode, _, err := runCommandWithOutputForDuration(eventsCmd, time.Duration(time.Millisecond*2500)) 62 c.Assert(err, checker.IsNil) 63 c.Assert(exitCode, checker.Equals, 0, check.Commentf("Failed to get events")) 64 events := strings.Split(out, "\n") 65 nEvents := len(events) 66 // The last element after the split above will be an empty string, so we 67 // get the two elements before the last, which are the untags we're 68 // looking for. 69 for _, v := range events[nEvents-3 : nEvents-1] { 70 c.Assert(v, checker.Contains, "untag", check.Commentf("event should be untag")) 71 } 72 } 73 74 func (s *DockerSuite) TestEventsContainerFailStartDie(c *check.C) { 75 _, _, err := dockerCmdWithError("run", "--name", "testeventdie", "busybox", "blerg") 76 c.Assert(err, checker.NotNil, check.Commentf("Container run with command blerg should have failed, but it did not")) 77 78 out, _ := dockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c)) 79 events := strings.Split(strings.TrimSpace(out), "\n") 80 81 nEvents := len(events) 82 c.Assert(nEvents, checker.GreaterOrEqualThan, 1) //Missing expected event 83 84 actions := eventActionsByIDAndType(c, events, "testeventdie", "container") 85 86 var startEvent bool 87 var dieEvent bool 88 for _, a := range actions { 89 switch a { 90 case "start": 91 startEvent = true 92 case "die": 93 dieEvent = true 94 } 95 } 96 97 // Windows platform is different from Linux, it will start container whatever 98 // so Windows can get start/die event but Linux can't 99 if daemonPlatform == "windows" { 100 c.Assert(startEvent, checker.True, check.Commentf("Start event not found: %v\n%v", actions, events)) 101 c.Assert(dieEvent, checker.True, check.Commentf("Die event not found: %v\n%v", actions, events)) 102 } else { 103 c.Assert(startEvent, checker.False, check.Commentf("Start event not expected: %v\n%v", actions, events)) 104 c.Assert(dieEvent, checker.False, check.Commentf("Die event not expected: %v\n%v", actions, events)) 105 } 106 } 107 108 func (s *DockerSuite) TestEventsLimit(c *check.C) { 109 var waitGroup sync.WaitGroup 110 errChan := make(chan error, 17) 111 112 args := []string{"run", "--rm", "busybox", "true"} 113 for i := 0; i < 17; i++ { 114 waitGroup.Add(1) 115 go func() { 116 defer waitGroup.Done() 117 out, err := exec.Command(dockerBinary, args...).CombinedOutput() 118 if err != nil { 119 err = fmt.Errorf("%v: %s", err, string(out)) 120 } 121 errChan <- err 122 }() 123 } 124 125 waitGroup.Wait() 126 close(errChan) 127 128 for err := range errChan { 129 c.Assert(err, checker.IsNil, check.Commentf("%q failed with error", strings.Join(args, " "))) 130 } 131 132 out, _ := dockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c)) 133 events := strings.Split(out, "\n") 134 nEvents := len(events) - 1 135 c.Assert(nEvents, checker.Equals, 64, check.Commentf("events should be limited to 64, but received %d", nEvents)) 136 } 137 138 func (s *DockerSuite) TestEventsContainerEvents(c *check.C) { 139 dockerCmd(c, "run", "--rm", "--name", "container-events-test", "busybox", "true") 140 141 out, _ := dockerCmd(c, "events", "--until", daemonUnixTime(c)) 142 events := strings.Split(out, "\n") 143 events = events[:len(events)-1] 144 145 nEvents := len(events) 146 c.Assert(nEvents, checker.GreaterOrEqualThan, 5) //Missing expected event 147 containerEvents := eventActionsByIDAndType(c, events, "container-events-test", "container") 148 c.Assert(containerEvents, checker.HasLen, 5, check.Commentf("events: %v", events)) 149 150 c.Assert(containerEvents[0], checker.Equals, "create", check.Commentf(out)) 151 c.Assert(containerEvents[1], checker.Equals, "attach", check.Commentf(out)) 152 c.Assert(containerEvents[2], checker.Equals, "start", check.Commentf(out)) 153 c.Assert(containerEvents[3], checker.Equals, "die", check.Commentf(out)) 154 c.Assert(containerEvents[4], checker.Equals, "destroy", check.Commentf(out)) 155 } 156 157 func (s *DockerSuite) TestEventsContainerEventsAttrSort(c *check.C) { 158 since := daemonUnixTime(c) 159 dockerCmd(c, "run", "--rm", "--name", "container-events-test", "busybox", "true") 160 161 out, _ := dockerCmd(c, "events", "--filter", "container=container-events-test", "--since", since, "--until", daemonUnixTime(c)) 162 events := strings.Split(out, "\n") 163 164 nEvents := len(events) 165 c.Assert(nEvents, checker.GreaterOrEqualThan, 3) //Missing expected event 166 matchedEvents := 0 167 for _, event := range events { 168 matches := eventstestutils.ScanMap(event) 169 if matches["eventType"] == "container" && matches["action"] == "create" { 170 matchedEvents++ 171 c.Assert(out, checker.Contains, "(image=busybox, name=container-events-test)", check.Commentf("Event attributes not sorted")) 172 } else if matches["eventType"] == "container" && matches["action"] == "start" { 173 matchedEvents++ 174 c.Assert(out, checker.Contains, "(image=busybox, name=container-events-test)", check.Commentf("Event attributes not sorted")) 175 } 176 } 177 c.Assert(matchedEvents, checker.Equals, 2, check.Commentf("missing events for container container-events-test:\n%s", out)) 178 } 179 180 func (s *DockerSuite) TestEventsContainerEventsSinceUnixEpoch(c *check.C) { 181 dockerCmd(c, "run", "--rm", "--name", "since-epoch-test", "busybox", "true") 182 timeBeginning := time.Unix(0, 0).Format(time.RFC3339Nano) 183 timeBeginning = strings.Replace(timeBeginning, "Z", ".000000000Z", -1) 184 out, _ := dockerCmd(c, "events", "--since", timeBeginning, "--until", daemonUnixTime(c)) 185 events := strings.Split(out, "\n") 186 events = events[:len(events)-1] 187 188 nEvents := len(events) 189 c.Assert(nEvents, checker.GreaterOrEqualThan, 5) //Missing expected event 190 containerEvents := eventActionsByIDAndType(c, events, "since-epoch-test", "container") 191 c.Assert(containerEvents, checker.HasLen, 5, check.Commentf("events: %v", events)) 192 193 c.Assert(containerEvents[0], checker.Equals, "create", check.Commentf(out)) 194 c.Assert(containerEvents[1], checker.Equals, "attach", check.Commentf(out)) 195 c.Assert(containerEvents[2], checker.Equals, "start", check.Commentf(out)) 196 c.Assert(containerEvents[3], checker.Equals, "die", check.Commentf(out)) 197 c.Assert(containerEvents[4], checker.Equals, "destroy", check.Commentf(out)) 198 } 199 200 func (s *DockerSuite) TestEventsImageTag(c *check.C) { 201 time.Sleep(1 * time.Second) // because API has seconds granularity 202 since := daemonUnixTime(c) 203 image := "testimageevents:tag" 204 dockerCmd(c, "tag", "busybox", image) 205 206 out, _ := dockerCmd(c, "events", 207 "--since", since, "--until", daemonUnixTime(c)) 208 209 events := strings.Split(strings.TrimSpace(out), "\n") 210 c.Assert(events, checker.HasLen, 1, check.Commentf("was expecting 1 event. out=%s", out)) 211 event := strings.TrimSpace(events[0]) 212 213 matches := eventstestutils.ScanMap(event) 214 c.Assert(matchEventID(matches, image), checker.True, check.Commentf("matches: %v\nout:\n%s", matches, out)) 215 c.Assert(matches["action"], checker.Equals, "tag") 216 } 217 218 func (s *DockerSuite) TestEventsImagePull(c *check.C) { 219 // TODO Windows: Enable this test once pull and reliable image names are available 220 testRequires(c, DaemonIsLinux) 221 since := daemonUnixTime(c) 222 testRequires(c, Network) 223 224 dockerCmd(c, "pull", "hello-world") 225 226 out, _ := dockerCmd(c, "events", 227 "--since", since, "--until", daemonUnixTime(c)) 228 229 events := strings.Split(strings.TrimSpace(out), "\n") 230 event := strings.TrimSpace(events[len(events)-1]) 231 matches := eventstestutils.ScanMap(event) 232 c.Assert(matches["id"], checker.Equals, "hello-world:latest") 233 c.Assert(matches["action"], checker.Equals, "pull") 234 235 } 236 237 func (s *DockerSuite) TestEventsImageImport(c *check.C) { 238 // TODO Windows CI. This should be portable once export/import are 239 // more reliable (@swernli) 240 testRequires(c, DaemonIsLinux) 241 242 out, _ := dockerCmd(c, "run", "-d", "busybox", "true") 243 cleanedContainerID := strings.TrimSpace(out) 244 245 since := daemonUnixTime(c) 246 out, _, err := runCommandPipelineWithOutput( 247 exec.Command(dockerBinary, "export", cleanedContainerID), 248 exec.Command(dockerBinary, "import", "-"), 249 ) 250 c.Assert(err, checker.IsNil, check.Commentf("import failed with output: %q", out)) 251 imageRef := strings.TrimSpace(out) 252 253 out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=import") 254 events := strings.Split(strings.TrimSpace(out), "\n") 255 c.Assert(events, checker.HasLen, 1) 256 matches := eventstestutils.ScanMap(events[0]) 257 c.Assert(matches["id"], checker.Equals, imageRef, check.Commentf("matches: %v\nout:\n%s\n", matches, out)) 258 c.Assert(matches["action"], checker.Equals, "import", check.Commentf("matches: %v\nout:\n%s\n", matches, out)) 259 } 260 261 func (s *DockerSuite) TestEventsImageLoad(c *check.C) { 262 testRequires(c, DaemonIsLinux) 263 myImageName := "footest:v1" 264 dockerCmd(c, "tag", "busybox", myImageName) 265 since := daemonUnixTime(c) 266 267 out, _ := dockerCmd(c, "images", "-q", "--no-trunc", myImageName) 268 longImageID := strings.TrimSpace(out) 269 c.Assert(longImageID, checker.Not(check.Equals), "", check.Commentf("Id should not be empty")) 270 271 dockerCmd(c, "save", "-o", "saveimg.tar", myImageName) 272 dockerCmd(c, "rmi", myImageName) 273 out, _ = dockerCmd(c, "images", "-q", myImageName) 274 noImageID := strings.TrimSpace(out) 275 c.Assert(noImageID, checker.Equals, "", check.Commentf("Should not have any image")) 276 dockerCmd(c, "load", "-i", "saveimg.tar") 277 278 cmd := exec.Command("rm", "-rf", "saveimg.tar") 279 runCommand(cmd) 280 281 out, _ = dockerCmd(c, "images", "-q", "--no-trunc", myImageName) 282 imageID := strings.TrimSpace(out) 283 c.Assert(imageID, checker.Equals, longImageID, check.Commentf("Should have same image id as before")) 284 285 out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=load") 286 events := strings.Split(strings.TrimSpace(out), "\n") 287 c.Assert(events, checker.HasLen, 1) 288 matches := eventstestutils.ScanMap(events[0]) 289 c.Assert(matches["id"], checker.Equals, imageID, check.Commentf("matches: %v\nout:\n%s\n", matches, out)) 290 c.Assert(matches["action"], checker.Equals, "load", check.Commentf("matches: %v\nout:\n%s\n", matches, out)) 291 292 out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=save") 293 events = strings.Split(strings.TrimSpace(out), "\n") 294 c.Assert(events, checker.HasLen, 1) 295 matches = eventstestutils.ScanMap(events[0]) 296 c.Assert(matches["id"], checker.Equals, imageID, check.Commentf("matches: %v\nout:\n%s\n", matches, out)) 297 c.Assert(matches["action"], checker.Equals, "save", check.Commentf("matches: %v\nout:\n%s\n", matches, out)) 298 } 299 300 func (s *DockerSuite) TestEventsFilters(c *check.C) { 301 since := daemonUnixTime(c) 302 dockerCmd(c, "run", "--rm", "busybox", "true") 303 dockerCmd(c, "run", "--rm", "busybox", "true") 304 out, _ := dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=die") 305 parseEvents(c, out, "die") 306 307 out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=die", "--filter", "event=start") 308 parseEvents(c, out, "die|start") 309 310 // make sure we at least got 2 start events 311 count := strings.Count(out, "start") 312 c.Assert(strings.Count(out, "start"), checker.GreaterOrEqualThan, 2, check.Commentf("should have had 2 start events but had %d, out: %s", count, out)) 313 314 } 315 316 func (s *DockerSuite) TestEventsFilterImageName(c *check.C) { 317 since := daemonUnixTime(c) 318 319 out, _ := dockerCmd(c, "run", "--name", "container_1", "-d", "busybox:latest", "true") 320 container1 := strings.TrimSpace(out) 321 322 out, _ = dockerCmd(c, "run", "--name", "container_2", "-d", "busybox", "true") 323 container2 := strings.TrimSpace(out) 324 325 name := "busybox" 326 out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("image=%s", name)) 327 events := strings.Split(out, "\n") 328 events = events[:len(events)-1] 329 c.Assert(events, checker.Not(checker.HasLen), 0) //Expected events but found none for the image busybox:latest 330 count1 := 0 331 count2 := 0 332 333 for _, e := range events { 334 if strings.Contains(e, container1) { 335 count1++ 336 } else if strings.Contains(e, container2) { 337 count2++ 338 } 339 } 340 c.Assert(count1, checker.Not(checker.Equals), 0, check.Commentf("Expected event from container but got %d from %s", count1, container1)) 341 c.Assert(count2, checker.Not(checker.Equals), 0, check.Commentf("Expected event from container but got %d from %s", count2, container2)) 342 343 } 344 345 func (s *DockerSuite) TestEventsFilterLabels(c *check.C) { 346 since := daemonUnixTime(c) 347 label := "io.docker.testing=foo" 348 349 out, _ := dockerCmd(c, "run", "-d", "-l", label, "busybox:latest", "true") 350 container1 := strings.TrimSpace(out) 351 352 out, _ = dockerCmd(c, "run", "-d", "busybox", "true") 353 container2 := strings.TrimSpace(out) 354 355 out, _ = dockerCmd( 356 c, 357 "events", 358 "--since", since, 359 "--until", daemonUnixTime(c), 360 "--filter", fmt.Sprintf("label=%s", label)) 361 362 events := strings.Split(strings.TrimSpace(out), "\n") 363 c.Assert(len(events), checker.Equals, 3) 364 365 for _, e := range events { 366 c.Assert(e, checker.Contains, container1) 367 c.Assert(e, checker.Not(checker.Contains), container2) 368 } 369 } 370 371 func (s *DockerSuite) TestEventsFilterImageLabels(c *check.C) { 372 since := daemonUnixTime(c) 373 name := "labelfiltertest" 374 label := "io.docker.testing=image" 375 376 // Build a test image. 377 _, err := buildImage(name, fmt.Sprintf(` 378 FROM busybox:latest 379 LABEL %s`, label), true) 380 c.Assert(err, checker.IsNil, check.Commentf("Couldn't create image")) 381 382 dockerCmd(c, "tag", name, "labelfiltertest:tag1") 383 dockerCmd(c, "tag", name, "labelfiltertest:tag2") 384 dockerCmd(c, "tag", "busybox:latest", "labelfiltertest:tag3") 385 386 out, _ := dockerCmd( 387 c, 388 "events", 389 "--since", since, 390 "--until", daemonUnixTime(c), 391 "--filter", fmt.Sprintf("label=%s", label), 392 "--filter", "type=image") 393 394 events := strings.Split(strings.TrimSpace(out), "\n") 395 396 // 2 events from the "docker tag" command, another one is from "docker build" 397 c.Assert(events, checker.HasLen, 3, check.Commentf("Events == %s", events)) 398 for _, e := range events { 399 c.Assert(e, checker.Contains, "labelfiltertest") 400 } 401 } 402 403 func (s *DockerSuite) TestEventsFilterContainer(c *check.C) { 404 since := daemonUnixTime(c) 405 nameID := make(map[string]string) 406 407 for _, name := range []string{"container_1", "container_2"} { 408 dockerCmd(c, "run", "--name", name, "busybox", "true") 409 id := inspectField(c, name, "Id") 410 nameID[name] = id 411 } 412 413 until := daemonUnixTime(c) 414 415 checkEvents := func(id string, events []string) error { 416 if len(events) != 4 { // create, attach, start, die 417 return fmt.Errorf("expected 4 events, got %v", events) 418 } 419 for _, event := range events { 420 matches := eventstestutils.ScanMap(event) 421 if !matchEventID(matches, id) { 422 return fmt.Errorf("expected event for container id %s: %s - parsed container id: %s", id, event, matches["id"]) 423 } 424 } 425 return nil 426 } 427 428 for name, ID := range nameID { 429 // filter by names 430 out, _ := dockerCmd(c, "events", "--since", since, "--until", until, "--filter", "container="+name) 431 events := strings.Split(strings.TrimSuffix(out, "\n"), "\n") 432 c.Assert(checkEvents(ID, events), checker.IsNil) 433 434 // filter by ID's 435 out, _ = dockerCmd(c, "events", "--since", since, "--until", until, "--filter", "container="+ID) 436 events = strings.Split(strings.TrimSuffix(out, "\n"), "\n") 437 c.Assert(checkEvents(ID, events), checker.IsNil) 438 } 439 } 440 441 func (s *DockerSuite) TestEventsCommit(c *check.C) { 442 // Problematic on Windows as cannot commit a running container 443 testRequires(c, DaemonIsLinux) 444 445 out, _ := runSleepingContainer(c) 446 cID := strings.TrimSpace(out) 447 c.Assert(waitRun(cID), checker.IsNil) 448 449 dockerCmd(c, "commit", "-m", "test", cID) 450 dockerCmd(c, "stop", cID) 451 c.Assert(waitExited(cID, 5*time.Second), checker.IsNil) 452 453 until := daemonUnixTime(c) 454 out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until) 455 c.Assert(out, checker.Contains, "commit", check.Commentf("Missing 'commit' log event")) 456 } 457 458 func (s *DockerSuite) TestEventsCopy(c *check.C) { 459 // Build a test image. 460 id, err := buildImage("cpimg", ` 461 FROM busybox 462 RUN echo HI > /file`, true) 463 c.Assert(err, checker.IsNil, check.Commentf("Couldn't create image")) 464 465 // Create an empty test file. 466 tempFile, err := ioutil.TempFile("", "test-events-copy-") 467 c.Assert(err, checker.IsNil) 468 defer os.Remove(tempFile.Name()) 469 470 c.Assert(tempFile.Close(), checker.IsNil) 471 472 dockerCmd(c, "create", "--name=cptest", id) 473 474 dockerCmd(c, "cp", "cptest:/file", tempFile.Name()) 475 476 until := daemonUnixTime(c) 477 out, _ := dockerCmd(c, "events", "--since=0", "-f", "container=cptest", "--until="+until) 478 c.Assert(out, checker.Contains, "archive-path", check.Commentf("Missing 'archive-path' log event\n")) 479 480 dockerCmd(c, "cp", tempFile.Name(), "cptest:/filecopy") 481 482 until = daemonUnixTime(c) 483 out, _ = dockerCmd(c, "events", "-f", "container=cptest", "--until="+until) 484 c.Assert(out, checker.Contains, "extract-to-dir", check.Commentf("Missing 'extract-to-dir' log event")) 485 } 486 487 func (s *DockerSuite) TestEventsResize(c *check.C) { 488 out, _ := runSleepingContainer(c, "-d") 489 cID := strings.TrimSpace(out) 490 c.Assert(waitRun(cID), checker.IsNil) 491 492 endpoint := "/containers/" + cID + "/resize?h=80&w=24" 493 status, _, err := sockRequest("POST", endpoint, nil) 494 c.Assert(status, checker.Equals, http.StatusOK) 495 c.Assert(err, checker.IsNil) 496 497 dockerCmd(c, "stop", cID) 498 499 until := daemonUnixTime(c) 500 out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until) 501 c.Assert(out, checker.Contains, "resize", check.Commentf("Missing 'resize' log event")) 502 } 503 504 func (s *DockerSuite) TestEventsAttach(c *check.C) { 505 // TODO Windows CI: Figure out why this test fails intermittently (TP5). 506 testRequires(c, DaemonIsLinux) 507 508 out, _ := dockerCmd(c, "run", "-di", "busybox", "cat") 509 cID := strings.TrimSpace(out) 510 c.Assert(waitRun(cID), checker.IsNil) 511 512 cmd := exec.Command(dockerBinary, "attach", cID) 513 stdin, err := cmd.StdinPipe() 514 c.Assert(err, checker.IsNil) 515 defer stdin.Close() 516 stdout, err := cmd.StdoutPipe() 517 c.Assert(err, checker.IsNil) 518 defer stdout.Close() 519 c.Assert(cmd.Start(), checker.IsNil) 520 defer cmd.Process.Kill() 521 522 // Make sure we're done attaching by writing/reading some stuff 523 _, err = stdin.Write([]byte("hello\n")) 524 c.Assert(err, checker.IsNil) 525 out, err = bufio.NewReader(stdout).ReadString('\n') 526 c.Assert(err, checker.IsNil) 527 c.Assert(strings.TrimSpace(out), checker.Equals, "hello", check.Commentf("expected 'hello'")) 528 529 c.Assert(stdin.Close(), checker.IsNil) 530 531 dockerCmd(c, "kill", cID) 532 c.Assert(waitExited(cID, 5*time.Second), checker.IsNil) 533 534 until := daemonUnixTime(c) 535 out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until) 536 c.Assert(out, checker.Contains, "attach", check.Commentf("Missing 'attach' log event")) 537 } 538 539 func (s *DockerSuite) TestEventsRename(c *check.C) { 540 out, _ := dockerCmd(c, "run", "--name", "oldName", "busybox", "true") 541 cID := strings.TrimSpace(out) 542 dockerCmd(c, "rename", "oldName", "newName") 543 544 until := daemonUnixTime(c) 545 // filter by the container id because the name in the event will be the new name. 546 out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until", until) 547 c.Assert(out, checker.Contains, "rename", check.Commentf("Missing 'rename' log event\n")) 548 } 549 550 func (s *DockerSuite) TestEventsTop(c *check.C) { 551 // Problematic on Windows as Windows does not support top 552 testRequires(c, DaemonIsLinux) 553 554 out, _ := runSleepingContainer(c, "-d") 555 cID := strings.TrimSpace(out) 556 c.Assert(waitRun(cID), checker.IsNil) 557 558 dockerCmd(c, "top", cID) 559 dockerCmd(c, "stop", cID) 560 561 until := daemonUnixTime(c) 562 out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until) 563 c.Assert(out, checker.Contains, " top", check.Commentf("Missing 'top' log event")) 564 } 565 566 // #14316 567 func (s *DockerRegistrySuite) TestEventsImageFilterPush(c *check.C) { 568 // Problematic to port for Windows CI during TP5 timeframe until 569 // supporting push 570 testRequires(c, DaemonIsLinux) 571 testRequires(c, Network) 572 repoName := fmt.Sprintf("%v/dockercli/testf", privateRegistryURL) 573 574 out, _ := dockerCmd(c, "run", "-d", "busybox", "top") 575 cID := strings.TrimSpace(out) 576 c.Assert(waitRun(cID), checker.IsNil) 577 578 dockerCmd(c, "commit", cID, repoName) 579 dockerCmd(c, "stop", cID) 580 dockerCmd(c, "push", repoName) 581 582 until := daemonUnixTime(c) 583 out, _ = dockerCmd(c, "events", "-f", "image="+repoName, "-f", "event=push", "--until", until) 584 c.Assert(out, checker.Contains, repoName, check.Commentf("Missing 'push' log event for %s", repoName)) 585 } 586 587 func (s *DockerSuite) TestEventsFilterType(c *check.C) { 588 since := daemonUnixTime(c) 589 name := "labelfiltertest" 590 label := "io.docker.testing=image" 591 592 // Build a test image. 593 _, err := buildImage(name, fmt.Sprintf(` 594 FROM busybox:latest 595 LABEL %s`, label), true) 596 c.Assert(err, checker.IsNil, check.Commentf("Couldn't create image")) 597 598 dockerCmd(c, "tag", name, "labelfiltertest:tag1") 599 dockerCmd(c, "tag", name, "labelfiltertest:tag2") 600 dockerCmd(c, "tag", "busybox:latest", "labelfiltertest:tag3") 601 602 out, _ := dockerCmd( 603 c, 604 "events", 605 "--since", since, 606 "--until", daemonUnixTime(c), 607 "--filter", fmt.Sprintf("label=%s", label), 608 "--filter", "type=image") 609 610 events := strings.Split(strings.TrimSpace(out), "\n") 611 612 // 2 events from the "docker tag" command, another one is from "docker build" 613 c.Assert(events, checker.HasLen, 3, check.Commentf("Events == %s", events)) 614 for _, e := range events { 615 c.Assert(e, checker.Contains, "labelfiltertest") 616 } 617 618 out, _ = dockerCmd( 619 c, 620 "events", 621 "--since", since, 622 "--until", daemonUnixTime(c), 623 "--filter", fmt.Sprintf("label=%s", label), 624 "--filter", "type=container") 625 events = strings.Split(strings.TrimSpace(out), "\n") 626 627 // Events generated by the container that builds the image 628 c.Assert(events, checker.HasLen, 3, check.Commentf("Events == %s", events)) 629 630 out, _ = dockerCmd( 631 c, 632 "events", 633 "--since", since, 634 "--until", daemonUnixTime(c), 635 "--filter", "type=network") 636 events = strings.Split(strings.TrimSpace(out), "\n") 637 c.Assert(len(events), checker.GreaterOrEqualThan, 1, check.Commentf("Events == %s", events)) 638 } 639 640 func (s *DockerSuite) TestEventsFilterImageInContainerAction(c *check.C) { 641 since := daemonUnixTime(c) 642 dockerCmd(c, "run", "--name", "test-container", "-d", "busybox", "true") 643 waitRun("test-container") 644 645 out, _ := dockerCmd(c, "events", "--filter", "image=busybox", "--since", since, "--until", daemonUnixTime(c)) 646 events := strings.Split(strings.TrimSpace(out), "\n") 647 c.Assert(len(events), checker.GreaterThan, 1, check.Commentf(out)) 648 } 649 650 func (s *DockerSuite) TestEventsContainerRestart(c *check.C) { 651 dockerCmd(c, "run", "-d", "--name=testEvent", "--restart=on-failure:3", "busybox", "false") 652 653 // wait until test2 is auto removed. 654 waitTime := 10 * time.Second 655 if daemonPlatform == "windows" { 656 // nslookup isn't present in Windows busybox. Is built-in. 657 waitTime = 90 * time.Second 658 } 659 660 err := waitInspect("testEvent", "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTime) 661 c.Assert(err, checker.IsNil) 662 663 var ( 664 createCount int 665 startCount int 666 dieCount int 667 ) 668 out, _ := dockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c), "-f", "container=testEvent") 669 events := strings.Split(strings.TrimSpace(out), "\n") 670 671 nEvents := len(events) 672 c.Assert(nEvents, checker.GreaterOrEqualThan, 1) //Missing expected event 673 actions := eventActionsByIDAndType(c, events, "testEvent", "container") 674 675 for _, a := range actions { 676 switch a { 677 case "create": 678 createCount++ 679 case "start": 680 startCount++ 681 case "die": 682 dieCount++ 683 } 684 } 685 c.Assert(createCount, checker.Equals, 1, check.Commentf("testEvent should be created 1 times: %v", actions)) 686 c.Assert(startCount, checker.Equals, 4, check.Commentf("testEvent should start 4 times: %v", actions)) 687 c.Assert(dieCount, checker.Equals, 4, check.Commentf("testEvent should die 4 times: %v", actions)) 688 } 689 690 func (s *DockerSuite) TestEventsSinceInTheFuture(c *check.C) { 691 dockerCmd(c, "run", "--name", "test-container", "-d", "busybox", "true") 692 waitRun("test-container") 693 694 since := daemonTime(c) 695 until := since.Add(time.Duration(-24) * time.Hour) 696 out, _, err := dockerCmdWithError("events", "--filter", "image=busybox", "--since", parseEventTime(since), "--until", parseEventTime(until)) 697 698 c.Assert(err, checker.NotNil) 699 c.Assert(out, checker.Contains, "cannot be after `until`") 700 } 701 702 func (s *DockerSuite) TestEventsUntilInThePast(c *check.C) { 703 since := daemonUnixTime(c) 704 705 dockerCmd(c, "run", "--name", "test-container", "-d", "busybox", "true") 706 waitRun("test-container") 707 708 until := daemonUnixTime(c) 709 710 dockerCmd(c, "run", "--name", "test-container2", "-d", "busybox", "true") 711 waitRun("test-container2") 712 713 out, _ := dockerCmd(c, "events", "--filter", "image=busybox", "--since", since, "--until", until) 714 715 c.Assert(out, checker.Not(checker.Contains), "test-container2") 716 c.Assert(out, checker.Contains, "test-container") 717 }