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

Add TestCommitRun unit test

This commit is contained in:
Guillaume J. Charmes 2013-03-12 05:35:47 -07:00
parent 2664668388
commit f2dc079c05

View file

@ -1,6 +1,7 @@
package docker package docker
import ( import (
"./fs"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -10,16 +11,16 @@ import (
"time" "time"
) )
func TestStart(t *testing.T) { func TestCommitRun(t *testing.T) {
docker, err := newTestDocker() docker, err := newTestDocker()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer nuke(docker) defer nuke(docker)
container, err := docker.Create( container1, err := docker.Create(
"start_test", "precommit_test",
"ls", "/bin/sh",
[]string{"-al"}, []string{"-c", "echo hello > /world"},
GetTestImage(docker), GetTestImage(docker),
&Config{ &Config{
Ram: 33554432, Ram: 33554432,
@ -28,22 +29,62 @@ func TestStart(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer docker.Destroy(container) defer docker.Destroy(container1)
if container.State.Running { if container1.State.Running {
t.Errorf("Container shouldn't be running") t.Errorf("Container shouldn't be running")
} }
if err := container.Start(); err != nil { if err := container1.Run(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
container.Wait() if container1.State.Running {
if container.State.Running {
t.Errorf("Container shouldn't be running") t.Errorf("Container shouldn't be running")
} }
// We should be able to call Wait again
container.Wait() // FIXME: freeze the container before copying it to avoid data corruption?
if container.State.Running { rwTar, err := fs.Tar(container1.Mountpoint.Rw, fs.Uncompressed)
t.Errorf("Container shouldn't be running") if err != nil {
t.Error(err)
}
// Create a new image from the container's base layers + a new layer from container changes
parentImg, err := docker.Store.Get(container1.Image)
if err != nil {
t.Error(err)
}
img, err := docker.Store.Create(rwTar, parentImg, "test_commitrun", "unit test commited image")
if err != nil {
t.Error(err)
}
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
container2, err := docker.Create(
"postcommit_test",
"cat",
[]string{"/world"},
img,
&Config{
Ram: 33554432,
},
)
if err != nil {
t.Fatal(err)
}
defer docker.Destroy(container2)
stdout, err := container2.StdoutPipe()
stderr, err := container2.StderrPipe()
if err := container2.Start(); err != nil {
t.Fatal(err)
}
container2.Wait()
output, err := ioutil.ReadAll(stdout)
output2, err := ioutil.ReadAll(stderr)
stdout.Close()
stderr.Close()
if string(output) != "hello\n" {
t.Fatalf("\nout: %s\nerr: %s\n", string(output), string(output2))
} }
} }
@ -308,7 +349,7 @@ func TestUser(t *testing.T) {
// Set a username // Set a username
container, err = docker.Create( container, err = docker.Create(
"user_root", "user_root",
"id", "/bin/id",
[]string{}, []string{},
GetTestImage(docker), GetTestImage(docker),
&Config{ &Config{