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

Unified Diff: luci_config/server/cfgclient/backend/testconfig/local_service.go

Issue 2582433002: server/config: Add test configuration package. (Closed)
Patch Set: Relocated, rebased. 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 | « no previous file | luci_config/server/cfgclient/backend/testconfig/local_service_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: luci_config/server/cfgclient/backend/testconfig/local_service.go
diff --git a/luci_config/server/cfgclient/backend/testconfig/local_service.go b/luci_config/server/cfgclient/backend/testconfig/local_service.go
new file mode 100644
index 0000000000000000000000000000000000000000..97e04b008e6b6a4fdeeee783eb784cf119910ed7
--- /dev/null
+++ b/luci_config/server/cfgclient/backend/testconfig/local_service.go
@@ -0,0 +1,105 @@
+// 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 testconfig
+
+import (
+ "net/url"
+
+ ccfg "github.com/luci/luci-go/common/config"
+ "github.com/luci/luci-go/luci_config/common/cfgtypes"
+ "github.com/luci/luci-go/luci_config/server/cfgclient"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/access"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend"
+ "github.com/luci/luci-go/luci_config/server/cfgclient/backend/client"
+
+ "golang.org/x/net/context"
+)
+
+// WithCommonClient installs a backend.B into the supplied Context that
+// loads its configuration from base.
+func WithCommonClient(c context.Context, base ccfg.Interface) context.Context {
+ return backend.WithBackend(c, &client.Backend{
+ Provider: &Provider{
+ Base: base,
+ },
+ })
+}
+
+// Provider is a client.Provider that is backed by a config interface.
+// This differs from client.RemoteProvider in that it takes an arbitrary
+// config interface instance rather than generating a remote one.
+//
+// Its intended use is for testing setups.
+//
+// Provider performs access checks locally using access.CheckAccess.
+type Provider struct {
+ // Base is the base interface.
+ Base ccfg.Interface
+}
+
+var _ client.Provider = (*Provider)(nil)
+
+// GetConfigClient implements ClientProvider.
+func (p *Provider) GetConfigClient(c context.Context, a backend.Authority) ccfg.Interface {
+ return &boundLocalInterface{
+ Context: c,
+ Interface: p.Base,
+ lsp: p,
+ a: a,
+ }
+}
+
+// boundLocalInterface is a ccfg.Interface implementation that adds ACL
+// pruning to the results based on the bound authority.
+type boundLocalInterface struct {
+ context.Context
+
+ // Interface is the config interface implementation.
+ ccfg.Interface
+
+ lsp *Provider
+ a backend.Authority
+}
+
+func (bli *boundLocalInterface) GetConfig(ctx context.Context, configSet, path string, hashOnly bool) (*ccfg.Config, error) {
+ if err := access.Check(bli, bli.a, cfgtypes.ConfigSet(configSet)); err != nil {
+ return nil, ccfg.ErrNoConfig
+ }
+ return bli.Interface.GetConfig(ctx, configSet, path, hashOnly)
+}
+
+func (bli *boundLocalInterface) GetProjectConfigs(ctx context.Context, path string, hashesOnly bool) ([]ccfg.Config, error) {
+ cfgs, err := bli.Interface.GetProjectConfigs(ctx, path, hashesOnly)
+ if err != nil {
+ return nil, err
+ }
+ return bli.pruneConfigList(cfgs), nil
+}
+
+func (bli *boundLocalInterface) GetRefConfigs(ctx context.Context, path string, hashesOnly bool) ([]ccfg.Config, error) {
+ cfgs, err := bli.Interface.GetRefConfigs(ctx, path, hashesOnly)
+ if err != nil {
+ return nil, err
+ }
+ return bli.pruneConfigList(cfgs), nil
+}
+
+func (bli *boundLocalInterface) GetConfigSetLocation(ctx context.Context, configSet string) (*url.URL, error) {
+ if err := access.Check(ctx, bli.a, cfgtypes.ConfigSet(configSet)); err != nil {
+ return nil, cfgclient.ErrNoConfig
+ }
+ return bli.Interface.GetConfigSetLocation(ctx, configSet)
+}
+
+func (bli *boundLocalInterface) pruneConfigList(cl []ccfg.Config) []ccfg.Config {
+ ptr := 0
+ for i := range cl {
+ if err := access.Check(bli, bli.a, cfgtypes.ConfigSet(cl[i].ConfigSet)); err == nil {
+ cl[ptr] = cl[i]
+ ptr++
+ }
+ }
+ return cl[:ptr]
+}
« no previous file with comments | « no previous file | luci_config/server/cfgclient/backend/testconfig/local_service_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698