Browse code

Cleanup container LogEvent calls

Move some calls to container.LogEvent down lower so that there's
less of a chance of them being missed. Also add a few more events
that appear to have been missed.

Added testcases for new events: commit, copy, resize, attach, rename, top

Signed-off-by: Doug Davis <dug@us.ibm.com>

Doug Davis authored on 2015/05/25 03:19:39
Showing 16 changed files
... ...
@@ -61,5 +61,6 @@ func (daemon *Daemon) Commit(container *Container, repository, tag, comment, aut
61 61
 			return img, err
62 62
 		}
63 63
 	}
64
+	container.LogEvent("commit")
64 65
 	return img, nil
65 66
 }
... ...
@@ -242,6 +242,7 @@ func (container *Container) Start() (err error) {
242 242
 			}
243 243
 			container.toDisk()
244 244
 			container.cleanup()
245
+			container.LogEvent("die")
245 246
 		}
246 247
 	}()
247 248
 
... ...
@@ -375,7 +376,11 @@ func (container *Container) KillSig(sig int) error {
375 375
 		return nil
376 376
 	}
377 377
 
378
-	return container.daemon.Kill(container, sig)
378
+	if err := container.daemon.Kill(container, sig); err != nil {
379
+		return err
380
+	}
381
+	container.LogEvent("kill")
382
+	return nil
379 383
 }
380 384
 
381 385
 // Wrapper aroung KillSig() suppressing "no such process" error.
... ...
@@ -406,6 +411,7 @@ func (container *Container) Pause() error {
406 406
 		return err
407 407
 	}
408 408
 	container.Paused = true
409
+	container.LogEvent("pause")
409 410
 	return nil
410 411
 }
411 412
 
... ...
@@ -427,6 +433,7 @@ func (container *Container) Unpause() error {
427 427
 		return err
428 428
 	}
429 429
 	container.Paused = false
430
+	container.LogEvent("unpause")
430 431
 	return nil
431 432
 }
432 433
 
... ...
@@ -488,6 +495,7 @@ func (container *Container) Stop(seconds int) error {
488 488
 		}
489 489
 	}
490 490
 
491
+	container.LogEvent("stop")
491 492
 	return nil
492 493
 }
493 494
 
... ...
@@ -502,14 +510,24 @@ func (container *Container) Restart(seconds int) error {
502 502
 	if err := container.Stop(seconds); err != nil {
503 503
 		return err
504 504
 	}
505
-	return container.Start()
505
+
506
+	if err := container.Start(); err != nil {
507
+		return err
508
+	}
509
+
510
+	container.LogEvent("restart")
511
+	return nil
506 512
 }
507 513
 
508 514
 func (container *Container) Resize(h, w int) error {
509 515
 	if !container.IsRunning() {
510 516
 		return fmt.Errorf("Cannot resize container %s, container is not running", container.ID)
511 517
 	}
512
-	return container.command.ProcessConfig.Terminal.Resize(h, w)
518
+	if err := container.command.ProcessConfig.Terminal.Resize(h, w); err != nil {
519
+		return err
520
+	}
521
+	container.LogEvent("resize")
522
+	return nil
513 523
 }
514 524
 
515 525
 func (container *Container) Export() (archive.Archive, error) {
... ...
@@ -522,12 +540,13 @@ func (container *Container) Export() (archive.Archive, error) {
522 522
 		container.Unmount()
523 523
 		return nil, err
524 524
 	}
525
-	return ioutils.NewReadCloserWrapper(archive, func() error {
526
-			err := archive.Close()
527
-			container.Unmount()
528
-			return err
529
-		}),
530
-		nil
525
+	arch := ioutils.NewReadCloserWrapper(archive, func() error {
526
+		err := archive.Close()
527
+		container.Unmount()
528
+		return err
529
+	})
530
+	container.LogEvent("export")
531
+	return arch, err
531 532
 }
532 533
 
533 534
 func (container *Container) Mount() error {
... ...
@@ -628,13 +647,14 @@ func (container *Container) Copy(resource string) (io.ReadCloser, error) {
628 628
 	if err != nil {
629 629
 		return nil, err
630 630
 	}
631
-	return ioutils.NewReadCloserWrapper(archive, func() error {
632
-			err := archive.Close()
633
-			container.UnmountVolumes(true)
634
-			container.Unmount()
635
-			return err
636
-		}),
637
-		nil
631
+	reader := ioutils.NewReadCloserWrapper(archive, func() error {
632
+		err := archive.Close()
633
+		container.UnmountVolumes(true)
634
+		container.Unmount()
635
+		return err
636
+	})
637
+	container.LogEvent("copy")
638
+	return reader, nil
638 639
 }
639 640
 
640 641
 // Returns true if the container exposes a certain port
... ...
@@ -826,6 +846,7 @@ func (c *Container) Attach(stdin io.ReadCloser, stdout io.Writer, stderr io.Writ
826 826
 }
827 827
 
828 828
 func (c *Container) AttachWithLogs(stdin io.ReadCloser, stdout, stderr io.Writer, logs, stream bool) error {
829
+
829 830
 	if logs {
830 831
 		logDriver, err := c.getLogger()
831 832
 		cLog, err := logDriver.GetReader()
... ...
@@ -855,6 +876,8 @@ func (c *Container) AttachWithLogs(stdin io.ReadCloser, stdout, stderr io.Writer
855 855
 		}
856 856
 	}
857 857
 
858
+	c.LogEvent("attach")
859
+
858 860
 	//stream
859 861
 	if stream {
860 862
 		var stdinPipe io.ReadCloser
... ...
@@ -39,7 +39,6 @@ func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hos
39 39
 		return "", warnings, err
40 40
 	}
41 41
 
42
-	container.LogEvent("create")
43 42
 	warnings = append(warnings, buildWarnings...)
44 43
 
45 44
 	return container.ID, warnings, nil
... ...
@@ -142,6 +141,7 @@ func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.Hos
142 142
 	if err := container.ToDisk(); err != nil {
143 143
 		return nil, nil, err
144 144
 	}
145
+	container.LogEvent("create")
145 146
 	return container, warnings, nil
146 147
 }
147 148
 
... ...
@@ -48,8 +48,6 @@ func (daemon *Daemon) ContainerRm(name string, config *ContainerRmConfig) error
48 48
 		return fmt.Errorf("Cannot destroy container %s: %v", name, err)
49 49
 	}
50 50
 
51
-	container.LogEvent("destroy")
52
-
53 51
 	if config.RemoveVolume {
54 52
 		container.removeMountPoints()
55 53
 	}
... ...
@@ -102,6 +100,7 @@ func (daemon *Daemon) rm(container *Container, forceRemove bool) (err error) {
102 102
 			daemon.idIndex.Delete(container.ID)
103 103
 			daemon.containers.Delete(container.ID)
104 104
 			os.RemoveAll(container.root)
105
+			container.LogEvent("destroy")
105 106
 		}
106 107
 	}()
107 108
 
... ...
@@ -130,6 +129,7 @@ func (daemon *Daemon) rm(container *Container, forceRemove bool) (err error) {
130 130
 	daemon.idIndex.Delete(container.ID)
131 131
 	daemon.containers.Delete(container.ID)
132 132
 
133
+	container.LogEvent("destroy")
133 134
 	return nil
134 135
 }
135 136
 
... ...
@@ -141,10 +141,10 @@ func (d *Daemon) ContainerExecCreate(config *runconfig.ExecConfig) (string, erro
141 141
 		Running:       false,
142 142
 	}
143 143
 
144
-	container.LogEvent("exec_create: " + execConfig.ProcessConfig.Entrypoint + " " + strings.Join(execConfig.ProcessConfig.Arguments, " "))
145
-
146 144
 	d.registerExecCommand(execConfig)
147 145
 
146
+	container.LogEvent("exec_create: " + execConfig.ProcessConfig.Entrypoint + " " + strings.Join(execConfig.ProcessConfig.Arguments, " "))
147
+
148 148
 	return execConfig.ID, nil
149 149
 
150 150
 }
... ...
@@ -21,7 +21,5 @@ func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
21 21
 	if _, err := io.Copy(out, data); err != nil {
22 22
 		return fmt.Errorf("%s: %s", name, err)
23 23
 	}
