2018-02-05 16:05:59 -05:00
|
|
|
package system // import "github.com/docker/docker/integration/system"
|
2017-12-08 03:01:34 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-02-22 01:00:05 -05:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2017-12-08 03:01:34 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/strslice"
|
2018-05-04 17:15:00 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2018-02-10 18:01:37 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
|
|
|
req "github.com/docker/docker/internal/test/request"
|
2018-02-22 01:00:05 -05:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/skip"
|
2017-12-08 03:01:34 -05:00
|
|
|
)
|
|
|
|
|
2018-05-04 17:15:00 -04:00
|
|
|
func TestEventsExecDie(t *testing.T) {
|
|
|
|
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.36"), "broken in earlier versions")
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME. Suspect may need to wait until container is running before exec")
|
2017-12-08 03:01:34 -05:00
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2017-12-08 03:01:34 -05:00
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
cID := container.Run(t, ctx, client)
|
2017-12-08 03:01:34 -05:00
|
|
|
|
2018-02-10 18:01:37 -05:00
|
|
|
id, err := client.ContainerExecCreate(ctx, cID,
|
2017-12-08 03:01:34 -05:00
|
|
|
types.ExecConfig{
|
|
|
|
Cmd: strslice.StrSlice([]string{"echo", "hello"}),
|
|
|
|
},
|
|
|
|
)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-08 03:01:34 -05:00
|
|
|
|
|
|
|
filters := filters.NewArgs(
|
2018-02-10 18:01:37 -05:00
|
|
|
filters.Arg("container", cID),
|
2017-12-08 03:01:34 -05:00
|
|
|
filters.Arg("event", "exec_die"),
|
|
|
|
)
|
|
|
|
msg, errors := client.Events(ctx, types.EventsOptions{
|
|
|
|
Filters: filters,
|
|
|
|
})
|
|
|
|
|
|
|
|
err = client.ContainerExecStart(ctx, id.ID,
|
|
|
|
types.ExecStartCheck{
|
|
|
|
Detach: true,
|
|
|
|
Tty: false,
|
|
|
|
},
|
|
|
|
)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-08 03:01:34 -05:00
|
|
|
|
|
|
|
select {
|
|
|
|
case m := <-msg:
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Equal(t, m.Type, "container")
|
|
|
|
assert.Equal(t, m.Actor.ID, cID)
|
|
|
|
assert.Equal(t, m.Action, "exec_die")
|
|
|
|
assert.Equal(t, m.Actor.Attributes["execID"], id.ID)
|
|
|
|
assert.Equal(t, m.Actor.Attributes["exitCode"], "0")
|
2017-12-08 03:01:34 -05:00
|
|
|
case err = <-errors:
|
2019-01-13 16:36:25 -05:00
|
|
|
assert.NilError(t, err)
|
2017-12-08 03:01:34 -05:00
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
t.Fatal("timeout hit")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-02-22 01:00:05 -05:00
|
|
|
|
|
|
|
// Test case for #18888: Events messages have been switched from generic
|
|
|
|
// `JSONMessage` to `events.Message` types. The switch does not break the
|
|
|
|
// backward compatibility so old `JSONMessage` could still be used.
|
|
|
|
// This test verifies that backward compatibility maintains.
|
|
|
|
func TestEventsBackwardsCompatible(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "Windows doesn't support back-compat messages")
|
2018-02-22 01:00:05 -05:00
|
|
|
defer setupTest(t)()
|
|
|
|
ctx := context.Background()
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-22 01:00:05 -05:00
|
|
|
|
|
|
|
since := request.DaemonTime(ctx, t, client, testEnv)
|
|
|
|
ts := strconv.FormatInt(since.Unix(), 10)
|
|
|
|
|
2019-06-06 07:00:37 -04:00
|
|
|
cID := container.Create(ctx, t, client)
|
2018-02-22 01:00:05 -05:00
|
|
|
|
|
|
|
// In case there is no events, the API should have responded immediately (not blocking),
|
|
|
|
// The test here makes sure the response time is less than 3 sec.
|
|
|
|
expectedTime := time.Now().Add(3 * time.Second)
|
|
|
|
emptyResp, emptyBody, err := req.Get("/events")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-22 01:00:05 -05:00
|
|
|
defer emptyBody.Close()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(http.StatusOK, emptyResp.StatusCode))
|
|
|
|
assert.Check(t, time.Now().Before(expectedTime), "timeout waiting for events api to respond, should have responded immediately")
|
2018-02-22 01:00:05 -05:00
|
|
|
|
|
|
|
// We also test to make sure the `events.Message` is compatible with `JSONMessage`
|
|
|
|
q := url.Values{}
|
|
|
|
q.Set("since", ts)
|
|
|
|
_, body, err := req.Get("/events?" + q.Encode())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-22 01:00:05 -05:00
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
dec := json.NewDecoder(body)
|
|
|
|
var containerCreateEvent *jsonmessage.JSONMessage
|
|
|
|
for {
|
|
|
|
var event jsonmessage.JSONMessage
|
|
|
|
if err := dec.Decode(&event); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2019-01-13 16:36:25 -05:00
|
|
|
assert.NilError(t, err)
|
2018-02-22 01:00:05 -05:00
|
|
|
}
|
|
|
|
if event.Status == "create" && event.ID == cID {
|
|
|
|
containerCreateEvent = &event
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, containerCreateEvent != nil)
|
|
|
|
assert.Check(t, is.Equal("create", containerCreateEvent.Status))
|
|
|
|
assert.Check(t, is.Equal(cID, containerCreateEvent.ID))
|
|
|
|
assert.Check(t, is.Equal("busybox", containerCreateEvent.From))
|
2018-02-22 01:00:05 -05:00
|
|
|
}
|