| OLD | NEW |
| 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" | 8 "errors" |
| 9 "fmt" | 9 "fmt" |
| 10 "testing" | 10 "testing" |
| 11 | 11 |
| 12 "cloud.google.com/go/pubsub" | 12 "cloud.google.com/go/pubsub" |
| 13 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
| 14 | 14 |
| 15 "github.com/luci/gae/impl/memory" | 15 "github.com/luci/gae/impl/memory" |
| 16 luciErrors "github.com/luci/luci-go/common/errors" |
| 16 "github.com/luci/luci-go/common/logging/gologger" | 17 "github.com/luci/luci-go/common/logging/gologger" |
| 17 | 18 |
| 18 . "github.com/smartystreets/goconvey/convey" | 19 . "github.com/smartystreets/goconvey/convey" |
| 19 ) | 20 ) |
| 20 | 21 |
| 21 type testPubSubClient struct { | 22 type testPubSubClient struct { |
| 22 topics map[string]error | 23 topics map[string]error |
| 23 subscriptions map[string]error | 24 subscriptions map[string]error |
| 24 createdSubsErr map[string]error | 25 createdSubsErr map[string]error |
| 25 createdSubs map[string]pubsub.SubscriptionConfig | 26 createdSubs map[string]pubsub.SubscriptionConfig |
| (...skipping 28 matching lines...) Expand all Loading... |
| 54 } | 55 } |
| 55 panic(fmt.Errorf("test error: unknown created sub %s", id)) | 56 panic(fmt.Errorf("test error: unknown created sub %s", id)) |
| 56 } | 57 } |
| 57 | 58 |
| 58 func TestPubSub(t *testing.T) { | 59 func TestPubSub(t *testing.T) { |
| 59 t.Parallel() | 60 t.Parallel() |
| 60 | 61 |
| 61 Convey("Test Environment", t, func() { | 62 Convey("Test Environment", t, func() { |
| 62 c := memory.UseWithAppID(context.Background(), "dev~luci-milo") | 63 c := memory.UseWithAppID(context.Background(), "dev~luci-milo") |
| 63 c = gologger.StdConfig.Use(c) | 64 c = gologger.StdConfig.Use(c) |
| 64 » » client := &testPubSubClient{ | 65 » » miloClient := &testPubSubClient{ |
| 65 topics: map[string]error{}, | 66 topics: map[string]error{}, |
| 66 subscriptions: map[string]error{}, | 67 subscriptions: map[string]error{}, |
| 67 createdSubsErr: map[string]error{}, | 68 createdSubsErr: map[string]error{}, |
| 68 createdSubs: map[string]pubsub.SubscriptionConfig{}} | 69 createdSubs: map[string]pubsub.SubscriptionConfig{}} |
| 69 » » c = context.WithValue(c, &pubSubClientKey, client) | 70 » » bbClient := &testPubSubClient{ |
| 71 » » » topics: map[string]error{}, |
| 72 » » » subscriptions: map[string]error{}, |
| 73 » » » createdSubsErr: map[string]error{}, |
| 74 » » » createdSubs: map[string]pubsub.SubscriptionConfig{}} |
| 75 » » clients := newClientsBundle() |
| 76 » » clients.clients["buildbucket"] = bbClient |
| 77 » » clients.clients["luci-milo"] = miloClient |
| 78 » » c = context.WithValue(c, &pubSubClientKey, clients) |
| 70 | 79 |
| 71 Convey("Buildbucket PubSub subscriber", func() { | 80 Convey("Buildbucket PubSub subscriber", func() { |
| 72 » » » proj := "foo" | 81 » » » proj := "buildbucket" |
| 73 Convey("Non-existant topic", func() { | 82 Convey("Non-existant topic", func() { |
| 74 » » » » client.topics["builds"] = errNotExist | 83 » » » » bbClient.topics["builds"] = errNotExist |
| 75 err := ensureBuildbucketSubscribed(c, proj) | 84 err := ensureBuildbucketSubscribed(c, proj) |
| 76 So(err.Error(), ShouldEndWith, "does not exist") | 85 So(err.Error(), ShouldEndWith, "does not exist") |
| 77 }) | 86 }) |
| 78 Convey("Permission denied", func() { | 87 Convey("Permission denied", func() { |
| 79 pErr := errors.New( | 88 pErr := errors.New( |
| 80 "something PermissionDenied something") | 89 "something PermissionDenied something") |
| 81 » » » » client.topics["builds"] = pErr | 90 » » » » bbClient.topics["builds"] = pErr |
| 82 err := ensureBuildbucketSubscribed(c, proj) | 91 err := ensureBuildbucketSubscribed(c, proj) |
| 83 So(err, ShouldEqual, pErr) | 92 So(err, ShouldEqual, pErr) |
| 84 }) | 93 }) |
| 85 Convey("Normal error", func() { | 94 Convey("Normal error", func() { |
| 86 pErr := errors.New("foobar") | 95 pErr := errors.New("foobar") |
| 87 » » » » client.topics["builds"] = pErr | 96 » » » » bbClient.topics["builds"] = pErr |
| 88 err := ensureBuildbucketSubscribed(c, proj) | 97 err := ensureBuildbucketSubscribed(c, proj) |
| 89 So(err, ShouldEqual, pErr) | 98 So(err, ShouldEqual, pErr) |
| 90 }) | 99 }) |
| 91 » » » client.topics["builds"] = nil | 100 » » » bbClient.topics["builds"] = nil |
| 92 Convey("Subscription exists", func() { | 101 Convey("Subscription exists", func() { |
| 93 » » » » client.subscriptions["luci-milo"] = nil | 102 » » » » miloClient.subscriptions["buildbucket"] = nil |
| 94 err := ensureBuildbucketSubscribed(c, proj) | 103 err := ensureBuildbucketSubscribed(c, proj) |
| 95 So(err, ShouldBeNil) | 104 So(err, ShouldBeNil) |
| 96 » » » » So(len(client.createdSubs), ShouldEqual, 0) | 105 » » » » So(len(miloClient.createdSubs), ShouldEqual, 0) |
| 106 » » » » So(len(bbClient.createdSubs), ShouldEqual, 0) |
| 97 }) | 107 }) |
| 98 » » » client.subscriptions["luci-milo"] = errNotExist | 108 » » » miloClient.subscriptions["buildbucket"] = errNotExist |
| 99 Convey("Not registered", func() { | 109 Convey("Not registered", func() { |
| 100 errNotReg := errors.New("The supplied HTTP URL i
s not registered") | 110 errNotReg := errors.New("The supplied HTTP URL i
s not registered") |
| 101 » » » » client.createdSubsErr["luci-milo"] = errNotReg | 111 » » » » miloClient.createdSubsErr["buildbucket"] = errNo
tReg |
| 102 err := ensureBuildbucketSubscribed(c, proj) | 112 err := ensureBuildbucketSubscribed(c, proj) |
| 103 » » » » So(err, ShouldEqual, errNotReg) | 113 » » » » So((err.(luciErrors.Wrapped)).InnerError(), Shou
ldEqual, errNotReg) |
| 104 }) | 114 }) |
| 105 Convey("Create subscription", func() { | 115 Convey("Create subscription", func() { |
| 106 » » » » client.createdSubsErr["luci-milo"] = nil | 116 » » » » miloClient.createdSubsErr["buildbucket"] = nil |
| 107 err := ensureBuildbucketSubscribed(c, proj) | 117 err := ensureBuildbucketSubscribed(c, proj) |
| 108 So(err, ShouldBeNil) | 118 So(err, ShouldBeNil) |
| 109 » » » » So(len(client.createdSubs), ShouldEqual, 1) | 119 » » » » So(len(miloClient.createdSubs), ShouldEqual, 1) |
| 110 » » » » _, ok := client.createdSubs["luci-milo"] | 120 » » » » _, ok := miloClient.createdSubs["buildbucket"] |
| 111 So(ok, ShouldEqual, true) | 121 So(ok, ShouldEqual, true) |
| 112 }) | 122 }) |
| 113 }) | 123 }) |
| 114 }) | 124 }) |
| 115 | 125 |
| 116 } | 126 } |
| OLD | NEW |