Improve robustness of /stats api test
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
f3aed2a297
commit
4d7707e183
|
@ -256,28 +256,46 @@ func TestVolumesFromHasPriority(t *testing.T) {
|
||||||
|
|
||||||
func TestGetContainerStats(t *testing.T) {
|
func TestGetContainerStats(t *testing.T) {
|
||||||
defer deleteAllContainers()
|
defer deleteAllContainers()
|
||||||
name := "statscontainer"
|
var (
|
||||||
|
name = "statscontainer"
|
||||||
runCmd := exec.Command(dockerBinary, "run", "-d", "--name", name, "busybox", "top")
|
runCmd = exec.Command(dockerBinary, "run", "-d", "--name", name, "busybox", "top")
|
||||||
|
)
|
||||||
out, _, err := runCommandWithOutput(runCmd)
|
out, _, err := runCommandWithOutput(runCmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error on container creation: %v, output: %q", err, out)
|
t.Fatalf("Error on container creation: %v, output: %q", err, out)
|
||||||
}
|
}
|
||||||
|
type b struct {
|
||||||
|
body []byte
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
bc := make(chan b, 1)
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(4 * time.Second)
|
body, err := sockRequest("GET", "/containers/"+name+"/stats", nil)
|
||||||
runCommand(exec.Command(dockerBinary, "kill", name))
|
bc <- b{body, err}
|
||||||
runCommand(exec.Command(dockerBinary, "rm", name))
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
body, err := sockRequest("GET", "/containers/"+name+"/stats", nil)
|
// allow some time to stream the stats from the container
|
||||||
if err != nil {
|
time.Sleep(4 * time.Second)
|
||||||
t.Fatalf("GET containers/stats sockRequest failed: %v", err)
|
if _, err := runCommand(exec.Command(dockerBinary, "rm", "-f", name)); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dec := json.NewDecoder(bytes.NewBuffer(body))
|
// collect the results from the stats stream or timeout and fail
|
||||||
var s *stats.Stats
|
// if the stream was not disconnected.
|
||||||
if err := dec.Decode(&s); err != nil {
|
select {
|
||||||
t.Fatal(err)
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("stream was not closed after container was removed")
|
||||||
|
case sr := <-bc:
|
||||||
|
if sr.err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dec := json.NewDecoder(bytes.NewBuffer(sr.body))
|
||||||
|
var s *stats.Stats
|
||||||
|
// decode only one object from the stream
|
||||||
|
if err := dec.Decode(&s); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
logDone("container REST API - check GET containers/stats")
|
logDone("container REST API - check GET containers/stats")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue