go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/config/validate_test.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 config 16 17 import ( 18 "context" 19 "os" 20 "testing" 21 22 "google.golang.org/protobuf/encoding/prototext" 23 24 "go.chromium.org/luci/config/validation" 25 26 configpb "go.chromium.org/luci/resultdb/proto/config" 27 28 . "github.com/smartystreets/goconvey/convey" 29 . "go.chromium.org/luci/common/testing/assertions" 30 ) 31 32 func TestProjectConfigValidator(t *testing.T) { 33 t.Parallel() 34 35 validate := func(cfg *configpb.ProjectConfig) error { 36 c := validation.Context{Context: context.Background()} 37 validateProjectConfig(&c, cfg) 38 return c.Finalize() 39 } 40 41 Convey("config template is valid", t, func() { 42 content, err := os.ReadFile( 43 "../../configs/projects/chromeos/luci-resultdb-dev-template.cfg", 44 ) 45 So(err, ShouldBeNil) 46 cfg := &configpb.ProjectConfig{} 47 So(prototext.Unmarshal(content, cfg), ShouldBeNil) 48 So(validate(cfg), ShouldBeNil) 49 }) 50 51 Convey("valid config is valid", t, func() { 52 cfg := CreatePlaceholderProjectConfig() 53 So(validate(cfg), ShouldBeNil) 54 }) 55 56 Convey("GCS allow list", t, func() { 57 cfg := CreatePlaceholderProjectConfig() 58 So(cfg.GcsAllowList, ShouldNotBeNil) 59 So(len(cfg.GcsAllowList), ShouldEqual, 1) 60 So(len(cfg.GcsAllowList[0].Buckets), ShouldEqual, 1) 61 gcsAllowList := cfg.GcsAllowList[0] 62 63 Convey("users", func() { 64 Convey("must be specified", func() { 65 gcsAllowList.Users = []string{} 66 So(validate(cfg), ShouldNotBeNil) 67 }) 68 Convey("must be non-empty", func() { 69 gcsAllowList.Users = []string{""} 70 So(validate(cfg), ShouldNotBeNil) 71 }) 72 Convey("invalid", func() { 73 gcsAllowList.Users = []string{"a:b"} 74 So(validate(cfg), ShouldNotBeNil) 75 }) 76 Convey("valid", func() { 77 gcsAllowList.Users = []string{"user:test@test.com"} 78 So(validate(cfg), ShouldBeNil) 79 }) 80 Convey("multiple", func() { 81 gcsAllowList.Users = []string{"user:test@test.com", "user:test2@test.com"} 82 So(validate(cfg), ShouldBeNil) 83 }) 84 }) 85 86 Convey("GCS buckets", func() { 87 Convey("bucket", func() { 88 Convey("must be specified", func() { 89 gcsAllowList.Buckets[0] = "" 90 So(validate(cfg), ShouldErrLike, "empty bucket is not allowed") 91 }) 92 Convey("invalid", func() { 93 gcsAllowList.Buckets[0] = "b" 94 So(validate(cfg), ShouldErrLike, `invalid bucket: "b"`) 95 }) 96 Convey("valid", func() { 97 gcsAllowList.Buckets[0] = "bucket" 98 So(validate(cfg), ShouldBeNil) 99 }) 100 }) 101 }) 102 }) 103 } 104 105 func TestServiceConfigValidator(t *testing.T) { 106 t.Parallel() 107 108 validate := func(cfg *configpb.Config) error { 109 c := validation.Context{Context: context.Background()} 110 validateServiceConfig(&c, cfg) 111 return c.Finalize() 112 } 113 114 Convey("config template is valid", t, func() { 115 content, err := os.ReadFile( 116 "../../configs/service/template.cfg", 117 ) 118 So(err, ShouldBeNil) 119 cfg := &configpb.Config{} 120 So(prototext.Unmarshal(content, cfg), ShouldBeNil) 121 So(validate(cfg), ShouldBeNil) 122 }) 123 124 Convey("valid config is valid", t, func() { 125 cfg := CreatePlaceHolderServiceConfig() 126 So(validate(cfg), ShouldBeNil) 127 }) 128 129 Convey("bq artifact export config", t, func() { 130 cfg := CreatePlaceHolderServiceConfig() 131 Convey("is nil", func() { 132 cfg.BqArtifactExportConfig = nil 133 So(validate(cfg), ShouldNotBeNil) 134 }) 135 136 Convey("percentage smaller than 0", func() { 137 cfg.BqArtifactExportConfig = &configpb.BqArtifactExportConfig{ 138 ExportPercent: -1, 139 } 140 So(validate(cfg), ShouldNotBeNil) 141 }) 142 143 Convey("percentage bigger than 100", func() { 144 cfg.BqArtifactExportConfig = &configpb.BqArtifactExportConfig{ 145 ExportPercent: 101, 146 } 147 So(validate(cfg), ShouldNotBeNil) 148 }) 149 }) 150 }