From d949e2804a35497bc041537734dfeff150ea7e21 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Sun, 31 Mar 2013 14:15:10 -0700 Subject: [PATCH] Add a check to avoid double start (resulting in dockerd to panic) and unit test for it --- container.go | 3 +++ container_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/container.go b/container.go index a03614c011..f49aa80d54 100644 --- a/container.go +++ b/container.go @@ -230,6 +230,9 @@ func (container *Container) start() error { } func (container *Container) Start() error { + if container.State.Running { + return fmt.Errorf("The container %s is already running.", container.Id) + } if err := container.EnsureMounted(); err != nil { return err } diff --git a/container_test.go b/container_test.go index fae1c3c19c..fdc7f5703a 100644 --- a/container_test.go +++ b/container_test.go @@ -231,6 +231,40 @@ func TestCommitRun(t *testing.T) { } } +func TestStart(t *testing.T) { + runtime, err := newTestRuntime() + if err != nil { + t.Fatal(err) + } + defer nuke(runtime) + container, err := runtime.Create( + &Config{ + Image: GetTestImage(runtime).Id, + Memory: 33554432, + Cmd: []string{"/bin/cat"}, + OpenStdin: true, + }, + ) + if err != nil { + t.Fatal(err) + } + defer runtime.Destroy(container) + + if err := container.Start(); err != nil { + t.Fatal(err) + } + + // Give some time to the process to start + container.WaitTimeout(500 * time.Millisecond) + + if !container.State.Running { + t.Errorf("Container should be running") + } + if err := container.Start(); err == nil { + t.Fatalf("A running containter should be able to be started") + } +} + func TestRun(t *testing.T) { runtime, err := newTestRuntime() if err != nil {