2013-05-28 22:31:06 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-07-02 10:47:37 +00:00
|
|
|
"fmt"
|
2013-06-15 18:07:49 +00:00
|
|
|
"io/ioutil"
|
2013-07-08 06:24:52 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
2013-05-28 22:31:06 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-06-15 18:07:49 +00: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-08 06:24:52 +00:00
|
|
|
context, err := mkBuildContext(dockerfile, files)
|
2013-06-15 18:07:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return context
|
|
|
|
}
|
2013-05-28 22:31:06 +00:00
|
|
|
|
2013-06-15 18:07:49 +00: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-07-08 06:24:52 +00:00
|
|
|
// Additional remote files to host on a local HTTP server.
|
|
|
|
remoteFiles [][2]string
|
2013-06-15 18:07:49 +00:00
|
|
|
}
|
2013-06-11 14:39:06 +00:00
|
|
|
|
2013-06-15 18:07:49 +00: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 22:27:22 +00:00
|
|
|
var testContexts = []testContextTemplate{
|
2013-06-15 18:07:49 +00:00
|
|
|
{
|
|
|
|
`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-06-11 14:39:06 +00:00
|
|
|
run sh -c 'echo root:testpass > /tmp/passwd'
|
2013-06-14 01:52:41 +00:00
|
|
|
run mkdir -p /var/run/sshd
|
2013-06-21 03:16:39 +00:00
|
|
|
run [ "$(cat /tmp/passwd)" = "root:testpass" ]
|
|
|
|
run [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]
|
2013-06-15 18:07:49 +00:00
|
|
|
`,
|
|
|
|
nil,
|
2013-07-08 06:24:52 +00:00
|
|
|
nil,
|
2013-06-15 18:07:49 +00:00
|
|
|
},
|
2013-06-15 18:35:56 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-06-21 03:42:19 +00:00
|
|
|
add foo /usr/lib/bla/bar
|
2013-07-08 06:24:52 +00:00
|
|
|
run [ "$(cat /usr/lib/bla/bar)" = 'hello' ]
|
|
|
|
add http://{SERVERADDR}/baz /usr/lib/baz/quux
|
|
|
|
run [ "$(cat /usr/lib/baz/quux)" = 'world!' ]
|
2013-06-21 03:42:19 +00:00
|
|
|
`,
|
2013-07-08 06:24:52 +00:00
|
|
|
[][2]string{{"foo", "hello"}},
|
|
|
|
[][2]string{{"/baz", "world!"}},
|
2013-06-21 03:42:19 +00:00
|
|
|
},
|
2013-06-11 14:39:06 +00:00
|
|
|
|
2013-06-21 03:42:19 +00:00
|
|
|
{
|
|
|
|
`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-06-21 03:42:19 +00: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 18:35:56 +00:00
|
|
|
},
|
2013-07-08 06:24:52 +00:00
|
|
|
nil,
|
2013-06-15 18:35:56 +00:00
|
|
|
},
|
2013-06-14 23:43:39 +00:00
|
|
|
|
2013-07-08 07:43:22 +00:00
|
|
|
{
|
|
|
|
`
|
|
|
|
from {IMAGE}
|
|
|
|
add http://{SERVERADDR}/x /a/b/c
|
|
|
|
run [ "$(cat /a/b/c)" = "hello" ]
|
|
|
|
add http://{SERVERADDR}/x?foo=bar /
|
|
|
|
run [ "$(cat /x)" = "hello" ]
|
|
|
|
add http://{SERVERADDR}/x /d/
|
|
|
|
run [ "$(cat /d/x)" = "hello" ]
|
|
|
|
add http://{SERVERADDR} /e
|
|
|
|
run [ "$(cat /e)" = "blah" ]
|
|
|
|
`,
|
|
|
|
nil,
|
|
|
|
[][2]string{{"/x", "hello"}, {"/", "blah"}},
|
|
|
|
},
|
|
|
|
|
2013-06-21 05:02:36 +00:00
|
|
|
{
|
|
|
|
`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-06-21 05:02:36 +00:00
|
|
|
env FOO BAR
|
|
|
|
run [ "$FOO" = "BAR" ]
|
2013-06-25 02:20:05 +00:00
|
|
|
`,
|
|
|
|
nil,
|
2013-07-08 06:24:52 +00:00
|
|
|
nil,
|
2013-06-25 02:20:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-06-25 02:20:05 +00:00
|
|
|
ENTRYPOINT /bin/echo
|
|
|
|
CMD Hello world
|
2013-07-04 02:33:30 +00:00
|
|
|
`,
|
|
|
|
nil,
|
2013-07-08 06:24:52 +00:00
|
|
|
nil,
|
2013-07-04 02:33:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-07-04 02:33:30 +00:00
|
|
|
VOLUME /test
|
|
|
|
CMD Hello world
|
2013-06-21 05:02:36 +00:00
|
|
|
`,
|
|
|
|
nil,
|
2013-07-08 06:24:52 +00:00
|
|
|
nil,
|
2013-06-21 05:02:36 +00:00
|
|
|
},
|
|
|
|
}
|
2013-06-14 23:43:39 +00:00
|
|
|
|
|
|
|
// FIXME: test building with 2 successive overlapping ADD commands
|
|
|
|
|
2013-07-08 06:24:52 +00:00
|
|
|
func constructDockerfile(template string, ip net.IP, port string) string {
|
|
|
|
serverAddr := fmt.Sprintf("%s:%s", ip, port)
|
|
|
|
replacer := strings.NewReplacer("{IMAGE}", unitTestImageID, "{SERVERADDR}", serverAddr)
|
|
|
|
return replacer.Replace(template)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mkTestingFileServer(files [][2]string) (*httptest.Server, error) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
for _, file := range files {
|
|
|
|
name, contents := file[0], file[1]
|
|
|
|
mux.HandleFunc(name, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(contents))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is how httptest.NewServer sets up a net.Listener, except that our listener must accept remote
|
|
|
|
// connections (from the container).
|
|
|
|
listener, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := httptest.NewUnstartedServer(mux)
|
|
|
|
s.Listener = listener
|
|
|
|
s.Start()
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2013-05-28 22:31:06 +00:00
|
|
|
func TestBuild(t *testing.T) {
|
2013-06-15 18:07:49 +00:00
|
|
|
for _, ctx := range testContexts {
|
2013-07-09 02:06:06 +00:00
|
|
|
buildImage(ctx, t)
|
2013-05-28 22:31:06 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-08 13:02:06 +00:00
|
|
|
|
2013-07-09 02:06:06 +00:00
|
|
|
func buildImage(context testContextTemplate, t *testing.T) *Image {
|
2013-07-08 13:02:06 +00:00
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nuke(runtime)
|
|
|
|
|
|
|
|
srv := &Server{
|
|
|
|
runtime: runtime,
|
|
|
|
pullingPool: make(map[string]struct{}),
|
|
|
|
pushingPool: make(map[string]struct{}),
|
|
|
|
}
|
2013-07-09 02:06:06 +00:00
|
|
|
|
2013-07-08 06:24:52 +00:00
|
|
|
httpServer, err := mkTestingFileServer(context.remoteFiles)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer httpServer.Close()
|
|
|
|
|
|
|
|
idx := strings.LastIndex(httpServer.URL, ":")
|
|
|
|
if idx < 0 {
|
|
|
|
t.Fatalf("could not get port from test http server address %s", httpServer.URL)
|
|
|
|
}
|
|
|
|
port := httpServer.URL[idx+1:]
|
|
|
|
|
|
|
|
ip := runtime.networkManager.bridgeNetwork.IP
|
|
|
|
dockerfile := constructDockerfile(context.dockerfile, ip, port)
|
|
|
|
|
|
|
|
buildfile := NewBuildFile(srv, ioutil.Discard, false)
|
|
|
|
id, err := buildfile.Build(mkTestContext(dockerfile, context.files, t))
|
2013-07-08 13:02:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-09 02:06:06 +00:00
|
|
|
|
|
|
|
img, err := srv.ImageInspect(id)
|
2013-07-08 13:02:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-09 02:06:06 +00:00
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVolume(t *testing.T) {
|
|
|
|
img := buildImage(testContextTemplate{`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-07-09 02:06:06 +00:00
|
|
|
volume /test
|
|
|
|
cmd Hello world
|
2013-07-08 06:24:52 +00:00
|
|
|
`, nil, nil}, t)
|
2013-07-09 02:06:06 +00:00
|
|
|
|
2013-07-08 13:02:06 +00:00
|
|
|
if len(img.Config.Volumes) == 0 {
|
|
|
|
t.Fail()
|
|
|
|
}
|
2013-07-12 00:37:26 +00:00
|
|
|
for key := range img.Config.Volumes {
|
2013-07-08 13:02:06 +00:00
|
|
|
if key != "/test" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 02:06:06 +00:00
|
|
|
|
|
|
|
func TestBuildMaintainer(t *testing.T) {
|
|
|
|
img := buildImage(testContextTemplate{`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-07-09 02:06:06 +00:00
|
|
|
maintainer dockerio
|
2013-07-08 06:24:52 +00:00
|
|
|
`, nil, nil}, t)
|
2013-07-09 02:06:06 +00:00
|
|
|
|
|
|
|
if img.Author != "dockerio" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildEnv(t *testing.T) {
|
|
|
|
img := buildImage(testContextTemplate{`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-07-09 02:06:06 +00:00
|
|
|
env port 4243
|
|
|
|
`,
|
2013-07-08 06:24:52 +00:00
|
|
|
nil, nil}, t)
|
2013-07-09 02:06:06 +00:00
|
|
|
|
|
|
|
if img.Config.Env[0] != "port=4243" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildCmd(t *testing.T) {
|
|
|
|
img := buildImage(testContextTemplate{`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-07-09 02:06:06 +00:00
|
|
|
cmd ["/bin/echo", "Hello World"]
|
|
|
|
`,
|
2013-07-08 06:24:52 +00:00
|
|
|
nil, nil}, t)
|
2013-07-09 02:06:06 +00:00
|
|
|
|
|
|
|
if img.Config.Cmd[0] != "/bin/echo" {
|
|
|
|
t.Log(img.Config.Cmd[0])
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if img.Config.Cmd[1] != "Hello World" {
|
|
|
|
t.Log(img.Config.Cmd[1])
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildExpose(t *testing.T) {
|
|
|
|
img := buildImage(testContextTemplate{`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-07-09 02:06:06 +00:00
|
|
|
expose 4243
|
|
|
|
`,
|
2013-07-08 06:24:52 +00:00
|
|
|
nil, nil}, t)
|
2013-07-09 02:06:06 +00:00
|
|
|
|
|
|
|
if img.Config.PortSpecs[0] != "4243" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildEntrypoint(t *testing.T) {
|
|
|
|
img := buildImage(testContextTemplate{`
|
2013-07-08 06:24:52 +00:00
|
|
|
from {IMAGE}
|
2013-07-09 02:06:06 +00:00
|
|
|
entrypoint ["/bin/echo"]
|
|
|
|
`,
|
2013-07-08 06:24:52 +00:00
|
|
|
nil, nil}, t)
|
2013-07-09 02:06:06 +00:00
|
|
|
|
|
|
|
if img.Config.Entrypoint[0] != "/bin/echo" {
|
|
|
|
}
|
|
|
|
}
|