| 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/luci_config/server/cfgclient" |
| 12 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" |
| 13 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/format" |
| 14 |
| 15 "github.com/golang/protobuf/proto" |
| 16 "golang.org/x/net/context" |
| 17 |
| 18 . "github.com/luci/luci-go/common/testing/assertions" |
| 19 . "github.com/smartystreets/goconvey/convey" |
| 20 ) |
| 21 |
| 22 func tpb(msg proto.Message) string { return proto.MarshalTextString(msg) } |
| 23 |
| 24 // testingBackend is a backend.B implementation that ignores Authority. |
| 25 type testingBackend struct { |
| 26 backend.B |
| 27 |
| 28 items []*backend.Item |
| 29 } |
| 30 |
| 31 // Get retrieves a single configuration. |
| 32 func (tb *testingBackend) Get(c context.Context, configSet, path string, p backe
nd.Params) (*backend.Item, error) { |
| 33 if len(tb.items) == 0 { |
| 34 return nil, cfgclient.ErrNoConfig |
| 35 } |
| 36 return tb.cloneItems()[0], nil |
| 37 } |
| 38 |
| 39 // GetAll retrieves all configurations of a given type. |
| 40 func (tb *testingBackend) GetAll(c context.Context, t backend.GetAllTarget, path
string, p backend.Params) ( |
| 41 []*backend.Item, error) { |
| 42 |
| 43 return tb.cloneItems(), nil |
| 44 } |
| 45 |
| 46 func (tb *testingBackend) cloneItems() []*backend.Item { |
| 47 clones := make([]*backend.Item, len(tb.items)) |
| 48 for i, it := range tb.items { |
| 49 clone := *it |
| 50 clones[i] = &clone |
| 51 } |
| 52 return clones |
| 53 } |
| 54 |
| 55 func TestResolver(t *testing.T) { |
| 56 t.Parallel() |
| 57 |
| 58 Convey(`A testing environment`, t, func() { |
| 59 c := context.Background() |
| 60 |
| 61 var be backend.B |
| 62 be = &testingBackend{ |
| 63 items: []*backend.Item{ |
| 64 {Meta: backend.Meta{"projects/foo", "path", "###
#", "v1"}, |
| 65 Content: tpb(&configPB.Project{Id: proto
.String("foo")})}, |
| 66 {Meta: backend.Meta{"projects/bar", "path", "###
#", "v1"}, |
| 67 Content: tpb(&configPB.Project{Id: proto
.String("bar")})}, |
| 68 }, |
| 69 } |
| 70 |
| 71 Convey(`Without a formatter backend, succeeds`, func() { |
| 72 c = backend.WithBackend(c, be) |
| 73 |
| 74 Convey(`Single`, func() { |
| 75 var val configPB.Project |
| 76 So(cfgclient.Get(c, cfgclient.AsService, "", "",
Message(&val), nil), ShouldBeNil) |
| 77 So(val, ShouldResemble, configPB.Project{Id: pro
to.String("foo")}) |
| 78 }) |
| 79 |
| 80 Convey(`Multi`, func() { |
| 81 var ( |
| 82 val []*configPB.Project |
| 83 meta []*cfgclient.Meta |
| 84 ) |
| 85 So(cfgclient.Projects(c, cfgclient.AsService, ""
, Slice(&val), &meta), ShouldBeNil) |
| 86 So(val, ShouldResemble, []*configPB.Project{ |
| 87 {Id: proto.String("foo")}, |
| 88 {Id: proto.String("bar")}, |
| 89 }) |
| 90 So(meta, ShouldResemble, []*cfgclient.Meta{ |
| 91 {"projects/foo", "path", "####", "v1"}, |
| 92 {"projects/bar", "path", "####", "v1"}, |
| 93 }) |
| 94 }) |
| 95 }) |
| 96 |
| 97 Convey(`With a formatter backend`, func() { |
| 98 var fr cfgclient.FormatterRegistry |
| 99 be = &format.Backend{ |
| 100 B: be, |
| 101 GetRegistry: func(context.Context) *cfgclient.Fo
rmatterRegistry { return &fr }, |
| 102 } |
| 103 c = backend.WithBackend(c, be) |
| 104 |
| 105 Convey(`If the Formatter is not registered, fails.`, fun
c() { |
| 106 Convey(`Single`, func() { |
| 107 var val configPB.Project |
| 108 So(cfgclient.Get(c, cfgclient.AsService,
"", "", Message(&val), nil), ShouldErrLike, "unknown formatter") |
| 109 }) |
| 110 |
| 111 Convey(`Multi`, func() { |
| 112 var val []*configPB.Project |
| 113 So(cfgclient.Projects(c, cfgclient.AsSer
vice, "", Slice(&val), nil), ShouldErrLike, "unknown formatter") |
| 114 }) |
| 115 }) |
| 116 |
| 117 Convey(`If the Formatter is registered, succeeds`, func(
) { |
| 118 RegisterFormatter(&fr) |
| 119 |
| 120 Convey(`Single`, func() { |
| 121 var val configPB.Project |
| 122 So(cfgclient.Get(c, cfgclient.AsService,
"", "", Message(&val), nil), ShouldBeNil) |
| 123 So(val, ShouldResemble, configPB.Project
{Id: proto.String("foo")}) |
| 124 }) |
| 125 |
| 126 Convey(`Multi`, func() { |
| 127 var ( |
| 128 val []*configPB.Project |
| 129 meta []*cfgclient.Meta |
| 130 ) |
| 131 So(cfgclient.Projects(c, cfgclient.AsSer
vice, "", Slice(&val), &meta), ShouldBeNil) |
| 132 So(val, ShouldResemble, []*configPB.Proj
ect{ |
| 133 {Id: proto.String("foo")}, |
| 134 {Id: proto.String("bar")}, |
| 135 }) |
| 136 So(meta, ShouldResemble, []*cfgclient.Me
ta{ |
| 137 {"projects/foo", "path", "####",
"v1"}, |
| 138 {"projects/bar", "path", "####",
"v1"}, |
| 139 }) |
| 140 }) |
| 141 }) |
| 142 }) |
| 143 }) |
| 144 } |
| OLD | NEW |