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

Side by Side Diff: logdog/appengine/coordinator/coordinatorTest/context.go

Issue 2575383002: Add server/cache support to gaeconfig. (Closed)
Patch Set: 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package coordinatorTest 5 package coordinatorTest
6 6
7 import ( 7 import (
8 "fmt" 8 "fmt"
9 "strings" 9 "strings"
10 "time" 10 "time"
11 11
12 "github.com/luci/luci-go/common/clock" 12 "github.com/luci/luci-go/common/clock"
13 "github.com/luci/luci-go/common/clock/testclock" 13 "github.com/luci/luci-go/common/clock/testclock"
14 luciConfig "github.com/luci/luci-go/common/config" 14 luciConfig "github.com/luci/luci-go/common/config"
15 "github.com/luci/luci-go/common/config/impl/memory" 15 "github.com/luci/luci-go/common/config/impl/memory"
16 "github.com/luci/luci-go/common/data/caching/cacheContext" 16 "github.com/luci/luci-go/common/data/caching/cacheContext"
17 "github.com/luci/luci-go/common/gcloud/gs" 17 "github.com/luci/luci-go/common/gcloud/gs"
18 "github.com/luci/luci-go/common/logging" 18 "github.com/luci/luci-go/common/logging"
19 "github.com/luci/luci-go/common/logging/gologger" 19 "github.com/luci/luci-go/common/logging/gologger"
20 configPB "github.com/luci/luci-go/common/proto/config"
20 "github.com/luci/luci-go/common/proto/google" 21 "github.com/luci/luci-go/common/proto/google"
21 "github.com/luci/luci-go/logdog/api/config/svcconfig" 22 "github.com/luci/luci-go/logdog/api/config/svcconfig"
22 "github.com/luci/luci-go/logdog/appengine/coordinator" 23 "github.com/luci/luci-go/logdog/appengine/coordinator"
23 "github.com/luci/luci-go/logdog/appengine/coordinator/config" 24 "github.com/luci/luci-go/logdog/appengine/coordinator/config"
24 "github.com/luci/luci-go/logdog/common/storage/archive" 25 "github.com/luci/luci-go/logdog/common/storage/archive"
25 "github.com/luci/luci-go/logdog/common/storage/bigtable" 26 "github.com/luci/luci-go/logdog/common/storage/bigtable"
26 "github.com/luci/luci-go/server/auth" 27 "github.com/luci/luci-go/server/auth"
27 "github.com/luci/luci-go/server/auth/authtest" 28 "github.com/luci/luci-go/server/auth/authtest"
28 "github.com/luci/luci-go/server/auth/identity" 29 "github.com/luci/luci-go/server/auth/identity"
30 serverConfig "github.com/luci/luci-go/server/config"
31 "github.com/luci/luci-go/server/config/testconfig"
32 "github.com/luci/luci-go/server/config/textproto"
29 "github.com/luci/luci-go/server/settings" 33 "github.com/luci/luci-go/server/settings"
30 "github.com/luci/luci-go/tumble" 34 "github.com/luci/luci-go/tumble"
31 35
32 ds "github.com/luci/gae/service/datastore" 36 ds "github.com/luci/gae/service/datastore"
33 "github.com/luci/gae/service/info" 37 "github.com/luci/gae/service/info"
34 38
35 "github.com/golang/protobuf/proto" 39 "github.com/golang/protobuf/proto"
36 "golang.org/x/net/context" 40 "golang.org/x/net/context"
37 ) 41 )
38 42
39 // Environment contains all of the testing facilities that are installed into 43 // Environment contains all of the testing facilities that are installed into
40 // the Context. 44 // the Context.
41 type Environment struct { 45 type Environment struct {
42 // Tumble is the Tumble testing instance. 46 // Tumble is the Tumble testing instance.
43 Tumble tumble.Testing 47 Tumble tumble.Testing
44 48
45 // Clock is the installed test clock instance. 49 // Clock is the installed test clock instance.
46 Clock testclock.TestClock 50 Clock testclock.TestClock
47 51
48 // AuthState is the fake authentication state. 52 // AuthState is the fake authentication state.
49 AuthState authtest.FakeState 53 AuthState authtest.FakeState
50 54
51 // Config is the luci-config configuration map that is installed. 55 // Config is the luci-config configuration map that is installed.
52 Config map[string]memory.ConfigSet 56 Config map[string]memory.ConfigSet
53 // ConfigIface is a reference to the memory config.Interface that Config is
54 // installed into.
55 ConfigIface luciConfig.Interface
56 57
57 // Services is the set of installed Coordinator services. 58 // Services is the set of installed Coordinator services.
58 Services Services 59 Services Services
59 60
60 // BigTable in-memory testing instance. 61 // BigTable in-memory testing instance.
61 BigTable bigtable.Testing 62 BigTable bigtable.Testing
62 // GSClient is the test GSClient instance installed (by default) into 63 // GSClient is the test GSClient instance installed (by default) into
63 // Services. 64 // Services.
64 GSClient GSClient 65 GSClient GSClient
65 66
(...skipping 16 matching lines...) Expand all
82 83
83 // JoinGroup adds the named group the to the list of groups for the current 84 // JoinGroup adds the named group the to the list of groups for the current
84 // identity. 85 // identity.
85 func (e *Environment) JoinGroup(g string) { 86 func (e *Environment) JoinGroup(g string) {
86 e.AuthState.IdentityGroups = append(e.AuthState.IdentityGroups, g) 87 e.AuthState.IdentityGroups = append(e.AuthState.IdentityGroups, g)
87 } 88 }
88 89
89 // LeaveAllGroups clears all auth groups that the user is currently a member of. 90 // LeaveAllGroups clears all auth groups that the user is currently a member of.
90 func (e *Environment) LeaveAllGroups() { 91 func (e *Environment) LeaveAllGroups() {
91 e.AuthState.IdentityGroups = nil 92 e.AuthState.IdentityGroups = nil
92 e.JoinGroup("all")
93 } 93 }
94 94
95 // ClearCoordinatorConfig removes the Coordinator configuration entry, 95 // ClearCoordinatorConfig removes the Coordinator configuration entry,
96 // simulating a missing config. 96 // simulating a missing config.
97 func (e *Environment) ClearCoordinatorConfig(c context.Context) { 97 func (e *Environment) ClearCoordinatorConfig(c context.Context) {
98 configSet, _ := config.ServiceConfigPath(c) 98 configSet, _ := config.ServiceConfigPath(c)
99 delete(e.Config, configSet) 99 delete(e.Config, configSet)
100 } 100 }
101 101
102 // ModServiceConfig loads the current service configuration, invokes the 102 // ModServiceConfig loads the current service configuration, invokes the
(...skipping 13 matching lines...) Expand all
116 configSet, configPath := luciConfig.ProjectConfigSet(proj), config.Proje ctConfigPath(c) 116 configSet, configPath := luciConfig.ProjectConfigSet(proj), config.Proje ctConfigPath(c)
117 117
118 var pcfg svcconfig.ProjectConfig 118 var pcfg svcconfig.ProjectConfig
119 e.modTextProtobuf(c, configSet, configPath, &pcfg, func() { 119 e.modTextProtobuf(c, configSet, configPath, &pcfg, func() {
120 fn(&pcfg) 120 fn(&pcfg)
121 }) 121 })
122 } 122 }
123 123
124 // IterateTumbleAll iterates all Tumble instances across all namespaces. 124 // IterateTumbleAll iterates all Tumble instances across all namespaces.
125 func (e *Environment) IterateTumbleAll(c context.Context) { 125 func (e *Environment) IterateTumbleAll(c context.Context) {
126 » projects, err := luciConfig.GetProjects(c) 126 » projects, err := config.ActiveProjects(c)
127 if err != nil { 127 if err != nil {
128 panic(err) 128 panic(err)
129 } 129 }
130 130
131 for _, proj := range projects { 131 for _, proj := range projects {
132 » » WithProjectNamespace(c, luciConfig.ProjectName(proj.ID), func(c context.Context) { 132 » » WithProjectNamespace(c, proj, func(c context.Context) {
133 e.Tumble.Iterate(c) 133 e.Tumble.Iterate(c)
134 }) 134 })
135 } 135 }
136 } 136 }
137 137
138 func (e *Environment) modTextProtobuf(c context.Context, configSet, path string, msg proto.Message, fn func()) { 138 func (e *Environment) modTextProtobuf(c context.Context, configSet, path string, msg proto.Message, fn func()) {
139 » cfg, err := e.ConfigIface.GetConfig(c, configSet, path, false) 139 » switch err := serverConfig.Get(c, serverConfig.AsService, configSet, pat h, textproto.Message(msg), nil); err {
140 140 » case nil, luciConfig.ErrNoConfig:
141 » switch err {
142 » case nil:
143 » » if err := proto.UnmarshalText(cfg.Content, msg); err != nil {
144 » » » panic(err)
145 » » }
146
147 » case luciConfig.ErrNoConfig:
148 break 141 break
149
150 default: 142 default:
151 panic(err) 143 panic(err)
152 } 144 }
153 145
154 fn() 146 fn()
155 e.addConfigEntry(configSet, path, proto.MarshalTextString(msg)) 147 e.addConfigEntry(configSet, path, proto.MarshalTextString(msg))
156 } 148 }
157 149
158 func (e *Environment) addConfigEntry(configSet, path, content string) { 150 func (e *Environment) addConfigEntry(configSet, path, content string) {
159 cset := e.Config[configSet] 151 cset := e.Config[configSet]
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 } 209 }
218 ds.GetTestable(c).AddIndexes(indexes...) 210 ds.GetTestable(c).AddIndexes(indexes...)
219 211
220 // Setup clock. 212 // Setup clock.
221 e.Clock = clock.Get(c).(testclock.TestClock) 213 e.Clock = clock.Get(c).(testclock.TestClock)
222 214
223 // Install GAE config service settings. 215 // Install GAE config service settings.
224 c = settings.Use(c, settings.New(&settings.MemoryStorage{})) 216 c = settings.Use(c, settings.New(&settings.MemoryStorage{}))
225 217
226 // Setup luci-config configuration. 218 // Setup luci-config configuration.
227 » e.ConfigIface = memory.New(e.Config) 219 » c = testconfig.WithCommonClient(c, memory.New(e.Config))
228 » c = luciConfig.SetImplementation(c, e.ConfigIface)
229 220
230 // luci-config: Projects. 221 // luci-config: Projects.
231 projectName := info.AppID(c) 222 projectName := info.AppID(c)
232 addProjectConfig := func(proj luciConfig.ProjectName, access ...string) { 223 addProjectConfig := func(proj luciConfig.ProjectName, access ...string) {
224 projectAccesses := make([]string, len(access))
225
226 // Build our service config. Also builds "projectAccesses".
233 e.ModProjectConfig(c, proj, func(pcfg *svcconfig.ProjectConfig) { 227 e.ModProjectConfig(c, proj, func(pcfg *svcconfig.ProjectConfig) {
234 » » » for _, a := range access { 228 » » » for i, a := range access {
235 parts := strings.SplitN(a, ":", 2) 229 parts := strings.SplitN(a, ":", 2)
236 group, field := parts[0], &pcfg.ReaderAuthGroups 230 group, field := parts[0], &pcfg.ReaderAuthGroups
237 if len(parts) == 2 { 231 if len(parts) == 2 {
238 switch parts[1] { 232 switch parts[1] {
239 case "R": 233 case "R":
240 break 234 break
241 case "W": 235 case "W":
242 field = &pcfg.WriterAuthGroups 236 field = &pcfg.WriterAuthGroups
243 default: 237 default:
244 panic(a) 238 panic(a)
245 } 239 }
246 } 240 }
247 *field = append(*field, group) 241 *field = append(*field, group)
242 projectAccesses[i] = fmt.Sprintf("group:%s", gro up)
243 }
244 })
245
246 var pcfg configPB.ProjectCfg
247 e.modTextProtobuf(c, serverConfig.ProjectConfigSet(proj), server Config.ProjectConfigPath, &pcfg, func() {
248 pcfg = configPB.ProjectCfg{
249 Name: proto.String(string(proj)),
250 Access: projectAccesses,
248 } 251 }
249 }) 252 })
250 } 253 }
251 addProjectConfig("proj-foo", "all:R", "all:W") 254 addProjectConfig("proj-foo", "all:R", "all:W")
252 addProjectConfig("proj-bar", "all:R", "auth:W") 255 addProjectConfig("proj-bar", "all:R", "auth:W")
253 addProjectConfig("proj-exclusive", "auth:R", "auth:W") 256 addProjectConfig("proj-exclusive", "auth:R", "auth:W")
254 257
255 // Add a project without a LogDog project config. 258 // Add a project without a LogDog project config.
256 e.addConfigEntry("projects/proj-unconfigured", "not-logdog.cfg", "junk") 259 e.addConfigEntry("projects/proj-unconfigured", "not-logdog.cfg", "junk")
257 260
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 } 329 }
327 330
328 // WithProjectNamespace runs f in proj's namespace, bypassing authentication 331 // WithProjectNamespace runs f in proj's namespace, bypassing authentication
329 // checks. 332 // checks.
330 func WithProjectNamespace(c context.Context, proj luciConfig.ProjectName, f func (context.Context)) { 333 func WithProjectNamespace(c context.Context, proj luciConfig.ProjectName, f func (context.Context)) {
331 if err := coordinator.WithProjectNamespace(&c, proj, coordinator.Namespa ceAccessAllTesting); err != nil { 334 if err := coordinator.WithProjectNamespace(&c, proj, coordinator.Namespa ceAccessAllTesting); err != nil {
332 panic(err) 335 panic(err)
333 } 336 }
334 f(c) 337 f(c)
335 } 338 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698