| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package registration |
| 6 |
| 7 import ( |
| 8 "errors" |
| 9 "testing" |
| 10 |
| 11 "github.com/luci/gae/filter/featureBreaker" |
| 12 ds "github.com/luci/gae/service/datastore" |
| 13 ct "github.com/luci/luci-go/appengine/logdog/coordinator/coordinatorTest
" |
| 14 "github.com/luci/luci-go/appengine/logdog/coordinator/hierarchy" |
| 15 "github.com/luci/luci-go/common/api/logdog_coordinator/registration/v1" |
| 16 "github.com/luci/luci-go/common/cryptorand" |
| 17 |
| 18 . "github.com/luci/luci-go/common/testing/assertions" |
| 19 . "github.com/smartystreets/goconvey/convey" |
| 20 ) |
| 21 |
| 22 func TestRegisterPrefix(t *testing.T) { |
| 23 t.Parallel() |
| 24 |
| 25 Convey(`With a testing configuration`, t, func() { |
| 26 c, _ := ct.Install() |
| 27 c, fb := featureBreaker.FilterRDS(c, nil) |
| 28 ds.Get(c).Testable().Consistent(true) |
| 29 |
| 30 // Mock random number generator so we can predict secrets. |
| 31 c = cryptorand.MockForTest(c, 0) |
| 32 randSecret := []byte{ |
| 33 250, 18, 249, 42, 251, 224, 15, 133, 8, 208, 232, 59, |
| 34 171, 156, 248, 206, 191, 66, 226, 94, 139, 20, 234, 252, |
| 35 129, 234, 224, 208, 15, 44, 173, 228, 193, 124, 22, 209, |
| 36 } |
| 37 |
| 38 svr := New() |
| 39 |
| 40 req := logdog.RegisterPrefixRequest{ |
| 41 Project: "proj-foo", |
| 42 Prefix: "testing/prefix", |
| 43 SourceInfo: []string{"unit test"}, |
| 44 } |
| 45 |
| 46 Convey(`Returns PermissionDenied error if not user does not have
write access.`, func() { |
| 47 // "proj-bar" does not have anonymous write. |
| 48 req.Project = "proj-bar" |
| 49 |
| 50 _, err := svr.RegisterPrefix(c, &req) |
| 51 So(err, ShouldBeRPCPermissionDenied) |
| 52 }) |
| 53 |
| 54 Convey(`Will register a new prefix.`, func() { |
| 55 resp, err := svr.RegisterPrefix(c, &req) |
| 56 So(err, ShouldBeNil) |
| 57 So(resp, ShouldResemble, &logdog.RegisterPrefixResponse{ |
| 58 LogBundleTopic: "projects/app/topics/test-topic"
, |
| 59 Secret: randSecret, |
| 60 }) |
| 61 |
| 62 // Should have registered path components. |
| 63 getComponents := func(b string) []string { |
| 64 l, err := hierarchy.Get(c, hierarchy.Request{Pro
ject: "proj-foo", PathBase: b}) |
| 65 if err != nil { |
| 66 panic(err) |
| 67 } |
| 68 names := make([]string, len(l.Comp)) |
| 69 for i, e := range l.Comp { |
| 70 names[i] = e.Name |
| 71 } |
| 72 return names |
| 73 } |
| 74 So(getComponents(""), ShouldResemble, []string{"testing"
}) |
| 75 So(getComponents("testing"), ShouldResemble, []string{"p
refix"}) |
| 76 So(getComponents("testing/prefix"), ShouldResemble, []st
ring{"+"}) |
| 77 So(getComponents("testing/prefix/+"), ShouldResemble, []
string{}) |
| 78 |
| 79 Convey(`Will refuse to register it again.`, func() { |
| 80 _, err := svr.RegisterPrefix(c, &req) |
| 81 So(err, ShouldBeRPCAlreadyExists) |
| 82 }) |
| 83 }) |
| 84 |
| 85 Convey(`Will fail to register the prefix if Put is broken.`, fun
c() { |
| 86 fb.BreakFeatures(errors.New("test error"), "PutMulti") |
| 87 _, err := svr.RegisterPrefix(c, &req) |
| 88 So(err, ShouldBeRPCInternal) |
| 89 }) |
| 90 }) |
| 91 } |
| OLD | NEW |