mirror of https://github.com/docker/docs.git
Add tests for the api
This commit is contained in:
parent
040c3b50d0
commit
ed7a4236b3
38
api_test.go
38
api_test.go
|
@ -89,6 +89,44 @@ func TestGetInfo(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetEvents(t *testing.T) {
|
||||||
|
runtime := mkRuntime(t)
|
||||||
|
srv := &Server{
|
||||||
|
runtime: runtime,
|
||||||
|
events: make([]utils.JSONMessage, 0, 64),
|
||||||
|
listeners: make(map[string]chan utils.JSONMessage),
|
||||||
|
}
|
||||||
|
|
||||||
|
srv.LogEvent("fakeaction", "fakeid")
|
||||||
|
srv.LogEvent("fakeaction2", "fakeid")
|
||||||
|
|
||||||
|
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 := getEvents(srv, APIVERSION, r, req, nil); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
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 != srv.events[i] {
|
||||||
|
t.Fatalf("Event received it different than expected")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetImagesJSON(t *testing.T) {
|
func TestGetImagesJSON(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
|
@ -38,7 +38,7 @@ func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
|
||||||
f()
|
f()
|
||||||
c <- false
|
c <- false
|
||||||
}()
|
}()
|
||||||
if <-c {
|
if <-c && msg != "" {
|
||||||
t.Fatal(msg)
|
t.Fatal(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue