Browse code

Add TestCommitAutoRun

Guillaume J. Charmes authored on 2013/04/26 09:03:13
Showing 1 changed files
... ...
@@ -226,6 +226,84 @@ func TestDiff(t *testing.T) {
226 226
 	}
227 227
 }
228 228
 
229
+func TestCommitAutoRun(t *testing.T) {
230
+	runtime, err := newTestRuntime()
231
+	if err != nil {
232
+		t.Fatal(err)
233
+	}
234
+	defer nuke(runtime)
235
+	container1, err := runtime.Create(
236
+		&Config{
237
+			Image: GetTestImage(runtime).Id,
238
+			Cmd:   []string{"/bin/sh", "-c", "echo hello > /world"},
239
+		},
240
+	)
241
+	if err != nil {
242
+		t.Fatal(err)
243
+	}
244
+	defer runtime.Destroy(container1)
245
+
246
+	if container1.State.Running {
247
+		t.Errorf("Container shouldn't be running")
248
+	}
249
+	if err := container1.Run(); err != nil {
250
+		t.Fatal(err)
251
+	}
252
+	if container1.State.Running {
253
+		t.Errorf("Container shouldn't be running")
254
+	}
255
+
256
+	rwTar, err := container1.ExportRw()
257
+	if err != nil {
258
+		t.Error(err)
259
+	}
260
+	img, err := runtime.graph.Create(rwTar, container1, "unit test commited image", "", &Config{Cmd: []string{"cat", "/world"}})
261
+	if err != nil {
262
+		t.Error(err)
263
+	}
264
+
265
+	// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
266
+
267
+	container2, err := runtime.Create(
268
+		&Config{
269
+			Image: img.Id,
270
+		},
271
+	)
272
+	if err != nil {
273
+		t.Fatal(err)
274
+	}
275
+	defer runtime.Destroy(container2)
276
+	stdout, err := container2.StdoutPipe()
277
+	if err != nil {
278
+		t.Fatal(err)
279
+	}
280
+	stderr, err := container2.StderrPipe()
281
+	if err != nil {
282
+		t.Fatal(err)
283
+	}
284
+	if err := container2.Start(); err != nil {
285
+		t.Fatal(err)
286
+	}
287
+	container2.Wait()
288
+	output, err := ioutil.ReadAll(stdout)
289
+	if err != nil {
290
+		t.Fatal(err)
291
+	}
292
+	output2, err := ioutil.ReadAll(stderr)
293
+	if err != nil {
294
+		t.Fatal(err)
295
+	}
296
+	if err := stdout.Close(); err != nil {
297
+		t.Fatal(err)
298
+	}
299
+	if err := stderr.Close(); err != nil {
300
+		t.Fatal(err)
301
+	}
302
+	if string(output) != "hello\n" {
303
+		t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
304
+	}
305
+}
306
+
229 307
 func TestCommitRun(t *testing.T) {
230 308
 	runtime, err := newTestRuntime()
231 309
 	if err != nil {