1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/server_test.go

330 lines
6.7 KiB
Go
Raw Normal View History

2013-05-06 10:59:33 -04:00
package docker
import (
"github.com/dotcloud/docker/utils"
2013-07-26 06:01:41 -04:00
"strings"
2013-05-06 10:59:33 -04:00
"testing"
"time"
2013-05-06 10:59:33 -04:00
)
2013-05-15 09:11:39 -04:00
func TestContainerTagImageDelete(t *testing.T) {
runtime := mkRuntime(t)
2013-05-15 09:11:39 -04:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
initialImages, err := srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
2013-05-15 09:11:39 -04:00
if err := srv.runtime.repositories.Set("utest", "tag1", unitTestImageName, false); err != nil {
t.Fatal(err)
}
2013-07-31 03:56:53 -04:00
2013-05-15 09:11:39 -04:00
if err := srv.runtime.repositories.Set("utest/docker", "tag2", unitTestImageName, false); err != nil {
t.Fatal(err)
}
2013-07-31 03:56:53 -04:00
if err := srv.runtime.repositories.Set("utest:5000/docker", "tag3", unitTestImageName, false); err != nil {
2013-05-15 09:11:39 -04:00
t.Fatal(err)
}
images, err := srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
2013-07-31 03:56:53 -04:00
if len(images) != len(initialImages)+3 {
2013-07-02 17:47:43 -04:00
t.Errorf("Expected %d images, %d found", len(initialImages)+2, len(images))
2013-05-15 09:11:39 -04:00
}
if _, err := srv.ImageDelete("utest/docker:tag2", true); err != nil {
2013-05-15 09:11:39 -04:00
t.Fatal(err)
}
images, err = srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
if len(images) != len(initialImages)+2 {
2013-07-02 17:47:43 -04:00
t.Errorf("Expected %d images, %d found", len(initialImages)+2, len(images))
2013-05-15 09:11:39 -04:00
}
2013-07-31 03:56:53 -04:00
if _, err := srv.ImageDelete("utest:5000/docker:tag3", true); err != nil {
2013-05-15 09:11:39 -04:00
t.Fatal(err)
}
images, err = srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
if len(images) != len(initialImages)+1 {
2013-07-02 17:47:43 -04:00
t.Errorf("Expected %d images, %d found", len(initialImages)+1, len(images))
2013-05-15 09:11:39 -04:00
}
if _, err := srv.ImageDelete("utest:tag1", true); err != nil {
2013-05-15 09:11:39 -04:00
t.Fatal(err)
}
images, err = srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
if len(images) != len(initialImages) {
2013-07-02 17:47:43 -04:00
t.Errorf("Expected %d image, %d found", len(initialImages), len(images))
2013-05-15 09:11:39 -04:00
}
}
2013-05-06 10:59:33 -04:00
func TestCreateRm(t *testing.T) {
runtime := mkRuntime(t)
2013-05-06 10:59:33 -04:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil)
2013-05-06 10:59:33 -04:00
if err != nil {
t.Fatal(err)
}
id, err := srv.ContainerCreate(config)
2013-05-06 10:59:33 -04:00
if err != nil {
t.Fatal(err)
}
if len(runtime.List()) != 1 {
t.Errorf("Expected 1 container, %v found", len(runtime.List()))
}
if err = srv.ContainerDestroy(id, true); err != nil {
t.Fatal(err)
}
if len(runtime.List()) != 0 {
t.Errorf("Expected 0 container, %v found", len(runtime.List()))
}
}
2013-07-25 11:20:56 -04:00
func TestCommit(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
srv := &Server{runtime: runtime}
config, _, _, err := ParseRun([]string{GetTestImage(runtime).ID, "/bin/cat"}, nil)
if err != nil {
t.Fatal(err)
}
id, err := srv.ContainerCreate(config)
if err != nil {
t.Fatal(err)
}
if _, err := srv.ContainerCommit(id, "testrepo", "testtag", "", "", config); err != nil {
t.Fatal(err)
}
}
2013-05-06 10:59:33 -04:00
func TestCreateStartRestartStopStartKillRm(t *testing.T) {
runtime := mkRuntime(t)
2013-05-06 10:59:33 -04:00
defer nuke(runtime)
srv := &Server{runtime: runtime}
config, hostConfig, _, err := ParseRun([]string{GetTestImage(runtime).ID, "/bin/cat"}, nil)
2013-05-06 10:59:33 -04:00
if err != nil {
t.Fatal(err)
}
id, err := srv.ContainerCreate(config)
2013-05-06 10:59:33 -04:00
if err != nil {
t.Fatal(err)
}
if len(runtime.List()) != 1 {
t.Errorf("Expected 1 container, %v found", len(runtime.List()))
}
err = srv.ContainerStart(id, hostConfig)
2013-05-06 10:59:33 -04:00
if err != nil {
t.Fatal(err)
}
err = srv.ContainerRestart(id, 1)
if err != nil {
t.Fatal(err)
}
err = srv.ContainerStop(id, 1)
if err != nil {
t.Fatal(err)
}
err = srv.ContainerStart(id, hostConfig)
2013-05-06 10:59:33 -04:00
if err != nil {
t.Fatal(err)
}
err = srv.ContainerKill(id)
if err != nil {
t.Fatal(err)
}
// FIXME: this failed once with a race condition ("Unable to remove filesystem for xxx: directory not empty")
2013-05-06 10:59:33 -04:00
if err = srv.ContainerDestroy(id, true); err != nil {
t.Fatal(err)
}
if len(runtime.List()) != 0 {
t.Errorf("Expected 0 container, %v found", len(runtime.List()))
}
}
func TestRunWithTooLowMemoryLimit(t *testing.T) {
var err error
runtime := mkRuntime(t)
srv := &Server{runtime: runtime}
defer nuke(runtime)
// Try to create a container with a memory limit of 1 byte less than the minimum allowed limit.
_, err = srv.ContainerCreate(
&Config{
Image: GetTestImage(runtime).ID,
Memory: 524287,
CpuShares: 1000,
Cmd: []string{"/bin/cat"},
},
)
if err == nil {
t.Errorf("Memory limit is smaller than the allowed limit. Container creation should've failed!")
}
}
func TestLogEvent(t *testing.T) {
runtime := mkRuntime(t)
srv := &Server{
runtime: runtime,
events: make([]utils.JSONMessage, 0, 64),
listeners: make(map[string]chan utils.JSONMessage),
}
srv.LogEvent("fakeaction", "fakeid")
listener := make(chan utils.JSONMessage)
srv.Lock()
srv.listeners["test"] = listener
srv.Unlock()
srv.LogEvent("fakeaction2", "fakeid")
if len(srv.events) != 2 {
t.Fatalf("Expected 2 events, found %d", len(srv.events))
}
go func() {
time.Sleep(200 * time.Millisecond)
srv.LogEvent("fakeaction3", "fakeid")
time.Sleep(200 * time.Millisecond)
srv.LogEvent("fakeaction4", "fakeid")
}()
setTimeout(t, "Listening for events timed out", 2*time.Second, func() {
for i := 2; i < 4; i++ {
event := <-listener
if event != srv.events[i] {
t.Fatalf("Event received it different than expected")
}
}
})
}
2013-07-26 06:01:41 -04:00
func TestRmi(t *testing.T) {
runtime := mkRuntime(t)
defer nuke(runtime)
srv := &Server{runtime: runtime}
initialImages, err := srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
config, hostConfig, _, err := ParseRun([]string{GetTestImage(runtime).ID, "echo test"}, nil)
if err != nil {
t.Fatal(err)
}
containerID, err := srv.ContainerCreate(config)
if err != nil {
t.Fatal(err)
}
//To remove
err = srv.ContainerStart(containerID, hostConfig)
if err != nil {
t.Fatal(err)
}
imageID, err := srv.ContainerCommit(containerID, "test", "", "", "", nil)
if err != nil {
t.Fatal(err)
}
err = srv.ContainerTag(imageID, "test", "0.1", false)
if err != nil {
t.Fatal(err)
}
containerID, err = srv.ContainerCreate(config)
if err != nil {
t.Fatal(err)
}
//To remove
err = srv.ContainerStart(containerID, hostConfig)
if err != nil {
t.Fatal(err)
}
_, err = srv.ContainerCommit(containerID, "test", "", "", "", nil)
if err != nil {
t.Fatal(err)
}
images, err := srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
if len(images)-len(initialImages) != 2 {
t.Fatalf("Expected 2 new images, found %d.", len(images)-len(initialImages))
}
_, err = srv.ImageDelete(imageID, true)
if err != nil {
t.Fatal(err)
}
images, err = srv.Images(false, "")
if err != nil {
t.Fatal(err)
}
if len(images)-len(initialImages) != 1 {
t.Fatalf("Expected 1 new image, found %d.", len(images)-len(initialImages))
}
for _, image := range images {
if strings.Contains(unitTestImageID, image.ID) {
continue
}
if image.Repository == "" {
t.Fatalf("Expected tagged image, got untagged one.")
}
}
}