| Index: server/config/textproto/resolver_test.go
|
| diff --git a/server/config/textproto/resolver_test.go b/server/config/textproto/resolver_test.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..f5b40c6bb97ceb54b70b9bbd6e49ecc781412af6
|
| --- /dev/null
|
| +++ b/server/config/textproto/resolver_test.go
|
| @@ -0,0 +1,140 @@
|
| +// 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 textproto
|
| +
|
| +import (
|
| + "testing"
|
| +
|
| + configPB "github.com/luci/luci-go/common/proto/config"
|
| + "github.com/luci/luci-go/server/config"
|
| +
|
| + "github.com/golang/protobuf/proto"
|
| + "golang.org/x/net/context"
|
| +
|
| + . "github.com/luci/luci-go/common/testing/assertions"
|
| + . "github.com/smartystreets/goconvey/convey"
|
| +)
|
| +
|
| +func tpb(msg proto.Message) string { return proto.MarshalTextString(msg) }
|
| +
|
| +// testingBackend is a Backend implementation that ignores Authority.
|
| +type testingBackend struct {
|
| + config.Backend
|
| +
|
| + items []*config.Item
|
| +}
|
| +
|
| +// Get retrieves a single configuration.
|
| +func (tb *testingBackend) Get(c context.Context, configSet, path string, p config.Params) (*config.Item, error) {
|
| + if len(tb.items) == 0 {
|
| + return nil, config.ErrNoConfig
|
| + }
|
| + return tb.cloneItems()[0], nil
|
| +}
|
| +
|
| +// GetAll retrieves all configurations of a given type.
|
| +func (tb *testingBackend) GetAll(c context.Context, t config.GetAllType, path string, p config.Params) ([]*config.Item, error) {
|
| + return tb.cloneItems(), nil
|
| +}
|
| +
|
| +func (tb *testingBackend) cloneItems() []*config.Item {
|
| + clones := make([]*config.Item, len(tb.items))
|
| + for i, it := range tb.items {
|
| + clone := *it
|
| + clones[i] = &clone
|
| + }
|
| + return clones
|
| +}
|
| +
|
| +func TestResolver(t *testing.T) {
|
| + t.Parallel()
|
| +
|
| + Convey(`A testing environment`, t, func() {
|
| + c := context.Background()
|
| +
|
| + var backend config.Backend
|
| + backend = &testingBackend{
|
| + items: []*config.Item{
|
| + {Meta: config.Meta{"projects/foo", "path", "####", "v1"},
|
| + Content: tpb(&configPB.Project{Id: proto.String("foo")})},
|
| + {Meta: config.Meta{"projects/bar", "path", "####", "v1"},
|
| + Content: tpb(&configPB.Project{Id: proto.String("bar")})},
|
| + },
|
| + }
|
| +
|
| + Convey(`Without a formatter backend, succeeds`, func() {
|
| + c = config.WithBackend(c, backend)
|
| +
|
| + Convey(`Single`, func() {
|
| + var val configPB.Project
|
| + So(config.Get(c, config.AsService, "", "", Message(&val), nil), ShouldBeNil)
|
| + So(val, ShouldResemble, configPB.Project{Id: proto.String("foo")})
|
| + })
|
| +
|
| + Convey(`Multi`, func() {
|
| + var (
|
| + val []*configPB.Project
|
| + meta []*config.Meta
|
| + )
|
| + So(config.GetAll(c, config.AsService, config.Project, "", Slice(&val), &meta), ShouldBeNil)
|
| + So(val, ShouldResemble, []*configPB.Project{
|
| + {Id: proto.String("foo")},
|
| + {Id: proto.String("bar")},
|
| + })
|
| + So(meta, ShouldResemble, []*config.Meta{
|
| + {"projects/foo", "path", "####", "v1"},
|
| + {"projects/bar", "path", "####", "v1"},
|
| + })
|
| + })
|
| + })
|
| +
|
| + Convey(`With a formatter backend`, func() {
|
| + var fr config.FormatterRegistry
|
| + backend = &config.FormatBackend{
|
| + Backend: backend,
|
| + GetRegistry: func(context.Context) *config.FormatterRegistry { return &fr },
|
| + }
|
| + c = config.WithBackend(c, backend)
|
| +
|
| + Convey(`If the Formatter is not registered, fails.`, func() {
|
| + Convey(`Single`, func() {
|
| + var val configPB.Project
|
| + So(config.Get(c, config.AsService, "", "", Message(&val), nil), ShouldErrLike, "unknown formatter")
|
| + })
|
| +
|
| + Convey(`Multi`, func() {
|
| + var val []*configPB.Project
|
| + So(config.GetAll(c, config.AsService, config.Project, "", Slice(&val), nil), ShouldErrLike, "unknown formatter")
|
| + })
|
| + })
|
| +
|
| + Convey(`If the Formatter is registered, succeeds`, func() {
|
| + RegisterFormatter(&fr)
|
| +
|
| + Convey(`Single`, func() {
|
| + var val configPB.Project
|
| + So(config.Get(c, config.AsService, "", "", Message(&val), nil), ShouldBeNil)
|
| + So(val, ShouldResemble, configPB.Project{Id: proto.String("foo")})
|
| + })
|
| +
|
| + Convey(`Multi`, func() {
|
| + var (
|
| + val []*configPB.Project
|
| + meta []*config.Meta
|
| + )
|
| + So(config.GetAll(c, config.AsService, config.Project, "", Slice(&val), &meta), ShouldBeNil)
|
| + So(val, ShouldResemble, []*configPB.Project{
|
| + {Id: proto.String("foo")},
|
| + {Id: proto.String("bar")},
|
| + })
|
| + So(meta, ShouldResemble, []*config.Meta{
|
| + {"projects/foo", "path", "####", "v1"},
|
| + {"projects/bar", "path", "####", "v1"},
|
| + })
|
| + })
|
| + })
|
| + })
|
| + })
|
| +}
|
|
|