2014-03-11 17:40:06 +00:00
|
|
|
package server
|
2013-11-14 06:08:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2014-05-15 23:27:36 +00:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-11-14 06:08:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPools(t *testing.T) {
|
|
|
|
srv := &Server{
|
2013-11-20 21:51:05 +00:00
|
|
|
pullingPool: make(map[string]chan struct{}),
|
|
|
|
pushingPool: make(map[string]chan struct{}),
|
2013-11-14 06:08:08 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 21:51:05 +00:00
|
|
|
if _, err := srv.poolAdd("pull", "test1"); err != nil {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-20 21:51:05 +00:00
|
|
|
if _, err := srv.poolAdd("pull", "test2"); err != nil {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-20 21:51:05 +00:00
|
|
|
if _, err := srv.poolAdd("push", "test1"); err == nil || err.Error() != "pull test1 is already in progress" {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatalf("Expected `pull test1 is already in progress`")
|
|
|
|
}
|
2013-11-20 21:51:05 +00:00
|
|
|
if _, err := srv.poolAdd("pull", "test1"); err == nil || err.Error() != "pull test1 is already in progress" {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatalf("Expected `pull test1 is already in progress`")
|
|
|
|
}
|
2013-11-20 21:51:05 +00:00
|
|
|
if _, err := srv.poolAdd("wait", "test3"); err == nil || err.Error() != "Unknown pool type" {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatalf("Expected `Unknown pool type`")
|
|
|
|
}
|
2013-11-20 21:51:05 +00:00
|
|
|
if err := srv.poolRemove("pull", "test2"); err != nil {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-20 21:51:05 +00:00
|
|
|
if err := srv.poolRemove("pull", "test2"); err != nil {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-20 21:51:05 +00:00
|
|
|
if err := srv.poolRemove("pull", "test1"); err != nil {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-20 21:51:05 +00:00
|
|
|
if err := srv.poolRemove("push", "test1"); err != nil {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-11-20 21:51:05 +00:00
|
|
|
if err := srv.poolRemove("wait", "test3"); err == nil || err.Error() != "Unknown pool type" {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatalf("Expected `Unknown pool type`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogEvent(t *testing.T) {
|
|
|
|
srv := &Server{
|
|
|
|
events: make([]utils.JSONMessage, 0, 64),
|
2014-05-15 23:27:36 +00:00
|
|
|
listeners: make(map[int64]chan utils.JSONMessage),
|
2013-11-14 06:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
srv.LogEvent("fakeaction", "fakeid", "fakeimage")
|
|
|
|
|
|
|
|
listener := make(chan utils.JSONMessage)
|
|
|
|
srv.Lock()
|
2014-05-15 23:27:36 +00:00
|
|
|
srv.listeners[1337] = listener
|
2013-11-14 06:08:08 +00:00
|
|
|
srv.Unlock()
|
|
|
|
|
|
|
|
srv.LogEvent("fakeaction2", "fakeid", "fakeimage")
|
|
|
|
|
2013-11-25 18:58:17 +00:00
|
|
|
numEvents := len(srv.GetEvents())
|
|
|
|
if numEvents != 2 {
|
|
|
|
t.Fatalf("Expected 2 events, found %d", numEvents)
|
2013-11-14 06:08:08 +00:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
srv.LogEvent("fakeaction3", "fakeid", "fakeimage")
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
srv.LogEvent("fakeaction4", "fakeid", "fakeimage")
|
|
|
|
}()
|
|
|
|
|
|
|
|
setTimeout(t, "Listening for events timed out", 2*time.Second, func() {
|
|
|
|
for i := 2; i < 4; i++ {
|
|
|
|
event := <-listener
|
2013-11-25 18:58:17 +00:00
|
|
|
if event != srv.GetEvents()[i] {
|
2013-11-14 06:08:08 +00:00
|
|
|
t.Fatalf("Event received it different than expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this is duplicated from integration/commands_test.go
|
|
|
|
func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
|
|
|
|
c := make(chan bool)
|
|
|
|
|
|
|
|
// Make sure we are not too long
|
|
|
|
go func() {
|
|
|
|
time.Sleep(d)
|
|
|
|
c <- true
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
f()
|
|
|
|
c <- false
|
|
|
|
}()
|
|
|
|
if <-c && msg != "" {
|
|
|
|
t.Fatal(msg)
|
|
|
|
}
|
|
|
|
}
|