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

Unified Diff: luci_config/server/cfgclient/textproto/resolver_test.go

Issue 2578893002: server/config: Add text protobuf support. (Closed)
Patch Set: Rebase, comments. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « luci_config/server/cfgclient/textproto/resolver.go ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: luci_config/server/cfgclient/textproto/resolver_test.go
diff --git a/luci_config/server/cfgclient/textproto/resolver_test.go b/luci_config/server/cfgclient/textproto/resolver_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..b1664f200821366f1f837d5026975d0b45b34d49
--- /dev/null
+++ b/luci_config/server/cfgclient/textproto/resolver_test.go
@@ -0,0 +1,144 @@
+// 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/luci_config/server/cfgclient"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend/format"
+
+ "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.B implementation that ignores Authority.
+type testingBackend struct {
+ backend.B
+
+ items []*backend.Item
+}
+
+// Get retrieves a single configuration.
+func (tb *testingBackend) Get(c context.Context, configSet, path string, p backend.Params) (*backend.Item, error) {
+ if len(tb.items) == 0 {
+ return nil, cfgclient.ErrNoConfig
+ }
+ return tb.cloneItems()[0], nil
+}
+
+// GetAll retrieves all configurations of a given type.
+func (tb *testingBackend) GetAll(c context.Context, t backend.GetAllTarget, path string, p backend.Params) (
+ []*backend.Item, error) {
+
+ return tb.cloneItems(), nil
+}
+
+func (tb *testingBackend) cloneItems() []*backend.Item {
+ clones := make([]*backend.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 be backend.B
+ be = &testingBackend{
+ items: []*backend.Item{
+ {Meta: backend.Meta{"projects/foo", "path", "####", "v1"},
+ Content: tpb(&configPB.Project{Id: proto.String("foo")})},
+ {Meta: backend.Meta{"projects/bar", "path", "####", "v1"},
+ Content: tpb(&configPB.Project{Id: proto.String("bar")})},
+ },
+ }
+
+ Convey(`Without a formatter backend, succeeds`, func() {
+ c = backend.WithBackend(c, be)
+
+ Convey(`Single`, func() {
+ var val configPB.Project
+ So(cfgclient.Get(c, cfgclient.AsService, "", "", Message(&val), nil), ShouldBeNil)
+ So(val, ShouldResemble, configPB.Project{Id: proto.String("foo")})
+ })
+
+ Convey(`Multi`, func() {
+ var (
+ val []*configPB.Project
+ meta []*cfgclient.Meta
+ )
+ So(cfgclient.Projects(c, cfgclient.AsService, "", Slice(&val), &meta), ShouldBeNil)
+ So(val, ShouldResemble, []*configPB.Project{
+ {Id: proto.String("foo")},
+ {Id: proto.String("bar")},
+ })
+ So(meta, ShouldResemble, []*cfgclient.Meta{
+ {"projects/foo", "path", "####", "v1"},
+ {"projects/bar", "path", "####", "v1"},
+ })
+ })
+ })
+
+ Convey(`With a formatter backend`, func() {
+ var fr cfgclient.FormatterRegistry
+ be = &format.Backend{
+ B: be,
+ GetRegistry: func(context.Context) *cfgclient.FormatterRegistry { return &fr },
+ }
+ c = backend.WithBackend(c, be)
+
+ Convey(`If the Formatter is not registered, fails.`, func() {
+ Convey(`Single`, func() {
+ var val configPB.Project
+ So(cfgclient.Get(c, cfgclient.AsService, "", "", Message(&val), nil), ShouldErrLike, "unknown formatter")
+ })
+
+ Convey(`Multi`, func() {
+ var val []*configPB.Project
+ So(cfgclient.Projects(c, cfgclient.AsService, "", Slice(&val), nil), ShouldErrLike, "unknown formatter")
+ })
+ })
+
+ Convey(`If the Formatter is registered, succeeds`, func() {
+ RegisterFormatter(&fr)
+
+ Convey(`Single`, func() {
+ var val configPB.Project
+ So(cfgclient.Get(c, cfgclient.AsService, "", "", Message(&val), nil), ShouldBeNil)
+ So(val, ShouldResemble, configPB.Project{Id: proto.String("foo")})
+ })
+
+ Convey(`Multi`, func() {
+ var (
+ val []*configPB.Project
+ meta []*cfgclient.Meta
+ )
+ So(cfgclient.Projects(c, cfgclient.AsService, "", Slice(&val), &meta), ShouldBeNil)
+ So(val, ShouldResemble, []*configPB.Project{
+ {Id: proto.String("foo")},
+ {Id: proto.String("bar")},
+ })
+ So(meta, ShouldResemble, []*cfgclient.Meta{
+ {"projects/foo", "path", "####", "v1"},
+ {"projects/bar", "path", "####", "v1"},
+ })
+ })
+ })
+ })
+ })
+}
« no previous file with comments | « luci_config/server/cfgclient/textproto/resolver.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698