2013-06-25 10:30:48 +04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-11-14 10:10:20 +04:00
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
2013-11-18 23:25:13 +04:00
|
|
|
"fmt"
|
2013-06-25 10:30:48 +04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2013-11-16 04:05:57 +04:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2013-06-25 10:30:48 +04:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2013-11-16 04:05:57 +04:00
|
|
|
"time"
|
2014-02-04 03:36:39 +04:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker"
|
|
|
|
"github.com/dotcloud/docker/engine"
|
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-06-25 10:30:48 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains utility functions for docker's unit test suite.
|
|
|
|
// It has to be named XXX_test.go, apparently, in other to access private functions
|
|
|
|
// from other XXX_test.go functions.
|
|
|
|
|
|
|
|
// Create a temporary runtime suitable for unit testing.
|
|
|
|
// Call t.Fatal() at the first error.
|
2013-11-14 10:10:20 +04:00
|
|
|
func mkRuntime(f utils.Fataler) *docker.Runtime {
|
2013-10-27 11:13:45 +04:00
|
|
|
root, err := newTestDirectory(unitTestStoreBase)
|
|
|
|
if err != nil {
|
|
|
|
f.Fatal(err)
|
2013-10-18 10:47:08 +04:00
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
config := &docker.DaemonConfig{
|
2013-11-13 23:25:55 +04:00
|
|
|
Root: root,
|
2013-10-27 11:13:45 +04:00
|
|
|
AutoRestart: false,
|
2014-02-04 03:36:39 +04:00
|
|
|
Mtu: docker.GetDefaultNetworkMtu(),
|
2013-10-27 11:13:45 +04:00
|
|
|
}
|
2014-01-30 23:50:59 +04:00
|
|
|
|
|
|
|
eng, err := engine.New(root)
|
|
|
|
if err != nil {
|
|
|
|
f.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := docker.NewRuntimeFromDirectory(config, eng)
|
2013-06-25 10:30:48 +04:00
|
|
|
if err != nil {
|
2013-07-12 04:59:25 +04:00
|
|
|
f.Fatal(err)
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
2013-10-27 11:13:45 +04:00
|
|
|
return r
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
func createNamedTestContainer(eng *engine.Engine, config *docker.Config, f utils.Fataler, name string) (shortId string) {
|
2013-10-28 06:20:00 +04:00
|
|
|
job := eng.Job("create", name)
|
|
|
|
if err := job.ImportEnv(config); err != nil {
|
|
|
|
f.Fatal(err)
|
|
|
|
}
|
2013-11-20 11:37:03 +04:00
|
|
|
job.Stdout.AddString(&shortId)
|
2013-10-28 06:20:00 +04:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
f.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
2013-07-12 04:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
func createTestContainer(eng *engine.Engine, config *docker.Config, f utils.Fataler) (shortId string) {
|
2013-10-28 06:20:00 +04:00
|
|
|
return createNamedTestContainer(eng, config, f, "")
|
|
|
|
}
|
|
|
|
|
2013-11-16 04:05:57 +04:00
|
|
|
func startContainer(eng *engine.Engine, id string, t utils.Fataler) {
|
|
|
|
job := eng.Job("start", id)
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerRun(eng *engine.Engine, id string, t utils.Fataler) {
|
|
|
|
startContainer(eng, id, t)
|
|
|
|
containerWait(eng, id, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerFileExists(eng *engine.Engine, id, dir string, t utils.Fataler) bool {
|
|
|
|
c := getContainer(eng, id, t)
|
2013-12-06 15:15:14 +04:00
|
|
|
if err := c.Mount(); err != nil {
|
2013-11-16 04:05:57 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-12-06 01:18:02 +04:00
|
|
|
defer c.Unmount()
|
2014-01-30 19:43:53 +04:00
|
|
|
if _, err := os.Stat(path.Join(c.BasefsPath(), dir)); err != nil {
|
2013-11-16 04:05:57 +04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerAttach(eng *engine.Engine, id string, t utils.Fataler) (io.WriteCloser, io.ReadCloser) {
|
|
|
|
c := getContainer(eng, id, t)
|
|
|
|
i, err := c.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
o, err := c.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return i, o
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerWait(eng *engine.Engine, id string, t utils.Fataler) int {
|
|
|
|
return getContainer(eng, id, t).Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerWaitTimeout(eng *engine.Engine, id string, t utils.Fataler) error {
|
|
|
|
return getContainer(eng, id, t).WaitTimeout(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerKill(eng *engine.Engine, id string, t utils.Fataler) {
|
2013-11-17 04:26:42 +04:00
|
|
|
if err := eng.Job("kill", id).Run(); err != nil {
|
2013-11-16 04:05:57 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerRunning(eng *engine.Engine, id string, t utils.Fataler) bool {
|
2013-11-22 00:21:03 +04:00
|
|
|
return getContainer(eng, id, t).State.IsRunning()
|
2013-11-16 04:05:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func containerAssertExists(eng *engine.Engine, id string, t utils.Fataler) {
|
|
|
|
getContainer(eng, id, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerAssertNotExists(eng *engine.Engine, id string, t utils.Fataler) {
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
|
|
|
if c := runtime.Get(id); c != nil {
|
|
|
|
t.Fatal(fmt.Errorf("Container %s should not exist", id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertHttpNotError expect the given response to not have an error.
|
|
|
|
// Otherwise the it causes the test to fail.
|
|
|
|
func assertHttpNotError(r *httptest.ResponseRecorder, t utils.Fataler) {
|
|
|
|
// Non-error http status are [200, 400)
|
|
|
|
if r.Code < http.StatusOK || r.Code >= http.StatusBadRequest {
|
|
|
|
t.Fatal(fmt.Errorf("Unexpected http error: %v", r.Code))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertHttpError expect the given response to have an error.
|
|
|
|
// Otherwise the it causes the test to fail.
|
|
|
|
func assertHttpError(r *httptest.ResponseRecorder, t utils.Fataler) {
|
|
|
|
// Non-error http status are [200, 400)
|
|
|
|
if !(r.Code < http.StatusOK || r.Code >= http.StatusBadRequest) {
|
|
|
|
t.Fatal(fmt.Errorf("Unexpected http success code: %v", r.Code))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContainer(eng *engine.Engine, id string, t utils.Fataler) *docker.Container {
|
|
|
|
runtime := mkRuntimeFromEngine(eng, t)
|
|
|
|
c := runtime.Get(id)
|
2013-11-18 23:25:13 +04:00
|
|
|
if c == nil {
|
2013-11-16 04:05:57 +04:00
|
|
|
t.Fatal(fmt.Errorf("No such container: %s", id))
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
func mkServerFromEngine(eng *engine.Engine, t utils.Fataler) *docker.Server {
|
2013-10-27 06:24:01 +04:00
|
|
|
iSrv := eng.Hack_GetGlobalVar("httpapi.server")
|
|
|
|
if iSrv == nil {
|
2013-10-27 11:16:32 +04:00
|
|
|
panic("Legacy server field not set in engine")
|
2013-10-18 10:47:08 +04:00
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
srv, ok := iSrv.(*docker.Server)
|
2013-10-27 06:24:01 +04:00
|
|
|
if !ok {
|
2013-11-14 10:10:20 +04:00
|
|
|
panic("Legacy server field in engine does not cast to *docker.Server")
|
2013-10-27 06:24:01 +04:00
|
|
|
}
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
func mkRuntimeFromEngine(eng *engine.Engine, t utils.Fataler) *docker.Runtime {
|
|
|
|
iRuntime := eng.Hack_GetGlobalVar("httpapi.runtime")
|
|
|
|
if iRuntime == nil {
|
|
|
|
panic("Legacy runtime field not set in engine")
|
|
|
|
}
|
|
|
|
runtime, ok := iRuntime.(*docker.Runtime)
|
|
|
|
if !ok {
|
|
|
|
panic("Legacy runtime field in engine does not cast to *docker.Runtime")
|
|
|
|
}
|
|
|
|
return runtime
|
|
|
|
}
|
|
|
|
|
2013-10-27 11:13:45 +04:00
|
|
|
func NewTestEngine(t utils.Fataler) *engine.Engine {
|
|
|
|
root, err := newTestDirectory(unitTestStoreBase)
|
2013-07-12 04:59:25 +04:00
|
|
|
if err != nil {
|
2013-10-27 11:13:45 +04:00
|
|
|
t.Fatal(err)
|
2013-07-12 04:59:25 +04:00
|
|
|
}
|
2013-10-27 11:13:45 +04:00
|
|
|
eng, err := engine.New(root)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2013-07-12 04:59:25 +04:00
|
|
|
}
|
2013-10-27 11:13:45 +04:00
|
|
|
// Load default plugins
|
|
|
|
// (This is manually copied and modified from main() until we have a more generic plugin system)
|
2014-01-31 23:30:43 +04:00
|
|
|
job := eng.Job("initserver")
|
2013-10-27 11:13:45 +04:00
|
|
|
job.Setenv("Root", root)
|
|
|
|
job.SetenvBool("AutoRestart", false)
|
2013-11-16 04:05:57 +04:00
|
|
|
// TestGetEnabledCors and TestOptionsRoute require EnableCors=true
|
|
|
|
job.SetenvBool("EnableCors", true)
|
2013-10-27 11:13:45 +04:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
2013-07-12 04:59:25 +04:00
|
|
|
}
|
2013-10-27 11:13:45 +04:00
|
|
|
return eng
|
|
|
|
}
|
2013-07-12 04:59:25 +04:00
|
|
|
|
2013-10-27 11:13:45 +04:00
|
|
|
func newTestDirectory(templateDir string) (dir string, err error) {
|
2013-11-14 10:10:20 +04:00
|
|
|
return utils.TestDirectory(templateDir)
|
2013-10-27 11:13:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCallerName(depth int) string {
|
2013-11-14 10:10:20 +04:00
|
|
|
return utils.GetCallerName(depth)
|
2013-07-12 04:59:25 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 10:30:48 +04:00
|
|
|
// Write `content` to the file at path `dst`, creating it if necessary,
|
|
|
|
// as well as any missing directories.
|
|
|
|
// The file is truncated if it already exists.
|
|
|
|
// Call t.Fatal() at the first error.
|
|
|
|
func writeFile(dst, content string, t *testing.T) {
|
|
|
|
// Create subdirectories if necessary
|
|
|
|
if err := os.MkdirAll(path.Dir(dst), 0700); err != nil && !os.IsExist(err) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-06-26 23:04:55 +04:00
|
|
|
f, err := os.OpenFile(dst, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0700)
|
2013-06-25 10:30:48 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Write content (truncate if it exists)
|
|
|
|
if _, err := io.Copy(f, strings.NewReader(content)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the contents of file at path `src`.
|
|
|
|
// Call t.Fatal() at the first error (including if the file doesn't exist)
|
|
|
|
func readFile(src string, t *testing.T) (content string) {
|
|
|
|
f, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a test container from the given runtime `r` and run arguments `args`.
|
2013-07-13 04:56:55 +04:00
|
|
|
// If the image name is "_", (eg. []string{"-i", "-t", "_", "bash"}, it is
|
|
|
|
// dynamically replaced by the current test image.
|
2013-06-25 10:30:48 +04:00
|
|
|
// The caller is responsible for destroying the container.
|
|
|
|
// Call t.Fatal() at the first error.
|
2013-11-15 00:33:15 +04:00
|
|
|
func mkContainer(r *docker.Runtime, args []string, t *testing.T) (*docker.Container, *docker.HostConfig, error) {
|
|
|
|
config, hc, _, err := docker.ParseRun(args, nil)
|
2013-07-13 04:56:55 +04:00
|
|
|
defer func() {
|
|
|
|
if err != nil && t != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2013-06-25 10:30:48 +04:00
|
|
|
if err != nil {
|
2013-11-15 00:33:15 +04:00
|
|
|
return nil, nil, err
|
2013-07-13 04:56:55 +04:00
|
|
|
}
|
|
|
|
if config.Image == "_" {
|
|
|
|
config.Image = GetTestImage(r).ID
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
2013-10-29 03:58:59 +04:00
|
|
|
c, _, err := r.Create(config, "")
|
2013-06-25 10:30:48 +04:00
|
|
|
if err != nil {
|
2013-11-15 00:33:15 +04:00
|
|
|
return nil, nil, err
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
// NOTE: hostConfig is ignored.
|
|
|
|
// If `args` specify privileged mode, custom lxc conf, external mount binds,
|
|
|
|
// port redirects etc. they will be ignored.
|
|
|
|
// This is because the correct way to set these things is to pass environment
|
|
|
|
// to the `start` job.
|
|
|
|
// FIXME: this helper function should be deprecated in favor of calling
|
|
|
|
// `create` and `start` jobs directly.
|
2013-11-15 00:33:15 +04:00
|
|
|
return c, hc, nil
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a test container, start it, wait for it to complete, destroy it,
|
|
|
|
// and return its standard output as a string.
|
2013-06-26 23:04:55 +04:00
|
|
|
// The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
|
2013-06-25 10:30:48 +04:00
|
|
|
// If t is not nil, call t.Fatal() at the first error. Otherwise return errors normally.
|
2013-11-15 00:33:15 +04:00
|
|
|
func runContainer(eng *engine.Engine, r *docker.Runtime, args []string, t *testing.T) (output string, err error) {
|
2013-06-25 10:30:48 +04:00
|
|
|
defer func() {
|
|
|
|
if err != nil && t != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2013-11-15 00:33:15 +04:00
|
|
|
container, hc, err := mkContainer(r, args, t)
|
2013-07-13 04:56:55 +04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-06-25 10:30:48 +04:00
|
|
|
defer r.Destroy(container)
|
|
|
|
stdout, err := container.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer stdout.Close()
|
2013-11-15 00:33:15 +04:00
|
|
|
|
|
|
|
job := eng.Job("start", container.ID)
|
|
|
|
if err := job.ImportEnv(hc); err != nil {
|
2013-06-25 10:30:48 +04:00
|
|
|
return "", err
|
|
|
|
}
|
2013-11-15 00:33:15 +04:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2013-06-25 10:30:48 +04:00
|
|
|
container.Wait()
|
|
|
|
data, err := ioutil.ReadAll(stdout)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
output = string(data)
|
|
|
|
return
|
|
|
|
}
|
2013-07-15 17:12:33 +04:00
|
|
|
|
2013-11-14 10:10:20 +04:00
|
|
|
// FIXME: this is duplicated from graph_test.go in the docker package.
|
|
|
|
func fakeTar() (io.Reader, error) {
|
|
|
|
content := []byte("Hello world!\n")
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buf)
|
|
|
|
for _, name := range []string{"/etc/postgres/postgres.conf", "/etc/passwd", "/var/log/postgres/postgres.conf"} {
|
|
|
|
hdr := new(tar.Header)
|
|
|
|
hdr.Size = int64(len(content))
|
|
|
|
hdr.Name = name
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return nil, err
|
2013-08-16 03:35:03 +04:00
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
tw.Write([]byte(content))
|
2013-08-16 03:35:03 +04:00
|
|
|
}
|
2013-11-14 10:10:20 +04:00
|
|
|
tw.Close()
|
|
|
|
return buf, nil
|
2013-08-16 03:35:03 +04:00
|
|
|
}
|
2013-12-13 02:39:35 +04:00
|
|
|
|
|
|
|
func getAllImages(eng *engine.Engine, t *testing.T) *engine.Table {
|
|
|
|
return getImages(eng, t, true, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getImages(eng *engine.Engine, t *testing.T, all bool, filter string) *engine.Table {
|
|
|
|
job := eng.Job("images")
|
|
|
|
job.SetenvBool("all", all)
|
|
|
|
job.Setenv("filter", filter)
|
2014-01-22 03:06:23 +04:00
|
|
|
images, err := job.Stdout.AddListTable()
|
2013-12-13 02:39:35 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return images
|
|
|
|
|
|
|
|
}
|