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

Merge pull request #3706 from creack/fix_wait_stopped

Fix wait on stopped container (after docker restart) + add Unit test for that case
This commit is contained in:
Michael Crosby 2014-01-21 17:03:35 -08:00
commit 0b789ca844
2 changed files with 67 additions and 0 deletions

View file

@ -2,8 +2,10 @@ package docker
import ( import (
"github.com/dotcloud/docker" "github.com/dotcloud/docker"
"github.com/dotcloud/docker/engine"
"strings" "strings"
"testing" "testing"
"time"
) )
func TestImageTagImageDelete(t *testing.T) { func TestImageTagImageDelete(t *testing.T) {
@ -154,6 +156,65 @@ func TestCommit(t *testing.T) {
} }
} }
func TestRestartKillWait(t *testing.T) {
eng := NewTestEngine(t)
srv := mkServerFromEngine(eng, t)
runtime := mkRuntimeFromEngine(eng, t)
defer runtime.Nuke()
config, hostConfig, _, err := docker.ParseRun([]string{"-i", unitTestImageID, "/bin/cat"}, nil)
if err != nil {
t.Fatal(err)
}
id := createTestContainer(eng, config, t)
if c := srv.Containers(true, false, -1, "", ""); len(c) != 1 {
t.Errorf("Expected 1 container, %v found", len(c))
}
job := eng.Job("start", id)
if err := job.ImportEnv(hostConfig); err != nil {
t.Fatal(err)
}
if err := job.Run(); err != nil {
t.Fatal(err)
}
job = eng.Job("kill", id)
if err := job.Run(); err != nil {
t.Fatal(err)
}
eng, err = engine.New(eng.Root())
if err != nil {
t.Fatal(err)
}
job = eng.Job("initapi")
job.Setenv("Root", eng.Root())
job.SetenvBool("AutoRestart", false)
// TestGetEnabledCors and TestOptionsRoute require EnableCors=true
job.SetenvBool("EnableCors", true)
if err := job.Run(); err != nil {
t.Fatal(err)
}
srv = mkServerFromEngine(eng, t)
c := srv.Containers(true, false, -1, "", "")
if len(c) != 1 {
t.Errorf("Expected 1 container, %v found", len(c))
}
setTimeout(t, "Waiting on stopped container timedout", 5*time.Second, func() {
job = srv.Eng.Job("wait", c[0].ID)
var statusStr string
job.Stdout.AddString(&statusStr)
if err := job.Run(); err != nil {
t.Fatal(err)
}
})
}
func TestCreateStartRestartStopStartKillRm(t *testing.T) { func TestCreateStartRestartStopStartKillRm(t *testing.T) {
eng := NewTestEngine(t) eng := NewTestEngine(t)
srv := mkServerFromEngine(eng, t) srv := mkServerFromEngine(eng, t)

View file

@ -183,6 +183,12 @@ func (runtime *Runtime) Register(container *Container) error {
container.waitLock = make(chan struct{}) container.waitLock = make(chan struct{})
go container.monitor(nil) go container.monitor(nil)
} }
} else {
// When the container is not running, we still initialize the waitLock
// chan and close it. Receiving on nil chan blocks whereas receiving on a
// closed chan does not. In this case we do not want to block.
container.waitLock = make(chan struct{})
close(container.waitLock)
} }
return nil return nil
} }