Signed-off-by: Antonio Murdaca <me@runcom.ninja>
| ... | ... |
@@ -1,6 +1,7 @@ |
| 1 | 1 |
package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "bufio" |
|
| 4 | 5 |
"fmt" |
| 5 | 6 |
"os/exec" |
| 6 | 7 |
"regexp" |
| ... | ... |
@@ -411,3 +412,102 @@ func checkEvents(t *testing.T, events []string) {
|
| 411 | 411 |
} |
| 412 | 412 |
|
| 413 | 413 |
} |
| 414 |
+ |
|
| 415 |
+func TestEventsStreaming(t *testing.T) {
|
|
| 416 |
+ start := daemonTime(t).Unix() |
|
| 417 |
+ |
|
| 418 |
+ finish := make(chan struct{})
|
|
| 419 |
+ defer close(finish) |
|
| 420 |
+ id := make(chan string) |
|
| 421 |
+ eventCreate := make(chan struct{})
|
|
| 422 |
+ eventStart := make(chan struct{})
|
|
| 423 |
+ eventDie := make(chan struct{})
|
|
| 424 |
+ eventDestroy := make(chan struct{})
|
|
| 425 |
+ |
|
| 426 |
+ go func() {
|
|
| 427 |
+ eventsCmd := exec.Command(dockerBinary, "events", "--since", string(start)) |
|
| 428 |
+ stdout, err := eventsCmd.StdoutPipe() |
|
| 429 |
+ if err != nil {
|
|
| 430 |
+ t.Fatal(err) |
|
| 431 |
+ } |
|
| 432 |
+ err = eventsCmd.Start() |
|
| 433 |
+ if err != nil {
|
|
| 434 |
+ t.Fatalf("failed to start 'docker events': %s", err)
|
|
| 435 |
+ } |
|
| 436 |
+ |
|
| 437 |
+ go func() {
|
|
| 438 |
+ <-finish |
|
| 439 |
+ eventsCmd.Process.Kill() |
|
| 440 |
+ }() |
|
| 441 |
+ |
|
| 442 |
+ containerID := <-id |
|
| 443 |
+ |
|
| 444 |
+ matchCreate := regexp.MustCompile(containerID + `: \(from busybox:latest\) create$`) |
|
| 445 |
+ matchStart := regexp.MustCompile(containerID + `: \(from busybox:latest\) start$`) |
|
| 446 |
+ matchDie := regexp.MustCompile(containerID + `: \(from busybox:latest\) die$`) |
|
| 447 |
+ matchDestroy := regexp.MustCompile(containerID + `: \(from busybox:latest\) destroy$`) |
|
| 448 |
+ |
|
| 449 |
+ scanner := bufio.NewScanner(stdout) |
|
| 450 |
+ for scanner.Scan() {
|
|
| 451 |
+ switch {
|
|
| 452 |
+ case matchCreate.MatchString(scanner.Text()): |
|
| 453 |
+ close(eventCreate) |
|
| 454 |
+ case matchStart.MatchString(scanner.Text()): |
|
| 455 |
+ close(eventStart) |
|
| 456 |
+ case matchDie.MatchString(scanner.Text()): |
|
| 457 |
+ close(eventDie) |
|
| 458 |
+ case matchDestroy.MatchString(scanner.Text()): |
|
| 459 |
+ close(eventDestroy) |
|
| 460 |
+ } |
|
| 461 |
+ } |
|
| 462 |
+ |
|
| 463 |
+ err = eventsCmd.Wait() |
|
| 464 |
+ if err != nil && !IsKilled(err) {
|
|
| 465 |
+ t.Fatalf("docker events had bad exit status: %s", err)
|
|
| 466 |
+ } |
|
| 467 |
+ }() |
|
| 468 |
+ |
|
| 469 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") |
|
| 470 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
| 471 |
+ if err != nil {
|
|
| 472 |
+ t.Fatal(out, err) |
|
| 473 |
+ } |
|
| 474 |
+ cleanedContainerID := strings.TrimSpace(out) |
|
| 475 |
+ id <- cleanedContainerID |
|
| 476 |
+ |
|
| 477 |
+ select {
|
|
| 478 |
+ case <-time.After(30 * time.Second): |
|
| 479 |
+ t.Fatal("failed to observe container create in timely fashion")
|
|
| 480 |
+ case <-eventCreate: |
|
| 481 |
+ // ignore, done |
|
| 482 |
+ } |
|
| 483 |
+ |
|
| 484 |
+ select {
|
|
| 485 |
+ case <-time.After(30 * time.Second): |
|
| 486 |
+ t.Fatal("failed to observe container start in timely fashion")
|
|
| 487 |
+ case <-eventStart: |
|
| 488 |
+ // ignore, done |
|
| 489 |
+ } |
|
| 490 |
+ |
|
| 491 |
+ select {
|
|
| 492 |
+ case <-time.After(30 * time.Second): |
|
| 493 |
+ t.Fatal("failed to observe container die in timely fashion")
|
|
| 494 |
+ case <-eventDie: |
|
| 495 |
+ // ignore, done |
|
| 496 |
+ } |
|
| 497 |
+ |
|
| 498 |
+ rmCmd := exec.Command(dockerBinary, "rm", cleanedContainerID) |
|
| 499 |
+ out, _, err = runCommandWithOutput(rmCmd) |
|
| 500 |
+ if err != nil {
|
|
| 501 |
+ t.Fatal(out, err) |
|
| 502 |
+ } |
|
| 503 |
+ |
|
| 504 |
+ select {
|
|
| 505 |
+ case <-time.After(30 * time.Second): |
|
| 506 |
+ t.Fatal("failed to observe container destroy in timely fashion")
|
|
| 507 |
+ case <-eventDestroy: |
|
| 508 |
+ // ignore, done |
|
| 509 |
+ } |
|
| 510 |
+ |
|
| 511 |
+ logDone("events - streamed to stdout")
|
|
| 512 |
+} |