Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Side by Side Diff: milo/common/pubsub_test.go

Issue 2981683002: Milo: Move buildbucket pubsub sub from buildbucket project to milo project (Closed)
Patch Set: comments Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « milo/common/pubsub.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The LUCI Authors. All rights reserved. 1 // Copyright 2017 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package common 5 package common
6 6
7 import ( 7 import (
8 "errors"
9 "fmt" 8 "fmt"
10 "testing" 9 "testing"
11 10
12 "cloud.google.com/go/pubsub" 11 "cloud.google.com/go/pubsub"
13 "golang.org/x/net/context" 12 "golang.org/x/net/context"
14 13
15 "github.com/luci/gae/impl/memory" 14 "github.com/luci/gae/impl/memory"
15 "github.com/luci/luci-go/common/errors"
16 "github.com/luci/luci-go/common/logging/gologger" 16 "github.com/luci/luci-go/common/logging/gologger"
17 17
18 . "github.com/smartystreets/goconvey/convey" 18 . "github.com/smartystreets/goconvey/convey"
19 ) 19 )
20 20
21 type testPubSubClient struct { 21 type testPubSubClient struct {
22 topics map[string]error 22 topics map[string]error
23 subscriptions map[string]error 23 subscriptions map[string]error
24 createdSubsErr map[string]error 24 createdSubsErr map[string]error
25 createdSubs map[string]pubsub.SubscriptionConfig 25 createdSubs map[string]pubsub.SubscriptionConfig
26 } 26 }
27 27
28 // Topic returns an empty pubsub topic reference. 28 // Topic returns an empty pubsub topic reference.
29 func (client *testPubSubClient) getTopic(id string) (*pubsub.Topic, error) { 29 func (client *testPubSubClient) getTopic(c context.Context, id string) (*pubsub. Topic, error) {
30 if err, ok := client.topics[id]; ok { 30 if err, ok := client.topics[id]; ok {
31 return &pubsub.Topic{}, err 31 return &pubsub.Topic{}, err
32 } 32 }
33 panic(fmt.Errorf("test error: unknown topic %s", id)) 33 panic(fmt.Errorf("test error: unknown topic %s", id))
34 } 34 }
35 35
36 // Subscription returns an empty subscription reference. 36 // Subscription returns an empty subscription reference.
37 func (client *testPubSubClient) getSubscription(id string) ( 37 func (client *testPubSubClient) getSubscription(c context.Context, id string) (
38 *pubsub.Subscription, error) { 38 *pubsub.Subscription, error) {
39 if err, ok := client.subscriptions[id]; ok { 39 if err, ok := client.subscriptions[id]; ok {
40 return &pubsub.Subscription{}, err 40 return &pubsub.Subscription{}, err
41 } 41 }
42 panic(fmt.Errorf("test error: unknown sub %s", id)) 42 panic(fmt.Errorf("test error: unknown sub %s", id))
43 } 43 }
44 44
45 // CreateSubscription records that an attempt to create a subscription with 45 // CreateSubscription records that an attempt to create a subscription with
46 // an id, then returns an empty subscription. 46 // an id, then returns an empty subscription.
47 func (client *testPubSubClient) createSubscription( 47 func (client *testPubSubClient) createSubscription(
48 » id string, cfg pubsub.SubscriptionConfig) ( 48 » c context.Context, id string, cfg pubsub.SubscriptionConfig) (
49 *pubsub.Subscription, error) { 49 *pubsub.Subscription, error) {
50 50
51 if err, ok := client.createdSubsErr[id]; ok { 51 if err, ok := client.createdSubsErr[id]; ok {
52 client.createdSubs[id] = cfg 52 client.createdSubs[id] = cfg
53 return &pubsub.Subscription{}, err 53 return &pubsub.Subscription{}, err
54 } 54 }
55 panic(fmt.Errorf("test error: unknown created sub %s", id)) 55 panic(fmt.Errorf("test error: unknown created sub %s", id))
56 } 56 }
57 57
58 type testFactory struct {
59 clients map[string]pubsubClient
60 }
61
62 // makeTestClientFactory returns a closed pubsubClientFactory.
63 // Golang Protip: A bound method will not match the function type signature
64 // of an unbound function, but a closed function will.
65 func (fac *testFactory) makeTestClientFactory() pubsubClientFactory {
66 return func(c context.Context, projectID string) (pubsubClient, error) {
67 if cli, ok := fac.clients[projectID]; ok {
68 return cli, nil
69 }
70 return nil, fmt.Errorf("client for project %s does not exist", p rojectID)
71 }
72 }
73
58 func TestPubSub(t *testing.T) { 74 func TestPubSub(t *testing.T) {
59 t.Parallel() 75 t.Parallel()
60 76
61 Convey("Test Environment", t, func() { 77 Convey("Test Environment", t, func() {
62 c := memory.UseWithAppID(context.Background(), "dev~luci-milo") 78 c := memory.UseWithAppID(context.Background(), "dev~luci-milo")
63 c = gologger.StdConfig.Use(c) 79 c = gologger.StdConfig.Use(c)
64 » » client := &testPubSubClient{ 80 » » miloClient := &testPubSubClient{
65 topics: map[string]error{}, 81 topics: map[string]error{},
66 subscriptions: map[string]error{}, 82 subscriptions: map[string]error{},
67 createdSubsErr: map[string]error{}, 83 createdSubsErr: map[string]error{},
68 createdSubs: map[string]pubsub.SubscriptionConfig{}} 84 createdSubs: map[string]pubsub.SubscriptionConfig{}}
69 » » c = context.WithValue(c, &pubSubClientKey, client) 85 » » bbClient := &testPubSubClient{
86 » » » topics: map[string]error{},
87 » » » subscriptions: map[string]error{},
88 » » » createdSubsErr: map[string]error{},
89 » » » createdSubs: map[string]pubsub.SubscriptionConfig{}}
90 » » fac := testFactory{
91 » » » clients: map[string]pubsubClient{
92 » » » » "luci-milo": miloClient,
93 » » » » "buildbucket": bbClient,
94 » » » },
95 » » }
96 » » c = context.WithValue(c, &pubsubClientFactoryKey, fac.makeTestCl ientFactory())
70 97
71 Convey("Buildbucket PubSub subscriber", func() { 98 Convey("Buildbucket PubSub subscriber", func() {
72 » » » proj := "foo" 99 » » » proj := "buildbucket"
73 Convey("Non-existant topic", func() { 100 Convey("Non-existant topic", func() {
74 » » » » client.topics["builds"] = errNotExist 101 » » » » bbClient.topics["builds"] = errNotExist
75 err := ensureBuildbucketSubscribed(c, proj) 102 err := ensureBuildbucketSubscribed(c, proj)
76 So(err.Error(), ShouldEndWith, "does not exist") 103 So(err.Error(), ShouldEndWith, "does not exist")
77 }) 104 })
78 Convey("Permission denied", func() { 105 Convey("Permission denied", func() {
79 pErr := errors.New( 106 pErr := errors.New(
80 "something PermissionDenied something") 107 "something PermissionDenied something")
81 » » » » client.topics["builds"] = pErr 108 » » » » bbClient.topics["builds"] = pErr
82 err := ensureBuildbucketSubscribed(c, proj) 109 err := ensureBuildbucketSubscribed(c, proj)
83 So(err, ShouldEqual, pErr) 110 So(err, ShouldEqual, pErr)
84 }) 111 })
85 Convey("Normal error", func() { 112 Convey("Normal error", func() {
86 pErr := errors.New("foobar") 113 pErr := errors.New("foobar")
87 » » » » client.topics["builds"] = pErr 114 » » » » bbClient.topics["builds"] = pErr
88 err := ensureBuildbucketSubscribed(c, proj) 115 err := ensureBuildbucketSubscribed(c, proj)
89 So(err, ShouldEqual, pErr) 116 So(err, ShouldEqual, pErr)
90 }) 117 })
91 » » » client.topics["builds"] = nil 118 » » » bbClient.topics["builds"] = nil
92 Convey("Subscription exists", func() { 119 Convey("Subscription exists", func() {
93 » » » » client.subscriptions["luci-milo"] = nil 120 » » » » miloClient.subscriptions["buildbucket"] = nil
94 err := ensureBuildbucketSubscribed(c, proj) 121 err := ensureBuildbucketSubscribed(c, proj)
95 So(err, ShouldBeNil) 122 So(err, ShouldBeNil)
96 » » » » So(len(client.createdSubs), ShouldEqual, 0) 123 » » » » So(len(miloClient.createdSubs), ShouldEqual, 0)
124 » » » » So(len(bbClient.createdSubs), ShouldEqual, 0)
97 }) 125 })
98 » » » client.subscriptions["luci-milo"] = errNotExist 126 » » » miloClient.subscriptions["buildbucket"] = errNotExist
99 Convey("Not registered", func() { 127 Convey("Not registered", func() {
100 errNotReg := errors.New("The supplied HTTP URL i s not registered") 128 errNotReg := errors.New("The supplied HTTP URL i s not registered")
101 » » » » client.createdSubsErr["luci-milo"] = errNotReg 129 » » » » miloClient.createdSubsErr["buildbucket"] = errNo tReg
102 err := ensureBuildbucketSubscribed(c, proj) 130 err := ensureBuildbucketSubscribed(c, proj)
103 » » » » So(err, ShouldEqual, errNotReg) 131 » » » » So((err.(errors.Wrapped)).InnerError(), ShouldEq ual, errNotReg)
104 }) 132 })
105 Convey("Create subscription", func() { 133 Convey("Create subscription", func() {
106 » » » » client.createdSubsErr["luci-milo"] = nil 134 » » » » miloClient.createdSubsErr["buildbucket"] = nil
107 err := ensureBuildbucketSubscribed(c, proj) 135 err := ensureBuildbucketSubscribed(c, proj)
108 So(err, ShouldBeNil) 136 So(err, ShouldBeNil)
109 » » » » So(len(client.createdSubs), ShouldEqual, 1) 137 » » » » So(len(miloClient.createdSubs), ShouldEqual, 1)
110 » » » » _, ok := client.createdSubs["luci-milo"] 138 » » » » _, ok := miloClient.createdSubs["buildbucket"]
111 So(ok, ShouldEqual, true) 139 So(ok, ShouldEqual, true)
112 }) 140 })
113 }) 141 })
114 }) 142 })
115 143
116 } 144 }
OLDNEW
« no previous file with comments | « milo/common/pubsub.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698