go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/internal/clients/pubsub_test.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package clients 16 17 import ( 18 "testing" 19 20 . "github.com/smartystreets/goconvey/convey" 21 22 . "go.chromium.org/luci/common/testing/assertions" 23 ) 24 25 func TestValidatePubSubTopicName(t *testing.T) { 26 Convey("validateTopicName", t, func() { 27 Convey("wrong topic name", func() { 28 _, _, err := ValidatePubSubTopicName("projects/adsf/") 29 So(err, ShouldErrLike, `topic "projects/adsf/" does not match "^projects/(.*)/topics/(.*)$"`) 30 }) 31 Convey("wrong project identifier", func() { 32 _, _, err := ValidatePubSubTopicName("projects/pro/topics/topic1") 33 So(err, ShouldErrLike, `cloud project id "pro" does not match "^[a-z]([a-z0-9-]){4,28}[a-z0-9]$"`) 34 }) 35 Convey("wrong topic id prefix", func() { 36 _, _, err := ValidatePubSubTopicName("projects/cloud-project/topics/goog11") 37 So(err, ShouldErrLike, `topic id "goog11" shouldn't begin with the string goog`) 38 }) 39 Convey("wrong topic id format", func() { 40 _, _, err := ValidatePubSubTopicName("projects/cloud-project/topics/abc##") 41 So(err, ShouldErrLike, `topic id "abc##" does not match "^[A-Za-z]([0-9A-Za-z\\._\\-~+%]){3,255}$"`) 42 }) 43 Convey("success", func() { 44 cloudProj, topic, err := ValidatePubSubTopicName("projects/cloud-project/topics/mytopic") 45 So(err, ShouldBeNil) 46 So(cloudProj, ShouldEqual, "cloud-project") 47 So(topic, ShouldEqual, "mytopic") 48 }) 49 }) 50 }