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

Unified Diff: server/config/config_test.go

Issue 2580713002: Implement a server-side config service interface. (Closed)
Patch Set: Update MultiResolver interface, add test for MultiError. Created 4 years 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 side-by-side diff with in-line comments
Download patch
Index: server/config/config_test.go
diff --git a/server/config/config_test.go b/server/config/config_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..3a32cf92e18f9a1b40681907abb1e263c1371162
--- /dev/null
+++ b/server/config/config_test.go
@@ -0,0 +1,220 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package config
+
+import (
+ "fmt"
+ "net/url"
+ "testing"
+
+ "github.com/luci/luci-go/common/config"
+ "github.com/luci/luci-go/common/errors"
+
+ "golang.org/x/net/context"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+// testingBackend is a Backend implementation that ignores Authority.
+type testingBackend struct {
+ serviceURL *url.URL
+ err error
+ items []*Item
+ url url.URL
+}
+
+func (tb *testingBackend) ServiceURL(context.Context) url.URL { return *tb.serviceURL }
+
+func (tb *testingBackend) Get(c context.Context, configSet, path string, p Params) (*Item, error) {
+ if err := tb.err; err != nil {
+ return nil, tb.err
+ }
+ if len(tb.items) == 0 {
+ return nil, config.ErrNoConfig
+ }
+ return tb.cloneItems()[0], nil
+}
+
+func (tb *testingBackend) GetAll(c context.Context, t GetAllType, path string, p Params) ([]*Item, error) {
+ if err := tb.err; err != nil {
+ return nil, tb.err
+ }
+ return tb.cloneItems(), nil
+}
+
+func (tb *testingBackend) ConfigSetURL(c context.Context, a Authority, configSet string) (url.URL, error) {
+ return tb.url, tb.err
+}
+
+func (tb *testingBackend) cloneItems() []*Item {
+ clones := make([]*Item, len(tb.items))
+ for i, it := range tb.items {
+ clone := *it
+ clones[i] = &clone
+ }
+ return clones
+}
+
+type errorMultiResolver struct {
+ getErr func(i int) error
+ out *[]string
+}
+
+func (er *errorMultiResolver) PrepareMulti(size int) {
+ *er.out = make([]string, size)
+}
+
+func (er *errorMultiResolver) ResolveItemAt(i int, it *Item) error {
+ if err := er.getErr(i); err != nil {
+ return err
+ }
+ (*er.out)[i] = it.Content
+ return nil
+}
+
+func TestConfig(t *testing.T) {
+ t.Parallel()
+
+ Convey(`A testing backend`, t, func() {
+ c := context.Background()
+
+ var err error
+ tb := testingBackend{
+ items: []*Item{
+ {Meta: Meta{"projects/foo", "path", "####", "v1"}, Content: "foo"},
+ {Meta: Meta{"projects/bar", "path", "####", "v1"}, Content: "bar"},
+ },
+ }
+ transformItems := func(fn func(int, *Item)) {
+ for i, itm := range tb.items {
+ fn(i, itm)
+ }
+ }
+ tb.serviceURL, err = url.Parse("http://example.com/config")
+ if err != nil {
+ panic(err)
+ }
+ c = WithBackend(c, &tb)
+
+ Convey(`Can resolve its service URL.`, func() {
+ So(ServiceURL(c), ShouldResemble, *tb.serviceURL)
+ })
+
+ // Caching / type test cases.
+ Convey(`Test byte resolver`, func() {
+ transformItems(func(i int, ci *Item) {
+ ci.Content = string([]byte{byte(i)})
+ })
+
+ Convey(`Single`, func() {
+ var val []byte
+ So(Get(c, AsService, "", "", Bytes(&val), nil), ShouldBeNil)
+ So(val, ShouldResemble, []byte{0})
+ })
+
+ Convey(`Multi`, func() {
+ var (
+ val [][]byte
+ meta []*Meta
+ )
+ So(GetAll(c, AsService, Project, "", BytesSlice(&val), &meta), ShouldBeNil)
+ So(val, ShouldResemble, [][]byte{{0}, {1}})
+ So(meta, ShouldResemble, []*Meta{
+ {"projects/foo", "path", "####", "v1"},
+ {"projects/bar", "path", "####", "v1"},
+ })
+ })
+ })
+
+ Convey(`Test string resolver`, func() {
+ transformItems(func(i int, ci *Item) {
+ ci.Content = fmt.Sprintf("[%d]", i)
+ })
+
+ Convey(`Single`, func() {
+ var val string
+ So(Get(c, AsService, "", "", String(&val), nil), ShouldBeNil)
+ So(val, ShouldEqual, "[0]")
+ })
+
+ Convey(`Multi`, func() {
+ var (
+ val []string
+ meta []*Meta
+ )
+ So(GetAll(c, AsService, Project, "", StringSlice(&val), &meta), ShouldBeNil)
+ So(val, ShouldResemble, []string{"[0]", "[1]"})
+ So(meta, ShouldResemble, []*Meta{
+ {"projects/foo", "path", "####", "v1"},
+ {"projects/bar", "path", "####", "v1"},
+ })
+ })
+ })
+
+ Convey(`Test nil resolver`, func() {
+ transformItems(func(i int, ci *Item) {
+ ci.Content = fmt.Sprintf("[%d]", i)
+ })
+
+ Convey(`Single`, func() {
+ var meta Meta
+ So(Get(c, AsService, "", "", nil, &meta), ShouldBeNil)
+ So(meta, ShouldResemble, Meta{"projects/foo", "path", "####", "v1"})
+ })
+
+ Convey(`Multi`, func() {
+ var meta []*Meta
+ So(GetAll(c, AsService, Project, "", nil, &meta), ShouldBeNil)
+ So(meta, ShouldResemble, []*Meta{
+ {"projects/foo", "path", "####", "v1"},
+ {"projects/bar", "path", "####", "v1"},
+ })
+ })
+ })
+
+ Convey(`GetAll with some errors returns a MultiError.`, func() {
+ testErr := errors.New("test error")
+ var (
+ val []string
+ meta []*Meta
+ )
+ er := errorMultiResolver{
+ out: &val,
+ getErr: func(i int) error {
+ if i == 1 {
+ return testErr
+ }
+ return nil
+ },
+ }
+
+ err := GetAll(c, AsService, Project, "", &er, &meta)
+
+ // We have a MultiError with an error in position 1.
+ So(err, ShouldResemble, errors.MultiError{nil, testErr})
+
+ // One of our configs should have resolved.
+ So(val, ShouldResemble, []string{"foo", ""})
+
+ // Meta still works.
+ So(meta, ShouldResemble, []*Meta{
+ {"projects/foo", "path", "####", "v1"},
+ {"projects/bar", "path", "####", "v1"},
+ })
+ })
+
+ Convey(`Test ConfigSetURL`, func() {
+ u, err := url.Parse("https://example.com/foo/bar")
+ if err != nil {
+ panic(err)
+ }
+
+ tb.url = *u
+ uv, err := GetConfigSetURL(c, AsService, "")
+ So(err, ShouldBeNil)
+ So(uv, ShouldResemble, url.URL{Scheme: "https", Host: "example.com", Path: "/foo/bar"})
+ })
+ })
+}

Powered by Google App Engine
This is Rietveld 408576698