2015-04-03 16:58:56 -04:00
|
|
|
package events
|
|
|
|
|
|
|
|
import (
|
2015-09-15 15:53:13 -04:00
|
|
|
"fmt"
|
2015-04-03 16:58:56 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types/events"
|
2015-04-03 16:58:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEventsLog(t *testing.T) {
|
|
|
|
e := New()
|
2015-10-12 14:54:46 -04:00
|
|
|
_, l1, _ := e.Subscribe()
|
|
|
|
_, l2, _ := e.Subscribe()
|
2015-04-03 16:58:56 -04:00
|
|
|
defer e.Evict(l1)
|
|
|
|
defer e.Evict(l2)
|
|
|
|
count := e.SubscribersCount()
|
|
|
|
if count != 2 {
|
|
|
|
t.Fatalf("Must be 2 subscribers, got %d", count)
|
|
|
|
}
|
2015-12-21 17:55:23 -05:00
|
|
|
actor := events.Actor{
|
|
|
|
ID: "cont",
|
|
|
|
Attributes: map[string]string{"image": "image"},
|
|
|
|
}
|
|
|
|
e.Log("test", events.ContainerEventType, actor)
|
2015-04-03 16:58:56 -04:00
|
|
|
select {
|
|
|
|
case msg := <-l1:
|
2015-12-21 17:55:23 -05:00
|
|
|
jmsg, ok := msg.(events.Message)
|
2015-04-03 16:58:56 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected type %T", msg)
|
|
|
|
}
|
|
|
|
if len(e.events) != 1 {
|
|
|
|
t.Fatalf("Must be only one event, got %d", len(e.events))
|
|
|
|
}
|
|
|
|
if jmsg.Status != "test" {
|
|
|
|
t.Fatalf("Status should be test, got %s", jmsg.Status)
|
|
|
|
}
|
|
|
|
if jmsg.ID != "cont" {
|
|
|
|
t.Fatalf("ID should be cont, got %s", jmsg.ID)
|
|
|
|
}
|
|
|
|
if jmsg.From != "image" {
|
|
|
|
t.Fatalf("From should be image, got %s", jmsg.From)
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Timeout waiting for broadcasted message")
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case msg := <-l2:
|
2015-12-21 17:55:23 -05:00
|
|
|
jmsg, ok := msg.(events.Message)
|
2015-04-03 16:58:56 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected type %T", msg)
|
|
|
|
}
|
|
|
|
if len(e.events) != 1 {
|
|
|
|
t.Fatalf("Must be only one event, got %d", len(e.events))
|
|
|
|
}
|
|
|
|
if jmsg.Status != "test" {
|
|
|
|
t.Fatalf("Status should be test, got %s", jmsg.Status)
|
|
|
|
}
|
|
|
|
if jmsg.ID != "cont" {
|
|
|
|
t.Fatalf("ID should be cont, got %s", jmsg.ID)
|
|
|
|
}
|
|
|
|
if jmsg.From != "image" {
|
|
|
|
t.Fatalf("From should be image, got %s", jmsg.From)
|
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Fatal("Timeout waiting for broadcasted message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventsLogTimeout(t *testing.T) {
|
|
|
|
e := New()
|
2015-10-12 14:54:46 -04:00
|
|
|
_, l, _ := e.Subscribe()
|
2015-04-03 16:58:56 -04:00
|
|
|
defer e.Evict(l)
|
|
|
|
|
|
|
|
c := make(chan struct{})
|
|
|
|
go func() {
|
2015-12-21 17:55:23 -05:00
|
|
|
actor := events.Actor{
|
|
|
|
ID: "image",
|
|
|
|
}
|
|
|
|
e.Log("test", events.ImageEventType, actor)
|
2015-04-03 16:58:56 -04:00
|
|
|
close(c)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("Timeout publishing message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 15:53:13 -04:00
|
|
|
func TestLogEvents(t *testing.T) {
|
2015-04-03 16:58:56 -04:00
|
|
|
e := New()
|
2015-08-19 14:51:14 -04:00
|
|
|
|
2015-09-15 15:53:13 -04:00
|
|
|
for i := 0; i < eventsLimit+16; i++ {
|
|
|
|
action := fmt.Sprintf("action_%d", i)
|
|
|
|
id := fmt.Sprintf("cont_%d", i)
|
|
|
|
from := fmt.Sprintf("image_%d", i)
|
2015-12-21 17:55:23 -05:00
|
|
|
|
|
|
|
actor := events.Actor{
|
|
|
|
ID: id,
|
|
|
|
Attributes: map[string]string{"image": from},
|
|
|
|
}
|
|
|
|
e.Log(action, events.ContainerEventType, actor)
|
2015-09-15 15:53:13 -04:00
|
|
|
}
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
2015-10-12 14:54:46 -04:00
|
|
|
current, l, _ := e.Subscribe()
|
2015-09-15 15:53:13 -04:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
num := i + eventsLimit + 16
|
|
|
|
action := fmt.Sprintf("action_%d", num)
|
|
|
|
id := fmt.Sprintf("cont_%d", num)
|
|
|
|
from := fmt.Sprintf("image_%d", num)
|
2015-12-21 17:55:23 -05:00
|
|
|
|
|
|
|
actor := events.Actor{
|
|
|
|
ID: id,
|
|
|
|
Attributes: map[string]string{"image": from},
|
|
|
|
}
|
|
|
|
e.Log(action, events.ContainerEventType, actor)
|
2015-04-03 16:58:56 -04:00
|
|
|
}
|
|
|
|
if len(e.events) != eventsLimit {
|
|
|
|
t.Fatalf("Must be %d events, got %d", eventsLimit, len(e.events))
|
|
|
|
}
|
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
var msgs []events.Message
|
2015-04-03 16:58:56 -04:00
|
|
|
for len(msgs) < 10 {
|
2015-09-15 15:53:13 -04:00
|
|
|
m := <-l
|
2015-12-21 17:55:23 -05:00
|
|
|
jm, ok := (m).(events.Message)
|
2015-09-15 15:53:13 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected type %T", m)
|
2015-04-03 16:58:56 -04:00
|
|
|
}
|
2015-09-15 15:53:13 -04:00
|
|
|
msgs = append(msgs, jm)
|
|
|
|
}
|
|
|
|
if len(current) != eventsLimit {
|
|
|
|
t.Fatalf("Must be %d events, got %d", eventsLimit, len(current))
|
|
|
|
}
|
|
|
|
first := current[0]
|
|
|
|
if first.Status != "action_16" {
|
|
|
|
t.Fatalf("First action is %s, must be action_16", first.Status)
|
|
|
|
}
|
|
|
|
last := current[len(current)-1]
|
|
|
|
if last.Status != "action_79" {
|
|
|
|
t.Fatalf("Last action is %s, must be action_79", last.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
firstC := msgs[0]
|
|
|
|
if firstC.Status != "action_80" {
|
|
|
|
t.Fatalf("First action is %s, must be action_80", firstC.Status)
|
|
|
|
}
|
|
|
|
lastC := msgs[len(msgs)-1]
|
|
|
|
if lastC.Status != "action_89" {
|
|
|
|
t.Fatalf("Last action is %s, must be action_89", lastC.Status)
|
2015-04-03 16:58:56 -04:00
|
|
|
}
|
|
|
|
}
|