Merge pull request #1452 from dotcloud/improve_TestGetContainersTop
* Tests: Improve TestGetContainersTop so it does not rely on sleep
This commit is contained in:
commit
55f9610cde
40
api_test.go
40
api_test.go
|
@ -461,26 +461,48 @@ func TestGetContainersTop(t *testing.T) {
|
||||||
|
|
||||||
container, err := builder.Create(
|
container, err := builder.Create(
|
||||||
&Config{
|
&Config{
|
||||||
Image: GetTestImage(runtime).ID,
|
Image: GetTestImage(runtime).ID,
|
||||||
Cmd: []string{"/bin/sh", "-c", "sleep 2"},
|
Cmd: []string{"/bin/sh", "-c", "cat"},
|
||||||
|
OpenStdin: true,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer runtime.Destroy(container)
|
defer runtime.Destroy(container)
|
||||||
|
defer func() {
|
||||||
|
// Make sure the process dies before destorying runtime
|
||||||
|
container.stdin.Close()
|
||||||
|
container.WaitTimeout(2 * time.Second)
|
||||||
|
}()
|
||||||
|
|
||||||
hostConfig := &HostConfig{}
|
hostConfig := &HostConfig{}
|
||||||
if err := container.Start(hostConfig); err != nil {
|
if err := container.Start(hostConfig); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Give some time to the process to start
|
setTimeout(t, "Waiting for the container to be started timed out", 10*time.Second, func() {
|
||||||
container.WaitTimeout(500 * time.Millisecond)
|
for {
|
||||||
|
if container.State.Running {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if !container.State.Running {
|
if !container.State.Running {
|
||||||
t.Errorf("Container should be running")
|
t.Fatalf("Container should be running")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure sh spawn up cat
|
||||||
|
setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
|
||||||
|
in, _ := container.StdinPipe()
|
||||||
|
out, _ := container.StdoutPipe()
|
||||||
|
if err := assertPipe("hello\n", "hello", out, in, 15); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
r := httptest.NewRecorder()
|
r := httptest.NewRecorder()
|
||||||
req, err := http.NewRequest("GET", "/"+container.ID+"/top?ps_args=u", bytes.NewReader([]byte{}))
|
req, err := http.NewRequest("GET", "/"+container.ID+"/top?ps_args=u", bytes.NewReader([]byte{}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -504,11 +526,11 @@ func TestGetContainersTop(t *testing.T) {
|
||||||
if len(procs.Processes) != 2 {
|
if len(procs.Processes) != 2 {
|
||||||
t.Fatalf("Expected 2 processes, found %d.", len(procs.Processes))
|
t.Fatalf("Expected 2 processes, found %d.", len(procs.Processes))
|
||||||
}
|
}
|
||||||
if procs.Processes[0][10] != "/bin/sh" && procs.Processes[0][10] != "sleep" {
|
if procs.Processes[0][10] != "/bin/sh" && procs.Processes[0][10] != "cat" {
|
||||||
t.Fatalf("Expected `sleep` or `/bin/sh`, found %s.", procs.Processes[0][10])
|
t.Fatalf("Expected `cat` or `/bin/sh`, found %s.", procs.Processes[0][10])
|
||||||
}
|
}
|
||||||
if procs.Processes[1][10] != "/bin/sh" && procs.Processes[1][10] != "sleep" {
|
if procs.Processes[1][10] != "/bin/sh" && procs.Processes[1][10] != "cat" {
|
||||||
t.Fatalf("Expected `sleep` or `/bin/sh`, found %s.", procs.Processes[1][10])
|
t.Fatalf("Expected `cat` or `/bin/sh`, found %s.", procs.Processes[1][10])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue