go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/testutil/projectconfig.go (about) 1 // Copyright 2022 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (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 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testutil 16 17 import ( 18 "context" 19 "fmt" 20 21 "google.golang.org/protobuf/encoding/prototext" 22 23 "go.chromium.org/luci/common/clock/testclock" 24 "go.chromium.org/luci/config" 25 "go.chromium.org/luci/config/cfgclient" 26 cfgmem "go.chromium.org/luci/config/impl/memory" 27 "go.chromium.org/luci/gae/impl/memory" 28 "go.chromium.org/luci/gae/service/datastore" 29 rdbcfg "go.chromium.org/luci/resultdb/internal/config" 30 "go.chromium.org/luci/server/caching" 31 32 . "github.com/smartystreets/goconvey/convey" 33 ) 34 35 var textPBMultiline = prototext.MarshalOptions{ 36 Multiline: true, 37 } 38 39 // TestProjectConfigContext returns a context to be used in project config related tests. 40 func TestProjectConfigContext(ctx context.Context, project, user, bucket string) context.Context { 41 ctx, _ = testclock.UseTime(ctx, testclock.TestRecentTimeUTC) 42 ctx = memory.Use(ctx) 43 ctx = caching.WithEmptyProcessCache(ctx) 44 ctx = SetGCSAllowedBuckets(ctx, project, user, bucket) 45 return ctx 46 } 47 48 // SetGCSAllowedBuckets overrides the only existing project-config 49 // GcsAllowlist entry to match the rule defined by project, realm, bucket 50 // and prefix. 51 func SetGCSAllowedBuckets(ctx context.Context, project, user, bucket string) context.Context { 52 testProject := rdbcfg.CreatePlaceholderProjectConfig() 53 So(len(testProject.GcsAllowList), ShouldEqual, 1) 54 testProject.GcsAllowList[0].Users = []string{user} 55 So(len(testProject.GcsAllowList[0].Buckets), ShouldEqual, 1) 56 testProject.GcsAllowList[0].Buckets[0] = bucket 57 58 cfgSet := config.Set(fmt.Sprintf("projects/%s", project)) 59 configs := map[config.Set]cfgmem.Files{ 60 cfgSet: {"${appid}.cfg": textPBMultiline.Format(testProject)}, 61 } 62 63 ctx = cfgclient.Use(ctx, cfgmem.New(configs)) 64 err := rdbcfg.UpdateProjects(ctx) 65 So(err, ShouldBeNil) 66 datastore.GetTestable(ctx).CatchupIndexes() 67 68 return ctx 69 }