Browse code

Add a check to avoid double start (resulting in dockerd to panic) and unit test for it

Guillaume J. Charmes authored on 2013/04/01 06:15:10
Showing 2 changed files
... ...
@@ -230,6 +230,9 @@ func (container *Container) start() error {
230 230
 }
231 231
 
232 232
 func (container *Container) Start() error {
233
+	if container.State.Running {
234
+		return fmt.Errorf("The container %s is already running.", container.Id)
235
+	}
233 236
 	if err := container.EnsureMounted(); err != nil {
234 237
 		return err
235 238
 	}
... ...
@@ -231,6 +231,40 @@ func TestCommitRun(t *testing.T) {
231 231
 	}
232 232
 }
233 233
 
234
+func TestStart(t *testing.T) {
235
+	runtime, err := newTestRuntime()
236
+	if err != nil {
237
+		t.Fatal(err)
238
+	}
239
+	defer nuke(runtime)
240
+	container, err := runtime.Create(
241
+		&Config{
242
+			Image:     GetTestImage(runtime).Id,
243
+			Memory:    33554432,
244
+			Cmd:       []string{"/bin/cat"},
245
+			OpenStdin: true,
246
+		},
247
+	)
248
+	if err != nil {
249
+		t.Fatal(err)
250
+	}
251
+	defer runtime.Destroy(container)
252
+
253
+	if err := container.Start(); err != nil {
254
+		t.Fatal(err)
255
+	}
256
+
257
+	// Give some time to the process to start
258
+	container.WaitTimeout(500 * time.Millisecond)
259
+
260
+	if !container.State.Running {
261
+		t.Errorf("Container should be running")
262
+	}
263
+	if err := container.Start(); err == nil {
264
+		t.Fatalf("A running containter should be able to be started")
265
+	}
266
+}
267
+
234 268
 func TestRun(t *testing.T) {
235 269
 	runtime, err := newTestRuntime()
236 270
 	if err != nil {