| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package cfgclient | 5 package cfgclient |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "net/url" | 9 "net/url" |
| 10 "testing" | 10 "testing" |
| 11 | 11 |
| 12 "github.com/luci/luci-go/common/errors" | 12 "github.com/luci/luci-go/common/errors" |
| 13 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" | 13 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" |
| 14 | 14 |
| 15 "golang.org/x/net/context" | 15 "golang.org/x/net/context" |
| 16 | 16 |
| 17 . "github.com/smartystreets/goconvey/convey" | 17 . "github.com/smartystreets/goconvey/convey" |
| 18 ) | 18 ) |
| 19 | 19 |
| 20 // testingBackend is a Backend implementation that ignores Authority. | 20 // testingBackend is a Backend implementation that ignores Authority. |
| 21 type testingBackend struct { | 21 type testingBackend struct { |
| 22 serviceURL *url.URL | 22 serviceURL *url.URL |
| 23 err error | 23 err error |
| 24 items []*backend.Item | 24 items []*backend.Item |
| 25 url url.URL | 25 url url.URL |
| 26 lastParams backend.Params |
| 26 } | 27 } |
| 27 | 28 |
| 28 func (tb *testingBackend) ServiceURL(context.Context) url.URL { return *tb.servi
ceURL } | 29 func (tb *testingBackend) ServiceURL(context.Context) url.URL { return *tb.servi
ceURL } |
| 29 | 30 |
| 30 func (tb *testingBackend) Get(c context.Context, configSet, path string, p backe
nd.Params) (*backend.Item, error) { | 31 func (tb *testingBackend) Get(c context.Context, configSet, path string, p backe
nd.Params) (*backend.Item, error) { |
| 32 tb.lastParams = p |
| 31 if err := tb.err; err != nil { | 33 if err := tb.err; err != nil { |
| 32 return nil, tb.err | 34 return nil, tb.err |
| 33 } | 35 } |
| 34 if len(tb.items) == 0 { | 36 if len(tb.items) == 0 { |
| 35 return nil, ErrNoConfig | 37 return nil, ErrNoConfig |
| 36 } | 38 } |
| 37 return tb.cloneItems()[0], nil | 39 return tb.cloneItems()[0], nil |
| 38 } | 40 } |
| 39 | 41 |
| 40 func (tb *testingBackend) GetAll(c context.Context, t backend.GetAllTarget, path
string, p backend.Params) ( | 42 func (tb *testingBackend) GetAll(c context.Context, t backend.GetAllTarget, path
string, p backend.Params) ( |
| 41 []*backend.Item, error) { | 43 []*backend.Item, error) { |
| 42 | 44 |
| 45 tb.lastParams = p |
| 43 if err := tb.err; err != nil { | 46 if err := tb.err; err != nil { |
| 44 return nil, tb.err | 47 return nil, tb.err |
| 45 } | 48 } |
| 46 return tb.cloneItems(), nil | 49 return tb.cloneItems(), nil |
| 47 } | 50 } |
| 48 | 51 |
| 49 func (tb *testingBackend) ConfigSetURL(c context.Context, configSet string, p ba
ckend.Params) (url.URL, error) { | 52 func (tb *testingBackend) ConfigSetURL(c context.Context, configSet string, p ba
ckend.Params) (url.URL, error) { |
| 53 tb.lastParams = p |
| 50 return tb.url, tb.err | 54 return tb.url, tb.err |
| 51 } | 55 } |
| 52 | 56 |
| 53 func (tb *testingBackend) cloneItems() []*backend.Item { | 57 func (tb *testingBackend) cloneItems() []*backend.Item { |
| 54 clones := make([]*backend.Item, len(tb.items)) | 58 clones := make([]*backend.Item, len(tb.items)) |
| 55 for i, it := range tb.items { | 59 for i, it := range tb.items { |
| 56 clone := *it | 60 clone := *it |
| 57 clones[i] = &clone | 61 clones[i] = &clone |
| 58 } | 62 } |
| 59 return clones | 63 return clones |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 | 161 |
| 158 Convey(`Test nil resolver`, func() { | 162 Convey(`Test nil resolver`, func() { |
| 159 transformItems(func(i int, ci *backend.Item) { | 163 transformItems(func(i int, ci *backend.Item) { |
| 160 ci.Content = fmt.Sprintf("[%d]", i) | 164 ci.Content = fmt.Sprintf("[%d]", i) |
| 161 }) | 165 }) |
| 162 | 166 |
| 163 Convey(`Single`, func() { | 167 Convey(`Single`, func() { |
| 164 var meta Meta | 168 var meta Meta |
| 165 So(Get(c, AsService, "", "", nil, &meta), Should
BeNil) | 169 So(Get(c, AsService, "", "", nil, &meta), Should
BeNil) |
| 166 So(meta, ShouldResemble, Meta{"projects/foo", "p
ath", "####", "v1"}) | 170 So(meta, ShouldResemble, Meta{"projects/foo", "p
ath", "####", "v1"}) |
| 171 So(tb.lastParams.Content, ShouldBeFalse) |
| 167 }) | 172 }) |
| 168 | 173 |
| 169 Convey(`Multi`, func() { | 174 Convey(`Multi`, func() { |
| 170 var meta []*Meta | 175 var meta []*Meta |
| 171 So(Projects(c, AsService, "", nil, &meta), Shoul
dBeNil) | 176 So(Projects(c, AsService, "", nil, &meta), Shoul
dBeNil) |
| 172 So(meta, ShouldResemble, []*Meta{ | 177 So(meta, ShouldResemble, []*Meta{ |
| 173 {"projects/foo", "path", "####", "v1"}, | 178 {"projects/foo", "path", "####", "v1"}, |
| 174 {"projects/bar", "path", "####", "v1"}, | 179 {"projects/bar", "path", "####", "v1"}, |
| 175 }) | 180 }) |
| 181 So(tb.lastParams.Content, ShouldBeFalse) |
| 176 }) | 182 }) |
| 177 }) | 183 }) |
| 178 | 184 |
| 179 Convey(`Projects with some errors returns a MultiError.`, func()
{ | 185 Convey(`Projects with some errors returns a MultiError.`, func()
{ |
| 180 testErr := errors.New("test error") | 186 testErr := errors.New("test error") |
| 181 var ( | 187 var ( |
| 182 val []string | 188 val []string |
| 183 meta []*Meta | 189 meta []*Meta |
| 184 ) | 190 ) |
| 185 er := errorMultiResolver{ | 191 er := errorMultiResolver{ |
| (...skipping 27 matching lines...) Expand all Loading... |
| 213 panic(err) | 219 panic(err) |
| 214 } | 220 } |
| 215 | 221 |
| 216 tb.url = *u | 222 tb.url = *u |
| 217 uv, err := GetConfigSetURL(c, AsService, "") | 223 uv, err := GetConfigSetURL(c, AsService, "") |
| 218 So(err, ShouldBeNil) | 224 So(err, ShouldBeNil) |
| 219 So(uv, ShouldResemble, url.URL{Scheme: "https", Host: "e
xample.com", Path: "/foo/bar"}) | 225 So(uv, ShouldResemble, url.URL{Scheme: "https", Host: "e
xample.com", Path: "/foo/bar"}) |
| 220 }) | 226 }) |
| 221 }) | 227 }) |
| 222 } | 228 } |
| OLD | NEW |