go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/configs/validation/rules_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  	"context"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/config/validation"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  func TestValidationRules(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	Convey("Validation Rules", t, func() {
    30  		r := validation.NewRuleSet()
    31  		r.Vars.Register("appid", func(context.Context) (string, error) { return "luci-change-verifier", nil })
    32  
    33  		addRules(r)
    34  
    35  		patterns, err := r.ConfigPatterns(context.Background())
    36  		So(err, ShouldBeNil)
    37  		So(len(patterns), ShouldEqual, 2)
    38  		Convey("project-scope cq.cfg", func() {
    39  			So(patterns[0].ConfigSet.Match("projects/xyz"), ShouldBeTrue)
    40  			So(patterns[0].ConfigSet.Match("projects/xyz/refs/heads/master"), ShouldBeFalse)
    41  			So(patterns[0].Path.Match("commit-queue.cfg"), ShouldBeTrue)
    42  		})
    43  		Convey("service-scope listener-settings.cfg", func() {
    44  			So(patterns[1].ConfigSet.Match("services/luci-change-verifier"), ShouldBeTrue)
    45  			So(patterns[1].ConfigSet.Match("projects/xyz/refs/heads/master"), ShouldBeFalse)
    46  			So(patterns[1].Path.Match("listener-settings.cfg"), ShouldBeTrue)
    47  		})
    48  	})
    49  }
    50  
    51  func mustHaveOnlySeverity(err error, severity validation.Severity) error {
    52  	So(err, ShouldNotBeNil)
    53  	for _, e := range err.(*validation.Error).Errors {
    54  		s, ok := validation.SeverityTag.In(e)
    55  		So(ok, ShouldBeTrue)
    56  		So(s, ShouldEqual, severity)
    57  	}
    58  	return err
    59  }
    60  
    61  func mustWarn(err error) error {
    62  	return mustHaveOnlySeverity(err, validation.Warning)
    63  }
    64  
    65  func mustError(err error) error {
    66  	return mustHaveOnlySeverity(err, validation.Blocking)
    67  }