2013-05-28 18:31:06 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/dotcloud/docker/utils"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Dockerfile = `
|
|
|
|
# VERSION 0.1
|
|
|
|
# DOCKER-VERSION 0.2
|
|
|
|
|
|
|
|
from ` + unitTestImageName + `
|
|
|
|
run sh -c 'echo root:testpass > /tmp/passwd'
|
|
|
|
run mkdir -p /var/run/sshd
|
|
|
|
`
|
|
|
|
|
2013-06-11 10:39:06 -04:00
|
|
|
const DockerfileNoNewLine = `
|
|
|
|
# VERSION 0.1
|
|
|
|
# DOCKER-VERSION 0.2
|
|
|
|
|
|
|
|
from ` + unitTestImageName + `
|
|
|
|
run sh -c 'echo root:testpass > /tmp/passwd'
|
|
|
|
run mkdir -p /var/run/sshd`
|
|
|
|
|
2013-05-28 18:31:06 -04:00
|
|
|
func TestBuild(t *testing.T) {
|
2013-06-11 10:39:06 -04:00
|
|
|
dockerfiles := []string{Dockerfile, DockerfileNoNewLine}
|
|
|
|
for _, Dockerfile := range dockerfiles {
|
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-11 10:39:06 -04:00
|
|
|
srv := &Server{runtime: runtime}
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-11 10:39:06 -04:00
|
|
|
buildfile := NewBuildFile(srv, &utils.NopWriter{})
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-11 10:39:06 -04:00
|
|
|
imgID, err := buildfile.Build(strings.NewReader(Dockerfile), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-11 10:39:06 -04:00
|
|
|
builder := NewBuilder(runtime)
|
|
|
|
container, err := builder.Create(
|
|
|
|
&Config{
|
|
|
|
Image: imgID,
|
|
|
|
Cmd: []string{"cat", "/tmp/passwd"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container)
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-11 10:39:06 -04:00
|
|
|
output, err := container.Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(output) != "root:testpass\n" {
|
|
|
|
t.Fatalf("Unexpected output. Read '%s', expected '%s'", output, "root:testpass\n")
|
|
|
|
}
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-11 10:39:06 -04:00
|
|
|
container2, err := builder.Create(
|
|
|
|
&Config{
|
|
|
|
Image: imgID,
|
|
|
|
Cmd: []string{"ls", "-d", "/var/run/sshd"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer runtime.Destroy(container2)
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-11 10:39:06 -04:00
|
|
|
output, err = container2.Output()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(output) != "/var/run/sshd\n" {
|
|
|
|
t.Fatal("/var/run/sshd has not been created")
|
|
|
|
}
|
2013-05-28 18:31:06 -04:00
|
|
|
}
|
|
|
|
}
|