mirror of https://github.com/docker/docs.git
Hack: use helper functions in tests for less copy-pasting
This commit is contained in:
parent
6bdb6f226b
commit
080243f040
|
@ -39,16 +39,11 @@ func TestIDFormat(t *testing.T) {
|
||||||
func TestMultipleAttachRestart(t *testing.T) {
|
func TestMultipleAttachRestart(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := NewBuilder(runtime).Create(
|
container, hostConfig, _ := mkContainer(
|
||||||
&Config{
|
runtime,
|
||||||
Image: GetTestImage(runtime).ID,
|
[]string{"_", "/bin/sh", "-c", "i=1; while [ $i -le 5 ]; do i=`expr $i + 1`; echo hello; done"},
|
||||||
Cmd: []string{"/bin/sh", "-c",
|
t,
|
||||||
"i=1; while [ $i -le 5 ]; do i=`expr $i + 1`; echo hello; done"},
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container)
|
defer runtime.Destroy(container)
|
||||||
|
|
||||||
// Simulate 3 client attaching to the container and stop/restart
|
// Simulate 3 client attaching to the container and stop/restart
|
||||||
|
@ -65,7 +60,6 @@ func TestMultipleAttachRestart(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
hostConfig := &HostConfig{}
|
|
||||||
if err := container.Start(hostConfig); err != nil {
|
if err := container.Start(hostConfig); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -140,19 +134,8 @@ func TestMultipleAttachRestart(t *testing.T) {
|
||||||
func TestDiff(t *testing.T) {
|
func TestDiff(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
builder := NewBuilder(runtime)
|
|
||||||
|
|
||||||
// Create a container and remove a file
|
// Create a container and remove a file
|
||||||
container1, err := builder.Create(
|
container1, _, _ := mkContainer(runtime, []string{"_", "/bin/rm", "/etc/passwd"}, t)
|
||||||
&Config{
|
|
||||||
Image: GetTestImage(runtime).ID,
|
|
||||||
Cmd: []string{"/bin/rm", "/etc/passwd"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container1)
|
defer runtime.Destroy(container1)
|
||||||
|
|
||||||
if err := container1.Run(); err != nil {
|
if err := container1.Run(); err != nil {
|
||||||
|
@ -185,15 +168,7 @@ func TestDiff(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new container from the commited image
|
// Create a new container from the commited image
|
||||||
container2, err := builder.Create(
|
container2, _, _ := mkContainer(runtime, []string{img.ID, "cat", "/etc/passwd"}, t)
|
||||||
&Config{
|
|
||||||
Image: img.ID,
|
|
||||||
Cmd: []string{"cat", "/etc/passwd"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container2)
|
defer runtime.Destroy(container2)
|
||||||
|
|
||||||
if err := container2.Run(); err != nil {
|
if err := container2.Run(); err != nil {
|
||||||
|
@ -212,15 +187,7 @@ func TestDiff(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a new containere
|
// Create a new containere
|
||||||
container3, err := builder.Create(
|
container3, _, _ := mkContainer(runtime, []string{"_", "rm", "/bin/httpd"}, t)
|
||||||
&Config{
|
|
||||||
Image: GetTestImage(runtime).ID,
|
|
||||||
Cmd: []string{"rm", "/bin/httpd"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container3)
|
defer runtime.Destroy(container3)
|
||||||
|
|
||||||
if err := container3.Run(); err != nil {
|
if err := container3.Run(); err != nil {
|
||||||
|
@ -246,17 +213,7 @@ func TestDiff(t *testing.T) {
|
||||||
func TestCommitAutoRun(t *testing.T) {
|
func TestCommitAutoRun(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
container1, _, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
|
||||||
builder := NewBuilder(runtime)
|
|
||||||
container1, err := builder.Create(
|
|
||||||
&Config{
|
|
||||||
Image: GetTestImage(runtime).ID,
|
|
||||||
Cmd: []string{"/bin/sh", "-c", "echo hello > /world"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container1)
|
defer runtime.Destroy(container1)
|
||||||
|
|
||||||
if container1.State.Running {
|
if container1.State.Running {
|
||||||
|
@ -279,14 +236,7 @@ func TestCommitAutoRun(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
||||||
container2, err := builder.Create(
|
container2, hostConfig, _ := mkContainer(runtime, []string{img.ID}, t)
|
||||||
&Config{
|
|
||||||
Image: img.ID,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container2)
|
defer runtime.Destroy(container2)
|
||||||
stdout, err := container2.StdoutPipe()
|
stdout, err := container2.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -296,7 +246,6 @@ func TestCommitAutoRun(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
hostConfig := &HostConfig{}
|
|
||||||
if err := container2.Start(hostConfig); err != nil {
|
if err := container2.Start(hostConfig); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -324,17 +273,7 @@ func TestCommitRun(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
builder := NewBuilder(runtime)
|
container1, hostConfig, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
|
||||||
|
|
||||||
container1, err := builder.Create(
|
|
||||||
&Config{
|
|
||||||
Image: GetTestImage(runtime).ID,
|
|
||||||
Cmd: []string{"/bin/sh", "-c", "echo hello > /world"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container1)
|
defer runtime.Destroy(container1)
|
||||||
|
|
||||||
if container1.State.Running {
|
if container1.State.Running {
|
||||||
|
@ -357,16 +296,7 @@ func TestCommitRun(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
// FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
|
||||||
|
container2, hostConfig, _ := mkContainer(runtime, []string{img.ID, "cat", "/world"}, t)
|
||||||
container2, err := builder.Create(
|
|
||||||
&Config{
|
|
||||||
Image: img.ID,
|
|
||||||
Cmd: []string{"cat", "/world"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container2)
|
defer runtime.Destroy(container2)
|
||||||
stdout, err := container2.StdoutPipe()
|
stdout, err := container2.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -376,7 +306,6 @@ func TestCommitRun(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
hostConfig := &HostConfig{}
|
|
||||||
if err := container2.Start(hostConfig); err != nil {
|
if err := container2.Start(hostConfig); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -403,18 +332,7 @@ func TestCommitRun(t *testing.T) {
|
||||||
func TestStart(t *testing.T) {
|
func TestStart(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := NewBuilder(runtime).Create(
|
container, hostConfig, _ := mkContainer(runtime, []string{"-m", "33554432", "-c", "1000", "-i", "_", "/bin/cat"}, t)
|
||||||
&Config{
|
|
||||||
Image: GetTestImage(runtime).ID,
|
|
||||||
Memory: 33554432,
|
|
||||||
CpuShares: 1000,
|
|
||||||
Cmd: []string{"/bin/cat"},
|
|
||||||
OpenStdin: true,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container)
|
defer runtime.Destroy(container)
|
||||||
|
|
||||||
cStdin, err := container.StdinPipe()
|
cStdin, err := container.StdinPipe()
|
||||||
|
@ -422,7 +340,6 @@ func TestStart(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hostConfig := &HostConfig{}
|
|
||||||
if err := container.Start(hostConfig); err != nil {
|
if err := container.Start(hostConfig); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -445,15 +362,7 @@ func TestStart(t *testing.T) {
|
||||||
func TestRun(t *testing.T) {
|
func TestRun(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
container, err := NewBuilder(runtime).Create(
|
container, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
||||||
&Config{
|
|
||||||
Image: GetTestImage(runtime).ID,
|
|
||||||
Cmd: []string{"ls", "-al"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container)
|
defer runtime.Destroy(container)
|
||||||
|
|
||||||
if container.State.Running {
|
if container.State.Running {
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dotcloud/docker/utils"
|
"github.com/dotcloud/docker/utils"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
@ -247,36 +246,13 @@ func TestGet(t *testing.T) {
|
||||||
runtime := mkRuntime(t)
|
runtime := mkRuntime(t)
|
||||||
defer nuke(runtime)
|
defer nuke(runtime)
|
||||||
|
|
||||||
builder := NewBuilder(runtime)
|
container1, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
||||||
|
|
||||||
container1, err := builder.Create(&Config{
|
|
||||||
Image: GetTestImage(runtime).ID,
|
|
||||||
Cmd: []string{"ls", "-al"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container1)
|
defer runtime.Destroy(container1)
|
||||||
|
|
||||||
container2, err := builder.Create(&Config{
|
container2, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
||||||
Image: GetTestImage(runtime).ID,
|
|
||||||
Cmd: []string{"ls", "-al"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container2)
|
defer runtime.Destroy(container2)
|
||||||
|
|
||||||
container3, err := builder.Create(&Config{
|
container3, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
|
||||||
Image: GetTestImage(runtime).ID,
|
|
||||||
Cmd: []string{"ls", "-al"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime.Destroy(container3)
|
defer runtime.Destroy(container3)
|
||||||
|
|
||||||
if runtime.Get(container1.ID) != container1 {
|
if runtime.Get(container1.ID) != container1 {
|
||||||
|
@ -431,46 +407,14 @@ func TestAllocateUDPPortLocalhost(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRestore(t *testing.T) {
|
func TestRestore(t *testing.T) {
|
||||||
|
runtime1 := mkRuntime(t)
|
||||||
root, err := ioutil.TempDir("", "docker-test")
|
defer nuke(runtime1)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := os.Remove(root); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := utils.CopyDirectory(unitTestStoreBase, root); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
runtime1, err := NewRuntimeFromDirectory(root, false)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
builder := NewBuilder(runtime1)
|
|
||||||
|
|
||||||
// Create a container with one instance of docker
|
// Create a container with one instance of docker
|
||||||
container1, err := builder.Create(&Config{
|
container1, _, _ := mkContainer(runtime1, []string{"_", "ls", "-al"}, t)
|
||||||
Image: GetTestImage(runtime1).ID,
|
|
||||||
Cmd: []string{"ls", "-al"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime1.Destroy(container1)
|
defer runtime1.Destroy(container1)
|
||||||
|
|
||||||
// Create a second container meant to be killed
|
// Create a second container meant to be killed
|
||||||
container2, err := builder.Create(&Config{
|
container2, _, _ := mkContainer(runtime1, []string{"-i", "_", "/bin/cat"}, t)
|
||||||
Image: GetTestImage(runtime1).ID,
|
|
||||||
Cmd: []string{"/bin/cat"},
|
|
||||||
OpenStdin: true,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer runtime1.Destroy(container2)
|
defer runtime1.Destroy(container2)
|
||||||
|
|
||||||
// Start the container non blocking
|
// Start the container non blocking
|
||||||
|
@ -505,7 +449,7 @@ func TestRestore(t *testing.T) {
|
||||||
|
|
||||||
// Here are are simulating a docker restart - that is, reloading all containers
|
// Here are are simulating a docker restart - that is, reloading all containers
|
||||||
// from scratch
|
// from scratch
|
||||||
runtime2, err := NewRuntimeFromDirectory(root, false)
|
runtime2, err := NewRuntimeFromDirectory(runtime1.root, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,20 +84,28 @@ func readFile(src string, t *testing.T) (content string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a test container from the given runtime `r` and run arguments `args`.
|
// Create a test container from the given runtime `r` and run arguments `args`.
|
||||||
// The image name (eg. the XXX in []string{"-i", "-t", "XXX", "bash"}, is dynamically replaced by the current test image.
|
// If the image name is "_", (eg. []string{"-i", "-t", "_", "bash"}, it is
|
||||||
|
// dynamically replaced by the current test image.
|
||||||
// The caller is responsible for destroying the container.
|
// The caller is responsible for destroying the container.
|
||||||
// Call t.Fatal() at the first error.
|
// Call t.Fatal() at the first error.
|
||||||
func mkContainer(r *Runtime, args []string, t *testing.T) (*Container, *HostConfig) {
|
func mkContainer(r *Runtime, args []string, t *testing.T) (*Container, *HostConfig, error) {
|
||||||
config, hostConfig, _, err := ParseRun(args, nil)
|
config, hostConfig, _, err := ParseRun(args, nil)
|
||||||
|
defer func() {
|
||||||
|
if err != nil && t != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if config.Image == "_" {
|
||||||
|
config.Image = GetTestImage(r).ID
|
||||||
}
|
}
|
||||||
config.Image = GetTestImage(r).ID
|
|
||||||
c, err := NewBuilder(r).Create(config)
|
c, err := NewBuilder(r).Create(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
return c, hostConfig
|
return c, hostConfig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a test container, start it, wait for it to complete, destroy it,
|
// Create a test container, start it, wait for it to complete, destroy it,
|
||||||
|
@ -110,7 +118,10 @@ func runContainer(r *Runtime, args []string, t *testing.T) (output string, err e
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
container, hostConfig := mkContainer(r, args, t)
|
container, hostConfig, err := mkContainer(r, args, t)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
defer r.Destroy(container)
|
defer r.Destroy(container)
|
||||||
stdout, err := container.StdoutPipe()
|
stdout, err := container.StdoutPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue