2015-01-28 08:27:46 +01:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
import (
|
2016-09-13 00:07:49 +02:00
|
|
|
"reflect"
|
2015-01-28 08:27:46 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
events "github.com/docker/go-events"
|
|
|
|
|
2017-06-23 21:45:04 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-01-28 08:27:46 +01:00
|
|
|
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEventQueue(t *testing.T) {
|
|
|
|
const nevents = 1000
|
|
|
|
var ts testSink
|
2018-10-11 15:39:02 +02:00
|
|
|
metrics := newSafeMetrics("")
|
2015-01-28 08:27:46 +01:00
|
|
|
eq := newEventQueue(
|
|
|
|
// delayed sync simulates destination slower than channel comms
|
|
|
|
&delayedSink{
|
|
|
|
Sink: &ts,
|
|
|
|
delay: time.Millisecond * 1,
|
|
|
|
}, metrics.eventQueueListener())
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
2018-03-14 01:08:11 +01:00
|
|
|
var event events.Event
|
2015-01-28 08:27:46 +01:00
|
|
|
for i := 1; i <= nevents; i++ {
|
2018-03-14 01:08:11 +01:00
|
|
|
event = createTestEvent("push", "library/test", "blob")
|
|
|
|
wg.Add(1)
|
|
|
|
go func(event events.Event) {
|
|
|
|
if err := eq.Write(event); err != nil {
|
|
|
|
t.Errorf("error writing event block: %v", err)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}(event)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2018-08-06 23:34:15 +02:00
|
|
|
if t.Failed() {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2015-01-28 08:27:46 +01:00
|
|
|
checkClose(t, eq)
|
|
|
|
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
|
|
|
metrics.Lock()
|
|
|
|
defer metrics.Unlock()
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
if ts.count != nevents {
|
|
|
|
t.Fatalf("events did not make it to the sink: %d != %d", ts.count, 1000)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ts.closed {
|
|
|
|
t.Fatalf("sink should have been closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if metrics.Events != nevents {
|
|
|
|
t.Fatalf("unexpected ingress count: %d != %d", metrics.Events, nevents)
|
|
|
|
}
|
|
|
|
|
|
|
|
if metrics.Pending != 0 {
|
|
|
|
t.Fatalf("unexpected egress count: %d != %d", metrics.Pending, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 11:26:54 +01:00
|
|
|
func TestIgnoredSink(t *testing.T) {
|
2016-09-13 00:07:49 +02:00
|
|
|
blob := createTestEvent("push", "library/test", "blob")
|
2018-01-18 11:26:54 +01:00
|
|
|
manifest := createTestEvent("pull", "library/test", "manifest")
|
2016-09-13 00:07:49 +02:00
|
|
|
|
|
|
|
type testcase struct {
|
2018-01-18 11:26:54 +01:00
|
|
|
ignoreMediaTypes []string
|
|
|
|
ignoreActions []string
|
2018-03-14 01:08:11 +01:00
|
|
|
expected events.Event
|
2016-09-13 00:07:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testcase{
|
2018-03-14 01:08:11 +01:00
|
|
|
{nil, nil, blob},
|
|
|
|
{[]string{"other"}, []string{"other"}, blob},
|
2018-01-18 11:26:54 +01:00
|
|
|
{[]string{"blob", "manifest"}, []string{"other"}, nil},
|
2018-03-14 01:08:11 +01:00
|
|
|
{[]string{"other"}, []string{"pull"}, blob},
|
2018-01-18 11:26:54 +01:00
|
|
|
{[]string{"other"}, []string{"pull", "push"}, nil},
|
2016-09-13 00:07:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ts := &testSink{}
|
2018-01-18 11:26:54 +01:00
|
|
|
s := newIgnoredSink(ts, c.ignoreMediaTypes, c.ignoreActions)
|
2016-09-13 00:07:49 +02:00
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
if err := s.Write(blob); err != nil {
|
2016-09-13 00:07:49 +02:00
|
|
|
t.Fatalf("error writing event: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts.mu.Lock()
|
2018-03-14 01:08:11 +01:00
|
|
|
if !reflect.DeepEqual(ts.event, c.expected) {
|
|
|
|
t.Fatalf("unexpected event: %#v != %#v", ts.event, c.expected)
|
2016-09-13 00:07:49 +02:00
|
|
|
}
|
|
|
|
ts.mu.Unlock()
|
|
|
|
}
|
2015-01-28 08:27:46 +01:00
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
cases = []testcase{
|
|
|
|
{nil, nil, manifest},
|
|
|
|
{[]string{"other"}, []string{"other"}, manifest},
|
|
|
|
{[]string{"blob"}, []string{"other"}, manifest},
|
|
|
|
{[]string{"blob", "manifest"}, []string{"other"}, nil},
|
|
|
|
{[]string{"other"}, []string{"push"}, manifest},
|
|
|
|
{[]string{"other"}, []string{"pull", "push"}, nil},
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
for _, c := range cases {
|
|
|
|
ts := &testSink{}
|
|
|
|
s := newIgnoredSink(ts, c.ignoreMediaTypes, c.ignoreActions)
|
2015-01-28 08:27:46 +01:00
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
if err := s.Write(manifest); err != nil {
|
|
|
|
t.Fatalf("error writing event: %v", err)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
ts.mu.Lock()
|
|
|
|
if !reflect.DeepEqual(ts.event, c.expected) {
|
|
|
|
t.Fatalf("unexpected event: %#v != %#v", ts.event, c.expected)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
2018-03-14 01:08:11 +01:00
|
|
|
ts.mu.Unlock()
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testSink struct {
|
2018-03-14 01:08:11 +01:00
|
|
|
event events.Event
|
|
|
|
count int
|
2015-01-28 08:27:46 +01:00
|
|
|
mu sync.Mutex
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
func (ts *testSink) Write(event events.Event) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
2018-03-14 01:08:11 +01:00
|
|
|
ts.event = event
|
|
|
|
ts.count++
|
2015-01-28 08:27:46 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *testSink) Close() error {
|
|
|
|
ts.mu.Lock()
|
|
|
|
defer ts.mu.Unlock()
|
|
|
|
ts.closed = true
|
|
|
|
|
|
|
|
logrus.Infof("closing testSink")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type delayedSink struct {
|
2018-03-14 01:08:11 +01:00
|
|
|
events.Sink
|
2015-01-28 08:27:46 +01:00
|
|
|
delay time.Duration
|
|
|
|
}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
func (ds *delayedSink) Write(event events.Event) error {
|
2015-01-28 08:27:46 +01:00
|
|
|
time.Sleep(ds.delay)
|
2018-03-14 01:08:11 +01:00
|
|
|
return ds.Sink.Write(event)
|
2015-01-28 08:27:46 +01:00
|
|
|
}
|
|
|
|
|
2018-03-14 01:08:11 +01:00
|
|
|
func checkClose(t *testing.T, sink events.Sink) {
|
2015-01-28 08:27:46 +01:00
|
|
|
if err := sink.Close(); err != nil {
|
|
|
|
t.Fatalf("unexpected error closing: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// second close should not crash but should return an error.
|
|
|
|
if err := sink.Close(); err == nil {
|
|
|
|
t.Fatalf("no error on double close")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write after closed should be an error
|
2018-03-14 01:08:11 +01:00
|
|
|
if err := sink.Write(Event{}); err == nil {
|
2015-01-28 08:27:46 +01:00
|
|
|
t.Fatalf("write after closed did not have an error")
|
|
|
|
} else if err != ErrSinkClosed {
|
|
|
|
t.Fatalf("error should be ErrSinkClosed")
|
|
|
|
}
|
|
|
|
}
|