1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/integration-cli/docker_api_events_test.go

58 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"testing"
"time"
)
func TestGetEventsLineDelim(t *testing.T) {
name := "testimageevents"
defer deleteImages(name)
_, err := buildImage(name,
`FROM scratch
MAINTAINER "docker"`,
true)
if err != nil {
t.Fatal(err)
}
if err := deleteImages(name); err != nil {
t.Fatal(err)
}
endpoint := fmt.Sprintf("/events?since=1&until=%d", time.Now().Unix())
body, err := sockRequest("GET", endpoint)
if err != nil {
t.Fatal(err)
}
linesRead := 0
scanner := bufio.NewScanner(bytes.NewReader(body))
for scanner.Scan() && linesRead < 2 {
line := scanner.Bytes()
if len(line) == 0 {
continue
}
// make sure line delimited json
res := make(map[string]interface{})
if err := json.Unmarshal(line, &res); err != nil {
t.Fatalf("Unmarshaling the line as JSON failed: %v", err)
}
linesRead++
}
if err := scanner.Err(); err != nil {
t.Fatalf("Scanner failed: %v", err)
}
if linesRead < 2 {
t.Fatalf("Only %d lines were read from the stream", linesRead)
}
logDone("events - test the api response is line delimited json")
}