24
-	// FIXME: factor job-specific LogEvent to engine.Job.Run()
25
-	container.LogEvent("export")
26 24
 	return nil
27 25
 }
... ...
@@ -26,6 +26,5 @@ func (daemon *Daemon) ContainerKill(name string, sig uint64) error {
26 26
 			return fmt.Errorf("Cannot kill container %s: %s", name, err)
27 27
 		}
28 28
 	}
29
-	container.LogEvent("kill")
30 29
 	return nil
31 30
 }
... ...
@@ -12,7 +12,6 @@ func (daemon *Daemon) ContainerPause(name string) error {
12 12
 	if err := container.Pause(); err != nil {
13 13
 		return fmt.Errorf("Cannot pause container %s: %s", name, err)
14 14
 	}
15
-	container.LogEvent("pause")
16 15
 
17 16
 	return nil
18 17
 }
... ...
@@ -40,5 +40,6 @@ func (daemon *Daemon) ContainerRename(oldName, newName string) error {
40 40
 		return err
41 41
 	}
42 42
 
43
+	container.LogEvent("rename")
43 44
 	return nil
44 45
 }
... ...
@@ -10,6 +10,5 @@ func (daemon *Daemon) ContainerRestart(name string, seconds int) error {
10 10
 	if err := container.Restart(seconds); err != nil {
11 11
 		return fmt.Errorf("Cannot restart container %s: %s\n", name, err)
12 12
 	}
13
-	container.LogEvent("restart")
14 13
 	return nil
15 14
 }
... ...
@@ -33,7 +33,6 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *runconfig.HostConf
33 33
 	}
34 34
 
35 35
 	if err := container.Start(); err != nil {
36
-		container.LogEvent("die")
37 36
 		return fmt.Errorf("Cannot start container %s: %s", name, err)
38 37
 	}
39 38
 
... ...
@@ -13,6 +13,5 @@ func (daemon *Daemon) ContainerStop(name string, seconds int) error {
13 13
 	if err := container.Stop(seconds); err != nil {
14 14
 		return fmt.Errorf("Cannot stop container %s: %s\n", name, err)
15 15
 	}
16
-	container.LogEvent("stop")
17 16
 	return nil
18 17
 }
... ...
@@ -68,5 +68,6 @@ func (daemon *Daemon) ContainerTop(name string, psArgs string) (*types.Container
68 68
 			}
69 69
 		}
70 70
 	}
71
+	container.LogEvent("top")
71 72
 	return procList, nil
72 73
 }
... ...
@@ -12,7 +12,6 @@ func (daemon *Daemon) ContainerUnpause(name string) error {
12 12
 	if err := container.Unpause(); err != nil {
13 13
 		return fmt.Errorf("Cannot unpause container %s: %s", name, err)
14 14
 	}
15
-	container.LogEvent("unpause")
16 15
 
17 16
 	return nil
18 17
 }
... ...
@@ -1794,7 +1794,7 @@ polling (using since).
1794 1794
 
1795 1795
 Docker containers report the following events:
1796 1796
 
1797
-    create, destroy, die, exec_create, exec_start, export, kill, oom, pause, restart, start, stop, unpause
1797
+    attach, commit, copy, create, destroy, die, exec_create, exec_start, export, kill, oom, pause, rename, resize, restart, start, stop, top, unpause
1798 1798
 
1799 1799
 and Docker images report:
1800 1800
 
