go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/engine/policy/validation_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 policy
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"google.golang.org/protobuf/types/known/durationpb"
    22  
    23  	"go.chromium.org/luci/config/validation"
    24  
    25  	"go.chromium.org/luci/scheduler/appengine/messages"
    26  
    27  	. "github.com/smartystreets/goconvey/convey"
    28  	. "go.chromium.org/luci/common/testing/assertions"
    29  )
    30  
    31  func TestValidation(t *testing.T) {
    32  	t.Parallel()
    33  
    34  	run := func(p messages.TriggeringPolicy) error {
    35  		ctx := validation.Context{}
    36  		ValidateDefinition(&ctx, &p)
    37  		return ctx.Finalize()
    38  	}
    39  
    40  	Convey("Works", t, func() {
    41  		So(run(messages.TriggeringPolicy{}), ShouldBeNil)
    42  		So(run(messages.TriggeringPolicy{Kind: 123}),
    43  			ShouldErrLike, "unrecognized policy kind 123")
    44  		So(run(messages.TriggeringPolicy{MaxConcurrentInvocations: -1}),
    45  			ShouldErrLike, "max_concurrent_invocations should be positive, got -1")
    46  		So(run(messages.TriggeringPolicy{MaxBatchSize: -1}),
    47  			ShouldErrLike, "max_batch_size should be positive, got -1")
    48  		So(run(messages.TriggeringPolicy{
    49  			Kind: messages.TriggeringPolicy_GREEDY_BATCHING}), ShouldBeNil)
    50  		So(run(messages.TriggeringPolicy{
    51  			Kind: messages.TriggeringPolicy_NEWEST_FIRST}), ShouldBeNil)
    52  		So(run(messages.TriggeringPolicy{
    53  			Kind: messages.TriggeringPolicy_LOGARITHMIC_BATCHING, LogBase: 0.5}),
    54  			ShouldErrLike, "log_base should be larger or equal 1.0001, got 0.5")
    55  		So(run(messages.TriggeringPolicy{
    56  			Kind:           messages.TriggeringPolicy_NEWEST_FIRST,
    57  			PendingTimeout: durationpb.New(-time.Hour)}),
    58  			ShouldErrLike, "pending_timeout should be positive, got -1h")
    59  		So(run(messages.TriggeringPolicy{
    60  			PendingTimeout: durationpb.New(time.Hour)}),
    61  			ShouldErrLike, "pending_timeout is non-zero with non-NEWEST_FIRST policy")
    62  	})
    63  }