Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: luci_config/server/cfgclient/config_test.go

Issue 2580713002: Implement a server-side config service interface. (Closed)
Patch Set: Renamed, moved to luci_config package, fixes, split out backends. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « luci_config/server/cfgclient/config.go ('k') | luci_config/server/cfgclient/doc.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 cfgclient
6
7 import (
8 "fmt"
9 "net/url"
10 "testing"
11
12 "github.com/luci/luci-go/common/errors"
13 "github.com/luci/luci-go/luci_config/server/cfgclient/backend"
14
15 "golang.org/x/net/context"
16
17 . "github.com/smartystreets/goconvey/convey"
18 )
19
20 // testingBackend is a Backend implementation that ignores Authority.
21 type testingBackend struct {
22 serviceURL *url.URL
23 err error
24 items []*backend.Item
25 url url.URL
26 }
27
28 func (tb *testingBackend) ServiceURL(context.Context) url.URL { return *tb.servi ceURL }
29
30 func (tb *testingBackend) Get(c context.Context, configSet, path string, p backe nd.Params) (*backend.Item, error) {
31 if err := tb.err; err != nil {
32 return nil, tb.err
33 }
34 if len(tb.items) == 0 {
35 return nil, ErrNoConfig
36 }
37 return tb.cloneItems()[0], nil
38 }
39
40 func (tb *testingBackend) GetAll(c context.Context, t backend.GetAllTarget, path string, p backend.Params) (
41 []*backend.Item, error) {
42
43 if err := tb.err; err != nil {
44 return nil, tb.err
45 }
46 return tb.cloneItems(), nil
47 }
48
49 func (tb *testingBackend) ConfigSetURL(c context.Context, configSet string, p ba ckend.Params) (url.URL, error) {
50 return tb.url, tb.err
51 }
52
53 func (tb *testingBackend) cloneItems() []*backend.Item {
54 clones := make([]*backend.Item, len(tb.items))
55 for i, it := range tb.items {
56 clone := *it
57 clones[i] = &clone
58 }
59 return clones
60 }
61
62 type errorMultiResolver struct {
63 getErr func(i int) error
64 out *[]string
65 }
66
67 func (er *errorMultiResolver) PrepareMulti(size int) {
68 *er.out = make([]string, size)
69 }
70
71 func (er *errorMultiResolver) ResolveItemAt(i int, it *backend.Item) error {
72 if err := er.getErr(i); err != nil {
73 return err
74 }
75 (*er.out)[i] = it.Content
76 return nil
77 }
78
79 func TestConfig(t *testing.T) {
80 t.Parallel()
81
82 Convey(`A testing backend`, t, func() {
83 c := context.Background()
84
85 var err error
86 tb := testingBackend{
87 items: []*backend.Item{
88 {Meta: backend.Meta{"projects/foo", "path", "### #", "v1"}, Content: "foo"},
89 {Meta: backend.Meta{"projects/bar", "path", "### #", "v1"}, Content: "bar"},
90 },
91 }
92 transformItems := func(fn func(int, *backend.Item)) {
93 for i, itm := range tb.items {
94 fn(i, itm)
95 }
96 }
97 tb.serviceURL, err = url.Parse("http://example.com/config")
98 if err != nil {
99 panic(err)
100 }
101 c = backend.WithBackend(c, &tb)
102
103 Convey(`Can resolve its service URL.`, func() {
104 So(ServiceURL(c), ShouldResemble, *tb.serviceURL)
105 })
106
107 // Caching / type test cases.
108 Convey(`Test byte resolver`, func() {
109 transformItems(func(i int, ci *backend.Item) {
110 ci.Content = string([]byte{byte(i)})
111 })
112
113 Convey(`Single`, func() {
114 var val []byte
115 So(Get(c, AsService, "", "", Bytes(&val), nil), ShouldBeNil)
116 So(val, ShouldResemble, []byte{0})
117 })
118
119 Convey(`Multi`, func() {
120 var (
121 val [][]byte
122 meta []*Meta
123 )
124 So(Projects(c, AsService, "", BytesSlice(&val), &meta), ShouldBeNil)
125 So(val, ShouldResemble, [][]byte{{0}, {1}})
126 So(meta, ShouldResemble, []*Meta{
127 {"projects/foo", "path", "####", "v1"},
128 {"projects/bar", "path", "####", "v1"},
129 })
130 })
131 })
132
133 Convey(`Test string resolver`, func() {
134 transformItems(func(i int, ci *backend.Item) {
135 ci.Content = fmt.Sprintf("[%d]", i)
136 })
137
138 Convey(`Single`, func() {
139 var val string
140 So(Get(c, AsService, "", "", String(&val), nil), ShouldBeNil)
141 So(val, ShouldEqual, "[0]")
142 })
143
144 Convey(`Multi`, func() {
145 var (
146 val []string
147 meta []*Meta
148 )
149 So(Projects(c, AsService, "", StringSlice(&val), &meta), ShouldBeNil)
150 So(val, ShouldResemble, []string{"[0]", "[1]"})
151 So(meta, ShouldResemble, []*Meta{
152 {"projects/foo", "path", "####", "v1"},
153 {"projects/bar", "path", "####", "v1"},
154 })
155 })
156 })
157
158 Convey(`Test nil resolver`, func() {
159 transformItems(func(i int, ci *backend.Item) {
160 ci.Content = fmt.Sprintf("[%d]", i)
161 })
162
163 Convey(`Single`, func() {
164 var meta Meta
165 So(Get(c, AsService, "", "", nil, &meta), Should BeNil)
166 So(meta, ShouldResemble, Meta{"projects/foo", "p ath", "####", "v1"})
167 })
168
169 Convey(`Multi`, func() {
170 var meta []*Meta
171 So(Projects(c, AsService, "", nil, &meta), Shoul dBeNil)
172 So(meta, ShouldResemble, []*Meta{
173 {"projects/foo", "path", "####", "v1"},
174 {"projects/bar", "path", "####", "v1"},
175 })
176 })
177 })
178
179 Convey(`Projects with some errors returns a MultiError.`, func() {
180 testErr := errors.New("test error")
181 var (
182 val []string
183 meta []*Meta
184 )
185 er := errorMultiResolver{
186 out: &val,
187 getErr: func(i int) error {
188 if i == 1 {
189 return testErr
190 }
191 return nil
192 },
193 }
194
195 err := Projects(c, AsService, "", &er, &meta)
196
197 // We have a MultiError with an error in position 1.
198 So(err, ShouldResemble, errors.MultiError{nil, testErr})
199
200 // One of our configs should have resolved.
201 So(val, ShouldResemble, []string{"foo", ""})
202
203 // Meta still works.
204 So(meta, ShouldResemble, []*Meta{
205 {"projects/foo", "path", "####", "v1"},
206 {"projects/bar", "path", "####", "v1"},
207 })
208 })
209
210 Convey(`Test ConfigSetURL`, func() {
211 u, err := url.Parse("https://example.com/foo/bar")
212 if err != nil {
213 panic(err)
214 }
215
216 tb.url = *u
217 uv, err := GetConfigSetURL(c, AsService, "")
218 So(err, ShouldBeNil)
219 So(uv, ShouldResemble, url.URL{Scheme: "https", Host: "e xample.com", Path: "/foo/bar"})
220 })
221 })
222 }
OLDNEW
« no previous file with comments | « luci_config/server/cfgclient/config.go ('k') | luci_config/server/cfgclient/doc.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698