| 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 config |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "net/url" |
| 10 "testing" |
| 11 |
| 12 "github.com/luci/luci-go/common/config" |
| 13 "github.com/luci/luci-go/common/errors" |
| 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 []*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 Param
s) (*Item, error) { |
| 31 if err := tb.err; err != nil { |
| 32 return nil, tb.err |
| 33 } |
| 34 if len(tb.items) == 0 { |
| 35 return nil, config.ErrNoConfig |
| 36 } |
| 37 return tb.cloneItems()[0], nil |
| 38 } |
| 39 |
| 40 func (tb *testingBackend) GetAll(c context.Context, t GetAllType, path string, p
Params) ([]*Item, error) { |
| 41 if err := tb.err; err != nil { |
| 42 return nil, tb.err |
| 43 } |
| 44 return tb.cloneItems(), nil |
| 45 } |
| 46 |
| 47 func (tb *testingBackend) ConfigSetURL(c context.Context, a Authority, configSet
string) (url.URL, error) { |
| 48 return tb.url, tb.err |
| 49 } |
| 50 |
| 51 func (tb *testingBackend) cloneItems() []*Item { |
| 52 clones := make([]*Item, len(tb.items)) |
| 53 for i, it := range tb.items { |
| 54 clone := *it |
| 55 clones[i] = &clone |
| 56 } |
| 57 return clones |
| 58 } |
| 59 |
| 60 type errorMultiResolver struct { |
| 61 getErr func(i int) error |
| 62 out *[]string |
| 63 } |
| 64 |
| 65 func (er *errorMultiResolver) PrepareMulti(size int) { |
| 66 *er.out = make([]string, size) |
| 67 } |
| 68 |
| 69 func (er *errorMultiResolver) ResolveItemAt(i int, it *Item) error { |
| 70 if err := er.getErr(i); err != nil { |
| 71 return err |
| 72 } |
| 73 (*er.out)[i] = it.Content |
| 74 return nil |
| 75 } |
| 76 |
| 77 func TestConfig(t *testing.T) { |
| 78 t.Parallel() |
| 79 |
| 80 Convey(`A testing backend`, t, func() { |
| 81 c := context.Background() |
| 82 |
| 83 var err error |
| 84 tb := testingBackend{ |
| 85 items: []*Item{ |
| 86 {Meta: Meta{"projects/foo", "path", "####", "v1"
}, Content: "foo"}, |
| 87 {Meta: Meta{"projects/bar", "path", "####", "v1"
}, Content: "bar"}, |
| 88 }, |
| 89 } |
| 90 transformItems := func(fn func(int, *Item)) { |
| 91 for i, itm := range tb.items { |
| 92 fn(i, itm) |
| 93 } |
| 94 } |
| 95 tb.serviceURL, err = url.Parse("http://example.com/config") |
| 96 if err != nil { |
| 97 panic(err) |
| 98 } |
| 99 c = WithBackend(c, &tb) |
| 100 |
| 101 Convey(`Can resolve its service URL.`, func() { |
| 102 So(ServiceURL(c), ShouldResemble, *tb.serviceURL) |
| 103 }) |
| 104 |
| 105 // Caching / type test cases. |
| 106 Convey(`Test byte resolver`, func() { |
| 107 transformItems(func(i int, ci *Item) { |
| 108 ci.Content = string([]byte{byte(i)}) |
| 109 }) |
| 110 |
| 111 Convey(`Single`, func() { |
| 112 var val []byte |
| 113 So(Get(c, AsService, "", "", Bytes(&val), nil),
ShouldBeNil) |
| 114 So(val, ShouldResemble, []byte{0}) |
| 115 }) |
| 116 |
| 117 Convey(`Multi`, func() { |
| 118 var ( |
| 119 val [][]byte |
| 120 meta []*Meta |
| 121 ) |
| 122 So(GetAll(c, AsService, Project, "", BytesSlice(
&val), &meta), ShouldBeNil) |
| 123 So(val, ShouldResemble, [][]byte{{0}, {1}}) |
| 124 So(meta, ShouldResemble, []*Meta{ |
| 125 {"projects/foo", "path", "####", "v1"}, |
| 126 {"projects/bar", "path", "####", "v1"}, |
| 127 }) |
| 128 }) |
| 129 }) |
| 130 |
| 131 Convey(`Test string resolver`, func() { |
| 132 transformItems(func(i int, ci *Item) { |
| 133 ci.Content = fmt.Sprintf("[%d]", i) |
| 134 }) |
| 135 |
| 136 Convey(`Single`, func() { |
| 137 var val string |
| 138 So(Get(c, AsService, "", "", String(&val), nil),
ShouldBeNil) |
| 139 So(val, ShouldEqual, "[0]") |
| 140 }) |
| 141 |
| 142 Convey(`Multi`, func() { |
| 143 var ( |
| 144 val []string |
| 145 meta []*Meta |
| 146 ) |
| 147 So(GetAll(c, AsService, Project, "", StringSlice
(&val), &meta), ShouldBeNil) |
| 148 So(val, ShouldResemble, []string{"[0]", "[1]"}) |
| 149 So(meta, ShouldResemble, []*Meta{ |
| 150 {"projects/foo", "path", "####", "v1"}, |
| 151 {"projects/bar", "path", "####", "v1"}, |
| 152 }) |
| 153 }) |
| 154 }) |
| 155 |
| 156 Convey(`Test nil resolver`, func() { |
| 157 transformItems(func(i int, ci *Item) { |
| 158 ci.Content = fmt.Sprintf("[%d]", i) |
| 159 }) |
| 160 |
| 161 Convey(`Single`, func() { |
| 162 var meta Meta |
| 163 So(Get(c, AsService, "", "", nil, &meta), Should
BeNil) |
| 164 So(meta, ShouldResemble, Meta{"projects/foo", "p
ath", "####", "v1"}) |
| 165 }) |
| 166 |
| 167 Convey(`Multi`, func() { |
| 168 var meta []*Meta |
| 169 So(GetAll(c, AsService, Project, "", nil, &meta)
, ShouldBeNil) |
| 170 So(meta, ShouldResemble, []*Meta{ |
| 171 {"projects/foo", "path", "####", "v1"}, |
| 172 {"projects/bar", "path", "####", "v1"}, |
| 173 }) |
| 174 }) |
| 175 }) |
| 176 |
| 177 Convey(`GetAll with some errors returns a MultiError.`, func() { |
| 178 testErr := errors.New("test error") |
| 179 var ( |
| 180 val []string |
| 181 meta []*Meta |
| 182 ) |
| 183 er := errorMultiResolver{ |
| 184 out: &val, |
| 185 getErr: func(i int) error { |
| 186 if i == 1 { |
| 187 return testErr |
| 188 } |
| 189 return nil |
| 190 }, |
| 191 } |
| 192 |
| 193 err := GetAll(c, AsService, Project, "", &er, &meta) |
| 194 |
| 195 // We have a MultiError with an error in position 1. |
| 196 So(err, ShouldResemble, errors.MultiError{nil, testErr}) |
| 197 |
| 198 // One of our configs should have resolved. |
| 199 So(val, ShouldResemble, []string{"foo", ""}) |
| 200 |
| 201 // Meta still works. |
| 202 So(meta, ShouldResemble, []*Meta{ |
| 203 {"projects/foo", "path", "####", "v1"}, |
| 204 {"projects/bar", "path", "####", "v1"}, |
| 205 }) |
| 206 }) |
| 207 |
| 208 Convey(`Test ConfigSetURL`, func() { |
| 209 u, err := url.Parse("https://example.com/foo/bar") |
| 210 if err != nil { |
| 211 panic(err) |
| 212 } |
| 213 |
| 214 tb.url = *u |
| 215 uv, err := GetConfigSetURL(c, AsService, "") |
| 216 So(err, ShouldBeNil) |
| 217 So(uv, ShouldResemble, url.URL{Scheme: "https", Host: "e
xample.com", Path: "/foo/bar"}) |
| 218 }) |
| 219 }) |
| 220 } |
| OLD | NEW |