| Index: milo/common/pubsub_test.go
|
| diff --git a/milo/common/pubsub_test.go b/milo/common/pubsub_test.go
|
| index 4ac7886d91ec9ec43f73b9a5b5c37aa7bc3648ff..aa9ca3b889a5ed27b20f7a6b7ef02bfe52c477d2 100644
|
| --- a/milo/common/pubsub_test.go
|
| +++ b/milo/common/pubsub_test.go
|
| @@ -5,7 +5,6 @@
|
| package common
|
|
|
| import (
|
| - "errors"
|
| "fmt"
|
| "testing"
|
|
|
| @@ -13,6 +12,7 @@ import (
|
| "golang.org/x/net/context"
|
|
|
| "github.com/luci/gae/impl/memory"
|
| + "github.com/luci/luci-go/common/errors"
|
| "github.com/luci/luci-go/common/logging/gologger"
|
|
|
| . "github.com/smartystreets/goconvey/convey"
|
| @@ -26,7 +26,7 @@ type testPubSubClient struct {
|
| }
|
|
|
| // Topic returns an empty pubsub topic reference.
|
| -func (client *testPubSubClient) getTopic(id string) (*pubsub.Topic, error) {
|
| +func (client *testPubSubClient) getTopic(c context.Context, id string) (*pubsub.Topic, error) {
|
| if err, ok := client.topics[id]; ok {
|
| return &pubsub.Topic{}, err
|
| }
|
| @@ -34,7 +34,7 @@ func (client *testPubSubClient) getTopic(id string) (*pubsub.Topic, error) {
|
| }
|
|
|
| // Subscription returns an empty subscription reference.
|
| -func (client *testPubSubClient) getSubscription(id string) (
|
| +func (client *testPubSubClient) getSubscription(c context.Context, id string) (
|
| *pubsub.Subscription, error) {
|
| if err, ok := client.subscriptions[id]; ok {
|
| return &pubsub.Subscription{}, err
|
| @@ -45,7 +45,7 @@ func (client *testPubSubClient) getSubscription(id string) (
|
| // CreateSubscription records that an attempt to create a subscription with
|
| // an id, then returns an empty subscription.
|
| func (client *testPubSubClient) createSubscription(
|
| - id string, cfg pubsub.SubscriptionConfig) (
|
| + c context.Context, id string, cfg pubsub.SubscriptionConfig) (
|
| *pubsub.Subscription, error) {
|
|
|
| if err, ok := client.createdSubsErr[id]; ok {
|
| @@ -55,59 +55,87 @@ func (client *testPubSubClient) createSubscription(
|
| panic(fmt.Errorf("test error: unknown created sub %s", id))
|
| }
|
|
|
| +type testFactory struct {
|
| + clients map[string]pubsubClient
|
| +}
|
| +
|
| +// makeTestClientFactory returns a closed pubsubClientFactory.
|
| +// Golang Protip: A bound method will not match the function type signature
|
| +// of an unbound function, but a closed function will.
|
| +func (fac *testFactory) makeTestClientFactory() pubsubClientFactory {
|
| + return func(c context.Context, projectID string) (pubsubClient, error) {
|
| + if cli, ok := fac.clients[projectID]; ok {
|
| + return cli, nil
|
| + }
|
| + return nil, fmt.Errorf("client for project %s does not exist", projectID)
|
| + }
|
| +}
|
| +
|
| func TestPubSub(t *testing.T) {
|
| t.Parallel()
|
|
|
| Convey("Test Environment", t, func() {
|
| c := memory.UseWithAppID(context.Background(), "dev~luci-milo")
|
| c = gologger.StdConfig.Use(c)
|
| - client := &testPubSubClient{
|
| + miloClient := &testPubSubClient{
|
| + topics: map[string]error{},
|
| + subscriptions: map[string]error{},
|
| + createdSubsErr: map[string]error{},
|
| + createdSubs: map[string]pubsub.SubscriptionConfig{}}
|
| + bbClient := &testPubSubClient{
|
| topics: map[string]error{},
|
| subscriptions: map[string]error{},
|
| createdSubsErr: map[string]error{},
|
| createdSubs: map[string]pubsub.SubscriptionConfig{}}
|
| - c = context.WithValue(c, &pubSubClientKey, client)
|
| + fac := testFactory{
|
| + clients: map[string]pubsubClient{
|
| + "luci-milo": miloClient,
|
| + "buildbucket": bbClient,
|
| + },
|
| + }
|
| + c = context.WithValue(c, &pubsubClientFactoryKey, fac.makeTestClientFactory())
|
|
|
| Convey("Buildbucket PubSub subscriber", func() {
|
| - proj := "foo"
|
| + proj := "buildbucket"
|
| Convey("Non-existant topic", func() {
|
| - client.topics["builds"] = errNotExist
|
| + bbClient.topics["builds"] = errNotExist
|
| err := ensureBuildbucketSubscribed(c, proj)
|
| So(err.Error(), ShouldEndWith, "does not exist")
|
| })
|
| Convey("Permission denied", func() {
|
| pErr := errors.New(
|
| "something PermissionDenied something")
|
| - client.topics["builds"] = pErr
|
| + bbClient.topics["builds"] = pErr
|
| err := ensureBuildbucketSubscribed(c, proj)
|
| So(err, ShouldEqual, pErr)
|
| })
|
| Convey("Normal error", func() {
|
| pErr := errors.New("foobar")
|
| - client.topics["builds"] = pErr
|
| + bbClient.topics["builds"] = pErr
|
| err := ensureBuildbucketSubscribed(c, proj)
|
| So(err, ShouldEqual, pErr)
|
| })
|
| - client.topics["builds"] = nil
|
| + bbClient.topics["builds"] = nil
|
| Convey("Subscription exists", func() {
|
| - client.subscriptions["luci-milo"] = nil
|
| + miloClient.subscriptions["buildbucket"] = nil
|
| err := ensureBuildbucketSubscribed(c, proj)
|
| So(err, ShouldBeNil)
|
| - So(len(client.createdSubs), ShouldEqual, 0)
|
| + So(len(miloClient.createdSubs), ShouldEqual, 0)
|
| + So(len(bbClient.createdSubs), ShouldEqual, 0)
|
| })
|
| - client.subscriptions["luci-milo"] = errNotExist
|
| + miloClient.subscriptions["buildbucket"] = errNotExist
|
| Convey("Not registered", func() {
|
| errNotReg := errors.New("The supplied HTTP URL is not registered")
|
| - client.createdSubsErr["luci-milo"] = errNotReg
|
| + miloClient.createdSubsErr["buildbucket"] = errNotReg
|
| err := ensureBuildbucketSubscribed(c, proj)
|
| - So(err, ShouldEqual, errNotReg)
|
| + So((err.(errors.Wrapped)).InnerError(), ShouldEqual, errNotReg)
|
| })
|
| Convey("Create subscription", func() {
|
| - client.createdSubsErr["luci-milo"] = nil
|
| + miloClient.createdSubsErr["buildbucket"] = nil
|
| err := ensureBuildbucketSubscribed(c, proj)
|
| So(err, ShouldBeNil)
|
| - So(len(client.createdSubs), ShouldEqual, 1)
|
| - _, ok := client.createdSubs["luci-milo"]
|
| + So(len(miloClient.createdSubs), ShouldEqual, 1)
|
| + _, ok := miloClient.createdSubs["buildbucket"]
|
| So(ok, ShouldEqual, true)
|
| })
|
| })
|
|
|