2014-06-05 06:32:19 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-05 09:01:52 -04:00
|
|
|
"bufio"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-09-02 03:40:06 -04:00
|
|
|
"encoding/json"
|
2014-07-07 11:47:06 -04:00
|
|
|
"fmt"
|
2016-09-02 03:40:06 -04:00
|
|
|
"io"
|
2015-05-14 16:44:29 -04:00
|
|
|
"os"
|
2014-06-05 06:32:19 -04:00
|
|
|
"os/exec"
|
2019-01-13 18:29:13 -05:00
|
|
|
"strconv"
|
2014-06-05 06:32:19 -04:00
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2014-07-07 11:47:06 -04:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2017-05-23 23:56:26 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-09-02 03:40:06 -04:00
|
|
|
eventtypes "github.com/docker/docker/api/types/events"
|
2017-05-23 23:56:26 -04:00
|
|
|
"github.com/docker/docker/client"
|
2016-09-12 03:34:19 -04:00
|
|
|
eventstestutils "github.com/docker/docker/daemon/events/testutils"
|
2017-04-11 15:18:30 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2017-03-23 13:35:22 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/icmd"
|
2014-06-05 06:32:19 -04:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsTimestampFormats(c *testing.T) {
|
2016-04-04 13:13:10 -04:00
|
|
|
name := "events-time-format-test"
|
2015-05-13 12:27:00 -04:00
|
|
|
|
|
|
|
// Start stopwatch, generate an event
|
|
|
|
start := daemonTime(c)
|
2016-04-04 13:13:10 -04:00
|
|
|
time.Sleep(1100 * time.Millisecond) // so that first event occur in different second from since (just for the case)
|
|
|
|
dockerCmd(c, "run", "--rm", "--name", name, "busybox", "true")
|
|
|
|
time.Sleep(1100 * time.Millisecond) // so that until > since
|
2015-05-13 12:27:00 -04:00
|
|
|
end := daemonTime(c)
|
|
|
|
|
|
|
|
// List of available time formats to --since
|
|
|
|
unixTs := func(t time.Time) string { return fmt.Sprintf("%v", t.Unix()) }
|
|
|
|
rfc3339 := func(t time.Time) string { return t.Format(time.RFC3339) }
|
2017-09-06 04:54:24 -04:00
|
|
|
duration := func(t time.Time) string { return time.Since(t).String() }
|
2015-05-13 12:27:00 -04:00
|
|
|
|
|
|
|
// --since=$start must contain only the 'untag' event
|
2015-05-12 15:59:34 -04:00
|
|
|
for _, f := range []func(time.Time) string{unixTs, rfc3339, duration} {
|
2015-05-13 12:27:00 -04:00
|
|
|
since, until := f(start), f(end)
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "events", "--since="+since, "--until="+until)
|
2016-04-04 13:13:10 -04:00
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
|
|
|
|
nEvents := len(events)
|
2019-11-27 09:36:45 -05:00
|
|
|
assert.Assert(c, nEvents >= 5)
|
2016-04-04 13:13:10 -04:00
|
|
|
containerEvents := eventActionsByIDAndType(c, events, name, "container")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.DeepEqual(containerEvents, []string{"create", "attach", "start", "die", "destroy"}), out)
|
2015-05-13 12:27:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsUntag(c *testing.T) {
|
2014-10-23 17:30:11 -04:00
|
|
|
image := "busybox"
|
2015-04-18 12:46:47 -04:00
|
|
|
dockerCmd(c, "tag", image, "utest:tag1")
|
|
|
|
dockerCmd(c, "tag", image, "utest:tag2")
|
|
|
|
dockerCmd(c, "rmi", "utest:tag1")
|
|
|
|
dockerCmd(c, "rmi", "utest:tag2")
|
2016-08-04 12:57:34 -04:00
|
|
|
|
|
|
|
result := icmd.RunCmd(icmd.Cmd{
|
|
|
|
Command: []string{dockerBinary, "events", "--since=1"},
|
|
|
|
Timeout: time.Millisecond * 2500,
|
|
|
|
})
|
2017-08-23 17:01:29 -04:00
|
|
|
result.Assert(c, icmd.Expected{Timeout: true})
|
2016-08-04 12:57:34 -04:00
|
|
|
|
|
|
|
events := strings.Split(result.Stdout(), "\n")
|
2014-10-06 10:26:55 -04:00
|
|
|
nEvents := len(events)
|
2014-06-05 06:32:19 -04:00
|
|
|
// The last element after the split above will be an empty string, so we
|
|
|
|
// get the two elements before the last, which are the untags we're
|
|
|
|
// looking for.
|
2014-10-06 10:26:55 -04:00
|
|
|
for _, v := range events[nEvents-3 : nEvents-1] {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Check(c, strings.Contains(v, "untag"), "event should be untag")
|
2014-06-05 06:32:19 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-07 11:47:06 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsContainerEvents(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
dockerCmd(c, "run", "--rm", "--name", "container-events-test", "busybox", "true")
|
2015-12-21 17:55:23 -05:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ := dockerCmd(c, "events", "--until", daemonUnixTime(c))
|
2014-08-11 01:54:59 -04:00
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
containerEvents := eventActionsByIDAndType(c, events, "container-events-test", "container")
|
integration-cli: prevent out of range in TestEventsContainerEvents
Prevents the test from panicking;
```
--- FAIL: TestDockerSuite/TestEventsContainerEvents (1.19s)
suite.go:65: test suite panicked: runtime error: slice bounds out of range [:5] with capacity 4
goroutine 3978 [running]:
runtime/debug.Stack(0xc0026e3908, 0x1ad9bc0, 0xc0008100c0)
/usr/local/go/src/runtime/debug/stack.go:24 +0x9d
github.com/docker/docker/internal/test/suite.failOnPanic(0xc00185e600)
/go/src/github.com/docker/docker/internal/test/suite/suite.go:65 +0x57
panic(0x1ad9bc0, 0xc0008100c0)
/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/docker/docker/integration-cli.(*DockerSuite).TestEventsContainerEvents(0x2f7d7a8, 0xc00185e600)
/go/src/github.com/docker/docker/integration-cli/docker_cli_events_test.go:89 +0x3c5
reflect.Value.call(0xc0000c4f00, 0xc0008036c0, 0x13, 0x1bfd18b, 0x4, 0xc000e8df30, 0x2, 0x2, 0xc00075c618, 0x40d903, ...)
/usr/local/go/src/reflect/value.go:460 +0x5f6
reflect.Value.Call(0xc0000c4f00, 0xc0008036c0, 0x13, 0xc00075c730, 0x2, 0x2, 0xf, 0x0, 0x0)
/usr/local/go/src/reflect/value.go:321 +0xb4
github.com/docker/docker/internal/test/suite.Run.func2(0xc00185e600)
/go/src/github.com/docker/docker/internal/test/suite/suite.go:57 +0x2c2
testing.tRunner(0xc00185e600, 0xc0008dbea0)
/usr/local/go/src/testing/testing.go:909 +0xc9
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:960 +0x350
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-10-01 14:15:35 -04:00
|
|
|
if len(containerEvents) > 5 {
|
|
|
|
containerEvents = containerEvents[:5]
|
|
|
|
}
|
|
|
|
assert.Assert(c, is.DeepEqual(containerEvents, []string{"create", "attach", "start", "die", "destroy"}), out)
|
2014-08-11 01:54:59 -04:00
|
|
|
}
|
2014-08-11 03:39:47 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsContainerEventsAttrSort(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
|
|
|
dockerCmd(c, "run", "--rm", "--name", "container-events-test", "busybox", "true")
|
2016-01-26 21:48:14 -05:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ := dockerCmd(c, "events", "--filter", "container=container-events-test", "--since", since, "--until", daemonUnixTime(c))
|
2016-01-26 21:48:14 -05:00
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
|
|
|
|
nEvents := len(events)
|
2019-11-27 09:36:45 -05:00
|
|
|
assert.Assert(c, nEvents >= 3)
|
2016-01-26 21:48:14 -05:00
|
|
|
matchedEvents := 0
|
|
|
|
for _, event := range events {
|
2016-03-07 19:02:35 -05:00
|
|
|
matches := eventstestutils.ScanMap(event)
|
2016-01-26 21:48:14 -05:00
|
|
|
if matches["eventType"] == "container" && matches["action"] == "create" {
|
|
|
|
matchedEvents++
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Check(c, strings.Contains(out, "(image=busybox, name=container-events-test)"), "Event attributes not sorted")
|
|
|
|
|
2016-01-26 21:48:14 -05:00
|
|
|
} else if matches["eventType"] == "container" && matches["action"] == "start" {
|
|
|
|
matchedEvents++
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Check(c, strings.Contains(out, "(image=busybox, name=container-events-test)"), "Event attributes not sorted")
|
2016-01-26 21:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, matchedEvents, 2, "missing events for container container-events-test:\n%s", out)
|
2016-01-26 21:48:14 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsContainerEventsSinceUnixEpoch(c *testing.T) {
|
2015-12-21 17:55:23 -05:00
|
|
|
dockerCmd(c, "run", "--rm", "--name", "since-epoch-test", "busybox", "true")
|
2015-03-22 15:40:16 -04:00
|
|
|
timeBeginning := time.Unix(0, 0).Format(time.RFC3339Nano)
|
2022-05-09 07:26:05 -04:00
|
|
|
timeBeginning = strings.ReplaceAll(timeBeginning, "Z", ".000000000Z")
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ := dockerCmd(c, "events", "--since", timeBeginning, "--until", daemonUnixTime(c))
|
2015-03-22 15:40:16 -04:00
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
nEvents := len(events)
|
2019-11-27 09:36:45 -05:00
|
|
|
assert.Assert(c, nEvents >= 5)
|
2015-12-21 17:55:23 -05:00
|
|
|
containerEvents := eventActionsByIDAndType(c, events, "since-epoch-test", "container")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.DeepEqual(containerEvents, []string{"create", "attach", "start", "die", "destroy"}), out)
|
2015-03-22 15:40:16 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsImageTag(c *testing.T) {
|
2015-08-11 03:41:11 -04:00
|
|
|
time.Sleep(1 * time.Second) // because API has seconds granularity
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
2015-05-14 20:01:06 -04:00
|
|
|
image := "testimageevents:tag"
|
|
|
|
dockerCmd(c, "tag", "busybox", image)
|
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "events",
|
2016-04-11 14:52:34 -04:00
|
|
|
"--since", since, "--until", daemonUnixTime(c))
|
2015-05-14 20:01:06 -04:00
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(events), 1, "was expecting 1 event. out=%s", out)
|
2015-05-14 20:01:06 -04:00
|
|
|
event := strings.TrimSpace(events[0])
|
|
|
|
|
2016-03-07 19:02:35 -05:00
|
|
|
matches := eventstestutils.ScanMap(event)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, matchEventID(matches, image), "matches: %v\nout:\n%s", matches, out)
|
|
|
|
assert.Equal(c, matches["action"], "tag")
|
2015-05-14 20:01:06 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsImagePull(c *testing.T) {
|
2016-01-26 18:14:58 -05:00
|
|
|
// TODO Windows: Enable this test once pull and reliable image names are available
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
2015-04-18 12:46:47 -04:00
|
|
|
testRequires(c, Network)
|
2015-02-05 20:46:55 -05:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "pull", "hello-world")
|
2014-10-22 09:48:02 -04:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "events",
|
2016-04-11 14:52:34 -04:00
|
|
|
"--since", since, "--until", daemonUnixTime(c))
|
2014-10-22 09:48:02 -04:00
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
event := strings.TrimSpace(events[len(events)-1])
|
2016-03-07 19:02:35 -05:00
|
|
|
matches := eventstestutils.ScanMap(event)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, matches["id"], "hello-world:latest")
|
|
|
|
assert.Equal(c, matches["action"], "pull")
|
2014-10-22 09:48:02 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsImageImport(c *testing.T) {
|
2016-01-26 18:14:58 -05:00
|
|
|
// TODO Windows CI. This should be portable once export/import are
|
|
|
|
// more reliable (@swernli)
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-10-22 09:48:02 -04:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2015-02-14 05:27:06 -05:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
2017-08-22 17:07:52 -04:00
|
|
|
out, err := RunCommandPipelineWithOutput(
|
2015-02-14 05:27:06 -05:00
|
|
|
exec.Command(dockerBinary, "export", cleanedContainerID),
|
|
|
|
exec.Command(dockerBinary, "import", "-"),
|
|
|
|
)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "import failed with output: %q", out)
|
2015-12-21 15:34:08 -05:00
|
|
|
imageRef := strings.TrimSpace(out)
|
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=import")
|
2015-12-21 17:55:23 -05:00
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(events), 1)
|
2016-03-07 19:02:35 -05:00
|
|
|
matches := eventstestutils.ScanMap(events[0])
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, matches["id"], imageRef, "matches: %v\nout:\n%s\n", matches, out)
|
|
|
|
assert.Equal(c, matches["action"], "import", "matches: %v\nout:\n%s\n", matches, out)
|
2016-04-19 00:45:59 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsImageLoad(c *testing.T) {
|
2016-04-19 00:45:59 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
myImageName := "footest:v1"
|
|
|
|
dockerCmd(c, "tag", "busybox", myImageName)
|
|
|
|
since := daemonUnixTime(c)
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "images", "-q", "--no-trunc", myImageName)
|
|
|
|
longImageID := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, longImageID != "", "Id should not be empty")
|
2016-04-19 00:45:59 -04:00
|
|
|
|
|
|
|
dockerCmd(c, "save", "-o", "saveimg.tar", myImageName)
|
|
|
|
dockerCmd(c, "rmi", myImageName)
|
|
|
|
out, _ = dockerCmd(c, "images", "-q", myImageName)
|
|
|
|
noImageID := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, noImageID, "", "Should not have any image")
|
2016-04-19 00:45:59 -04:00
|
|
|
dockerCmd(c, "load", "-i", "saveimg.tar")
|
|
|
|
|
2016-08-04 12:57:34 -04:00
|
|
|
result := icmd.RunCommand("rm", "-rf", "saveimg.tar")
|
2017-08-23 17:01:29 -04:00
|
|
|
result.Assert(c, icmd.Success)
|
2016-04-19 00:45:59 -04:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "images", "-q", "--no-trunc", myImageName)
|
|
|
|
imageID := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, imageID, longImageID, "Should have same image id as before")
|
2016-04-19 00:45:59 -04:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=load")
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(events), 1)
|
2016-04-19 00:45:59 -04:00
|
|
|
matches := eventstestutils.ScanMap(events[0])
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, matches["id"], imageID, "matches: %v\nout:\n%s\n", matches, out)
|
|
|
|
assert.Equal(c, matches["action"], "load", "matches: %v\nout:\n%s\n", matches, out)
|
2016-04-19 00:45:59 -04:00
|
|
|
|
|
|
|
out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=save")
|
|
|
|
events = strings.Split(strings.TrimSpace(out), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(events), 1)
|
2016-04-19 00:45:59 -04:00
|
|
|
matches = eventstestutils.ScanMap(events[0])
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, matches["id"], imageID, "matches: %v\nout:\n%s\n", matches, out)
|
|
|
|
assert.Equal(c, matches["action"], "save", "matches: %v\nout:\n%s\n", matches, out)
|
2014-10-22 09:48:02 -04:00
|
|
|
}
|
2014-11-20 14:46:48 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsPluginOps(c *testing.T) {
|
2016-12-12 15:46:36 -05:00
|
|
|
testRequires(c, DaemonIsLinux, IsAmd64, Network)
|
2016-07-18 11:02:12 -04:00
|
|
|
|
|
|
|
since := daemonUnixTime(c)
|
|
|
|
|
2016-11-10 16:07:53 -05:00
|
|
|
dockerCmd(c, "plugin", "install", pNameWithTag, "--grant-all-permissions")
|
|
|
|
dockerCmd(c, "plugin", "disable", pNameWithTag)
|
|
|
|
dockerCmd(c, "plugin", "remove", pNameWithTag)
|
2016-07-18 11:02:12 -04:00
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c))
|
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(events) >= 4)
|
2016-07-18 11:02:12 -04:00
|
|
|
|
2016-11-10 16:07:53 -05:00
|
|
|
pluginEvents := eventActionsByIDAndType(c, events, pNameWithTag, "plugin")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.DeepEqual(pluginEvents, []string{"pull", "enable", "disable", "remove"}), out)
|
2016-07-18 11:02:12 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFilters(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "run", "--rm", "busybox", "true")
|
|
|
|
dockerCmd(c, "run", "--rm", "busybox", "true")
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ := dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=die")
|
2015-12-21 17:55:23 -05:00
|
|
|
parseEvents(c, out, "die")
|
2014-11-20 14:46:48 -05:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", "event=die", "--filter", "event=start")
|
2015-12-21 17:55:23 -05:00
|
|
|
parseEvents(c, out, "die|start")
|
2015-02-24 19:24:19 -05:00
|
|
|
|
|
|
|
// make sure we at least got 2 start events
|
|
|
|
count := strings.Count(out, "start")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, count >= 2, "should have had 2 start events but had %d, out: %s", count, out)
|
2014-11-20 14:46:48 -05:00
|
|
|
}
|
2015-02-09 16:02:53 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFilterImageName(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
2015-02-09 16:02:53 -05:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "--name", "container_1", "-d", "busybox:latest", "true")
|
2015-04-06 09:21:18 -04:00
|
|
|
container1 := strings.TrimSpace(out)
|
2015-02-09 01:03:42 -05:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ = dockerCmd(c, "run", "--name", "container_2", "-d", "busybox", "true")
|
2015-04-06 09:21:18 -04:00
|
|
|
container2 := strings.TrimSpace(out)
|
2015-02-09 16:02:53 -05:00
|
|
|
|
2015-04-18 12:46:47 -04:00
|
|
|
name := "busybox"
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ = dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--filter", fmt.Sprintf("image=%s", name))
|
2015-04-01 10:08:00 -04:00
|
|
|
events := strings.Split(out, "\n")
|
|
|
|
events = events[:len(events)-1]
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(events) != 0, "Expected events but found none for the image busybox:latest")
|
2015-04-01 10:08:00 -04:00
|
|
|
count1 := 0
|
|
|
|
count2 := 0
|
|
|
|
|
|
|
|
for _, e := range events {
|
|
|
|
if strings.Contains(e, container1) {
|
|
|
|
count1++
|
|
|
|
} else if strings.Contains(e, container2) {
|
|
|
|
count2++
|
2015-02-09 16:02:53 -05:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, count1 != 0, "Expected event from container but got %d from %s", count1, container1)
|
|
|
|
assert.Assert(c, count2 != 0, "Expected event from container but got %d from %s", count2, container2)
|
2015-02-09 01:03:42 -05:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFilterLabels(c *testing.T) {
|
2019-01-13 18:29:13 -05:00
|
|
|
since := strconv.FormatUint(uint64(daemonTime(c).Unix()), 10)
|
2015-09-23 14:51:43 -04:00
|
|
|
label := "io.docker.testing=foo"
|
|
|
|
|
2019-01-13 18:29:13 -05:00
|
|
|
out, exit := dockerCmd(c, "create", "-l", label, "busybox")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, exit, 0)
|
2015-09-23 14:51:43 -04:00
|
|
|
container1 := strings.TrimSpace(out)
|
|
|
|
|
2019-01-13 18:29:13 -05:00
|
|
|
out, exit = dockerCmd(c, "create", "busybox")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, exit, 0)
|
2015-09-23 14:51:43 -04:00
|
|
|
container2 := strings.TrimSpace(out)
|
|
|
|
|
2019-01-13 18:29:13 -05:00
|
|
|
// fetch events with `--until`, so that the client detaches after a second
|
|
|
|
// instead of staying attached, waiting for more events to arrive.
|
2015-09-23 14:51:43 -04:00
|
|
|
out, _ = dockerCmd(
|
|
|
|
c,
|
|
|
|
"events",
|
2016-04-11 14:52:34 -04:00
|
|
|
"--since", since,
|
2019-01-13 18:29:13 -05:00
|
|
|
"--until", strconv.FormatUint(uint64(daemonTime(c).Add(time.Second).Unix()), 10),
|
|
|
|
"--filter", "label="+label,
|
|
|
|
)
|
2015-09-23 14:51:43 -04:00
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(events) > 0)
|
2015-09-23 14:51:43 -04:00
|
|
|
|
2019-01-13 18:29:13 -05:00
|
|
|
var found bool
|
2015-09-23 14:51:43 -04:00
|
|
|
for _, e := range events {
|
2019-01-13 18:29:13 -05:00
|
|
|
if strings.Contains(e, container1) {
|
|
|
|
found = true
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, !strings.Contains(e, container2))
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, found)
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFilterImageLabels(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
2015-10-16 11:54:05 -04:00
|
|
|
name := "labelfiltertest"
|
2015-09-23 14:51:43 -04:00
|
|
|
label := "io.docker.testing=image"
|
|
|
|
|
|
|
|
// Build a test image.
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile(fmt.Sprintf(`
|
2015-09-23 14:51:43 -04:00
|
|
|
FROM busybox:latest
|
2017-01-16 05:30:14 -05:00
|
|
|
LABEL %s`, label)))
|
2015-09-23 14:51:43 -04:00
|
|
|
dockerCmd(c, "tag", name, "labelfiltertest:tag1")
|
|
|
|
dockerCmd(c, "tag", name, "labelfiltertest:tag2")
|
|
|
|
dockerCmd(c, "tag", "busybox:latest", "labelfiltertest:tag3")
|
|
|
|
|
|
|
|
out, _ := dockerCmd(
|
|
|
|
c,
|
|
|
|
"events",
|
2016-04-11 14:52:34 -04:00
|
|
|
"--since", since,
|
|
|
|
"--until", daemonUnixTime(c),
|
2015-12-28 15:15:34 -05:00
|
|
|
"--filter", fmt.Sprintf("label=%s", label),
|
|
|
|
"--filter", "type=image")
|
2015-09-23 14:51:43 -04:00
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
2015-10-16 11:54:05 -04:00
|
|
|
|
|
|
|
// 2 events from the "docker tag" command, another one is from "docker build"
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(events), 3, "Events == %s", events)
|
2015-09-23 14:51:43 -04:00
|
|
|
for _, e := range events {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Check(c, strings.Contains(e, "labelfiltertest"))
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFilterContainer(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
2015-04-06 13:18:38 -04:00
|
|
|
nameID := make(map[string]string)
|
2015-02-09 01:03:42 -05:00
|
|
|
|
2015-04-06 13:18:38 -04:00
|
|
|
for _, name := range []string{"container_1", "container_2"} {
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "run", "--name", name, "busybox", "true")
|
2016-01-28 09:19:25 -05:00
|
|
|
id := inspectField(c, name, "Id")
|
2015-04-23 14:43:08 -04:00
|
|
|
nameID[name] = id
|
2015-02-09 01:03:42 -05:00
|
|
|
}
|
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
until := daemonUnixTime(c)
|
2015-02-09 01:03:42 -05:00
|
|
|
|
2015-04-06 13:18:38 -04:00
|
|
|
checkEvents := func(id string, events []string) error {
|
2015-05-24 14:19:39 -04:00
|
|
|
if len(events) != 4 { // create, attach, start, die
|
2015-09-23 14:51:43 -04:00
|
|
|
return fmt.Errorf("expected 4 events, got %v", events)
|
2015-04-06 13:18:38 -04:00
|
|
|
}
|
|
|
|
for _, event := range events {
|
2016-03-07 19:02:35 -05:00
|
|
|
matches := eventstestutils.ScanMap(event)
|
2015-12-21 17:55:23 -05:00
|
|
|
if !matchEventID(matches, id) {
|
|
|
|
return fmt.Errorf("expected event for container id %s: %s - parsed container id: %s", id, event, matches["id"])
|
2015-04-06 13:18:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2015-02-09 01:03:42 -05:00
|
|
|
}
|
|
|
|
|
2015-04-06 13:18:38 -04:00
|
|
|
for name, ID := range nameID {
|
|
|
|
// filter by names
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "events", "--since", since, "--until", until, "--filter", "container="+name)
|
2015-04-06 13:18:38 -04:00
|
|
|
events := strings.Split(strings.TrimSuffix(out, "\n"), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, checkEvents(ID, events))
|
2015-03-30 23:26:27 -04:00
|
|
|
|
2015-04-06 13:18:38 -04:00
|
|
|
// filter by ID's
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ = dockerCmd(c, "events", "--since", since, "--until", until, "--filter", "container="+ID)
|
2015-04-06 13:18:38 -04:00
|
|
|
events = strings.Split(strings.TrimSuffix(out, "\n"), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, checkEvents(ID, events))
|
2015-03-26 15:43:00 -04:00
|
|
|
}
|
|
|
|
}
|
2015-04-05 09:01:52 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsCommit(c *testing.T) {
|
2016-01-26 18:14:58 -05:00
|
|
|
// Problematic on Windows as cannot commit a running container
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c)
|
2015-05-24 14:19:39 -04:00
|
|
|
cID := strings.TrimSpace(out)
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.WaitRun(c, cID)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, "commit", "-m", "test", cID)
|
|
|
|
cli.DockerCmd(c, "stop", cID)
|
|
|
|
cli.WaitExited(c, cID, 5*time.Second)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
until := daemonUnixTime(c)
|
2017-04-11 15:18:30 -04:00
|
|
|
out = cli.DockerCmd(c, "events", "-f", "container="+cID, "--until="+until).Combined()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "commit"), "Missing 'commit' log event")
|
2015-05-24 14:19:39 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsCopy(c *testing.T) {
|
2015-05-14 16:44:29 -04:00
|
|
|
// Build a test image.
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, "cpimg", build.WithDockerfile(`
|
2015-05-24 14:19:39 -04:00
|
|
|
FROM busybox
|
2017-01-16 05:30:14 -05:00
|
|
|
RUN echo HI > /file`))
|
|
|
|
id := getIDByName(c, "cpimg")
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2015-05-14 16:44:29 -04:00
|
|
|
// Create an empty test file.
|
2021-08-24 06:10:50 -04:00
|
|
|
tempFile, err := os.CreateTemp("", "test-events-copy-")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-05-14 16:44:29 -04:00
|
|
|
defer os.Remove(tempFile.Name())
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, tempFile.Close())
|
2015-05-14 16:44:29 -04:00
|
|
|
|
|
|
|
dockerCmd(c, "create", "--name=cptest", id)
|
|
|
|
|
2016-01-26 18:14:58 -05:00
|
|
|
dockerCmd(c, "cp", "cptest:/file", tempFile.Name())
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
until := daemonUnixTime(c)
|
|
|
|
out, _ := dockerCmd(c, "events", "--since=0", "-f", "container=cptest", "--until="+until)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "archive-path"), "Missing 'archive-path' log event")
|
2015-05-14 16:44:29 -04:00
|
|
|
|
2016-01-26 18:14:58 -05:00
|
|
|
dockerCmd(c, "cp", tempFile.Name(), "cptest:/filecopy")
|
2015-05-14 16:44:29 -04:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
until = daemonUnixTime(c)
|
|
|
|
out, _ = dockerCmd(c, "events", "-f", "container=cptest", "--until="+until)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "extract-to-dir"), "Missing 'extract-to-dir' log event")
|
2015-05-24 14:19:39 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsResize(c *testing.T) {
|
2021-03-18 11:55:11 -04:00
|
|
|
out := runSleepingContainer(c, "-d", "-t")
|
2015-05-24 14:19:39 -04:00
|
|
|
cID := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(cID))
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2019-01-03 16:49:00 -05:00
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2017-05-23 23:56:26 -04:00
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
options := types.ResizeOptions{
|
|
|
|
Height: 80,
|
|
|
|
Width: 24,
|
|
|
|
}
|
|
|
|
err = cli.ContainerResize(context.Background(), cID, options)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "stop", cID)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
until := daemonUnixTime(c)
|
|
|
|
out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "resize"), "Missing 'resize' log event")
|
2015-05-24 14:19:39 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsAttach(c *testing.T) {
|
2016-04-06 17:01:12 -04:00
|
|
|
// TODO Windows CI: Figure out why this test fails intermittently (TP5).
|
2016-01-31 18:51:56 -05:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
out := cli.DockerCmd(c, "run", "-di", "busybox", "cat").Combined()
|
2015-05-24 14:19:39 -04:00
|
|
|
cID := strings.TrimSpace(out)
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.WaitRun(c, cID)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
|
|
|
cmd := exec.Command(dockerBinary, "attach", cID)
|
|
|
|
stdin, err := cmd.StdinPipe()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-05-24 14:19:39 -04:00
|
|
|
defer stdin.Close()
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-05-24 14:19:39 -04:00
|
|
|
defer stdout.Close()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, cmd.Start())
|
2017-09-22 09:52:41 -04:00
|
|
|
defer func() {
|
|
|
|
cmd.Process.Kill()
|
|
|
|
cmd.Wait()
|
|
|
|
}()
|
2015-05-24 14:19:39 -04:00
|
|
|
|
|
|
|
// Make sure we're done attaching by writing/reading some stuff
|
2015-10-19 22:56:29 -04:00
|
|
|
_, err = stdin.Write([]byte("hello\n"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-05-24 14:19:39 -04:00
|
|
|
out, err = bufio.NewReader(stdout).ReadString('\n')
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, strings.TrimSpace(out), "hello")
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, stdin.Close())
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, "kill", cID)
|
|
|
|
cli.WaitExited(c, cID, 5*time.Second)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
until := daemonUnixTime(c)
|
2017-04-11 15:18:30 -04:00
|
|
|
out = cli.DockerCmd(c, "events", "-f", "container="+cID, "--until="+until).Combined()
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "attach"), "Missing 'attach' log event")
|
2015-05-24 14:19:39 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsRename(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "--name", "oldName", "busybox", "true")
|
|
|
|
cID := strings.TrimSpace(out)
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "rename", "oldName", "newName")
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
until := daemonUnixTime(c)
|
|
|
|
// filter by the container id because the name in the event will be the new name.
|
|
|
|
out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until", until)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "rename"), "Missing 'rename' log event")
|
2015-05-24 14:19:39 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsTop(c *testing.T) {
|
2016-01-26 18:14:58 -05:00
|
|
|
// Problematic on Windows as Windows does not support top
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2017-04-16 17:39:30 -04:00
|
|
|
out := runSleepingContainer(c, "-d")
|
2015-05-24 14:19:39 -04:00
|
|
|
cID := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(cID))
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
dockerCmd(c, "top", cID)
|
|
|
|
dockerCmd(c, "stop", cID)
|
2015-05-24 14:19:39 -04:00
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
until := daemonUnixTime(c)
|
|
|
|
out, _ = dockerCmd(c, "events", "-f", "container="+cID, "--until="+until)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, "top"), "Missing 'top' log event")
|
2015-05-24 14:19:39 -04:00
|
|
|
}
|
2015-06-04 22:47:46 -04:00
|
|
|
|
2015-07-03 17:26:49 -04:00
|
|
|
// #14316
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerRegistrySuite) TestEventsImageFilterPush(c *testing.T) {
|
2016-04-06 17:01:12 -04:00
|
|
|
// Problematic to port for Windows CI during TP5 timeframe until
|
|
|
|
// supporting push
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-03 17:26:49 -04:00
|
|
|
testRequires(c, Network)
|
|
|
|
repoName := fmt.Sprintf("%v/dockercli/testf", privateRegistryURL)
|
|
|
|
|
2015-07-20 02:55:40 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
|
2015-07-03 17:26:49 -04:00
|
|
|
cID := strings.TrimSpace(out)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, waitRun(cID))
|
2015-07-03 17:26:49 -04:00
|
|
|
|
|
|
|
dockerCmd(c, "commit", cID, repoName)
|
|
|
|
dockerCmd(c, "stop", cID)
|
|
|
|
dockerCmd(c, "push", repoName)
|
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
until := daemonUnixTime(c)
|
|
|
|
out, _ = dockerCmd(c, "events", "-f", "image="+repoName, "-f", "event=push", "--until", until)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, strings.Contains(out, repoName), "Missing 'push' log event for %s", repoName)
|
2015-07-03 17:26:49 -04:00
|
|
|
}
|
2015-12-28 15:15:34 -05:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFilterType(c *testing.T) {
|
2018-03-27 12:13:47 -04:00
|
|
|
// FIXME(vdemeester) fails on e2e run
|
2018-12-24 07:25:53 -05:00
|
|
|
testRequires(c, testEnv.IsLocalDaemon)
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
2015-12-28 15:15:34 -05:00
|
|
|
name := "labelfiltertest"
|
|
|
|
label := "io.docker.testing=image"
|
|
|
|
|
|
|
|
// Build a test image.
|
2017-03-23 13:35:22 -04:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile(fmt.Sprintf(`
|
2015-12-28 15:15:34 -05:00
|
|
|
FROM busybox:latest
|
2017-01-16 05:30:14 -05:00
|
|
|
LABEL %s`, label)))
|
2015-12-28 15:15:34 -05:00
|
|
|
dockerCmd(c, "tag", name, "labelfiltertest:tag1")
|
|
|
|
dockerCmd(c, "tag", name, "labelfiltertest:tag2")
|
|
|
|
dockerCmd(c, "tag", "busybox:latest", "labelfiltertest:tag3")
|
|
|
|
|
|
|
|
out, _ := dockerCmd(
|
|
|
|
c,
|
|
|
|
"events",
|
2016-04-11 14:52:34 -04:00
|
|
|
"--since", since,
|
|
|
|
"--until", daemonUnixTime(c),
|
2015-12-28 15:15:34 -05:00
|
|
|
"--filter", fmt.Sprintf("label=%s", label),
|
|
|
|
"--filter", "type=image")
|
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
|
|
|
|
// 2 events from the "docker tag" command, another one is from "docker build"
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(events), 3, "Events == %s", events)
|
2015-12-28 15:15:34 -05:00
|
|
|
for _, e := range events {
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Check(c, strings.Contains(e, "labelfiltertest"))
|
2015-12-28 15:15:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
out, _ = dockerCmd(
|
|
|
|
c,
|
|
|
|
"events",
|
2016-04-11 14:52:34 -04:00
|
|
|
"--since", since,
|
|
|
|
"--until", daemonUnixTime(c),
|
2015-12-28 15:15:34 -05:00
|
|
|
"--filter", fmt.Sprintf("label=%s", label),
|
|
|
|
"--filter", "type=container")
|
|
|
|
events = strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
|
|
|
|
// Events generated by the container that builds the image
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(events), 2, "Events == %s", events)
|
2015-12-28 15:15:34 -05:00
|
|
|
|
|
|
|
out, _ = dockerCmd(
|
|
|
|
c,
|
|
|
|
"events",
|
2016-04-11 14:52:34 -04:00
|
|
|
"--since", since,
|
|
|
|
"--until", daemonUnixTime(c),
|
2015-12-28 15:15:34 -05:00
|
|
|
"--filter", "type=network")
|
|
|
|
events = strings.Split(strings.TrimSpace(out), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(events) >= 1, "Events == %s", events)
|
2015-12-28 15:15:34 -05:00
|
|
|
}
|
|
|
|
|
2016-10-27 19:14:13 -04:00
|
|
|
// #25798
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsSpecialFiltersWithExecCreate(c *testing.T) {
|
2016-10-27 19:14:13 -04:00
|
|
|
since := daemonUnixTime(c)
|
|
|
|
runSleepingContainer(c, "--name", "test-container", "-d")
|
|
|
|
waitRun("test-container")
|
|
|
|
|
|
|
|
dockerCmd(c, "exec", "test-container", "echo", "hello-world")
|
|
|
|
|
|
|
|
out, _ := dockerCmd(
|
|
|
|
c,
|
|
|
|
"events",
|
|
|
|
"--since", since,
|
|
|
|
"--until", daemonUnixTime(c),
|
|
|
|
"--filter",
|
|
|
|
"event='exec_create: echo hello-world'",
|
|
|
|
)
|
|
|
|
|
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(events), 1, out)
|
2016-10-27 19:14:13 -04:00
|
|
|
|
|
|
|
out, _ = dockerCmd(
|
|
|
|
c,
|
|
|
|
"events",
|
|
|
|
"--since", since,
|
|
|
|
"--until", daemonUnixTime(c),
|
|
|
|
"--filter",
|
|
|
|
"event=exec_create",
|
|
|
|
)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, len(events), 1, out)
|
2016-10-27 19:14:13 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFilterImageInContainerAction(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
2015-12-28 15:15:34 -05:00
|
|
|
dockerCmd(c, "run", "--name", "test-container", "-d", "busybox", "true")
|
|
|
|
waitRun("test-container")
|
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ := dockerCmd(c, "events", "--filter", "image=busybox", "--since", since, "--until", daemonUnixTime(c))
|
2015-12-28 15:15:34 -05:00
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(events) > 1, out)
|
2015-12-28 15:15:34 -05:00
|
|
|
}
|
2016-04-06 23:38:12 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsContainerRestart(c *testing.T) {
|
2016-04-06 23:38:12 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name=testEvent", "--restart=on-failure:3", "busybox", "false")
|
|
|
|
|
|
|
|
// wait until test2 is auto removed.
|
|
|
|
waitTime := 10 * time.Second
|
2018-01-15 09:32:06 -05:00
|
|
|
if testEnv.OSType == "windows" {
|
2016-07-21 22:39:35 -04:00
|
|
|
// Windows takes longer...
|
2016-04-06 23:38:12 -04:00
|
|
|
waitTime = 90 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
err := waitInspect("testEvent", "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTime)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-04-06 23:38:12 -04:00
|
|
|
|
|
|
|
var (
|
|
|
|
createCount int
|
|
|
|
startCount int
|
|
|
|
dieCount int
|
|
|
|
)
|
2016-04-11 14:52:34 -04:00
|
|
|
out, _ := dockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c), "-f", "container=testEvent")
|
2016-04-06 23:38:12 -04:00
|
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
|
|
|
|
|
|
nEvents := len(events)
|
2019-11-27 09:36:45 -05:00
|
|
|
assert.Assert(c, nEvents >= 1)
|
2016-04-06 23:38:12 -04:00
|
|
|
actions := eventActionsByIDAndType(c, events, "testEvent", "container")
|
|
|
|
|
|
|
|
for _, a := range actions {
|
|
|
|
switch a {
|
|
|
|
case "create":
|
|
|
|
createCount++
|
|
|
|
case "start":
|
|
|
|
startCount++
|
|
|
|
case "die":
|
|
|
|
dieCount++
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, createCount, 1, "testEvent should be created 1 times: %v", actions)
|
|
|
|
assert.Equal(c, startCount, 4, "testEvent should start 4 times: %v", actions)
|
|
|
|
assert.Equal(c, dieCount, 4, "testEvent should die 4 times: %v", actions)
|
2016-04-11 14:52:34 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsSinceInTheFuture(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
dockerCmd(c, "run", "--name", "test-container", "-d", "busybox", "true")
|
|
|
|
waitRun("test-container")
|
|
|
|
|
|
|
|
since := daemonTime(c)
|
|
|
|
until := since.Add(time.Duration(-24) * time.Hour)
|
|
|
|
out, _, err := dockerCmdWithError("events", "--filter", "image=busybox", "--since", parseEventTime(since), "--until", parseEventTime(until))
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.ErrorContains(c, err, "")
|
|
|
|
assert.Assert(c, strings.Contains(out, "cannot be after `until`"))
|
2016-04-11 14:52:34 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsUntilInThePast(c *testing.T) {
|
2016-04-11 14:52:34 -04:00
|
|
|
since := daemonUnixTime(c)
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "--name", "test-container", "-d", "busybox", "true")
|
|
|
|
waitRun("test-container")
|
|
|
|
|
|
|
|
until := daemonUnixTime(c)
|
|
|
|
|
|
|
|
dockerCmd(c, "run", "--name", "test-container2", "-d", "busybox", "true")
|
|
|
|
waitRun("test-container2")
|
|
|
|
|
|
|
|
out, _ := dockerCmd(c, "events", "--filter", "image=busybox", "--since", since, "--until", until)
|
2016-04-06 23:38:12 -04:00
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, !strings.Contains(out, "test-container2"))
|
|
|
|
assert.Assert(c, strings.Contains(out, "test-container"))
|
2016-04-06 23:38:12 -04:00
|
|
|
}
|
2016-09-02 03:40:06 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFormat(c *testing.T) {
|
2016-09-02 03:40:06 -04:00
|
|
|
since := daemonUnixTime(c)
|
|
|
|
dockerCmd(c, "run", "--rm", "busybox", "true")
|
|
|
|
dockerCmd(c, "run", "--rm", "busybox", "true")
|
|
|
|
out, _ := dockerCmd(c, "events", "--since", since, "--until", daemonUnixTime(c), "--format", "{{json .}}")
|
|
|
|
dec := json.NewDecoder(strings.NewReader(out))
|
|
|
|
// make sure we got 2 start events
|
|
|
|
startCount := 0
|
|
|
|
for {
|
|
|
|
var err error
|
|
|
|
var ev eventtypes.Message
|
|
|
|
if err = dec.Decode(&ev); err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-09-02 03:40:06 -04:00
|
|
|
if ev.Status == "start" {
|
|
|
|
startCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Equal(c, startCount, 2, "should have had 2 start events but had %d, out: %s", startCount, out)
|
2016-09-02 03:40:06 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFormatBadFunc(c *testing.T) {
|
2016-09-02 03:40:06 -04:00
|
|
|
// make sure it fails immediately, without receiving any event
|
|
|
|
result := dockerCmdWithResult("events", "--format", "{{badFuncString .}}")
|
2017-08-23 17:01:29 -04:00
|
|
|
result.Assert(c, icmd.Expected{
|
2016-09-02 03:40:06 -04:00
|
|
|
Error: "exit status 64",
|
|
|
|
ExitCode: 64,
|
|
|
|
Err: "Error parsing format: template: :1: function \"badFuncString\" not defined",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestEventsFormatBadField(c *testing.T) {
|
2016-09-02 03:40:06 -04:00
|
|
|
// make sure it fails immediately, without receiving any event
|
|
|
|
result := dockerCmdWithResult("events", "--format", "{{.badFieldString}}")
|
2017-08-23 17:01:29 -04:00
|
|
|
result.Assert(c, icmd.Expected{
|
2016-09-02 03:40:06 -04:00
|
|
|
Error: "exit status 64",
|
|
|
|
ExitCode: 64,
|
|
|
|
Err: "Error parsing format: template: :1:2: executing \"\" at <.badFieldString>: can't evaluate field badFieldString in type *events.Message",
|
|
|
|
})
|
|
|
|
}
|