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

Side by Side Diff: luci_config/server/cfgclient/backend/format/format_test.go

Issue 2580713002: Implement a server-side config service interface. (Closed)
Patch Set: Renamed, moved to luci_config package, fixes, split out backends. 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 unified diff | Download patch
OLDNEW
(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 format
6
7 import (
8 "testing"
9
10 "github.com/luci/luci-go/luci_config/server/cfgclient"
11 "github.com/luci/luci-go/luci_config/server/cfgclient/backend"
12
13 "golang.org/x/net/context"
14
15 . "github.com/smartystreets/goconvey/convey"
16 )
17
18 // testingBackend is a Backend implementation that ignores Authority.
19 type testingBackend struct {
20 backend.B
21 err error
22 items []*backend.Item
23 }
24
25 func (tb *testingBackend) Get(c context.Context, configSet, path string, p backe nd.Params) (*backend.Item, error) {
26 if err := tb.err; err != nil {
27 return nil, tb.err
28 }
29 if len(tb.items) == 0 {
30 return nil, cfgclient.ErrNoConfig
31 }
32 return tb.cloneItems()[0], nil
33 }
34
35 func (tb *testingBackend) GetAll(c context.Context, t backend.GetAllTarget, path string, p backend.Params) (
36 []*backend.Item, error) {
37
38 if err := tb.err; err != nil {
39 return nil, tb.err
40 }
41 return tb.cloneItems(), nil
42 }
43
44 func (tb *testingBackend) cloneItems() []*backend.Item {
45 clones := make([]*backend.Item, len(tb.items))
46 for i, it := range tb.items {
47 clone := *it
48 clones[i] = &clone
49 }
50 return clones
51 }
52
53 // retainingBackend is a simple Backend implementation that retains the last set
54 // of data that it received.
55 type retainingBackend struct {
56 backend.B
57
58 lastItems []*backend.Item
59 lastErr error
60 }
61
62 func (b *retainingBackend) Get(c context.Context, configSet, path string, p back end.Params) (*backend.Item, error) {
63 var item *backend.Item
64 item, b.lastErr = b.B.Get(c, configSet, path, p)
65 b.lastItems = []*backend.Item{item}
66 return item, b.lastErr
67 }
68
69 func (b *retainingBackend) GetAll(c context.Context, t backend.GetAllTarget, pat h string, p backend.Params) (
70 []*backend.Item, error) {
71
72 b.lastItems, b.lastErr = b.B.GetAll(c, t, path, p)
73 return b.lastItems, b.lastErr
74 }
75
76 type customFormatter string
77
78 func (cf customFormatter) FormatItem(c string, fd string) (string, error) {
79 return string(cf), nil
80 }
81
82 type retainingResolver struct {
83 format string
84 item *backend.Item
85 }
86
87 func (rr *retainingResolver) Format() backend.FormatSpec { return backend.Format Spec{rr.format, ""} }
88 func (rr *retainingResolver) Resolve(it *backend.Item) error {
89 rr.item = it
90 return nil
91 }
92
93 var _ interface {
94 cfgclient.Resolver
95 cfgclient.FormattingResolver
96 } = (*retainingResolver)(nil)
97
98 type panicFormatter struct{}
99
100 func (pf panicFormatter) FormatItem(string, string) (string, error) { panic("pan ic") }
101
102 func TestFormatBackend(t *testing.T) {
103 t.Parallel()
104
105 Convey(`A testing environment`, t, func() {
106 tb := testingBackend{
107 items: []*backend.Item{
108 {Meta: backend.Meta{"projects/foo", "path", "### #", "v1"}, Content: "foo"},
109 {Meta: backend.Meta{"projects/bar", "path", "### #", "v1"}, Content: "bar"},
110 },
111 }
112
113 // Pass all things from the backend through the formatter.
114 var fr cfgclient.FormatterRegistry
115 fb := Backend{
116 B: &tb,
117 GetRegistry: func(context.Context) *cfgclient.FormatterR egistry { return &fr },
118 }
119
120 // Retain all raw Items that were returned for examination.
121 rb := retainingBackend{
122 B: &fb,
123 }
124
125 c := context.Background()
126 c = backend.WithBackend(c, &rb)
127
128 Convey(`Will panic if an attempt to register empty key is made.` , func() {
129 So(func() { fr.Register("", nil) }, ShouldPanic)
130 })
131
132 Convey(`Will ignore items that have already been formatted.`, fu nc() {
133 rr := retainingResolver{"test", nil}
134 fr.Register("test", panicFormatter{})
135
136 // Confirm that this setup correctly attempts to format the item.
137 So(func() { cfgclient.Get(c, cfgclient.AsService, "", "" , &rr, nil) }, ShouldPanic)
138
139 // Now, pretend the item is already formatter. We should not get a panic.
140 tb.items[0].FormatSpec.Formatter = "something"
141 So(cfgclient.Get(c, cfgclient.AsService, "", "", &rr, ni l), ShouldBeNil)
142 })
143
144 Convey(`Testing custom formatter`, func() {
145 cf := customFormatter("content")
146 fr.Register("test", cf)
147
148 // Confirm that this setup correctly attempts to format the item.
149 rr := retainingResolver{"test", nil}
150 So(cfgclient.Get(c, cfgclient.AsService, "", "", &rr, ni l), ShouldBeNil)
151 So(rr.item, ShouldNotBeNil)
152 So(rr.item.FormatSpec.Formatter, ShouldEqual, "test")
153 So(rr.item.Content, ShouldEqual, "content")
154 })
155 })
156 }
OLDNEW
« no previous file with comments | « luci_config/server/cfgclient/backend/format/format.go ('k') | luci_config/server/cfgclient/backend/gen.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698