2013-06-25 10:30:48 +04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2013-07-15 17:12:33 +04:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-06-25 10:30:48 +04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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-07-12 04:59:25 +04:00
|
|
|
func mkRuntime(f Fataler) *Runtime {
|
2013-06-25 10:30:48 +04:00
|
|
|
runtime, err := newTestRuntime()
|
|
|
|
if err != nil {
|
2013-07-12 04:59:25 +04:00
|
|
|
f.Fatal(err)
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
|
|
|
return runtime
|
|
|
|
}
|
|
|
|
|
2013-07-12 04:59:25 +04:00
|
|
|
// A common interface to access the Fatal method of
|
|
|
|
// both testing.B and testing.T.
|
|
|
|
type Fataler interface {
|
|
|
|
Fatal(args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestRuntime() (*Runtime, error) {
|
|
|
|
root, err := ioutil.TempDir("", "docker-test")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := os.Remove(root); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime, err := NewRuntimeFromDirectory(root, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
runtime.UpdateCapabilities(true)
|
|
|
|
return runtime, nil
|
|
|
|
}
|
|
|
|
|
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-07-13 04:56:55 +04:00
|
|
|
func mkContainer(r *Runtime, args []string, t *testing.T) (*Container, *HostConfig, error) {
|
2013-06-25 10:30:48 +04:00
|
|
|
config, hostConfig, _, err := 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-07-13 04:56:55 +04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if config.Image == "_" {
|
|
|
|
config.Image = GetTestImage(r).ID
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
|
|
|
c, err := NewBuilder(r).Create(config)
|
|
|
|
if err != nil {
|
2013-07-13 04:56:55 +04:00
|
|
|
return nil, nil, err
|
2013-06-25 10:30:48 +04:00
|
|
|
}
|
2013-07-13 04:56:55 +04:00
|
|
|
return c, hostConfig, 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.
|
|
|
|
func runContainer(r *Runtime, args []string, t *testing.T) (output string, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil && t != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2013-07-13 04:56:55 +04:00
|
|
|
container, hostConfig, err := mkContainer(r, args, t)
|
|
|
|
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()
|
|
|
|
if err := container.Start(hostConfig); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
container.Wait()
|
|
|
|
data, err := ioutil.ReadAll(stdout)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
output = string(data)
|
|
|
|
return
|
|
|
|
}
|
2013-07-15 17:12:33 +04:00
|
|
|
|
|
|
|
func TestMergeConfig(t *testing.T) {
|
|
|
|
volumesImage := make(map[string]struct{})
|
|
|
|
volumesImage["/test1"] = struct{}{}
|
|
|
|
volumesImage["/test2"] = struct{}{}
|
|
|
|
configImage := &Config{
|
|
|
|
Dns: []string{"1.1.1.1", "2.2.2.2"},
|
|
|
|
PortSpecs: []string{"1111:1111", "2222:2222"},
|
|
|
|
Env: []string{"VAR1=1", "VAR2=2"},
|
|
|
|
Volumes: volumesImage,
|
|
|
|
}
|
|
|
|
|
|
|
|
volumesUser := make(map[string]struct{})
|
|
|
|
volumesUser["/test3"] = struct{}{}
|
|
|
|
configUser := &Config{
|
|
|
|
Dns: []string{"3.3.3.3"},
|
|
|
|
PortSpecs: []string{"2222:3333", "3333:3333"},
|
|
|
|
Env: []string{"VAR2=3", "VAR3=3"},
|
|
|
|
Volumes: volumesUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
MergeConfig(configUser, configImage)
|
|
|
|
|
|
|
|
if len(configUser.Dns) != 3 {
|
|
|
|
t.Fatalf("Expected 3 dns, 1.1.1.1, 2.2.2.2 and 3.3.3.3, found %d", len(configUser.Dns))
|
|
|
|
}
|
|
|
|
for _, dns := range configUser.Dns {
|
|
|
|
if dns != "1.1.1.1" && dns != "2.2.2.2" && dns != "3.3.3.3" {
|
|
|
|
t.Fatalf("Expected 1.1.1.1 or 2.2.2.2 or 3.3.3.3, found %s", dns)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configUser.PortSpecs) != 3 {
|
|
|
|
t.Fatalf("Expected 3 portSpecs, 1111:1111, 2222:3333 and 3333:3333, found %d", len(configUser.PortSpecs))
|
|
|
|
}
|
|
|
|
for _, portSpecs := range configUser.PortSpecs {
|
|
|
|
if portSpecs != "1111:1111" && portSpecs != "2222:3333" && portSpecs != "3333:3333" {
|
|
|
|
t.Fatalf("Expected 1111:1111 or 2222:3333 or 3333:3333, found %s", portSpecs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(configUser.Env) != 3 {
|
|
|
|
t.Fatalf("Expected 3 env var, VAR1=1, VAR2=3 and VAR3=3, found %d", len(configUser.Env))
|
|
|
|
}
|
|
|
|
for _, env := range configUser.Env {
|
|
|
|
if env != "VAR1=1" && env != "VAR2=3" && env != "VAR3=3" {
|
|
|
|
t.Fatalf("Expected VAR1=1 or VAR2=3 or VAR3=3, found %s", env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(configUser.Volumes) != 3 {
|
|
|
|
t.Fatalf("Expected 3 volumes, /test1, /test2 and /test3, found %d", len(configUser.Volumes))
|
|
|
|
}
|
|
|
|
for v, _ := range configUser.Volumes {
|
|
|
|
if v != "/test1" && v != "/test2" && v != "/test3" {
|
|
|
|
t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|