github.com/portworx/docker@v1.12.1/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) TestEventsPluginOps(c *check.C) {
   301  	testRequires(c, DaemonIsLinux, ExperimentalDaemon)
   302  
   303  	pluginName := "tiborvass/no-remove:latest"
   304  	since := daemonUnixTime(c)
   305  
   306  	dockerCmd(c, "plugin", "install", pluginName, "--grant-all-permissions")
   307  	dockerCmd(c, "plugin", "disable", pluginName)
   308  	dockerCmd(c, "plugin", "remove", pluginName)
   309  
   310  	out, _ := dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c))
   311  	events := strings.Split(out, "\n")
   312  	events = events[:len(events)-1]
   313  
   314  	nEvents := len(events)
   315  	c.Assert(nEvents, checker.GreaterOrEqualThan, 4)
   316  
   317  	pluginEvents := eventActionsByIDAndType(c, events, pluginName, "plugin")
   318  	c.Assert(pluginEvents, checker.HasLen, 4, check.Commentf("events: %v", events))
   319  
   320  	c.Assert(pluginEvents[0], checker.Equals, "pull", check.Commentf(out))
   321  	c.Assert(pluginEvents[1], checker.Equals, "enable", check.Commentf(out))
   322  	c.Assert(pluginEvents[2], checker.Equals, "disable", check.Commentf(out))
   323  	c.Assert(pluginEvents[3], checker.Equals, "remove", check.Commentf(out))
   324  }
   325  
   326  func (s *DockerSuite) TestEventsFilters(c *check.C) {
   327  	since := daemonUnixTime(c)
   328  	dockerCmd(c, "run", "--rm", "busybox", "true")
   329  	dockerCmd(c, "run", "--rm", "busybox", "true")
   330  	out, _ := dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=die")
   331  	parseEvents(c, out, "die")
   332  
   333  	out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=die", "--filter", "event=start")
   334  	parseEvents(c, out, "die|start")
   335  
   336  	// make sure we at least got 2 start events
   337  	count := strings.Count(out, "start")
   338  	c.Assert(strings.Count(out, "start"), checker.GreaterOrEqualThan, 2, check.Commentf("should have had 2 start events but had %d, out: %s", count, out))
   339  
   340  }
   341  
   342  func (s *DockerSuite) TestEventsFilterImageName(c *check.C) {
   343  	since := daemonUnixTime(c)
   344  
   345  	out, _ := dockerCmd(c, "run", "--name", "container_1", "-d", "busybox:latest", "true")
   346  	container1 := strings.TrimSpace(out)
   347  
   348  	out, _ = dockerCmd(c, "run", "--name", "container_2", "-d", "busybox", "true")
   349  	container2 := strings.TrimSpace(out)
   350  
   351  	name := "busybox"
   352  	out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("image=%s", name))
   353  	events := strings.Split(out, "\n")
   354  	events = events[:len(events)-1]
   355  	c.Assert(events, checker.Not(checker.HasLen), 0) //Expected events but found none for the image busybox:latest
   356  	count1 := 0
   357  	count2 := 0
   358  
   359  	for _, e := range events {
   360  		if strings.Contains(e, container1) {
   361  			count1++
   362  		} else if strings.Contains(e, container2) {
   363  			count2++
   364  		}
   365  	}
   366  	c.Assert(count1, checker.Not(checker.Equals), 0, check.Commentf("Expected event from container but got %d from %s", count1, container1))
   367  	c.Assert(count2, checker.Not(checker.Equals), 0, check.Commentf("Expected event from container but got %d from %s", count2, container2))
   368  
   369  }
   370  
   371  func (s *DockerSuite) TestEventsFilterLabels(c *check.C) {
   372  	since := daemonUnixTime(c)
   373  	label := "io.docker.testing=foo"
   374  
   375  	out, _ := dockerCmd(c, "run", "-d", "-l", label, "busybox:latest", "true")
   376  	container1 := strings.TrimSpace(out)
   377  
   378  	out, _ = dockerCmd(c, "run", "-d", "busybox", "true")
   379  	container2 := strings.TrimSpace(out)
   380  
   381  	out, _ = dockerCmd(
   382  		c,
   383  		"events",
   384  		"--since", since,
   385  		"--until", daemonUnixTime(c),
   386  		"--filter", fmt.Sprintf("label=%s", label))
   387  
   388  	events := strings.Split(strings.TrimSpace(out), "\n")
   389  	c.Assert(len(events), checker.Equals, 3)
   390  
   391  	for _, e := range events {
   392  		c.Assert(e, checker.Contains, container1)
   393  		c.Assert(e, checker.Not(checker.Contains), container2)
   394  	}
   395  }
   396  
   397  func (s *DockerSuite) TestEventsFilterImageLabels(c *check.C) {
   398  	since := daemonUnixTime(c)
   399  	name := "labelfiltertest"
   400  	label := "io.docker.testing=image"
   401  
   402  	// Build a test image.
   403  	_, err := buildImage(name, fmt.Sprintf(`
   404  		FROM busybox:latest
   405  		LABEL %s`, label), true)
   406  	c.Assert(err, checker.IsNil, check.Commentf("Couldn't create image"))
   407  
   408  	dockerCmd(c, "tag", name, "labelfiltertest:tag1")
   409  	dockerCmd(c, "tag", name, "labelfiltertest:tag2")
   410  	dockerCmd(c, "tag", "busybox:latest", "labelfiltertest:tag3")
   411  
   412  	out, _ := dockerCmd(
   413  		c,
   414  		"events",
   415  		"--since", since,
   416  		"--until", daemonUnixTime(c),
   417  		"--filter", fmt.Sprintf("label=%s", label),
   418  		"--filter", "type=image")
   419  
   420  	events := strings.Split(strings.TrimSpace(out), "\n")
   421  
   422  	// 2 events from the "docker tag" command, another one is from "docker build"
   423  	c.Assert(events, checker.HasLen, 3, check.Commentf("Events == %s", events))
   424  	for _, e := range events {
   425  		c.Assert(e, checker.Contains, "labelfiltertest")
   426  	}
   427  }
   428  
   429  func (s *DockerSuite) TestEventsFilterContainer(c *check.C) {
   430  	since := daemonUnixTime(c)
   431  	nameID := make(map[string]string)
   432  
   433  	for _, name := range []string{"container_1", "container_2"} {
   434  		dockerCmd(c, "run", "--name", name, "busybox", "true")
   435  		id := inspectField(c, name, "Id")
   436  		nameID[name] = id
   437  	}
   438  
   439  	until := daemonUnixTime(c)
   440  
   441  	checkEvents := func(id string, events []string) error {
   442  		if len(events) != 4 { // create, attach, start, die
   443  			return fmt.Errorf("expected 4 events, got %v", events)
   444  		}
   445  		for _, event := range events {
   446  			matches := eventstestutils.ScanMap(event)
   447  			if !matchEventID(matches, id) {
   448  				return fmt.Errorf("expected event for container id %s: %s - parsed container id: %s", id, event, matches["id"])
   449  			}
   450  		}
   451  		return nil
   452  	}
   453  
   454  	for name, ID := range nameID {
   455  		// filter by names
   456  		out, _ := dockerCmd(c, "events", "--since", since, "--until", until, "--filter", "container="+name)
   457  		events := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
   458  		c.Assert(checkEvents(ID, events), checker.IsNil)
   459  
   460  		// filter by ID's
   461  		out, _ = dockerCmd(c, "events", "--since", since, "--until", until, "--filter", "container="+ID)
   462  		events = strings.Split(strings.TrimSuffix(out, "\n"), "\n")
   463  		c.Assert(checkEvents(ID, events), checker.IsNil)
   464  	}
   465  }
   466  
   467  func (s *DockerSuite) TestEventsCommit(c *check.C) {
   468  	// Problematic on Windows as cannot commit a running container
   469  	testRequires(c, DaemonIsLinux)
   470  
   471  	out, _ := runSleepingContainer(c)
   472  	cID := strings.TrimSpace(out)
   473  	c.Assert(waitRun(cID), checker.IsNil)
   474  
   475  	dockerCmd(c, "commit", "-m", "test", cID)
   476  	dockerCmd(c, "stop", cID)
   477  	c.Assert(waitExited(cID, 5*time.Second), checker.IsNil)
   478  
   479  	until := daemonUnixTime(c)
   480  	out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until)
   481  	c.Assert(out, checker.Contains, "commit", check.Commentf("Missing 'commit' log event"))
   482  }
   483  
   484  func (s *DockerSuite) TestEventsCopy(c *check.C) {
   485  	// Build a test image.
   486  	id, err := buildImage("cpimg", `
   487  		  FROM busybox
   488  		  RUN echo HI > /file`, true)
   489  	c.Assert(err, checker.IsNil, check.Commentf("Couldn't create image"))
   490  
   491  	// Create an empty test file.
   492  	tempFile, err := ioutil.TempFile("", "test-events-copy-")
   493  	c.Assert(err, checker.IsNil)
   494  	defer os.Remove(tempFile.Name())
   495  
   496  	c.Assert(tempFile.Close(), checker.IsNil)
   497  
   498  	dockerCmd(c, "create", "--name=cptest", id)
   499  
   500  	dockerCmd(c, "cp", "cptest:/file", tempFile.Name())
   501  
   502  	until := daemonUnixTime(c)
   503  	out, _ := dockerCmd(c, "events", "--since=0", "-f", "container=cptest", "--until="+until)
   504  	c.Assert(out, checker.Contains, "archive-path", check.Commentf("Missing 'archive-path' log event\n"))
   505  
   506  	dockerCmd(c, "cp", tempFile.Name(), "cptest:/filecopy")
   507  
   508  	until = daemonUnixTime(c)
   509  	out, _ = dockerCmd(c, "events", "-f", "container=cptest", "--until="+until)
   510  	c.Assert(out, checker.Contains, "extract-to-dir", check.Commentf("Missing 'extract-to-dir' log event"))
   511  }
   512  
   513  func (s *DockerSuite) TestEventsResize(c *check.C) {
   514  	out, _ := runSleepingContainer(c, "-d")
   515  	cID := strings.TrimSpace(out)
   516  	c.Assert(waitRun(cID), checker.IsNil)
   517  
   518  	endpoint := "/containers/" + cID + "/resize?h=80&w=24"
   519  	status, _, err := sockRequest("POST", endpoint, nil)
   520  	c.Assert(status, checker.Equals, http.StatusOK)
   521  	c.Assert(err, checker.IsNil)
   522  
   523  	dockerCmd(c, "stop", cID)
   524  
   525  	until := daemonUnixTime(c)
   526  	out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until)
   527  	c.Assert(out, checker.Contains, "resize", check.Commentf("Missing 'resize' log event"))
   528  }
   529  
   530  func (s *DockerSuite) TestEventsAttach(c *check.C) {
   531  	// TODO Windows CI: Figure out why this test fails intermittently (TP5).
   532  	testRequires(c, DaemonIsLinux)
   533  
   534  	out, _ := dockerCmd(c, "run", "-di", "busybox", "cat")
   535  	cID := strings.TrimSpace(out)
   536  	c.Assert(waitRun(cID), checker.IsNil)
   537  
   538  	cmd := exec.Command(dockerBinary, "attach", cID)
   539  	stdin, err := cmd.StdinPipe()
   540  	c.Assert(err, checker.IsNil)
   541  	defer stdin.Close()
   542  	stdout, err := cmd.StdoutPipe()
   543  	c.Assert(err, checker.IsNil)
   544  	defer stdout.Close()
   545  	c.Assert(cmd.Start(), checker.IsNil)
   546  	defer cmd.Process.Kill()
   547  
   548  	// Make sure we're done attaching by writing/reading some stuff
   549  	_, err = stdin.Write([]byte("hello\n"))
   550  	c.Assert(err, checker.IsNil)
   551  	out, err = bufio.NewReader(stdout).ReadString('\n')
   552  	c.Assert(err, checker.IsNil)
   553  	c.Assert(strings.TrimSpace(out), checker.Equals, "hello", check.Commentf("expected 'hello'"))
   554  
   555  	c.Assert(stdin.Close(), checker.IsNil)
   556  
   557  	dockerCmd(c, "kill", cID)
   558  	c.Assert(waitExited(cID, 5*time.Second), checker.IsNil)
   559  
   560  	until := daemonUnixTime(c)
   561  	out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until)
   562  	c.Assert(out, checker.Contains, "attach", check.Commentf("Missing 'attach' log event"))
   563  }
   564  
   565  func (s *DockerSuite) TestEventsRename(c *check.C) {
   566  	out, _ := dockerCmd(c, "run", "--name", "oldName", "busybox", "true")
   567  	cID := strings.TrimSpace(out)
   568  	dockerCmd(c, "rename", "oldName", "newName")
   569  
   570  	until := daemonUnixTime(c)
   571  	// filter by the container id because the name in the event will be the new name.
   572  	out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until", until)
   573  	c.Assert(out, checker.Contains, "rename", check.Commentf("Missing 'rename' log event\n"))
   574  }
   575  
   576  func (s *DockerSuite) TestEventsTop(c *check.C) {
   577  	// Problematic on Windows as Windows does not support top
   578  	testRequires(c, DaemonIsLinux)
   579  
   580  	out, _ := runSleepingContainer(c, "-d")
   581  	cID := strings.TrimSpace(out)
   582  	c.Assert(waitRun(cID), checker.IsNil)
   583  
   584  	dockerCmd(c, "top", cID)
   585  	dockerCmd(c, "stop", cID)
   586  
   587  	until := daemonUnixTime(c)
   588  	out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until)
   589  	c.Assert(out, checker.Contains, " top", check.Commentf("Missing 'top' log event"))
   590  }
   591  
   592  // #14316
   593  func (s *DockerRegistrySuite) TestEventsImageFilterPush(c *check.C) {
   594  	// Problematic to port for Windows CI during TP5 timeframe until
   595  	// supporting push
   596  	testRequires(c, DaemonIsLinux)
   597  	testRequires(c, Network)
   598  	repoName := fmt.Sprintf("%v/dockercli/testf", privateRegistryURL)
   599  
   600  	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
   601  	cID := strings.TrimSpace(out)
   602  	c.Assert(waitRun(cID), checker.IsNil)
   603  
   604  	dockerCmd(c, "commit", cID, repoName)
   605  	dockerCmd(c, "stop", cID)
   606  	dockerCmd(c, "push", repoName)
   607  
   608  	until := daemonUnixTime(c)
   609  	out, _ = dockerCmd(c, "events", "-f", "image="+repoName, "-f", "event=push", "--until", until)
   610  	c.Assert(out, checker.Contains, repoName, check.Commentf("Missing 'push' log event for %s", repoName))
   611  }
   612  
   613  func (s *DockerSuite) TestEventsFilterType(c *check.C) {
   614  	since := daemonUnixTime(c)
   615  	name := "labelfiltertest"
   616  	label := "io.docker.testing=image"
   617  
   618  	// Build a test image.
   619  	_, err := buildImage(name, fmt.Sprintf(`
   620  		FROM busybox:latest
   621  		LABEL %s`, label), true)
   622  	c.Assert(err, checker.IsNil, check.Commentf("Couldn't create image"))
   623  
   624  	dockerCmd(c, "tag", name, "labelfiltertest:tag1")
   625  	dockerCmd(c, "tag", name, "labelfiltertest:tag2")
   626  	dockerCmd(c, "tag", "busybox:latest", "labelfiltertest:tag3")
   627  
   628  	out, _ := dockerCmd(
   629  		c,
   630  		"events",
   631  		"--since", since,
   632  		"--until", daemonUnixTime(c),
   633  		"--filter", fmt.Sprintf("label=%s", label),
   634  		"--filter", "type=image")
   635  
   636  	events := strings.Split(strings.TrimSpace(out), "\n")
   637  
   638  	// 2 events from the "docker tag" command, another one is from "docker build"
   639  	c.Assert(events, checker.HasLen, 3, check.Commentf("Events == %s", events))
   640  	for _, e := range events {
   641  		c.Assert(e, checker.Contains, "labelfiltertest")
   642  	}
   643  
   644  	out, _ = dockerCmd(
   645  		c,
   646  		"events",
   647  		"--since", since,
   648  		"--until", daemonUnixTime(c),
   649  		"--filter", fmt.Sprintf("label=%s", label),
   650  		"--filter", "type=container")
   651  	events = strings.Split(strings.TrimSpace(out), "\n")
   652  
   653  	// Events generated by the container that builds the image
   654  	c.Assert(events, checker.HasLen, 3, check.Commentf("Events == %s", events))
   655  
   656  	out, _ = dockerCmd(
   657  		c,
   658  		"events",
   659  		"--since", since,
   660  		"--until", daemonUnixTime(c),
   661  		"--filter", "type=network")
   662  	events = strings.Split(strings.TrimSpace(out), "\n")
   663  	c.Assert(len(events), checker.GreaterOrEqualThan, 1, check.Commentf("Events == %s", events))
   664  }
   665  
   666  func (s *DockerSuite) TestEventsFilterImageInContainerAction(c *check.C) {
   667  	since := daemonUnixTime(c)
   668  	dockerCmd(c, "run", "--name", "test-container", "-d", "busybox", "true")
   669  	waitRun("test-container")
   670  
   671  	out, _ := dockerCmd(c, "events", "--filter", "image=busybox", "--since", since, "--until", daemonUnixTime(c))
   672  	events := strings.Split(strings.TrimSpace(out), "\n")
   673  	c.Assert(len(events), checker.GreaterThan, 1, check.Commentf(out))
   674  }
   675  
   676  func (s *DockerSuite) TestEventsContainerRestart(c *check.C) {
   677  	dockerCmd(c, "run", "-d", "--name=testEvent", "--restart=on-failure:3", "busybox", "false")
   678  
   679  	// wait until test2 is auto removed.
   680  	waitTime := 10 * time.Second
   681  	if daemonPlatform == "windows" {
   682  		// nslookup isn't present in Windows busybox. Is built-in.
   683  		waitTime = 90 * time.Second
   684  	}
   685  
   686  	err := waitInspect("testEvent", "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTime)
   687  	c.Assert(err, checker.IsNil)
   688  
   689  	var (
   690  		createCount int
   691  		startCount  int
   692  		dieCount    int
   693  	)
   694  	out, _ := dockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c), "-f", "container=testEvent")
   695  	events := strings.Split(strings.TrimSpace(out), "\n")
   696  
   697  	nEvents := len(events)
   698  	c.Assert(nEvents, checker.GreaterOrEqualThan, 1) //Missing expected event
   699  	actions := eventActionsByIDAndType(c, events, "testEvent", "container")
   700  
   701  	for _, a := range actions {
   702  		switch a {
   703  		case "create":
   704  			createCount++
   705  		case "start":
   706  			startCount++
   707  		case "die":
   708  			dieCount++
   709  		}
   710  	}
   711  	c.Assert(createCount, checker.Equals, 1, check.Commentf("testEvent should be created 1 times: %v", actions))
   712  	c.Assert(startCount, checker.Equals, 4, check.Commentf("testEvent should start 4 times: %v", actions))
   713  	c.Assert(dieCount, checker.Equals, 4, check.Commentf("testEvent should die 4 times: %v", actions))
   714  }
   715  
   716  func (s *DockerSuite) TestEventsSinceInTheFuture(c *check.C) {
   717  	dockerCmd(c, "run", "--name", "test-container", "-d", "busybox", "true")
   718  	waitRun("test-container")
   719  
   720  	since := daemonTime(c)
   721  	until := since.Add(time.Duration(-24) * time.Hour)
   722  	out, _, err := dockerCmdWithError("events", "--filter", "image=busybox", "--since", parseEventTime(since), "--until", parseEventTime(until))
   723  
   724  	c.Assert(err, checker.NotNil)
   725  	c.Assert(out, checker.Contains, "cannot be after `until`")
   726  }
   727  
   728  func (s *DockerSuite) TestEventsUntilInThePast(c *check.C) {
   729  	since := daemonUnixTime(c)
   730  
   731  	dockerCmd(c, "run", "--name", "test-container", "-d", "busybox", "true")
   732  	waitRun("test-container")
   733  
   734  	until := daemonUnixTime(c)
   735  
   736  	dockerCmd(c, "run", "--name", "test-container2", "-d", "busybox", "true")
   737  	waitRun("test-container2")
   738  
   739  	out, _ := dockerCmd(c, "events", "--filter", "image=busybox", "--since", since, "--until", until)
   740  
   741  	c.Assert(out, checker.Not(checker.Contains), "test-container2")
   742  	c.Assert(out, checker.Contains, "test-container")
   743  }