| OLD | NEW |
| (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 testconfig |
| 6 |
| 7 import ( |
| 8 "net/url" |
| 9 "testing" |
| 10 |
| 11 "github.com/luci/luci-go/common/config/impl/memory" |
| 12 configPB "github.com/luci/luci-go/common/proto/config" |
| 13 "github.com/luci/luci-go/luci_config/common/cfgtypes" |
| 14 "github.com/luci/luci-go/luci_config/server/cfgclient" |
| 15 "github.com/luci/luci-go/luci_config/server/cfgclient/backend" |
| 16 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/client" |
| 17 "github.com/luci/luci-go/server/auth" |
| 18 "github.com/luci/luci-go/server/auth/authtest" |
| 19 |
| 20 "github.com/golang/protobuf/proto" |
| 21 "golang.org/x/net/context" |
| 22 |
| 23 . "github.com/smartystreets/goconvey/convey" |
| 24 ) |
| 25 |
| 26 func tpb(msg proto.Message) string { return proto.MarshalTextString(msg) } |
| 27 |
| 28 func accessCfg(access ...string) string { |
| 29 return tpb(&configPB.ProjectCfg{ |
| 30 Access: access, |
| 31 }) |
| 32 } |
| 33 |
| 34 func TestLocalService(t *testing.T) { |
| 35 t.Parallel() |
| 36 |
| 37 Convey(`Testing the local service`, t, func() { |
| 38 c := context.Background() |
| 39 |
| 40 fs := authtest.FakeState{ |
| 41 Identity: "user:foo@bar.baz", |
| 42 IdentityGroups: []string{"all", "special"}, |
| 43 } |
| 44 c = auth.WithState(c, &fs) |
| 45 |
| 46 configs := map[string]memory.ConfigSet{ |
| 47 "projects/foo": { |
| 48 "path.cfg": "foo", |
| 49 "project.cfg": accessCfg("group:all"), |
| 50 }, |
| 51 "projects/exclusive": { |
| 52 "path.cfg": "exclusive", |
| 53 "project.cfg": accessCfg("group:special"), |
| 54 }, |
| 55 "projects/exclusive/refs/heads/master": { |
| 56 "path.cfg": "exclusive master", |
| 57 }, |
| 58 "projects/nouser": { |
| 59 "path.cfg": "nouser", |
| 60 "project.cfg": accessCfg("group:impossible"), |
| 61 }, |
| 62 "projects/nouser/refs/heads/master": { |
| 63 "path.cfg": "nouser master", |
| 64 }, |
| 65 "services/baz": { |
| 66 "path.cfg": "service only", |
| 67 }, |
| 68 } |
| 69 mbase := memory.New(configs) |
| 70 c = backend.WithBackend(c, &client.Backend{&Provider{ |
| 71 Base: mbase, |
| 72 }}) |
| 73 |
| 74 metaFor := func(configSet, path string) *cfgclient.Meta { |
| 75 cfg, err := mbase.GetConfig(c, configSet, path, false) |
| 76 if err != nil { |
| 77 panic(err) |
| 78 } |
| 79 return &cfgclient.Meta{ |
| 80 ConfigSet: cfgtypes.ConfigSet(cfg.ConfigSet), |
| 81 Path: cfg.Path, |
| 82 ContentHash: cfg.ContentHash, |
| 83 Revision: cfg.Revision, |
| 84 } |
| 85 } |
| 86 |
| 87 Convey(`Can get the service URL`, func() { |
| 88 So(cfgclient.ServiceURL(c), ShouldResemble, url.URL{Sche
me: "memory"}) |
| 89 }) |
| 90 |
| 91 Convey(`Can get a single config`, func() { |
| 92 var ( |
| 93 val string |
| 94 meta cfgclient.Meta |
| 95 ) |
| 96 |
| 97 Convey(`AsService`, func() { |
| 98 So(cfgclient.Get(c, cfgclient.AsService, "projec
ts/foo", "path.cfg", cfgclient.String(&val), &meta), ShouldBeNil) |
| 99 So(val, ShouldEqual, "foo") |
| 100 So(&meta, ShouldResemble, metaFor("projects/foo"
, "path.cfg")) |
| 101 |
| 102 So(cfgclient.Get(c, cfgclient.AsService, "projec
ts/exclusive", "path.cfg", cfgclient.String(&val), nil), ShouldBeNil) |
| 103 So(val, ShouldEqual, "exclusive") |
| 104 |
| 105 So(cfgclient.Get(c, cfgclient.AsService, "servic
es/baz", "path.cfg", cfgclient.String(&val), nil), ShouldBeNil) |
| 106 So(val, ShouldEqual, "service only") |
| 107 }) |
| 108 |
| 109 Convey(`AsUser`, func() { |
| 110 So(cfgclient.Get(c, cfgclient.AsUser, "projects/
foo", "path.cfg", cfgclient.String(&val), nil), ShouldBeNil) |
| 111 So(val, ShouldEqual, "foo") |
| 112 |
| 113 So(cfgclient.Get(c, cfgclient.AsUser, "projects/
exclusive", "path.cfg", cfgclient.String(&val), nil), ShouldBeNil) |
| 114 So(val, ShouldEqual, "exclusive") |
| 115 |
| 116 So(cfgclient.Get(c, cfgclient.AsUser, "services/
baz", "path.cfg", cfgclient.String(&val), nil), |
| 117 ShouldEqual, cfgclient.ErrNoConfig) |
| 118 }) |
| 119 |
| 120 Convey(`AsAnonymous`, func() { |
| 121 fs.IdentityGroups = []string{"all"} |
| 122 |
| 123 So(cfgclient.Get(c, cfgclient.AsAnonymous, "proj
ects/foo", "path.cfg", cfgclient.String(&val), nil), ShouldBeNil) |
| 124 So(val, ShouldEqual, "foo") |
| 125 |
| 126 So(cfgclient.Get(c, cfgclient.AsAnonymous, "proj
ects/exclusive", "path.cfg", cfgclient.String(&val), nil), |
| 127 ShouldEqual, cfgclient.ErrNoConfig) |
| 128 So(cfgclient.Get(c, cfgclient.AsAnonymous, "serv
ices/baz", "path.cfg", cfgclient.String(&val), nil), |
| 129 ShouldEqual, cfgclient.ErrNoConfig) |
| 130 }) |
| 131 }) |
| 132 |
| 133 Convey(`Can get multiple configs`, func() { |
| 134 var vals []string |
| 135 var meta []*cfgclient.Meta |
| 136 |
| 137 Convey(`AsService`, func() { |
| 138 So(cfgclient.Projects(c, cfgclient.AsService, "p
ath.cfg", cfgclient.StringSlice(&vals), &meta), |
| 139 ShouldBeNil) |
| 140 So(vals, ShouldResemble, []string{"exclusive", "
foo", "nouser"}) |
| 141 So(meta, ShouldResemble, []*cfgclient.Meta{ |
| 142 metaFor("projects/exclusive", "path.cfg"
), |
| 143 metaFor("projects/foo", "path.cfg"), |
| 144 metaFor("projects/nouser", "path.cfg"), |
| 145 }) |
| 146 |
| 147 So(cfgclient.Refs(c, cfgclient.AsService, "path.
cfg", cfgclient.StringSlice(&vals), &meta), |
| 148 ShouldBeNil) |
| 149 So(vals, ShouldResemble, []string{"exclusive mas
ter", "nouser master"}) |
| 150 So(meta, ShouldResemble, []*cfgclient.Meta{ |
| 151 metaFor("projects/exclusive/refs/heads/m
aster", "path.cfg"), |
| 152 metaFor("projects/nouser/refs/heads/mast
er", "path.cfg"), |
| 153 }) |
| 154 }) |
| 155 |
| 156 Convey(`AsUser`, func() { |
| 157 So(cfgclient.Projects(c, cfgclient.AsUser, "path
.cfg", cfgclient.StringSlice(&vals), &meta), |
| 158 ShouldBeNil) |
| 159 So(vals, ShouldResemble, []string{"exclusive", "
foo"}) |
| 160 So(meta, ShouldResemble, []*cfgclient.Meta{ |
| 161 metaFor("projects/exclusive", "path.cfg"
), |
| 162 metaFor("projects/foo", "path.cfg"), |
| 163 }) |
| 164 |
| 165 So(cfgclient.Refs(c, cfgclient.AsUser, "path.cfg
", cfgclient.StringSlice(&vals), &meta), |
| 166 ShouldBeNil) |
| 167 So(vals, ShouldResemble, []string{"exclusive mas
ter"}) |
| 168 So(meta, ShouldResemble, []*cfgclient.Meta{ |
| 169 metaFor("projects/exclusive/refs/heads/m
aster", "path.cfg"), |
| 170 }) |
| 171 }) |
| 172 |
| 173 Convey(`AsAnonymous`, func() { |
| 174 fs.IdentityGroups = []string{"all"} |
| 175 |
| 176 So(cfgclient.Projects(c, cfgclient.AsAnonymous,
"path.cfg", cfgclient.StringSlice(&vals), &meta), |
| 177 ShouldBeNil) |
| 178 So(vals, ShouldResemble, []string{"foo"}) |
| 179 So(meta, ShouldResemble, []*cfgclient.Meta{ |
| 180 metaFor("projects/foo", "path.cfg"), |
| 181 }) |
| 182 |
| 183 So(cfgclient.Refs(c, cfgclient.AsAnonymous, "pat
h.cfg", cfgclient.StringSlice(&vals), &meta), |
| 184 ShouldBeNil) |
| 185 So(vals, ShouldHaveLength, 0) |
| 186 So(meta, ShouldHaveLength, 0) |
| 187 }) |
| 188 }) |
| 189 |
| 190 Convey(`Can get config set URLs`, func() { |
| 191 Convey(`AsService`, func() { |
| 192 u, err := cfgclient.GetConfigSetURL(c, cfgclient
.AsService, "projects/foo") |
| 193 So(err, ShouldBeNil) |
| 194 So(u, ShouldResemble, url.URL{Scheme: "https", H
ost: "example.com", Path: "/fake-config/projects/foo"}) |
| 195 |
| 196 u, err = cfgclient.GetConfigSetURL(c, cfgclient.
AsService, "projects/exclusive") |
| 197 So(err, ShouldBeNil) |
| 198 So(u, ShouldResemble, url.URL{Scheme: "https", H
ost: "example.com", Path: "/fake-config/projects/exclusive"}) |
| 199 |
| 200 u, err = cfgclient.GetConfigSetURL(c, cfgclient.
AsService, "projects/nouser") |
| 201 So(err, ShouldBeNil) |
| 202 So(u, ShouldResemble, url.URL{Scheme: "https", H
ost: "example.com", Path: "/fake-config/projects/nouser"}) |
| 203 }) |
| 204 |
| 205 Convey(`AsUser`, func() { |
| 206 u, err := cfgclient.GetConfigSetURL(c, cfgclient
.AsUser, "projects/foo") |
| 207 So(err, ShouldBeNil) |
| 208 So(u, ShouldResemble, url.URL{Scheme: "https", H
ost: "example.com", Path: "/fake-config/projects/foo"}) |
| 209 |
| 210 u, err = cfgclient.GetConfigSetURL(c, cfgclient.
AsUser, "projects/exclusive") |
| 211 So(err, ShouldBeNil) |
| 212 So(u, ShouldResemble, url.URL{Scheme: "https", H
ost: "example.com", Path: "/fake-config/projects/exclusive"}) |
| 213 |
| 214 u, err = cfgclient.GetConfigSetURL(c, cfgclient.
AsUser, "projects/nouser") |
| 215 So(err, ShouldEqual, cfgclient.ErrNoConfig) |
| 216 }) |
| 217 |
| 218 Convey(`AsAnonymous`, func() { |
| 219 fs.IdentityGroups = []string{"all"} |
| 220 |
| 221 u, err := cfgclient.GetConfigSetURL(c, cfgclient
.AsAnonymous, "projects/foo") |
| 222 So(err, ShouldBeNil) |
| 223 So(u, ShouldResemble, url.URL{Scheme: "https", H
ost: "example.com", Path: "/fake-config/projects/foo"}) |
| 224 |
| 225 _, err = cfgclient.GetConfigSetURL(c, cfgclient.
AsAnonymous, "projects/exclusive") |
| 226 So(err, ShouldEqual, cfgclient.ErrNoConfig) |
| 227 |
| 228 _, err = cfgclient.GetConfigSetURL(c, cfgclient.
AsAnonymous, "projects/nouser") |
| 229 So(err, ShouldEqual, cfgclient.ErrNoConfig) |
| 230 }) |
| 231 }) |
| 232 }) |
| 233 } |
| OLD | NEW |