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

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: luciErrors -> errors 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
« milo/common/pubsub.go ('K') | « 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 func (fac *testFactory) newClient(c context.Context, projectID string) (pubsubCl ient, error) {
63 if cli, ok := fac.clients[projectID]; ok {
64 return cli, nil
65 }
66 return nil, fmt.Errorf("client for project %s does not exist", projectID )
67 }
68
58 func TestPubSub(t *testing.T) { 69 func TestPubSub(t *testing.T) {
59 t.Parallel() 70 t.Parallel()
60 71
61 Convey("Test Environment", t, func() { 72 Convey("Test Environment", t, func() {
62 c := memory.UseWithAppID(context.Background(), "dev~luci-milo") 73 c := memory.UseWithAppID(context.Background(), "dev~luci-milo")
63 c = gologger.StdConfig.Use(c) 74 c = gologger.StdConfig.Use(c)
64 » » client := &testPubSubClient{ 75 » » miloClient := &testPubSubClient{
65 topics: map[string]error{}, 76 topics: map[string]error{},
66 subscriptions: map[string]error{}, 77 subscriptions: map[string]error{},
67 createdSubsErr: map[string]error{}, 78 createdSubsErr: map[string]error{},
68 createdSubs: map[string]pubsub.SubscriptionConfig{}} 79 createdSubs: map[string]pubsub.SubscriptionConfig{}}
69 » » c = context.WithValue(c, &pubSubClientKey, client) 80 » » bbClient := &testPubSubClient{
81 » » » topics: map[string]error{},
82 » » » subscriptions: map[string]error{},
83 » » » createdSubsErr: map[string]error{},
84 » » » createdSubs: map[string]pubsub.SubscriptionConfig{}}
85 » » fac := testFactory{
86 » » » clients: map[string]pubsubClient{
87 » » » » "luci-milo": miloClient,
88 » » » » "buildbucket": bbClient,
89 » » » },
90 » » }
91 » » c = context.WithValue(c, &pubsubClientFactoryKey, &fac)
70 92
71 Convey("Buildbucket PubSub subscriber", func() { 93 Convey("Buildbucket PubSub subscriber", func() {
72 » » » proj := "foo" 94 » » » proj := "buildbucket"
73 Convey("Non-existant topic", func() { 95 Convey("Non-existant topic", func() {
74 » » » » client.topics["builds"] = errNotExist 96 » » » » bbClient.topics["builds"] = errNotExist
75 err := ensureBuildbucketSubscribed(c, proj) 97 err := ensureBuildbucketSubscribed(c, proj)
76 So(err.Error(), ShouldEndWith, "does not exist") 98 So(err.Error(), ShouldEndWith, "does not exist")
77 }) 99 })
78 Convey("Permission denied", func() { 100 Convey("Permission denied", func() {
79 pErr := errors.New( 101 pErr := errors.New(
80 "something PermissionDenied something") 102 "something PermissionDenied something")
81 » » » » client.topics["builds"] = pErr 103 » » » » bbClient.topics["builds"] = pErr
82 err := ensureBuildbucketSubscribed(c, proj) 104 err := ensureBuildbucketSubscribed(c, proj)
83 So(err, ShouldEqual, pErr) 105 So(err, ShouldEqual, pErr)
84 }) 106 })
85 Convey("Normal error", func() { 107 Convey("Normal error", func() {
86 pErr := errors.New("foobar") 108 pErr := errors.New("foobar")
87 » » » » client.topics["builds"] = pErr 109 » » » » bbClient.topics["builds"] = pErr
88 err := ensureBuildbucketSubscribed(c, proj) 110 err := ensureBuildbucketSubscribed(c, proj)
89 So(err, ShouldEqual, pErr) 111 So(err, ShouldEqual, pErr)
90 }) 112 })
91 » » » client.topics["builds"] = nil 113 » » » bbClient.topics["builds"] = nil
92 Convey("Subscription exists", func() { 114 Convey("Subscription exists", func() {
93 » » » » client.subscriptions["luci-milo"] = nil 115 » » » » miloClient.subscriptions["buildbucket"] = nil
94 err := ensureBuildbucketSubscribed(c, proj) 116 err := ensureBuildbucketSubscribed(c, proj)
95 So(err, ShouldBeNil) 117 So(err, ShouldBeNil)
96 » » » » So(len(client.createdSubs), ShouldEqual, 0) 118 » » » » So(len(miloClient.createdSubs), ShouldEqual, 0)
119 » » » » So(len(bbClient.createdSubs), ShouldEqual, 0)
97 }) 120 })
98 » » » client.subscriptions["luci-milo"] = errNotExist 121 » » » miloClient.subscriptions["buildbucket"] = errNotExist
99 Convey("Not registered", func() { 122 Convey("Not registered", func() {
100 errNotReg := errors.New("The supplied HTTP URL i s not registered") 123 errNotReg := errors.New("The supplied HTTP URL i s not registered")
101 » » » » client.createdSubsErr["luci-milo"] = errNotReg 124 » » » » miloClient.createdSubsErr["buildbucket"] = errNo tReg
102 err := ensureBuildbucketSubscribed(c, proj) 125 err := ensureBuildbucketSubscribed(c, proj)
103 » » » » So(err, ShouldEqual, errNotReg) 126 » » » » So((err.(errors.Wrapped)).InnerError(), ShouldEq ual, errNotReg)
104 }) 127 })
105 Convey("Create subscription", func() { 128 Convey("Create subscription", func() {
106 » » » » client.createdSubsErr["luci-milo"] = nil 129 » » » » miloClient.createdSubsErr["buildbucket"] = nil
107 err := ensureBuildbucketSubscribed(c, proj) 130 err := ensureBuildbucketSubscribed(c, proj)
108 So(err, ShouldBeNil) 131 So(err, ShouldBeNil)
109 » » » » So(len(client.createdSubs), ShouldEqual, 1) 132 » » » » So(len(miloClient.createdSubs), ShouldEqual, 1)
110 » » » » _, ok := client.createdSubs["luci-milo"] 133 » » » » _, ok := miloClient.createdSubs["buildbucket"]
111 So(ok, ShouldEqual, true) 134 So(ok, ShouldEqual, true)
112 }) 135 })
113 }) 136 })
114 }) 137 })
115 138
116 } 139 }
OLDNEW
« milo/common/pubsub.go ('K') | « milo/common/pubsub.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698