go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/configs/validation/service_test.go (about)

     1  // Copyright 2018 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 validation
    16  
    17  import (
    18  	"testing"
    19  
    20  	"go.chromium.org/luci/config/validation"
    21  	cfgpb "go.chromium.org/luci/cv/api/config/v2"
    22  	"go.chromium.org/luci/cv/internal/configs/prjcfg"
    23  	"go.chromium.org/luci/cv/internal/cvtesting"
    24  	"go.chromium.org/luci/gae/service/datastore"
    25  
    26  	. "github.com/smartystreets/goconvey/convey"
    27  	. "go.chromium.org/luci/common/testing/assertions"
    28  )
    29  
    30  func TestListenerConfigValidation(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	Convey("Validate Config", t, func() {
    34  		ct := cvtesting.Test{}
    35  		ctx, cancel := ct.SetUp(t)
    36  		defer cancel()
    37  
    38  		vctx := &validation.Context{Context: ctx}
    39  		configSet := "services/luci-change-verifier"
    40  		path := "listener-settings.cfg"
    41  
    42  		Convey("Loading bad proto", func() {
    43  			content := []byte(` bad: "config" `)
    44  			So(validateListenerSettings(vctx, configSet, path, content), ShouldBeNil)
    45  			So(vctx.Finalize().Error(), ShouldContainSubstring, "unknown field")
    46  		})
    47  
    48  		Convey("OK", func() {
    49  			cfg := []byte(`
    50  				gerrit_subscriptions {
    51  					host: "chromium-review.googlesource.com"
    52  					receive_settings: {
    53  						num_goroutines: 100
    54  						max_outstanding_messages: 5000
    55  					}
    56  					message_format: JSON
    57  				}
    58  				gerrit_subscriptions {
    59  					host: "pigweed-review.googlesource.com"
    60  					subscription_id: "pigweed_gerrit"
    61  					receive_settings: {
    62  						num_goroutines: 100
    63  						max_outstanding_messages: 5000
    64  					}
    65  					message_format: PROTO_BINARY
    66  				}
    67  			`)
    68  			Convey("fully loaded", func() {
    69  				So(validateListenerSettings(vctx, configSet, path, cfg), ShouldBeNil)
    70  				So(vctx.Finalize(), ShouldBeNil)
    71  			})
    72  			Convey("empty", func() {
    73  				So(validateListenerSettings(vctx, configSet, path, []byte{}), ShouldBeNil)
    74  				So(vctx.Finalize(), ShouldBeNil)
    75  			})
    76  		})
    77  
    78  		Convey("Fail", func() {
    79  			Convey("multiple subscriptions for the same host", func() {
    80  				cfg := []byte(`
    81  					gerrit_subscriptions {
    82  						host: "example.org"
    83  						subscription_id: "sub"
    84  						message_format: JSON
    85  					}
    86  					gerrit_subscriptions {
    87  						host: "example.org"
    88  						subscription_id: "sub2"
    89  						message_format: JSON
    90  					}
    91  				`)
    92  				So(validateListenerSettings(vctx, configSet, path, cfg), ShouldBeNil)
    93  				So(vctx.Finalize(), ShouldErrLike, `already exists for host "example.org"`)
    94  			})
    95  
    96  			Convey("invalid disabled_project_regexps", func() {
    97  				cfg := []byte(`
    98  					disabled_project_regexps: "(123"
    99  				`)
   100  				So(validateListenerSettings(vctx, configSet, path, cfg), ShouldBeNil)
   101  				So(vctx.Finalize(), ShouldErrLike, "missing closing")
   102  			})
   103  
   104  			Convey("invalid message_format", func() {
   105  				cfg := []byte(`
   106  					gerrit_subscriptions {
   107  						host: "example.org"
   108  					}
   109  				`)
   110  				So(validateListenerSettings(vctx, configSet, path, cfg), ShouldBeNil)
   111  				So(vctx.Finalize().Error(), ShouldContainSubstring,
   112  					"message_format): must be specified")
   113  			})
   114  		})
   115  
   116  		Convey("watched repo", func() {
   117  			pc := &prjcfg.ProjectConfig{
   118  				Project:          "chromium",
   119  				ConfigGroupNames: []string{"main"},
   120  				Hash:             "sha256:deadbeef",
   121  				Enabled:          true,
   122  			}
   123  			cpb := &cfgpb.ConfigGroup{
   124  				Name: "main",
   125  				Gerrit: []*cfgpb.ConfigGroup_Gerrit{
   126  					{
   127  						Url: "https://chromium-review.googlesoure.com/foo",
   128  						Projects: []*cfgpb.ConfigGroup_Gerrit_Project{
   129  							{Name: "cr/src1"},
   130  							{Name: "cr/src2"},
   131  						},
   132  					},
   133  				},
   134  			}
   135  			cg := &prjcfg.ConfigGroup{
   136  				Project: prjcfg.ProjectConfigKey(ctx, pc.Project),
   137  				ID:      prjcfg.MakeConfigGroupID(pc.Hash, "main"),
   138  				Content: cpb,
   139  			}
   140  			So(datastore.Put(ctx, pc, cg), ShouldBeNil)
   141  
   142  			Convey("missing gerrit_subscriptions", func() {
   143  				cfg := []byte(`
   144  					gerrit_subscriptions {
   145  						host: "pigweed-review.googlesource.com"
   146  						subscription_id: "pigweed_gerrit"
   147  						receive_settings: {
   148  							num_goroutines: 100
   149  							max_outstanding_messages: 5000
   150  						}
   151  						message_format: PROTO_BINARY
   152  					}
   153  				`)
   154  
   155  				Convey("fails", func() {
   156  					So(validateListenerSettings(vctx, configSet, path, cfg), ShouldBeNil)
   157  					So(vctx.Finalize(), ShouldErrLike, "there is no gerrit_subscriptions")
   158  				})
   159  
   160  				Convey("succeeeds if disabled in listener settings", func() {
   161  					cfg := append(cfg, []byte(`
   162  						disabled_project_regexps: "chromium"
   163  					`)...)
   164  					So(validateListenerSettings(vctx, configSet, path, cfg), ShouldBeNil)
   165  					So(vctx.Finalize(), ShouldBeNil)
   166  				})
   167  			})
   168  		})
   169  	})
   170  }