| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package coordinatorTest | 5 package coordinatorTest |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 | 9 |
| 10 "github.com/golang/protobuf/proto" | 10 "github.com/golang/protobuf/proto" |
| 11 ds "github.com/luci/gae/service/datastore" | 11 ds "github.com/luci/gae/service/datastore" |
| 12 "github.com/luci/luci-go/appengine/logdog/coordinator" | 12 "github.com/luci/luci-go/appengine/logdog/coordinator" |
| 13 "github.com/luci/luci-go/appengine/logdog/coordinator/config" | 13 "github.com/luci/luci-go/appengine/logdog/coordinator/config" |
| 14 "github.com/luci/luci-go/appengine/tumble" | 14 "github.com/luci/luci-go/appengine/tumble" |
| 15 "github.com/luci/luci-go/common/clock" | 15 "github.com/luci/luci-go/common/clock" |
| 16 "github.com/luci/luci-go/common/clock/testclock" | 16 "github.com/luci/luci-go/common/clock/testclock" |
| 17 luciConfig "github.com/luci/luci-go/common/config" | 17 luciConfig "github.com/luci/luci-go/common/config" |
| 18 "github.com/luci/luci-go/common/config/impl/memory" | 18 "github.com/luci/luci-go/common/config/impl/memory" |
| 19 "github.com/luci/luci-go/common/gcloud/gs" | 19 "github.com/luci/luci-go/common/gcloud/gs" |
| 20 "github.com/luci/luci-go/common/logging" | 20 "github.com/luci/luci-go/common/logging" |
| 21 "github.com/luci/luci-go/common/logging/gologger" | 21 "github.com/luci/luci-go/common/logging/gologger" |
| 22 configProto "github.com/luci/luci-go/common/proto/config" | |
| 23 "github.com/luci/luci-go/common/proto/logdog/svcconfig" | 22 "github.com/luci/luci-go/common/proto/logdog/svcconfig" |
| 24 "github.com/luci/luci-go/server/auth" | 23 "github.com/luci/luci-go/server/auth" |
| 25 "github.com/luci/luci-go/server/auth/authtest" | 24 "github.com/luci/luci-go/server/auth/authtest" |
| 26 "github.com/luci/luci-go/server/logdog/storage" | 25 "github.com/luci/luci-go/server/logdog/storage" |
| 27 memoryStorage "github.com/luci/luci-go/server/logdog/storage/memory" | 26 memoryStorage "github.com/luci/luci-go/server/logdog/storage/memory" |
| 28 "github.com/luci/luci-go/server/settings" | 27 "github.com/luci/luci-go/server/settings" |
| 29 "golang.org/x/net/context" | 28 "golang.org/x/net/context" |
| 30 ) | 29 ) |
| 31 | 30 |
| 32 // Environment contains all of the testing facilities that are installed into | 31 // Environment contains all of the testing facilities that are installed into |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 80 |
| 82 var cfg svcconfig.Config | 81 var cfg svcconfig.Config |
| 83 e.modTextProtobuf(configSet, configPath, &cfg, func() { | 82 e.modTextProtobuf(configSet, configPath, &cfg, func() { |
| 84 if cfg.Coordinator == nil { | 83 if cfg.Coordinator == nil { |
| 85 cfg.Coordinator = &svcconfig.Coordinator{} | 84 cfg.Coordinator = &svcconfig.Coordinator{} |
| 86 } | 85 } |
| 87 fn(cfg.Coordinator) | 86 fn(cfg.Coordinator) |
| 88 }) | 87 }) |
| 89 } | 88 } |
| 90 | 89 |
| 90 // ModProjectConfig loads the current configuration for the named project, |
| 91 // invokes the callback with its contents, and writes the result back to config. |
| 92 func (e *Environment) ModProjectConfig(proj luciConfig.ProjectName, fn func(*svc
config.ProjectConfig)) { |
| 93 configSet, configPath := config.ProjectConfigPath(proj) |
| 94 |
| 95 var pcfg svcconfig.ProjectConfig |
| 96 e.modTextProtobuf(configSet, configPath, &pcfg, func() { |
| 97 fn(&pcfg) |
| 98 }) |
| 99 } |
| 100 |
| 91 // DrainTumbleAll drains all Tumble instances across all namespaces. | 101 // DrainTumbleAll drains all Tumble instances across all namespaces. |
| 92 func (e *Environment) DrainTumbleAll(c context.Context) { | 102 func (e *Environment) DrainTumbleAll(c context.Context) { |
| 93 projects, err := luciConfig.Get(c).GetProjects() | 103 projects, err := luciConfig.Get(c).GetProjects() |
| 94 if err != nil { | 104 if err != nil { |
| 95 panic(err) | 105 panic(err) |
| 96 } | 106 } |
| 97 | 107 |
| 98 for _, proj := range projects { | 108 for _, proj := range projects { |
| 99 WithProjectNamespace(c, luciConfig.ProjectName(proj.ID), func(c
context.Context) { | 109 WithProjectNamespace(c, luciConfig.ProjectName(proj.ID), func(c
context.Context) { |
| 100 e.Tumble.Drain(c) | 110 e.Tumble.Drain(c) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 112 } | 122 } |
| 113 | 123 |
| 114 case luciConfig.ErrNoConfig: | 124 case luciConfig.ErrNoConfig: |
| 115 break | 125 break |
| 116 | 126 |
| 117 default: | 127 default: |
| 118 panic(err) | 128 panic(err) |
| 119 } | 129 } |
| 120 | 130 |
| 121 fn() | 131 fn() |
| 132 e.addConfigEntry(configSet, path, proto.MarshalTextString(msg)) |
| 133 } |
| 122 | 134 |
| 135 func (e *Environment) addConfigEntry(configSet, path, content string) { |
| 123 cset := e.Config[configSet] | 136 cset := e.Config[configSet] |
| 124 if cset == nil { | 137 if cset == nil { |
| 125 cset = make(map[string]string) | 138 cset = make(map[string]string) |
| 126 e.Config[configSet] = cset | 139 e.Config[configSet] = cset |
| 127 } | 140 } |
| 128 » cset[path] = proto.MarshalTextString(msg) | 141 » cset[path] = content |
| 129 } | 142 } |
| 130 | 143 |
| 131 // Install creates a testing Context and installs common test facilities into | 144 // Install creates a testing Context and installs common test facilities into |
| 132 // it, returning the Environment to which they're bound. | 145 // it, returning the Environment to which they're bound. |
| 133 func Install() (context.Context, *Environment) { | 146 func Install() (context.Context, *Environment) { |
| 134 e := Environment{ | 147 e := Environment{ |
| 135 Config: make(map[string]memory.ConfigSet), | 148 Config: make(map[string]memory.ConfigSet), |
| 136 } | 149 } |
| 137 | 150 |
| 138 // Get our starting context. This installs, among other things, in-memor
y | 151 // Get our starting context. This installs, among other things, in-memor
y |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 e.Clock = clock.Get(c).(testclock.TestClock) | 189 e.Clock = clock.Get(c).(testclock.TestClock) |
| 177 | 190 |
| 178 // Install GAE config service settings. | 191 // Install GAE config service settings. |
| 179 c = settings.Use(c, settings.New(&settings.MemoryStorage{})) | 192 c = settings.Use(c, settings.New(&settings.MemoryStorage{})) |
| 180 | 193 |
| 181 // Setup luci-config configuration. | 194 // Setup luci-config configuration. |
| 182 c = memory.Use(c, e.Config) | 195 c = memory.Use(c, e.Config) |
| 183 e.ConfigIface = luciConfig.Get(c) | 196 e.ConfigIface = luciConfig.Get(c) |
| 184 | 197 |
| 185 // luci-config: Projects. | 198 // luci-config: Projects. |
| 186 » addProjectConfig := func(name luciConfig.ProjectName, localName string,
access ...string) { | 199 » addProjectConfig := func(proj luciConfig.ProjectName, access ...string)
{ |
| 187 » » var cfg configProto.ProjectCfg | 200 » » e.ModProjectConfig(proj, func(pcfg *svcconfig.ProjectConfig) { |
| 188 » » e.modTextProtobuf(fmt.Sprintf("projects/%s", name), "project.cfg
", &cfg, func() { | 201 » » » pcfg.ReaderAuthGroups = access |
| 189 » » » cfg.Name = &localName | |
| 190 » » » cfg.Access = access | |
| 191 }) | 202 }) |
| 192 } | 203 } |
| 193 » addProjectConfig("proj-foo", "Foo Project", "group:all") | 204 » addProjectConfig("proj-foo", "all") |
| 194 » addProjectConfig("proj-bar", "Bar Project", "group:all") | 205 » addProjectConfig("proj-bar", "all") |
| 195 » addProjectConfig("proj-baz", "Baz Project", "group:all") | 206 » addProjectConfig("proj-exclusive", "auth") |
| 196 » addProjectConfig("proj-qux", "Qux Project", "group:all") | 207 |
| 197 » addProjectConfig("proj-exclusive", "Exclusive Project", "group:auth") | 208 » // Add a project without a LogDog project config. |
| 209 » e.addConfigEntry("projects/proj-unconfigured", "not-logdog.cfg", "junk") |
| 210 » e.addConfigEntry("projects/proj-malformed", svcconfig.ProjectConfigFilen
ame, "!!! not a text protobuf !!!") |
| 198 | 211 |
| 199 // luci-config: Coordinator Defaults | 212 // luci-config: Coordinator Defaults |
| 200 e.ModServiceConfig(c, func(cfg *svcconfig.Coordinator) { | 213 e.ModServiceConfig(c, func(cfg *svcconfig.Coordinator) { |
| 201 *cfg = svcconfig.Coordinator{ | 214 *cfg = svcconfig.Coordinator{ |
| 202 AdminAuthGroup: "admin", | 215 AdminAuthGroup: "admin", |
| 203 ServiceAuthGroup: "services", | 216 ServiceAuthGroup: "services", |
| 204 } | 217 } |
| 205 }) | 218 }) |
| 206 | 219 |
| 207 // Setup Tumble. This also adds the two Tumble indexes to datastore. | 220 // Setup Tumble. This also adds the two Tumble indexes to datastore. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 235 } | 248 } |
| 236 | 249 |
| 237 // WithProjectNamespace runs f in proj's namespace, bypassing authentication | 250 // WithProjectNamespace runs f in proj's namespace, bypassing authentication |
| 238 // checks. | 251 // checks. |
| 239 func WithProjectNamespace(c context.Context, proj luciConfig.ProjectName, f func
(context.Context)) { | 252 func WithProjectNamespace(c context.Context, proj luciConfig.ProjectName, f func
(context.Context)) { |
| 240 if err := coordinator.WithProjectNamespaceNoAuth(&c, proj); err != nil { | 253 if err := coordinator.WithProjectNamespaceNoAuth(&c, proj); err != nil { |
| 241 panic(err) | 254 panic(err) |
| 242 } | 255 } |
| 243 f(c) | 256 f(c) |
| 244 } | 257 } |
| OLD | NEW |