From e7c82564d03da3b501aed746e91c9b5ea6408fa2 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 16 Jul 2013 14:38:18 +0000 Subject: [PATCH 1/7] add server.ContainerTop, server.poolAdd and ser.poolRemove tests Upstream-commit: fb005a3da813ba855b6597c146b66842846df3d8 Component: engine --- components/engine/server.go | 2 +- components/engine/server_test.go | 88 ++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/components/engine/server.go b/components/engine/server.go index c43cae0c38..48a64aa8c8 100644 --- a/components/engine/server.go +++ b/components/engine/server.go @@ -475,7 +475,7 @@ func (srv *Server) poolAdd(kind, key string) error { defer srv.Unlock() if _, exists := srv.pullingPool[key]; exists { - return fmt.Errorf("%s %s is already in progress", key, kind) + return fmt.Errorf("pull %s is already in progress", key) } switch kind { diff --git a/components/engine/server_test.go b/components/engine/server_test.go index 05a286aaa8..5181a501c9 100644 --- a/components/engine/server_test.go +++ b/components/engine/server_test.go @@ -2,6 +2,7 @@ package docker import ( "testing" + "time" ) func TestContainerTagImageDelete(t *testing.T) { @@ -163,3 +164,90 @@ func TestRunWithTooLowMemoryLimit(t *testing.T) { } } + +func TestContainerTop(t *testing.T) { + runtime := mkRuntime(t) + srv := &Server{runtime: runtime} + defer nuke(runtime) + + c, hostConfig := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "sleep 2"}, t) + defer runtime.Destroy(c) + if err := c.Start(hostConfig); err != nil { + t.Fatal(err) + } + + // Give some time to the process to start + c.WaitTimeout(500 * time.Millisecond) + + if !c.State.Running { + t.Errorf("Container should be running") + } + procs, err := srv.ContainerTop(c.ID) + if err != nil { + t.Fatal(err) + } + + if len(procs) != 2 { + t.Fatalf("Expected 2 processes, found %d.", len(procs)) + } + + if procs[0].Cmd != "sh" && procs[0].Cmd != "busybox" { + t.Fatalf("Expected `busybox` or `sh`, found %s.", procs[0].Cmd) + } + + if procs[1].Cmd != "sh" && procs[1].Cmd != "busybox" { + t.Fatalf("Expected `busybox` or `sh`, found %s.", procs[1].Cmd) + } +} + +func TestPools(t *testing.T) { + runtime := mkRuntime(t) + srv := &Server{ + runtime: runtime, + pullingPool: make(map[string]struct{}), + pushingPool: make(map[string]struct{}), + } + defer nuke(runtime) + + err := srv.poolAdd("pull", "test1") + if err != nil { + t.Fatal(err) + } + err = srv.poolAdd("pull", "test2") + if err != nil { + t.Fatal(err) + } + err = srv.poolAdd("push", "test1") + if err == nil || err.Error() != "pull test1 is already in progress" { + t.Fatalf("Expected `pull test1 is already in progress`") + } + err = srv.poolAdd("pull", "test1") + if err == nil || err.Error() != "pull test1 is already in progress" { + t.Fatalf("Expected `pull test1 is already in progress`") + } + err = srv.poolAdd("wait", "test3") + if err == nil || err.Error() != "Unkown pool type" { + t.Fatalf("Expected `Unkown pool type`") + } + + err = srv.poolRemove("pull", "test2") + if err != nil { + t.Fatal(err) + } + err = srv.poolRemove("pull", "test2") + if err != nil { + t.Fatal(err) + } + err = srv.poolRemove("pull", "test1") + if err != nil { + t.Fatal(err) + } + err = srv.poolRemove("push", "test1") + if err != nil { + t.Fatal(err) + } + err = srv.poolRemove("wait", "test3") + if err == nil || err.Error() != "Unkown pool type" { + t.Fatalf("Expected `Unkown pool type`") + } +} From edde9aeb1cc8053ce910154883fe75f9a6c0774a Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Tue, 16 Jul 2013 15:58:23 +0000 Subject: [PATCH 2/7] Add CompareConfig test Upstream-commit: 48a892bee5270feea3d0e7b64963acb38dbd634b Component: engine --- components/engine/utils_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/components/engine/utils_test.go b/components/engine/utils_test.go index af2dcc88c5..31489446f8 100644 --- a/components/engine/utils_test.go +++ b/components/engine/utils_test.go @@ -129,6 +129,30 @@ func runContainer(r *Runtime, args []string, t *testing.T) (output string, err e return } +func TestCompareConfig(t *testing.T) { + config1 := Config{ + Dns: []string{"1.1.1.1", "2.2.2.2"}, + PortSpecs: []string{"1111:1111", "2222:2222"}, + Env: []string{"VAR1=1", "VAR2=2"}, + } + config2 := Config{ + Dns: []string{"0.0.0.0", "2.2.2.2"}, + PortSpecs: []string{"1111:1111", "2222:2222"}, + Env: []string{"VAR1=1", "VAR2=2"}, + } + config3 := Config{ + Dns: []string{"1.1.1.1", "2.2.2.2"}, + PortSpecs: []string{"0000:0000", "2222:2222"}, + Env: []string{"VAR1=1", "VAR2=2"}, + } + if CompareConfig(&config1, &config2) { + t.Fatalf("CompareConfig should return false, Dns are different") + } + if CompareConfig(&config1, &config3) { + t.Fatalf("CompareConfig should return false, PortSpecs are different") + } +} + func TestMergeConfig(t *testing.T) { volumesImage := make(map[string]struct{}) volumesImage["/test1"] = struct{}{} From fb0bdc7ba877b26628b432d0ae752c08c867e4f5 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Wed, 17 Jul 2013 20:39:36 +0000 Subject: [PATCH 3/7] prevent any kind of operation simultaneously Upstream-commit: 2db99441c85fdf1e7d468bc02b085d9e1b95704c Component: engine --- components/engine/server.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/engine/server.go b/components/engine/server.go index 48a64aa8c8..de20ff65c7 100644 --- a/components/engine/server.go +++ b/components/engine/server.go @@ -477,6 +477,9 @@ func (srv *Server) poolAdd(kind, key string) error { if _, exists := srv.pullingPool[key]; exists { return fmt.Errorf("pull %s is already in progress", key) } + if _, exists := srv.pushingPool[key]; exists { + return fmt.Errorf("push %s is already in progress", key) + } switch kind { case "pull": From ef71cbddf6706a2037a9cb7c40575f8ca3fcb45e Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Wed, 17 Jul 2013 20:51:25 +0000 Subject: [PATCH 4/7] add Volumes and VolumesFrom to CompareConfig Upstream-commit: 7c00201222e29989214b7e8a221b057daba0a27d Component: engine --- components/engine/utils.go | 11 +++++-- components/engine/utils_test.go | 51 +++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/components/engine/utils.go b/components/engine/utils.go index 33cfbe506f..4f3a846d75 100644 --- a/components/engine/utils.go +++ b/components/engine/utils.go @@ -18,14 +18,16 @@ func CompareConfig(a, b *Config) bool { a.MemorySwap != b.MemorySwap || a.CpuShares != b.CpuShares || a.OpenStdin != b.OpenStdin || - a.Tty != b.Tty { + a.Tty != b.Tty || + a.VolumesFrom != b.VolumesFrom { return false } if len(a.Cmd) != len(b.Cmd) || len(a.Dns) != len(b.Dns) || len(a.Env) != len(b.Env) || len(a.PortSpecs) != len(b.PortSpecs) || - len(a.Entrypoint) != len(b.Entrypoint) { + len(a.Entrypoint) != len(b.Entrypoint) || + len(a.Volumes) != len(b.Volumes) { return false } @@ -54,6 +56,11 @@ func CompareConfig(a, b *Config) bool { return false } } + for key := range a.Volumes { + if _, exists := b.Volumes[key]; !exists { + return false + } + } return true } diff --git a/components/engine/utils_test.go b/components/engine/utils_test.go index 31489446f8..95f3d4bfdd 100644 --- a/components/engine/utils_test.go +++ b/components/engine/utils_test.go @@ -130,20 +130,44 @@ func runContainer(r *Runtime, args []string, t *testing.T) (output string, err e } func TestCompareConfig(t *testing.T) { + volumes1 := make(map[string]struct{}) + volumes1["/test1"] = struct{}{} config1 := Config{ - Dns: []string{"1.1.1.1", "2.2.2.2"}, - PortSpecs: []string{"1111:1111", "2222:2222"}, - Env: []string{"VAR1=1", "VAR2=2"}, + Dns: []string{"1.1.1.1", "2.2.2.2"}, + PortSpecs: []string{"1111:1111", "2222:2222"}, + Env: []string{"VAR1=1", "VAR2=2"}, + VolumesFrom: "11111111", + Volumes: volumes1, } config2 := Config{ - Dns: []string{"0.0.0.0", "2.2.2.2"}, - PortSpecs: []string{"1111:1111", "2222:2222"}, - Env: []string{"VAR1=1", "VAR2=2"}, + Dns: []string{"0.0.0.0", "2.2.2.2"}, + PortSpecs: []string{"1111:1111", "2222:2222"}, + Env: []string{"VAR1=1", "VAR2=2"}, + VolumesFrom: "11111111", + Volumes: volumes1, } config3 := Config{ - Dns: []string{"1.1.1.1", "2.2.2.2"}, - PortSpecs: []string{"0000:0000", "2222:2222"}, - Env: []string{"VAR1=1", "VAR2=2"}, + Dns: []string{"1.1.1.1", "2.2.2.2"}, + PortSpecs: []string{"0000:0000", "2222:2222"}, + Env: []string{"VAR1=1", "VAR2=2"}, + VolumesFrom: "11111111", + Volumes: volumes1, + } + config4 := Config{ + Dns: []string{"1.1.1.1", "2.2.2.2"}, + PortSpecs: []string{"0000:0000", "2222:2222"}, + Env: []string{"VAR1=1", "VAR2=2"}, + VolumesFrom: "22222222", + Volumes: volumes1, + } + volumes2 := make(map[string]struct{}) + volumes2["/test2"] = struct{}{} + config5 := Config{ + Dns: []string{"1.1.1.1", "2.2.2.2"}, + PortSpecs: []string{"0000:0000", "2222:2222"}, + Env: []string{"VAR1=1", "VAR2=2"}, + VolumesFrom: "11111111", + Volumes: volumes2, } if CompareConfig(&config1, &config2) { t.Fatalf("CompareConfig should return false, Dns are different") @@ -151,6 +175,15 @@ func TestCompareConfig(t *testing.T) { if CompareConfig(&config1, &config3) { t.Fatalf("CompareConfig should return false, PortSpecs are different") } + if CompareConfig(&config1, &config4) { + t.Fatalf("CompareConfig should return false, VolumesFrom are different") + } + if CompareConfig(&config1, &config5) { + t.Fatalf("CompareConfig should return false, Volumes are different") + } + if !CompareConfig(&config1, &config1) { + t.Fatalf("CompareConfig should return true") + } } func TestMergeConfig(t *testing.T) { From 13dc02d1d85b14eba9e9f2f189340dd3dcd476dd Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Wed, 17 Jul 2013 21:06:46 +0000 Subject: [PATCH 5/7] add VolumesFrom to MergeConfig, and test Upstream-commit: 1a226f0e28e7da4eb3701b31dbe959142c42b752 Component: engine --- components/engine/utils.go | 3 +++ components/engine/utils_test.go | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/components/engine/utils.go b/components/engine/utils.go index 4f3a846d75..85bf61e8e2 100644 --- a/components/engine/utils.go +++ b/components/engine/utils.go @@ -132,6 +132,9 @@ func MergeConfig(userConf, imageConf *Config) { if userConf.Entrypoint == nil || len(userConf.Entrypoint) == 0 { userConf.Entrypoint = imageConf.Entrypoint } + if userConf.VolumesFrom == "" { + userConf.VolumesFrom = imageConf.VolumesFrom + } if userConf.Volumes == nil || len(userConf.Volumes) == 0 { userConf.Volumes = imageConf.Volumes } else { diff --git a/components/engine/utils_test.go b/components/engine/utils_test.go index 95f3d4bfdd..90292ef9d0 100644 --- a/components/engine/utils_test.go +++ b/components/engine/utils_test.go @@ -191,10 +191,11 @@ func TestMergeConfig(t *testing.T) { 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, + Dns: []string{"1.1.1.1", "2.2.2.2"}, + PortSpecs: []string{"1111:1111", "2222:2222"}, + Env: []string{"VAR1=1", "VAR2=2"}, + VolumesFrom: "1111", + Volumes: volumesImage, } volumesUser := make(map[string]struct{}) @@ -242,4 +243,8 @@ func TestMergeConfig(t *testing.T) { t.Fatalf("Expected /test1 or /test2 or /test3, found %s", v) } } + + if configUser.VolumesFrom != "1111" { + t.Fatalf("Expected VolumesFrom to be 1111, found %s", configUser.VolumesFrom) + } } From a60f1182debc702bbc3d00dc3501f198de5e4019 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 7 Aug 2013 19:07:39 +0000 Subject: [PATCH 6/7] Modify test to accept error from mkContainer Upstream-commit: ced93bcabdc0dd0f6dcfa52174be552adf6c5bd1 Component: engine --- components/engine/server_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/components/engine/server_test.go b/components/engine/server_test.go index 58aa66bd20..911a4b1c7f 100644 --- a/components/engine/server_test.go +++ b/components/engine/server_test.go @@ -171,7 +171,10 @@ func TestContainerTop(t *testing.T) { srv := &Server{runtime: runtime} defer nuke(runtime) - c, hostConfig := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "sleep 2"}, t) + c, hostConfig, err := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "sleep 2"}, t) + if err != nil { + t.Fatal(err) + } defer runtime.Destroy(c) if err := c.Start(hostConfig); err != nil { t.Fatal(err) From cf9140c17a3e20ab6c768e3ef2065538b6fc6508 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 8 Aug 2013 14:56:37 +0000 Subject: [PATCH 7/7] rebase master Upstream-commit: c804a5f827f31c5300f34ec4614a5067a0c17456 Component: engine --- components/engine/server_test.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/components/engine/server_test.go b/components/engine/server_test.go index 0e683a4909..19923eb7dd 100644 --- a/components/engine/server_test.go +++ b/components/engine/server_test.go @@ -210,7 +210,7 @@ func TestContainerTop(t *testing.T) { srv := &Server{runtime: runtime} defer nuke(runtime) - c, hostConfig := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "sleep 2"}, t) + c, hostConfig, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "sleep 2"}, t) defer runtime.Destroy(c) if err := c.Start(hostConfig); err != nil { t.Fatal(err) @@ -222,21 +222,33 @@ func TestContainerTop(t *testing.T) { if !c.State.Running { t.Errorf("Container should be running") } - procs, err := srv.ContainerTop(c.ID) + procs, err := srv.ContainerTop(c.ID, "") if err != nil { t.Fatal(err) } - if len(procs) != 2 { - t.Fatalf("Expected 2 processes, found %d.", len(procs)) + if len(procs.Processes) != 2 { + t.Fatalf("Expected 2 processes, found %d.", len(procs.Processes)) } - if procs[0].Cmd != "sh" && procs[0].Cmd != "busybox" { - t.Fatalf("Expected `busybox` or `sh`, found %s.", procs[0].Cmd) + pos := -1 + for i := 0; i < len(procs.Titles); i++ { + if procs.Titles[i] == "CMD" { + pos = i + break + } } - if procs[1].Cmd != "sh" && procs[1].Cmd != "busybox" { - t.Fatalf("Expected `busybox` or `sh`, found %s.", procs[1].Cmd) + if pos == -1 { + t.Fatalf("Expected CMD, not found.") + } + + if procs.Processes[0][pos] != "sh" && procs.Processes[0][pos] != "busybox" { + t.Fatalf("Expected `busybox` or `sh`, found %s.", procs.Processes[0][pos]) + } + + if procs.Processes[1][pos] != "sh" && procs.Processes[1][pos] != "busybox" { + t.Fatalf("Expected `busybox` or `sh`, found %s.", procs.Processes[1][pos]) } }