moby--moby/buildfile_test.go

125 lines
2.4 KiB
Go
Raw Normal View History

2013-05-28 22:31:06 +00:00
package docker
import (
"github.com/dotcloud/docker/utils"
"strings"
"testing"
)
const Dockerfile = `
# VERSION 0.1
# DOCKER-VERSION 0.2
from ` + unitTestImageName + `
2013-06-14 01:52:41 +00:00
maintainer docker
expose 22
env FOO BAR
cmd ["echo", "hello", "world"]
2013-05-28 22:31:06 +00:00
run sh -c 'echo root:testpass > /tmp/passwd'
run mkdir -p /var/run/sshd
2013-06-14 01:52:41 +00:00
add . /src
2013-05-28 22:31:06 +00:00
`
const DockerfileNoNewLine = `
# VERSION 0.1
# DOCKER-VERSION 0.2
from ` + unitTestImageName + `
2013-06-14 01:52:41 +00:00
maintainer docker
expose 22
env FOO BAR
cmd ["echo", "hello", "world"]
run sh -c 'echo root:testpass > /tmp/passwd'
2013-06-14 01:52:41 +00:00
run mkdir -p /var/run/sshd
add . /src`
// FIXME: test building with a context
// FIXME: test building with a local ADD as first command
// FIXME: test building with 2 successive overlapping ADD commands
2013-06-14 01:52:41 +00:00
func TestBuildFile(t *testing.T) {
dockerfiles := []string{Dockerfile, DockerfileNoNewLine}
for _, Dockerfile := range dockerfiles {
runtime, err := newTestRuntime()
if err != nil {
t.Fatal(err)
}
defer nuke(runtime)
2013-05-28 22:31:06 +00:00
srv := &Server{runtime: runtime}
2013-05-28 22:31:06 +00:00
buildfile := NewBuildFile(srv, &utils.NopWriter{})
2013-05-28 22:31:06 +00:00
2013-06-14 01:52:41 +00:00
context, err := fakeTar()
if err != nil {
t.Fatal(err)
}
imgID, err := buildfile.Build(strings.NewReader(Dockerfile), context)
if err != nil {
t.Fatal(err)
}
2013-05-28 22:31:06 +00: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 22:31:06 +00: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 22:31:06 +00: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 22:31:06 +00: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-06-14 01:52:41 +00:00
container3, err := builder.Create(
&Config{
Image: imgID,
Cmd: []string{"ls", "/src"},
},
)
if err != nil {
t.Fatal(err)
}
defer runtime.Destroy(container3)
output, err = container3.Output()
if err != nil {
t.Fatal(err)
}
if string(output) != "etc\nvar\n" {
t.Fatalf("Unexpected output. Expected: '%s', received: '%s'", "etc\nvar\n", string(output))
}
2013-05-28 22:31:06 +00:00
}
}