2013-05-06 10:59:33 -04:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2014-05-09 14:09:59 -04:00
|
|
|
"bytes"
|
2013-05-06 10:59:33 -04:00
|
|
|
"testing"
|
2014-01-21 19:38:17 -05:00
|
|
|
"time"
|
2014-05-09 14:09:59 -04:00
|
|
|
|
2014-10-01 11:13:59 -04:00
|
|
|
"github.com/docker/docker/builder"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/engine"
|
2013-05-06 10:59:33 -04:00
|
|
|
)
|
|
|
|
|
2014-01-29 19:45:55 -05:00
|
|
|
func TestCreateNumberHostname(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(eng, t).Nuke()
|
2014-01-29 19:45:55 -05:00
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err := parseRun([]string{"-h", "web.0", unitTestImageID, "echo test"})
|
2014-01-29 19:45:55 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createTestContainer(eng, config, t)
|
|
|
|
}
|
|
|
|
|
2013-07-25 11:20:56 -04:00
|
|
|
func TestCommit(t *testing.T) {
|
2013-10-27 22:20:00 -04:00
|
|
|
eng := NewTestEngine(t)
|
2014-10-01 11:13:59 -04:00
|
|
|
b := &builder.BuilderJob{Engine: eng}
|
|
|
|
b.Install()
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(eng, t).Nuke()
|
2013-07-25 11:20:56 -04:00
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err := parseRun([]string{unitTestImageID, "/bin/cat"})
|
2013-07-25 11:20:56 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:20:00 -04:00
|
|
|
id := createTestContainer(eng, config, t)
|
2013-07-25 11:20:56 -04:00
|
|
|
|
2013-12-13 17:29:27 -05:00
|
|
|
job := eng.Job("commit", id)
|
2013-12-11 20:03:48 -05:00
|
|
|
job.Setenv("repo", "testrepo")
|
|
|
|
job.Setenv("tag", "testtag")
|
|
|
|
job.SetenvJson("config", config)
|
|
|
|
if err := job.Run(); err != nil {
|
2013-07-25 11:20:56 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 14:00:18 -05:00
|
|
|
func TestMergeConfigOnCommit(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
2014-10-01 11:13:59 -04:00
|
|
|
b := &builder.BuilderJob{Engine: eng}
|
|
|
|
b.Install()
|
2014-04-17 17:43:01 -04:00
|
|
|
runtime := mkDaemonFromEngine(eng, t)
|
2014-01-30 14:00:18 -05:00
|
|
|
defer runtime.Nuke()
|
|
|
|
|
|
|
|
container1, _, _ := mkContainer(runtime, []string{"-e", "FOO=bar", unitTestImageID, "echo test > /tmp/foo"}, t)
|
2015-02-23 10:15:56 -05:00
|
|
|
defer runtime.Rm(container1)
|
2014-01-30 14:00:18 -05:00
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, _, _, err := parseRun([]string{container1.ID, "cat /tmp/foo"})
|
2014-01-30 14:00:18 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
job := eng.Job("commit", container1.ID)
|
|
|
|
job.Setenv("repo", "testrepo")
|
|
|
|
job.Setenv("tag", "testtag")
|
|
|
|
job.SetenvJson("config", config)
|
2014-05-09 14:09:59 -04:00
|
|
|
var outputBuffer = bytes.NewBuffer(nil)
|
|
|
|
job.Stdout.Add(outputBuffer)
|
2014-01-30 14:00:18 -05:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-05-09 14:09:59 -04:00
|
|
|
container2, _, _ := mkContainer(runtime, []string{engine.Tail(outputBuffer, 1)}, t)
|
2015-02-23 10:15:56 -05:00
|
|
|
defer runtime.Rm(container2)
|
2014-01-30 14:00:18 -05:00
|
|
|
|
2014-05-20 15:36:15 -04:00
|
|
|
job = eng.Job("container_inspect", container1.Name)
|
2014-01-30 14:00:18 -05:00
|
|
|
baseContainer, _ := job.Stdout.AddEnv()
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:36:15 -04:00
|
|
|
job = eng.Job("container_inspect", container2.Name)
|
2014-01-30 14:00:18 -05:00
|
|
|
commitContainer, _ := job.Stdout.AddEnv()
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
baseConfig := baseContainer.GetSubEnv("Config")
|
|
|
|
commitConfig := commitContainer.GetSubEnv("Config")
|
|
|
|
|
|
|
|
if commitConfig.Get("Env") != baseConfig.Get("Env") {
|
|
|
|
t.Fatalf("Env config in committed container should be %v, was %v",
|
|
|
|
baseConfig.Get("Env"), commitConfig.Get("Env"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if baseConfig.Get("Cmd") != "[\"echo test \\u003e /tmp/foo\"]" {
|
|
|
|
t.Fatalf("Cmd in base container should be [\"echo test \\u003e /tmp/foo\"], was %s",
|
|
|
|
baseConfig.Get("Cmd"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if commitConfig.Get("Cmd") != "[\"cat /tmp/foo\"]" {
|
|
|
|
t.Fatalf("Cmd in committed container should be [\"cat /tmp/foo\"], was %s",
|
|
|
|
commitConfig.Get("Cmd"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-21 19:38:17 -05:00
|
|
|
func TestRestartKillWait(t *testing.T) {
|
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
runtime := mkDaemonFromEngine(eng, t)
|
2014-01-21 19:38:17 -05:00
|
|
|
defer runtime.Nuke()
|
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, hostConfig, _, err := parseRun([]string{"-i", unitTestImageID, "/bin/cat"})
|
2014-01-21 19:38:17 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id := createTestContainer(eng, config, t)
|
|
|
|
|
2014-01-16 17:00:18 -05:00
|
|
|
job := eng.Job("containers")
|
|
|
|
job.SetenvBool("all", true)
|
|
|
|
outs, err := job.Stdout.AddListTable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
2014-01-21 19:38:17 -05:00
|
|
|
}
|
|
|
|
|
2014-01-16 17:00:18 -05:00
|
|
|
if len(outs.Data) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(outs.Data))
|
|
|
|
}
|
|
|
|
|
|
|
|
job = eng.Job("start", id)
|
2014-01-21 19:38:17 -05:00
|
|
|
if err := job.ImportEnv(hostConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
job = eng.Job("kill", id)
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:53:43 -04:00
|
|
|
eng = newTestEngine(t, false, runtime.Config().Root)
|
2014-01-16 17:00:18 -05:00
|
|
|
|
2014-08-08 05:12:39 -04:00
|
|
|
job = eng.Job("containers")
|
2014-01-16 17:00:18 -05:00
|
|
|
job.SetenvBool("all", true)
|
|
|
|
outs, err = job.Stdout.AddListTable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(outs.Data) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(outs.Data))
|
2014-01-21 19:38:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(t, "Waiting on stopped container timedout", 5*time.Second, func() {
|
2014-08-08 05:12:39 -04:00
|
|
|
job = eng.Job("wait", outs.Data[0].Get("Id"))
|
2014-01-21 19:38:17 -05:00
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-03-24 08:03:41 -04:00
|
|
|
func TestCreateStartRestartStopStartKillRm(t *testing.T) {
|
2013-10-27 03:13:45 -04:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(eng, t).Nuke()
|
2013-05-06 10:59:33 -04:00
|
|
|
|
2014-10-30 12:35:49 -04:00
|
|
|
config, hostConfig, _, err := parseRun([]string{"-i", unitTestImageID, "/bin/cat"})
|
2013-05-06 10:59:33 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-27 22:20:00 -04:00
|
|
|
id := createTestContainer(eng, config, t)
|
2013-05-06 10:59:33 -04:00
|
|
|
|
2014-08-08 05:12:39 -04:00
|
|
|
job := eng.Job("containers")
|
2014-01-16 17:00:18 -05:00
|
|
|
job.SetenvBool("all", true)
|
|
|
|
outs, err := job.Stdout.AddListTable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(outs.Data) != 1 {
|
|
|
|
t.Errorf("Expected 1 container, %v found", len(outs.Data))
|
2013-05-06 10:59:33 -04:00
|
|
|
}
|
|
|
|
|
2014-01-16 17:00:18 -05:00
|
|
|
job = eng.Job("start", id)
|
2013-10-26 22:24:01 -04:00
|
|
|
if err := job.ImportEnv(hostConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
2013-05-06 10:59:33 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-01-06 20:34:51 -05:00
|
|
|
job = eng.Job("restart", id)
|
2014-09-23 18:20:41 -04:00
|
|
|
job.SetenvInt("t", 2)
|
2014-01-06 20:34:51 -05:00
|
|
|
if err := job.Run(); err != nil {
|
2013-05-06 10:59:33 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-03-24 08:03:41 -04:00
|
|
|
job = eng.Job("stop", id)
|
2014-09-23 18:20:41 -04:00
|
|
|
job.SetenvInt("t", 2)
|
2013-12-11 18:36:50 -05:00
|
|
|
if err := job.Run(); err != nil {
|
2013-05-06 10:59:33 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-10-26 22:24:01 -04:00
|
|
|
job = eng.Job("start", id)
|
|
|
|
if err := job.ImportEnv(hostConfig); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
2013-05-06 10:59:33 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-11-16 19:26:42 -05:00
|
|
|
if err := eng.Job("kill", id).Run(); err != nil {
|
2013-05-06 10:59:33 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-05-24 21:31:47 -04:00
|
|
|
// FIXME: this failed once with a race condition ("Unable to remove filesystem for xxx: directory not empty")
|
2014-08-21 06:40:47 -04:00
|
|
|
job = eng.Job("rm", id)
|
2013-12-11 18:23:38 -05:00
|
|
|
job.SetenvBool("removeVolume", true)
|
|
|
|
if err := job.Run(); err != nil {
|
2013-05-06 10:59:33 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-08 05:12:39 -04:00
|
|
|
job = eng.Job("containers")
|
2014-01-16 17:00:18 -05:00
|
|
|
job.SetenvBool("all", true)
|
|
|
|
outs, err = job.Stdout.AddListTable()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := job.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(outs.Data) != 0 {
|
|
|
|
t.Errorf("Expected 0 container, %v found", len(outs.Data))
|
2013-05-06 10:59:33 -04:00
|
|
|
}
|
|
|
|
}
|
2013-06-14 15:55:00 -04:00
|
|
|
|
|
|
|
func TestRunWithTooLowMemoryLimit(t *testing.T) {
|
2013-10-27 22:20:00 -04:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer mkDaemonFromEngine(eng, t).Nuke()
|
2013-10-08 15:15:29 -04:00
|
|
|
|
2013-06-14 15:55:00 -04:00
|
|
|
// Try to create a container with a memory limit of 1 byte less than the minimum allowed limit.
|
2013-10-27 22:20:00 -04:00
|
|
|
job := eng.Job("create")
|
2013-11-14 01:10:20 -05:00
|
|
|
job.Setenv("Image", unitTestImageID)
|
2013-10-27 22:20:00 -04:00
|
|
|
job.Setenv("Memory", "524287")
|
|
|
|
job.Setenv("CpuShares", "1000")
|
|
|
|
job.SetenvList("Cmd", []string{"/bin/cat"})
|
|
|
|
if err := job.Run(); err == nil {
|
2013-06-14 15:55:00 -04:00
|
|
|
t.Errorf("Memory limit is smaller than the allowed limit. Container creation should've failed!")
|
|
|
|
}
|
|
|
|
}
|
2013-07-16 10:38:18 -04:00
|
|
|
|
2013-09-06 16:16:10 -04:00
|
|
|
func TestImagesFilter(t *testing.T) {
|
2013-11-14 01:10:20 -05:00
|
|
|
eng := NewTestEngine(t)
|
2014-04-17 17:43:01 -04:00
|
|
|
defer nuke(mkDaemonFromEngine(eng, t))
|
2013-09-06 16:16:10 -04:00
|
|
|
|
2013-12-11 20:52:41 -05:00
|
|
|
if err := eng.Job("tag", unitTestImageName, "utest", "tag1").Run(); err != nil {
|
2013-09-06 16:16:10 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:52:41 -05:00
|
|
|
if err := eng.Job("tag", unitTestImageName, "utest/docker", "tag2").Run(); err != nil {
|
2013-09-06 16:16:10 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-12-11 20:52:41 -05:00
|
|
|
|
|
|
|
if err := eng.Job("tag", unitTestImageName, "utest:5000/docker", "tag3").Run(); err != nil {
|
2013-09-06 16:16:10 -04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
images := getImages(eng, t, false, "utest*/*")
|
2013-09-06 16:16:10 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
if len(images.Data[0].GetList("RepoTags")) != 2 {
|
2013-09-06 16:16:10 -04:00
|
|
|
t.Fatal("incorrect number of matches returned")
|
|
|
|
}
|
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
images = getImages(eng, t, false, "utest")
|
2013-09-06 16:16:10 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
if len(images.Data[0].GetList("RepoTags")) != 1 {
|
2013-09-06 16:16:10 -04:00
|
|
|
t.Fatal("incorrect number of matches returned")
|
|
|
|
}
|
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
images = getImages(eng, t, false, "utest*")
|
2013-09-06 16:16:10 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
if len(images.Data[0].GetList("RepoTags")) != 1 {
|
2013-09-06 16:16:10 -04:00
|
|
|
t.Fatal("incorrect number of matches returned")
|
|
|
|
}
|
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
images = getImages(eng, t, false, "*5000*/*")
|
2013-09-06 16:16:10 -04:00
|
|
|
|
2013-12-12 17:39:35 -05:00
|
|
|
if len(images.Data[0].GetList("RepoTags")) != 1 {
|
2013-09-06 16:16:10 -04:00
|
|
|
t.Fatal("incorrect number of matches returned")
|
|
|
|
}
|
|
|
|
}
|