Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The LUCI Authors. | 1 // Copyright 2015 The LUCI Authors. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 package coordinatorTest | 15 package coordinatorTest |
| 16 | 16 |
| 17 import ( | 17 import ( |
| 18 "fmt" | 18 "fmt" |
| 19 "path/filepath" | |
| 19 "strings" | 20 "strings" |
| 20 "time" | 21 "time" |
| 21 | 22 |
| 22 "github.com/luci/luci-go/common/clock" | 23 "github.com/luci/luci-go/common/clock" |
| 23 "github.com/luci/luci-go/common/clock/testclock" | 24 "github.com/luci/luci-go/common/clock/testclock" |
| 24 "github.com/luci/luci-go/common/config/impl/memory" | 25 "github.com/luci/luci-go/common/config/impl/memory" |
| 25 "github.com/luci/luci-go/common/data/caching/cacheContext" | 26 "github.com/luci/luci-go/common/data/caching/cacheContext" |
| 26 "github.com/luci/luci-go/common/gcloud/gs" | 27 "github.com/luci/luci-go/common/gcloud/gs" |
| 27 "github.com/luci/luci-go/common/logging" | 28 "github.com/luci/luci-go/common/logging" |
| 28 "github.com/luci/luci-go/common/logging/gologger" | 29 "github.com/luci/luci-go/common/logging/gologger" |
| 29 configPB "github.com/luci/luci-go/common/proto/config" | 30 configPB "github.com/luci/luci-go/common/proto/config" |
| 30 "github.com/luci/luci-go/common/proto/google" | 31 "github.com/luci/luci-go/common/proto/google" |
| 31 "github.com/luci/luci-go/logdog/api/config/svcconfig" | 32 "github.com/luci/luci-go/logdog/api/config/svcconfig" |
| 32 "github.com/luci/luci-go/logdog/appengine/coordinator" | 33 "github.com/luci/luci-go/logdog/appengine/coordinator" |
| 34 "github.com/luci/luci-go/logdog/appengine/coordinator/backend" | |
| 33 "github.com/luci/luci-go/logdog/appengine/coordinator/config" | 35 "github.com/luci/luci-go/logdog/appengine/coordinator/config" |
| 34 "github.com/luci/luci-go/logdog/common/storage/archive" | 36 "github.com/luci/luci-go/logdog/common/storage/archive" |
| 35 "github.com/luci/luci-go/logdog/common/storage/bigtable" | 37 "github.com/luci/luci-go/logdog/common/storage/bigtable" |
| 36 "github.com/luci/luci-go/luci_config/common/cfgtypes" | 38 "github.com/luci/luci-go/luci_config/common/cfgtypes" |
| 37 "github.com/luci/luci-go/luci_config/server/cfgclient" | 39 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 38 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/testconfig " | 40 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/testconfig " |
| 39 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" | 41 "github.com/luci/luci-go/luci_config/server/cfgclient/textproto" |
| 40 "github.com/luci/luci-go/server/auth" | 42 "github.com/luci/luci-go/server/auth" |
| 41 "github.com/luci/luci-go/server/auth/authtest" | 43 "github.com/luci/luci-go/server/auth/authtest" |
| 42 "github.com/luci/luci-go/server/auth/identity" | 44 "github.com/luci/luci-go/server/auth/identity" |
| 45 "github.com/luci/luci-go/server/router" | |
| 43 "github.com/luci/luci-go/server/settings" | 46 "github.com/luci/luci-go/server/settings" |
| 44 "github.com/luci/luci-go/tumble" | 47 "github.com/luci/luci-go/tumble" |
| 45 | 48 |
| 46 ds "github.com/luci/gae/service/datastore" | 49 ds "github.com/luci/gae/service/datastore" |
| 47 "github.com/luci/gae/service/info" | 50 "github.com/luci/gae/service/info" |
| 51 tq "github.com/luci/gae/service/taskqueue" | |
| 48 | 52 |
| 49 "github.com/golang/protobuf/proto" | 53 "github.com/golang/protobuf/proto" |
| 50 "golang.org/x/net/context" | 54 "golang.org/x/net/context" |
| 51 ) | 55 ) |
| 52 | 56 |
| 53 // Environment contains all of the testing facilities that are installed into | 57 // Environment contains all of the testing facilities that are installed into |
| 54 // the Context. | 58 // the Context. |
| 55 type Environment struct { | 59 type Environment struct { |
| 56 // Tumble is the Tumble testing instance. | 60 // Tumble is the Tumble testing instance. |
| 57 Tumble tumble.Testing | 61 Tumble tumble.Testing |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 | 154 |
| 151 func (e *Environment) addConfigEntry(configSet cfgtypes.ConfigSet, path, content string) { | 155 func (e *Environment) addConfigEntry(configSet cfgtypes.ConfigSet, path, content string) { |
| 152 cset := e.Config[string(configSet)] | 156 cset := e.Config[string(configSet)] |
| 153 if cset == nil { | 157 if cset == nil { |
| 154 cset = make(map[string]string) | 158 cset = make(map[string]string) |
| 155 e.Config[string(configSet)] = cset | 159 e.Config[string(configSet)] = cset |
| 156 } | 160 } |
| 157 cset[path] = content | 161 cset[path] = content |
| 158 } | 162 } |
| 159 | 163 |
| 164 // RunTaskQueues processes all tasks in all task queues if they are available. | |
| 165 func (e *Environment) RunTaskQueues(c context.Context, tls *TestStream) { | |
| 166 r := router.New() | |
| 167 | |
| 168 tls.WithProjectNamespace(c, func(c context.Context) { | |
| 169 backend.InstallHandlers(r, router.NewMiddlewareChain(func(ctx *r outer.Context, next router.Handler) { | |
| 170 ctx.Context = c | |
| 171 next(ctx) | |
| 172 })) | |
| 173 | |
| 174 drainTaskQueues(c, r) | |
| 175 }) | |
| 176 } | |
| 177 | |
| 160 // Install creates a testing Context and installs common test facilities into | 178 // Install creates a testing Context and installs common test facilities into |
| 161 // it, returning the Environment to which they're bound. | 179 // it, returning the Environment to which they're bound. |
| 162 func Install() (context.Context, *Environment) { | 180 func Install() (context.Context, *Environment) { |
| 163 e := Environment{ | 181 e := Environment{ |
| 164 Config: make(map[string]memory.ConfigSet), | 182 Config: make(map[string]memory.ConfigSet), |
| 165 GSClient: GSClient{}, | 183 GSClient: GSClient{}, |
| 166 StorageCache: StorageCache{ | 184 StorageCache: StorageCache{ |
| 167 Base: &coordinator.StorageCache{}, | 185 Base: &coordinator.StorageCache{}, |
| 168 }, | 186 }, |
| 169 } | 187 } |
| 170 | 188 |
| 171 // Get our starting context. This installs, among other things, in-memor y | 189 // Get our starting context. This installs, among other things, in-memor y |
| 172 // gae, settings, and logger. | 190 // gae, settings, and logger. |
| 173 c := e.Tumble.Context() | 191 c := e.Tumble.Context() |
| 174 if *testGoLogger { | 192 if *testGoLogger { |
| 175 c = logging.SetLevel(gologger.StdConfig.Use(c), logging.Debug) | 193 c = logging.SetLevel(gologger.StdConfig.Use(c), logging.Debug) |
| 176 } | 194 } |
| 177 | 195 |
| 178 // Create/install our BigTable memory instance. | 196 // Create/install our BigTable memory instance. |
| 179 e.BigTable = bigtable.NewMemoryInstance(c, bigtable.Options{ | 197 e.BigTable = bigtable.NewMemoryInstance(c, bigtable.Options{ |
| 180 Cache: &e.StorageCache, | 198 Cache: &e.StorageCache, |
| 181 }) | 199 }) |
| 182 | 200 |
| 183 » // Add indexes. These should match the indexes defined in the applicatio n's | 201 » // Register our task queues. |
| 184 » // "index.yaml". | 202 » tq.GetTestable(c).CreateQueue(backend.ArchivalTaskQueue) |
| 185 » indexDefs := [][]string{ | 203 |
| 186 » » {"Prefix", "-Created"}, | 204 » // Load indexes from "index.yaml". |
| 187 » » {"Name", "-Created"}, | 205 » mainServicePath := filepath.Join("..", "..", "..", "cmd", "coordinator", "vmuser") |
|
dnj
2017/08/03 03:45:55
This is technically independent, but small enough
| |
| 188 » » {"State", "-Created"}, | 206 » indexDefs, err := ds.FindAndParseIndexYAML(mainServicePath) |
| 189 » » {"Purged", "-Created"}, | 207 » if err != nil { |
| 190 » » {"ProtoVersion", "-Created"}, | 208 » » panic(fmt.Errorf("failed to load 'index.yaml': %s", err)) |
| 191 » » {"ContentType", "-Created"}, | |
| 192 » » {"StreamType", "-Created"}, | |
| 193 » » {"Timestamp", "-Created"}, | |
| 194 » » {"_C", "-Created"}, | |
| 195 » » {"_Tags", "-Created"}, | |
| 196 » » {"_Terminated", "-Created"}, | |
| 197 » » {"_Archived", "-Created"}, | |
| 198 } | 209 } |
| 199 » indexes := make([]*ds.IndexDefinition, len(indexDefs)) | 210 » ds.GetTestable(c).AddIndexes(indexDefs...) |
| 200 » for i, id := range indexDefs { | |
| 201 » » cols := make([]ds.IndexColumn, len(id)) | |
| 202 » » for j, ic := range id { | |
| 203 » » » var err error | |
| 204 » » » cols[j], err = ds.ParseIndexColumn(ic) | |
| 205 » » » if err != nil { | |
| 206 » » » » panic(fmt.Errorf("failed to parse index %q: %s", ic, err)) | |
| 207 » » » } | |
| 208 » » } | |
| 209 » » indexes[i] = &ds.IndexDefinition{Kind: "LogStream", SortBy: cols } | |
| 210 » } | |
| 211 » ds.GetTestable(c).AddIndexes(indexes...) | |
| 212 | 211 |
| 213 // Setup clock. | 212 // Setup clock. |
| 214 e.Clock = clock.Get(c).(testclock.TestClock) | 213 e.Clock = clock.Get(c).(testclock.TestClock) |
| 215 | 214 |
| 216 // Install GAE config service settings. | 215 // Install GAE config service settings. |
| 217 c = settings.Use(c, settings.New(&settings.MemoryStorage{})) | 216 c = settings.Use(c, settings.New(&settings.MemoryStorage{})) |
| 218 | 217 |
| 219 // Setup luci-config configuration. | 218 // Setup luci-config configuration. |
| 220 c = testconfig.WithCommonClient(c, memory.New(e.Config)) | 219 c = testconfig.WithCommonClient(c, memory.New(e.Config)) |
| 221 | 220 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 } | 329 } |
| 331 | 330 |
| 332 // WithProjectNamespace runs f in proj's namespace, bypassing authentication | 331 // WithProjectNamespace runs f in proj's namespace, bypassing authentication |
| 333 // checks. | 332 // checks. |
| 334 func WithProjectNamespace(c context.Context, proj cfgtypes.ProjectName, f func(c ontext.Context)) { | 333 func WithProjectNamespace(c context.Context, proj cfgtypes.ProjectName, f func(c ontext.Context)) { |
| 335 if err := coordinator.WithProjectNamespace(&c, proj, coordinator.Namespa ceAccessAllTesting); err != nil { | 334 if err := coordinator.WithProjectNamespace(&c, proj, coordinator.Namespa ceAccessAllTesting); err != nil { |
| 336 panic(err) | 335 panic(err) |
| 337 } | 336 } |
| 338 f(c) | 337 f(c) |
| 339 } | 338 } |
| OLD | NEW |