keel/trigger/pubsub/manager_test.go

95 lines
1.9 KiB
Go
Raw Normal View History

2017-06-14 18:40:07 +00:00
package pubsub
import (
"sync"
"time"
2017-09-24 12:06:28 +00:00
"golang.org/x/net/context"
2017-11-01 18:25:28 +00:00
"github.com/keel-hq/keel/approvals"
"github.com/keel-hq/keel/cache/memory"
"github.com/keel-hq/keel/provider"
"github.com/keel-hq/keel/types"
"github.com/keel-hq/keel/util/image"
2017-06-14 18:40:07 +00:00
"testing"
)
type fakeSubscriber struct {
TimesSubscribed int
SubscribedTopicName string
SubscribedSubName string
}
func (s *fakeSubscriber) Subscribe(ctx context.Context, topic, subscription string) error {
s.TimesSubscribed++
s.SubscribedTopicName = topic
s.SubscribedSubName = subscription
for {
select {
case <-ctx.Done():
return nil
}
}
}
2017-07-20 19:40:51 +00:00
type fakeProvider struct {
images []*types.TrackedImage
submitted []types.Event
}
func (p *fakeProvider) Submit(event types.Event) error {
p.submitted = append(p.submitted, event)
return nil
}
func (p *fakeProvider) TrackedImages() ([]*types.TrackedImage, error) {
return p.images, nil
}
func (p *fakeProvider) List() []string {
return []string{"fakeprovider"}
}
func (p *fakeProvider) Stop() {
return
}
func (p *fakeProvider) GetName() string {
return "fp"
}
2017-06-14 18:40:07 +00:00
func TestCheckDeployment(t *testing.T) {
2017-07-20 19:40:51 +00:00
img, _ := image.Parse("gcr.io/v2-namespace/hello-world:1.1")
fp := &fakeProvider{
images: []*types.TrackedImage{
&types.TrackedImage{
Image: img,
Provider: "fp",
},
},
}
2017-09-24 12:06:28 +00:00
2018-10-07 13:17:22 +00:00
mem := memory.NewMemoryCache()
am := approvals.New(mem)
2017-09-24 12:06:28 +00:00
providers := provider.New([]provider.Provider{fp}, am)
2017-07-20 19:40:51 +00:00
2017-06-14 18:40:07 +00:00
fs := &fakeSubscriber{}
mng := &DefaultManager{
2017-07-20 19:40:51 +00:00
providers: providers,
2017-06-14 18:40:07 +00:00
client: fs,
mu: &sync.Mutex{},
ctx: context.Background(),
subscribers: make(map[string]context.Context),
}
2017-07-20 19:40:51 +00:00
err := mng.scan(context.Background())
2017-06-14 18:40:07 +00:00
if err != nil {
2017-07-20 19:40:51 +00:00
t.Errorf("failed to scan: %s", err)
2017-06-14 18:40:07 +00:00
}
// sleeping a bit since our fake subscriber goes into a separate goroutine
time.Sleep(100 * time.Millisecond)
if fs.TimesSubscribed != 1 {
t.Errorf("expected to find one subscription, found: %d", fs.TimesSubscribed)
}
}