... ...
@@ -3,6 +3,7 @@ package main
3 3
 import (
4 4
 	"bufio"
5 5
 	"fmt"
6
+	"net/http"
6 7
 	"os/exec"
7 8
 	"regexp"
8 9
 	"strconv"
... ...
@@ -141,16 +142,20 @@ func (s *DockerSuite) TestEventsContainerEvents(c *check.C) {
141 141
 	}
142 142
 	events := strings.Split(out, "\n")
143 143
 	events = events[:len(events)-1]
144
-	if len(events) < 4 {
144
+	if len(events) < 5 {
145 145
 		c.Fatalf("Missing expected event")
146 146
 	}
147
-	createEvent := strings.Fields(events[len(events)-4])
147
+	createEvent := strings.Fields(events[len(events)-5])
148
+	attachEvent := strings.Fields(events[len(events)-4])
148 149
 	startEvent := strings.Fields(events[len(events)-3])
149 150
 	dieEvent := strings.Fields(events[len(events)-2])
150 151
 	destroyEvent := strings.Fields(events[len(events)-1])
151 152
 	if createEvent[len(createEvent)-1] != "create" {
152 153
 		c.Fatalf("event should be create, not %#v", createEvent)
153 154
 	}
155
+	if attachEvent[len(createEvent)-1] != "attach" {
156
+		c.Fatalf("event should be attach, not %#v", attachEvent)
157
+	}
154 158
 	if startEvent[len(startEvent)-1] != "start" {
155 159
 		c.Fatalf("event should be start, not %#v", startEvent)
156 160
 	}
... ...
@@ -175,16 +180,20 @@ func (s *DockerSuite) TestEventsContainerEventsSinceUnixEpoch(c *check.C) {
175 175
 	}
176 176
 	events := strings.Split(out, "\n")
177 177
 	events = events[:len(events)-1]
178
-	if len(events) < 4 {
178
+	if len(events) < 5 {
179 179
 		c.Fatalf("Missing expected event")
180 180
 	}
181
-	createEvent := strings.Fields(events[len(events)-4])
181
+	createEvent := strings.Fields(events[len(events)-5])
182
+	attachEvent := strings.Fields(events[len(events)-4])
182 183
 	startEvent := strings.Fields(events[len(events)-3])
183 184
 	dieEvent := strings.Fields(events[len(events)-2])
184 185
 	destroyEvent := strings.Fields(events[len(events)-1])
185 186
 	if createEvent[len(createEvent)-1] != "create" {
186 187
 		c.Fatalf("event should be create, not %#v", createEvent)
187 188
 	}
189
+	if attachEvent[len(attachEvent)-1] != "attach" {
190
+		c.Fatalf("event should be attach, not %#v", attachEvent)
191
+	}
188 192
 	if startEvent[len(startEvent)-1] != "start" {
189 193
 		c.Fatalf("event should be start, not %#v", startEvent)
190 194
 	}
... ...
@@ -433,7 +442,7 @@ func (s *DockerSuite) TestEventsFilterContainer(c *check.C) {
433 433
 	until := fmt.Sprintf("%d", daemonTime(c).Unix())
434 434
 
435 435
 	checkEvents := func(id string, events []string) error {
436
-		if len(events) != 3 { // create, start, die
436
+		if len(events) != 4 { // create, attach, start, die
437 437
 			return fmt.Errorf("expected 3 events, got %v", events)
438 438
 		}
439 439
 		for _, event := range events {
... ...
@@ -563,3 +572,208 @@ func (s *DockerSuite) TestEventsStreaming(c *check.C) {
563 563
 		// ignore, done
564 564
 	}
565 565
 }
566
+
567
+func (s *DockerSuite) TestEventsCommit(c *check.C) {
568
+	since := daemonTime(c).Unix()
569
+
570
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
571
+	out, _, err := runCommandWithOutput(runCmd)
572
+	if err != nil {
573
+		c.Fatalf("Couldn't run top: %s\n%q", out, err)
574
+	}
575
+	cID := strings.TrimSpace(out)
576
+	c.Assert(waitRun(cID), check.IsNil)
577
+
578
+	cmd := exec.Command(dockerBinary, "commit", "-m", "test", cID)
579
+	out, _, err = runCommandWithOutput(cmd)
580
+	if err != nil {
581
+		c.Fatalf("Couldn't commit: %s\n%q", out, err)
582
+	}
583
+
584
+	cmd = exec.Command(dockerBinary, "stop", cID)
585
+	out, _, err = runCommandWithOutput(cmd)
586
+	if err != nil {
587
+		c.Fatalf("Couldn't stop: %s\n%q", out, err)
588
+	}
589
+
590
+	cmd = exec.Command(dockerBinary, "events", "-f", "container="+cID, "--until="+strconv.Itoa(int(since)))
591
+	out, _, err = runCommandWithOutput(cmd)
592
+	if err != nil {
593
+		c.Fatalf("Couldn't get events: %s\n%q", out, err)
594
+	}
595
+
596
+	if !strings.Contains(out, " commit\n") {
597
+		c.Fatalf("Missing 'commit' log event\n%s", out)
598
+	}
599
+}
600
+
601
+func (s *DockerSuite) TestEventsCopy(c *check.C) {
602
+	since := daemonTime(c).Unix()
603
+
604
+	id, err := buildImage("cpimg", `
605
+		  FROM busybox
606
+		  RUN echo HI > /tmp/file`, true)
607
+	if err != nil {
608
+		c.Fatalf("Couldn't create image: %q", err)
609
+	}
610
+
611
+	runCmd := exec.Command(dockerBinary, "run", "--name=cptest", id, "true")
612
+	out, _, err := runCommandWithOutput(runCmd)
613
+	if err != nil {
614
+		c.Fatalf("Couldn't run top: %s\n%q", out, err)
615
+	}
616
+
617
+	cmd := exec.Command(dockerBinary, "cp", "cptest:/tmp/file", "-")
618
+	out, _, err = runCommandWithOutput(cmd)
619
+	if err != nil {
620
+		c.Fatalf("Failed getting file:%q\n%q", out, err)
621
+	}
622
+
623
+	cmd = exec.Command(dockerBinary, "events", "-f", "container=cptest", "--until="+strconv.Itoa(int(since)))
624
+	out, _, err = runCommandWithOutput(cmd)
625
+	if err != nil {
626
+		c.Fatalf("Couldn't get events: %s\n%q", out, err)
627
+	}
628
+
629
+	if !strings.Contains(out, " copy\n") {
630
+		c.Fatalf("Missing 'copy' log event\n%s", out)
631
+	}
632
+}
633
+
634
+func (s *DockerSuite) TestEventsResize(c *check.C) {
635
+	since := daemonTime(c).Unix()
636
+
637
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
638
+	out, _, err := runCommandWithOutput(runCmd)
639
+	if err != nil {
640
+		c.Fatalf("Couldn't run top: %s\n%q", out, err)
641
+	}
642
+	cID := strings.TrimSpace(out)
643
+	c.Assert(waitRun(cID), check.IsNil)
644
+
645
+	endpoint := "/containers/" + cID + "/resize?h=80&w=24"
646
+	status, _, err := sockRequest("POST", endpoint, nil)
647
+	c.Assert(status, check.Equals, http.StatusOK)
648
+	c.Assert(err, check.IsNil)
649
+
650
+	cmd := exec.Command(dockerBinary, "stop", cID)
651
+	out, _, err = runCommandWithOutput(cmd)
652
+	if err != nil {
653
+		c.Fatalf("Couldn't stop: %s\n%q", out, err)
654
+	}
655
+
656
+	cmd = exec.Command(dockerBinary, "events", "-f", "container="+cID, "--until="+strconv.Itoa(int(since)))
657
+	out, _, err = runCommandWithOutput(cmd)
658
+	if err != nil {
659
+		c.Fatalf("Couldn't get events: %s\n%q", out, err)
660
+	}
661
+
662
+	if !strings.Contains(out, " resize\n") {
663
+		c.Fatalf("Missing 'resize' log event\n%s", out)
664
+	}
665
+}
666
+
667
+func (s *DockerSuite) TestEventsAttach(c *check.C) {
668
+	since := daemonTime(c).Unix()
669
+
670
+	out, _ := dockerCmd(c, "run", "-di", "busybox", "/bin/cat")
671
+	cID := strings.TrimSpace(out)
672
+
673
+	cmd := exec.Command(dockerBinary, "attach", cID)
674
+	stdin, err := cmd.StdinPipe()
675
+	c.Assert(err, check.IsNil)
676
+	defer stdin.Close()
677
+	stdout, err := cmd.StdoutPipe()
678
+	c.Assert(err, check.IsNil)
679
+	defer stdout.Close()
680
+	c.Assert(cmd.Start(), check.IsNil)
681
+	defer cmd.Process.Kill()
682
+
683
+	// Make sure we're done attaching by writing/reading some stuff
684
+	if _, err := stdin.Write([]byte("hello\n")); err != nil {
685
+		c.Fatal(err)
686
+	}
687
+	out, err = bufio.NewReader(stdout).ReadString('\n')
688
+	c.Assert(err, check.IsNil)
689
+	if strings.TrimSpace(out) != "hello" {
690
+		c.Fatalf("expected 'hello', got %q", out)
691
+	}
692
+
693
+	c.Assert(stdin.Close(), check.IsNil)
694
+
695
+	cmd = exec.Command(dockerBinary, "stop", cID)
696
+	out, _, err = runCommandWithOutput(cmd)
697
+	if err != nil {
698
+		c.Fatalf("Couldn't stop: %s\n%q", out, err)
699
+	}
700
+
701
+	cmd = exec.Command(dockerBinary, "events", "-f", "container="+cID, "--until="+strconv.Itoa(int(since)))
702
+	out, _, err = runCommandWithOutput(cmd)
703
+	if err != nil {
704
+		c.Fatalf("Couldn't get events: %s\n%q", out, err)
705
+	}
706
+
707
+	if !strings.Contains(out, " attach\n") {
708
+		c.Fatalf("Missing 'attach' log event\n%s", out)
709
+	}
710
+}
711
+
712
+func (s *DockerSuite) TestEventsRename(c *check.C) {
713
+	since := daemonTime(c).Unix()
714
+
715
+	runCmd := exec.Command(dockerBinary, "run", "--name", "oldName", "busybox", "true")
716
+	out, _, err := runCommandWithOutput(runCmd)
717
+	if err != nil {
718
+		c.Fatalf("Couldn't run true: %s\n%q", out, err)
719
+	}
720
+
721
+	renameCmd := exec.Command(dockerBinary, "rename", "oldName", "newName")
722
+	out, _, err = runCommandWithOutput(renameCmd)
723
+	if err != nil {
724
+		c.Fatalf("Couldn't rename: %s\n%q", out, err)
725
+	}
726
+
727
+	cmd := exec.Command(dockerBinary, "events", "-f", "container=newName", "--until="+strconv.Itoa(int(since)))
728
+	out, _, err = runCommandWithOutput(cmd)
729
+	if err != nil {
730
+		c.Fatalf("Couldn't get events: %s\n%q", out, err)
731
+	}
732
+
733
+	if !strings.Contains(out, " rename\n") {
734
+		c.Fatalf("Missing 'rename' log event\n%s", out)
735
+	}
736
+}
737
+
738
+func (s *DockerSuite) TestEventsTop(c *check.C) {
739
+	since := daemonTime(c).Unix()
740
+
741
+	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
742
+	out, _, err := runCommandWithOutput(runCmd)
743
+	if err != nil {
744
+		c.Fatalf("Couldn't run true: %s\n%q", out, err)
745
+	}
746
+	cID := strings.TrimSpace(out)
747
+	c.Assert(waitRun(cID), check.IsNil)
748
+
749
+	cmd := exec.Command(dockerBinary, "top", cID)
750
+	out, _, err = runCommandWithOutput(cmd)
751
+	if err != nil {
752
+		c.Fatalf("Couldn't run docker top: %s\n%q", out, err)
753
+	}
754
+
755
+	cmd = exec.Command(dockerBinary, "stop", cID)
756
+	out, _, err = runCommandWithOutput(cmd)
757
+	if err != nil {
758
+		c.Fatalf("Couldn't stop: %s\n%q", out, err)
759
+	}
760
+
761
+	cmd = exec.Command(dockerBinary, "events", "-f", "container="+cID, "--until="+strconv.Itoa(int(since)))
762
+	out, _, err = runCommandWithOutput(cmd)
763
+	if err != nil {
764
+		c.Fatalf("Couldn't get events: %s\n%q", out, err)
765
+	}
766
+
767
+	if !strings.Contains(out, " top\n") {
768
+		c.Fatalf("Missing 'top' log event\n%s", out)
769
+	}
770
+}