2013-05-28 18:31:06 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-07-02 06:47:37 -04:00
|
|
|
"fmt"
|
2013-06-15 14:07:49 -04:00
|
|
|
"io/ioutil"
|
2013-05-28 18:31:06 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-06-15 14:07:49 -04:00
|
|
|
// mkTestContext generates a build context from the contents of the provided dockerfile.
|
|
|
|
// This context is suitable for use as an argument to BuildFile.Build()
|
|
|
|
func mkTestContext(dockerfile string, files [][2]string, t *testing.T) Archive {
|
2013-07-02 18:27:22 -04:00
|
|
|
context, err := mkBuildContext(fmt.Sprintf(dockerfile, unitTestImageID), files)
|
2013-06-15 14:07:49 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return context
|
|
|
|
}
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-15 14:07:49 -04:00
|
|
|
// A testContextTemplate describes a build context and how to test it
|
|
|
|
type testContextTemplate struct {
|
|
|
|
// Contents of the Dockerfile
|
|
|
|
dockerfile string
|
|
|
|
// Additional files in the context, eg [][2]string{"./passwd", "gordon"}
|
|
|
|
files [][2]string
|
|
|
|
}
|
2013-06-11 10:39:06 -04:00
|
|
|
|
2013-06-15 14:07:49 -04:00
|
|
|
// A table of all the contexts to build and test.
|
|
|
|
// A new docker runtime will be created and torn down for each context.
|
2013-07-02 18:27:22 -04:00
|
|
|
var testContexts = []testContextTemplate{
|
2013-06-15 14:07:49 -04:00
|
|
|
{
|
|
|
|
`
|
2013-06-30 00:22:15 -04:00
|
|
|
from %s
|
2013-06-11 10:39:06 -04:00
|
|
|
run sh -c 'echo root:testpass > /tmp/passwd'
|
2013-06-13 21:52:41 -04:00
|
|
|
run mkdir -p /var/run/sshd
|
2013-06-20 23:16:39 -04:00
|
|
|
run [ "$(cat /tmp/passwd)" = "root:testpass" ]
|
|
|
|
run [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]
|
2013-06-15 14:07:49 -04:00
|
|
|
`,
|
|
|
|
nil,
|
|
|
|
},
|
2013-06-15 14:35:56 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
`
|
2013-06-30 00:22:15 -04:00
|
|
|
from %s
|
2013-06-20 23:42:19 -04:00
|
|
|
add foo /usr/lib/bla/bar
|
|
|
|
run [ "$(cat /usr/lib/bla/bar)" = 'hello world!' ]
|
|
|
|
`,
|
2013-06-15 14:35:56 -04:00
|
|
|
[][2]string{{"foo", "hello world!"}},
|
2013-06-20 23:42:19 -04:00
|
|
|
},
|
2013-06-11 10:39:06 -04:00
|
|
|
|
2013-06-20 23:42:19 -04:00
|
|
|
{
|
|
|
|
`
|
2013-06-30 00:22:15 -04:00
|
|
|
from %s
|
2013-06-20 23:42:19 -04:00
|
|
|
add f /
|
|
|
|
run [ "$(cat /f)" = "hello" ]
|
|
|
|
add f /abc
|
|
|
|
run [ "$(cat /abc)" = "hello" ]
|
|
|
|
add f /x/y/z
|
|
|
|
run [ "$(cat /x/y/z)" = "hello" ]
|
|
|
|
add f /x/y/d/
|
|
|
|
run [ "$(cat /x/y/d/f)" = "hello" ]
|
|
|
|
add d /
|
|
|
|
run [ "$(cat /ga)" = "bu" ]
|
|
|
|
add d /somewhere
|
|
|
|
run [ "$(cat /somewhere/ga)" = "bu" ]
|
|
|
|
add d /anotherplace/
|
|
|
|
run [ "$(cat /anotherplace/ga)" = "bu" ]
|
|
|
|
add d /somewheeeere/over/the/rainbooow
|
|
|
|
run [ "$(cat /somewheeeere/over/the/rainbooow/ga)" = "bu" ]
|
|
|
|
`,
|
|
|
|
[][2]string{
|
|
|
|
{"f", "hello"},
|
|
|
|
{"d/ga", "bu"},
|
2013-06-15 14:35:56 -04:00
|
|
|
},
|
|
|
|
},
|
2013-06-14 19:43:39 -04:00
|
|
|
|
2013-06-21 01:02:36 -04:00
|
|
|
{
|
|
|
|
`
|
2013-06-30 00:22:15 -04:00
|
|
|
from %s
|
2013-06-21 01:02:36 -04:00
|
|
|
env FOO BAR
|
|
|
|
run [ "$FOO" = "BAR" ]
|
2013-06-24 22:20:05 -04:00
|
|
|
`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`
|
2013-07-08 18:23:04 -04:00
|
|
|
from %s
|
2013-06-24 22:20:05 -04:00
|
|
|
ENTRYPOINT /bin/echo
|
|
|
|
CMD Hello world
|
2013-07-03 22:33:30 -04:00
|
|
|
`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`
|
|
|
|
from docker-ut
|
|
|
|
VOLUME /test
|
|
|
|
CMD Hello world
|
2013-06-21 01:02:36 -04:00
|
|
|
`,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
2013-06-14 19:43:39 -04:00
|
|
|
|
|
|
|
// FIXME: test building with 2 successive overlapping ADD commands
|
|
|
|
|
2013-05-28 18:31:06 -04:00
|
|
|
func TestBuild(t *testing.T) {
|
2013-06-15 14:07:49 -04:00
|
|
|
for _, ctx := range testContexts {
|
2013-06-11 10:39:06 -04:00
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-27 20:52:47 -04:00
|
|
|
srv := &Server{
|
2013-07-02 06:47:37 -04:00
|
|
|
runtime: runtime,
|
2013-06-27 20:52:47 -04:00
|
|
|
pullingPool: make(map[string]struct{}),
|
|
|
|
pushingPool: make(map[string]struct{}),
|
|
|
|
}
|
2013-05-28 18:31:06 -04:00
|
|
|
|
2013-06-15 14:07:49 -04:00
|
|
|
buildfile := NewBuildFile(srv, ioutil.Discard)
|
2013-06-20 23:42:19 -04:00
|
|
|
if _, err := buildfile.Build(mkTestContext(ctx.dockerfile, ctx.files, t)); err != nil {
|
2013-06-11 10:39:06 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-05-28 18:31:06 -04:00
|
|
|
}
|
|
|
|
}
|
2013-07-08 09:02:06 -04:00
|
|
|
|
|
|
|
func TestVolume(t *testing.T) {
|
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{
|
|
|
|
runtime: runtime,
|
|
|
|
lock: &sync.Mutex{},
|
|
|
|
pullingPool: make(map[string]struct{}),
|
|
|
|
pushingPool: make(map[string]struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
buildfile := NewBuildFile(srv, ioutil.Discard)
|
|
|
|
imgId, err := buildfile.Build(mkTestContext(`
|
|
|
|
from docker-ut
|
|
|
|
VOLUME /test
|
|
|
|
CMD Hello world
|
|
|
|
`, nil, t))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
img, err := srv.ImageInspect(imgId)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(img.Config.Volumes) == 0 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
for key, _ := range img.Config.Volumes {
|
|
|
|
if key != "/test" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|