mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #6216 from gsalgado/convert-integration-tests
Convert integration/api_test:TestGetEvents into a unit test and a CLI test
This commit is contained in:
commit
f6d11c8d50
3 changed files with 72 additions and 47 deletions
|
@ -153,6 +153,49 @@ func TestGetContainersByName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGetEvents(t *testing.T) {
|
||||
eng := engine.New()
|
||||
var called bool
|
||||
eng.Register("events", func(job *engine.Job) engine.Status {
|
||||
called = true
|
||||
since := job.Getenv("since")
|
||||
if since != "1" {
|
||||
t.Fatalf("'since' should be 1, found %#v instead", since)
|
||||
}
|
||||
until := job.Getenv("until")
|
||||
if until != "0" {
|
||||
t.Fatalf("'until' should be 0, found %#v instead", until)
|
||||
}
|
||||
v := &engine.Env{}
|
||||
v.Set("since", since)
|
||||
v.Set("until", until)
|
||||
if _, err := v.WriteTo(job.Stdout); err != nil {
|
||||
return job.Error(err)
|
||||
}
|
||||
return engine.StatusOK
|
||||
})
|
||||
r := serveRequest("GET", "/events?since=1&until=0", nil, eng, t)
|
||||
if !called {
|
||||
t.Fatal("handler was not called")
|
||||
}
|
||||
if r.HeaderMap.Get("Content-Type") != "application/json" {
|
||||
t.Fatalf("%#v\n", r)
|
||||
}
|
||||
var stdout_json struct {
|
||||
Since int
|
||||
Until int
|
||||
}
|
||||
if err := json.Unmarshal(r.Body.Bytes(), &stdout_json); err != nil {
|
||||
t.Fatalf("%#v", err)
|
||||
}
|
||||
if stdout_json.Since != 1 {
|
||||
t.Fatalf("since != 1: %#v", stdout_json.Since)
|
||||
}
|
||||
if stdout_json.Until != 0 {
|
||||
t.Fatalf("until != 0: %#v", stdout_json.Until)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogs(t *testing.T) {
|
||||
eng := engine.New()
|
||||
var inspect bool
|
||||
|
|
29
integration-cli/docker_cli_events_test.go
Normal file
29
integration-cli/docker_cli_events_test.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCLIGetEvents(t *testing.T) {
|
||||
out, _, _ := cmd(t, "images", "-q")
|
||||
image := strings.Split(out, "\n")[0]
|
||||
cmd(t, "tag", image, "utest:tag1")
|
||||
cmd(t, "tag", image, "utest:tag2")
|
||||
cmd(t, "rmi", "utest:tag1")
|
||||
cmd(t, "rmi", "utest:tag2")
|
||||
eventsCmd := exec.Command("timeout", "0.2", dockerBinary, "events", "--since=1")
|
||||
out, _, _ = runCommandWithOutput(eventsCmd)
|
||||
events := strings.Split(out, "\n")
|
||||
n_events := len(events)
|
||||
// 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.
|
||||
for _, v := range events[n_events-3 : n_events-1] {
|
||||
if !strings.Contains(v, "untag") {
|
||||
t.Fatalf("event should be untag, not %#v", v)
|
||||
}
|
||||
}
|
||||
logDone("events - untags are logged")
|
||||
}
|
|
@ -19,56 +19,9 @@ import (
|
|||
"github.com/dotcloud/docker/engine"
|
||||
"github.com/dotcloud/docker/image"
|
||||
"github.com/dotcloud/docker/runconfig"
|
||||
"github.com/dotcloud/docker/utils"
|
||||
"github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
|
||||
)
|
||||
|
||||
func TestGetEvents(t *testing.T) {
|
||||
eng := NewTestEngine(t)
|
||||
srv := mkServerFromEngine(eng, t)
|
||||
// FIXME: we might not need daemon, why not simply nuke
|
||||
// the engine?
|
||||
daemon := mkDaemonFromEngine(eng, t)
|
||||
defer nuke(daemon)
|
||||
|
||||
var events []*utils.JSONMessage
|
||||
for _, parts := range [][3]string{
|
||||
{"fakeaction", "fakeid", "fakeimage"},
|
||||
{"fakeaction2", "fakeid", "fakeimage"},
|
||||
} {
|
||||
action, id, from := parts[0], parts[1], parts[2]
|
||||
ev := srv.LogEvent(action, id, from)
|
||||
events = append(events, ev)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", "/events?since=1", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := httptest.NewRecorder()
|
||||
setTimeout(t, "", 500*time.Millisecond, func() {
|
||||
if err := server.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertHttpNotError(r, t)
|
||||
})
|
||||
|
||||
dec := json.NewDecoder(r.Body)
|
||||
for i := 0; i < 2; i++ {
|
||||
var jm utils.JSONMessage
|
||||
if err := dec.Decode(&jm); err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if jm != *events[i] {
|
||||
t.Fatalf("Event received it different than expected")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestGetImagesJSON(t *testing.T) {
|
||||
eng := NewTestEngine(t)
|
||||
defer mkDaemonFromEngine(eng, t).Nuke()
|
||||
|
|
Loading…
Reference in a new issue