| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package textproto |
| 6 |
| 7 import ( |
| 8 "testing" |
| 9 |
| 10 configPB "github.com/luci/luci-go/common/proto/config" |
| 11 "github.com/luci/luci-go/server/config" |
| 12 |
| 13 "github.com/golang/protobuf/proto" |
| 14 "golang.org/x/net/context" |
| 15 |
| 16 . "github.com/luci/luci-go/common/testing/assertions" |
| 17 . "github.com/smartystreets/goconvey/convey" |
| 18 ) |
| 19 |
| 20 func tpb(msg proto.Message) string { return proto.MarshalTextString(msg) } |
| 21 |
| 22 // testingBackend is a Backend implementation that ignores Authority. |
| 23 type testingBackend struct { |
| 24 config.Backend |
| 25 |
| 26 items []*config.Item |
| 27 } |
| 28 |
| 29 // Get retrieves a single configuration. |
| 30 func (tb *testingBackend) Get(c context.Context, configSet, path string, p confi
g.Params) (*config.Item, error) { |
| 31 if len(tb.items) == 0 { |
| 32 return nil, config.ErrNoConfig |
| 33 } |
| 34 return tb.cloneItems()[0], nil |
| 35 } |
| 36 |
| 37 // GetAll retrieves all configurations of a given type. |
| 38 func (tb *testingBackend) GetAll(c context.Context, t config.GetAllType, path st
ring, p config.Params) ([]*config.Item, error) { |
| 39 return tb.cloneItems(), nil |
| 40 } |
| 41 |
| 42 func (tb *testingBackend) cloneItems() []*config.Item { |
| 43 clones := make([]*config.Item, len(tb.items)) |
| 44 for i, it := range tb.items { |
| 45 clone := *it |
| 46 clones[i] = &clone |
| 47 } |
| 48 return clones |
| 49 } |
| 50 |
| 51 func TestResolver(t *testing.T) { |
| 52 t.Parallel() |
| 53 |
| 54 Convey(`A testing environment`, t, func() { |
| 55 c := context.Background() |
| 56 |
| 57 var backend config.Backend |
| 58 backend = &testingBackend{ |
| 59 items: []*config.Item{ |
| 60 {Meta: config.Meta{"projects/foo", "path", "####
", "v1"}, |
| 61 Content: tpb(&configPB.Project{Id: proto
.String("foo")})}, |
| 62 {Meta: config.Meta{"projects/bar", "path", "####
", "v1"}, |
| 63 Content: tpb(&configPB.Project{Id: proto
.String("bar")})}, |
| 64 }, |
| 65 } |
| 66 |
| 67 Convey(`Without a formatter backend, succeeds`, func() { |
| 68 c = config.WithBackend(c, backend) |
| 69 |
| 70 Convey(`Single`, func() { |
| 71 var val configPB.Project |
| 72 So(config.Get(c, config.AsService, "", "", Messa
ge(&val), nil), ShouldBeNil) |
| 73 So(val, ShouldResemble, configPB.Project{Id: pro
to.String("foo")}) |
| 74 }) |
| 75 |
| 76 Convey(`Multi`, func() { |
| 77 var ( |
| 78 val []*configPB.Project |
| 79 meta []*config.Meta |
| 80 ) |
| 81 So(config.GetAll(c, config.AsService, config.Pro
ject, "", Slice(&val), &meta), ShouldBeNil) |
| 82 So(val, ShouldResemble, []*configPB.Project{ |
| 83 {Id: proto.String("foo")}, |
| 84 {Id: proto.String("bar")}, |
| 85 }) |
| 86 So(meta, ShouldResemble, []*config.Meta{ |
| 87 {"projects/foo", "path", "####", "v1"}, |
| 88 {"projects/bar", "path", "####", "v1"}, |
| 89 }) |
| 90 }) |
| 91 }) |
| 92 |
| 93 Convey(`With a formatter backend`, func() { |
| 94 var fr config.FormatterRegistry |
| 95 backend = &config.FormatBackend{ |
| 96 Backend: backend, |
| 97 GetRegistry: func(context.Context) *config.Forma
tterRegistry { return &fr }, |
| 98 } |
| 99 c = config.WithBackend(c, backend) |
| 100 |
| 101 Convey(`If the Formatter is not registered, fails.`, fun
c() { |
| 102 Convey(`Single`, func() { |
| 103 var val configPB.Project |
| 104 So(config.Get(c, config.AsService, "", "
", Message(&val), nil), ShouldErrLike, "unknown formatter") |
| 105 }) |
| 106 |
| 107 Convey(`Multi`, func() { |
| 108 var val []*configPB.Project |
| 109 So(config.GetAll(c, config.AsService, co
nfig.Project, "", Slice(&val), nil), ShouldErrLike, "unknown formatter") |
| 110 }) |
| 111 }) |
| 112 |
| 113 Convey(`If the Formatter is registered, succeeds`, func(
) { |
| 114 RegisterFormatter(&fr) |
| 115 |
| 116 Convey(`Single`, func() { |
| 117 var val configPB.Project |
| 118 So(config.Get(c, config.AsService, "", "
", Message(&val), nil), ShouldBeNil) |
| 119 So(val, ShouldResemble, configPB.Project
{Id: proto.String("foo")}) |
| 120 }) |
| 121 |
| 122 Convey(`Multi`, func() { |
| 123 var ( |
| 124 val []*configPB.Project |
| 125 meta []*config.Meta |
| 126 ) |
| 127 So(config.GetAll(c, config.AsService, co
nfig.Project, "", Slice(&val), &meta), ShouldBeNil) |
| 128 So(val, ShouldResemble, []*configPB.Proj
ect{ |
| 129 {Id: proto.String("foo")}, |
| 130 {Id: proto.String("bar")}, |
| 131 }) |
| 132 So(meta, ShouldResemble, []*config.Meta{ |
| 133 {"projects/foo", "path", "####",
"v1"}, |
| 134 {"projects/bar", "path", "####",
"v1"}, |
| 135 }) |
| 136 }) |
| 137 }) |
| 138 }) |
| 139 }) |
| 140 } |
| OLD | NEW |