| 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 "github.com/luci/luci-go/milo/api/config" |
| 18 |
| 19 . "github.com/smartystreets/goconvey/convey" |
| 20 ) |
| 21 |
| 22 type testPubSubClient struct { |
| 23 topics map[string]error |
| 24 subscriptions map[string]error |
| 25 createdSubsErr map[string]error |
| 26 createdSubs map[string]pubsub.SubscriptionConfig |
| 27 } |
| 28 |
| 29 // Topic returns an empty pubsub topic reference. |
| 30 func (client *testPubSubClient) getTopic(id string) (*pubsub.Topic, error) { |
| 31 if err, ok := client.topics[id]; ok { |
| 32 return &pubsub.Topic{}, err |
| 33 } |
| 34 panic(fmt.Errorf("test error: unknown topic %s", id)) |
| 35 } |
| 36 |
| 37 // Subscription returns an empty subscription reference. |
| 38 func (client *testPubSubClient) getSubscription(id string) ( |
| 39 *pubsub.Subscription, error) { |
| 40 if err, ok := client.subscriptions[id]; ok { |
| 41 return &pubsub.Subscription{}, err |
| 42 } |
| 43 panic(fmt.Errorf("test error: unknown sub %s", id)) |
| 44 } |
| 45 |
| 46 // CreateSubscription records that an attempt to create a subscription with |
| 47 // an id, then returns an empty subscription. |
| 48 func (client *testPubSubClient) createSubscription( |
| 49 id string, cfg pubsub.SubscriptionConfig) ( |
| 50 *pubsub.Subscription, error) { |
| 51 |
| 52 if err, ok := client.createdSubsErr[id]; ok { |
| 53 client.createdSubs[id] = cfg |
| 54 return &pubsub.Subscription{}, err |
| 55 } |
| 56 panic(fmt.Errorf("test error: unknown created sub %s", id)) |
| 57 } |
| 58 |
| 59 func TestPubSub(t *testing.T) { |
| 60 t.Parallel() |
| 61 |
| 62 Convey("Test Environment", t, func() { |
| 63 c := memory.UseWithAppID(context.Background(), "dev~luci-milo") |
| 64 c = gologger.StdConfig.Use(c) |
| 65 client := &testPubSubClient{ |
| 66 topics: map[string]error{}, |
| 67 subscriptions: map[string]error{}, |
| 68 createdSubsErr: map[string]error{}, |
| 69 createdSubs: map[string]pubsub.SubscriptionConfig{}} |
| 70 c = context.WithValue(c, &pubSubClientKey, client) |
| 71 |
| 72 Convey("Buildbucket PubSub subscriber", func() { |
| 73 settings := &config.Settings{ |
| 74 Buildbucket: &config.Settings_Buildbucket{}, |
| 75 } |
| 76 Convey("Empty config", func() { |
| 77 err := EnsurePubSubSubscribed(c, settings) |
| 78 So(err.Error(), ShouldEqual, "missing buildbucke
t project") |
| 79 }) |
| 80 settings.Buildbucket.Project = "foo" |
| 81 Convey("Non-existant topic", func() { |
| 82 client.topics["builds"] = errNotExist |
| 83 err := EnsurePubSubSubscribed(c, settings) |
| 84 So(err.Error(), ShouldEndWith, "does not exist") |
| 85 }) |
| 86 Convey("Permission denied", func() { |
| 87 pErr := errors.New( |
| 88 "something PermissionDenied something") |
| 89 client.topics["builds"] = pErr |
| 90 err := EnsurePubSubSubscribed(c, settings) |
| 91 So(err, ShouldEqual, pErr) |
| 92 }) |
| 93 Convey("Normal error", func() { |
| 94 pErr := errors.New("foobar") |
| 95 client.topics["builds"] = pErr |
| 96 err := EnsurePubSubSubscribed(c, settings) |
| 97 So(err, ShouldEqual, pErr) |
| 98 }) |
| 99 client.topics["builds"] = nil |
| 100 Convey("Subscription exists", func() { |
| 101 client.subscriptions["luci-milo"] = nil |
| 102 err := EnsurePubSubSubscribed(c, settings) |
| 103 So(err, ShouldBeNil) |
| 104 So(len(client.createdSubs), ShouldEqual, 0) |
| 105 }) |
| 106 client.subscriptions["luci-milo"] = errNotExist |
| 107 Convey("Not registered", func() { |
| 108 errNotReg := errors.New("The supplied HTTP URL i
s not registered") |
| 109 client.createdSubsErr["luci-milo"] = errNotReg |
| 110 err := EnsurePubSubSubscribed(c, settings) |
| 111 So(err, ShouldEqual, errNotReg) |
| 112 }) |
| 113 Convey("Create subscription", func() { |
| 114 client.createdSubsErr["luci-milo"] = nil |
| 115 err := EnsurePubSubSubscribed(c, settings) |
| 116 So(err, ShouldBeNil) |
| 117 So(len(client.createdSubs), ShouldEqual, 1) |
| 118 _, ok := client.createdSubs["luci-milo"] |
| 119 So(ok, ShouldEqual, true) |
| 120 }) |
| 121 }) |
| 122 }) |
| 123 |
| 124 } |
| OLD | NEW |