| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package common |
| 6 |
| 7 import ( |
| 8 "errors" |
| 9 "fmt" |
| 10 "testing" |
| 11 |
| 12 "cloud.google.com/go/pubsub" |
| 13 "golang.org/x/net/context" |
| 14 |
| 15 "github.com/luci/gae/impl/memory" |
| 16 "github.com/luci/luci-go/common/logging/gologger" |
| 17 |
| 18 . "github.com/smartystreets/goconvey/convey" |
| 19 ) |
| 20 |
| 21 type testPubSubClient struct { |
| 22 topics map[string]error |
| 23 subscriptions map[string]error |
| 24 createdSubsErr map[string]error |
| 25 createdSubs map[string]pubsub.SubscriptionConfig |
| 26 } |
| 27 |
| 28 // Topic returns an empty pubsub topic reference. |
| 29 func (client *testPubSubClient) getTopic(id string) (*pubsub.Topic, error) { |
| 30 if err, ok := client.topics[id]; ok { |
| 31 return &pubsub.Topic{}, err |
| 32 } |
| 33 panic(fmt.Errorf("test error: unknown topic %s", id)) |
| 34 } |
| 35 |
| 36 // Subscription returns an empty subscription reference. |
| 37 func (client *testPubSubClient) getSubscription(id string) ( |
| 38 *pubsub.Subscription, error) { |
| 39 if err, ok := client.subscriptions[id]; ok { |
| 40 return &pubsub.Subscription{}, err |
| 41 } |
| 42 panic(fmt.Errorf("test error: unknown sub %s", id)) |
| 43 } |
| 44 |
| 45 // CreateSubscription records that an attempt to create a subscription with |
| 46 // an id, then returns an empty subscription. |
| 47 func (client *testPubSubClient) createSubscription( |
| 48 id string, cfg pubsub.SubscriptionConfig) ( |
| 49 *pubsub.Subscription, error) { |
| 50 |
| 51 if err, ok := client.createdSubsErr[id]; ok { |
| 52 client.createdSubs[id] = cfg |
| 53 return &pubsub.Subscription{}, err |
| 54 } |
| 55 panic(fmt.Errorf("test error: unknown created sub %s", id)) |
| 56 } |
| 57 |
| 58 func TestPubSub(t *testing.T) { |
| 59 t.Parallel() |
| 60 |
| 61 Convey("Test Environment", t, func() { |
| 62 c := memory.UseWithAppID(context.Background(), "dev~luci-milo") |
| 63 c = gologger.StdConfig.Use(c) |
| 64 client := &testPubSubClient{ |
| 65 topics: map[string]error{}, |
| 66 subscriptions: map[string]error{}, |
| 67 createdSubsErr: map[string]error{}, |
| 68 createdSubs: map[string]pubsub.SubscriptionConfig{}} |
| 69 c = context.WithValue(c, &pubSubClientKey, client) |
| 70 |
| 71 Convey("Buildbucket PubSub subscriber", func() { |
| 72 proj := "foo" |
| 73 Convey("Non-existant topic", func() { |
| 74 client.topics["builds"] = errNotExist |
| 75 err := ensureBuildbucketSubscribed(c, proj) |
| 76 So(err.Error(), ShouldEndWith, "does not exist") |
| 77 }) |
| 78 Convey("Permission denied", func() { |
| 79 pErr := errors.New( |
| 80 "something PermissionDenied something") |
| 81 client.topics["builds"] = pErr |
| 82 err := ensureBuildbucketSubscribed(c, proj) |
| 83 So(err, ShouldEqual, pErr) |
| 84 }) |
| 85 Convey("Normal error", func() { |
| 86 pErr := errors.New("foobar") |
| 87 client.topics["builds"] = pErr |
| 88 err := ensureBuildbucketSubscribed(c, proj) |
| 89 So(err, ShouldEqual, pErr) |
| 90 }) |
| 91 client.topics["builds"] = nil |
| 92 Convey("Subscription exists", func() { |
| 93 client.subscriptions["luci-milo"] = nil |
| 94 err := ensureBuildbucketSubscribed(c, proj) |
| 95 So(err, ShouldBeNil) |
| 96 So(len(client.createdSubs), ShouldEqual, 0) |
| 97 }) |
| 98 client.subscriptions["luci-milo"] = errNotExist |
| 99 Convey("Not registered", func() { |
| 100 errNotReg := errors.New("The supplied HTTP URL i
s not registered") |
| 101 client.createdSubsErr["luci-milo"] = errNotReg |
| 102 err := ensureBuildbucketSubscribed(c, proj) |
| 103 So(err, ShouldEqual, errNotReg) |
| 104 }) |
| 105 Convey("Create subscription", func() { |
| 106 client.createdSubsErr["luci-milo"] = nil |
| 107 err := ensureBuildbucketSubscribed(c, proj) |
| 108 So(err, ShouldBeNil) |
| 109 So(len(client.createdSubs), ShouldEqual, 1) |
| 110 _, ok := client.createdSubs["luci-milo"] |
| 111 So(ok, ShouldEqual, true) |
| 112 }) |
| 113 }) |
| 114 }) |
| 115 |
| 116 } |
| OLD | NEW |