go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/internal/config/gerrit_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  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  
    23  	configpb "go.chromium.org/luci/bisection/proto/config"
    24  	pb "go.chromium.org/luci/bisection/proto/v1"
    25  	"go.chromium.org/luci/bisection/util/testutil"
    26  
    27  	"go.chromium.org/luci/common/clock"
    28  	"go.chromium.org/luci/common/clock/testclock"
    29  	"go.chromium.org/luci/gae/impl/memory"
    30  )
    31  
    32  func TestCanCreateRevert(t *testing.T) {
    33  	ctx := memory.Use(context.Background())
    34  	testutil.UpdateIndices(ctx)
    35  
    36  	// Set test clock
    37  	cl := testclock.New(testclock.TestTimeUTC)
    38  	ctx = clock.Set(ctx, cl)
    39  
    40  	Convey("Disabling all Gerrit actions should override CanCreateRevert to false", t, func() {
    41  		gerritCfg := &configpb.GerritConfig{
    42  			ActionsEnabled: false,
    43  			CreateRevertSettings: &configpb.GerritConfig_RevertActionSettings{
    44  				Enabled:    true,
    45  				DailyLimit: 10,
    46  			},
    47  			SubmitRevertSettings: &configpb.GerritConfig_RevertActionSettings{
    48  				Enabled:    true,
    49  				DailyLimit: 4,
    50  			},
    51  			MaxRevertibleCulpritAge: 21600, // 6 hours
    52  		}
    53  		canCreate, reason, err := CanCreateRevert(ctx, gerritCfg, pb.AnalysisType_COMPILE_FAILURE_ANALYSIS)
    54  		So(err, ShouldBeNil)
    55  		So(canCreate, ShouldEqual, false)
    56  		So(reason, ShouldEqual, "all Gerrit actions are disabled")
    57  	})
    58  
    59  	Convey("CanCreateRevert should be false when create is disabled", t, func() {
    60  		gerritCfg := &configpb.GerritConfig{
    61  			ActionsEnabled: true,
    62  			CreateRevertSettings: &configpb.GerritConfig_RevertActionSettings{
    63  				Enabled:    false,
    64  				DailyLimit: 10,
    65  			},
    66  			SubmitRevertSettings: &configpb.GerritConfig_RevertActionSettings{
    67  				Enabled:    true,
    68  				DailyLimit: 4,
    69  			},
    70  			MaxRevertibleCulpritAge: 21600, // 6 hours
    71  		}
    72  		canCreate, reason, err := CanCreateRevert(ctx, gerritCfg, pb.AnalysisType_COMPILE_FAILURE_ANALYSIS)
    73  		So(err, ShouldBeNil)
    74  		So(canCreate, ShouldEqual, false)
    75  		So(reason, ShouldEqual, "LUCI Bisection's revert creation has been disabled")
    76  	})
    77  
    78  	Convey("CanCreateRevert should be true", t, func() {
    79  		gerritCfg := &configpb.GerritConfig{
    80  			ActionsEnabled: true,
    81  			CreateRevertSettings: &configpb.GerritConfig_RevertActionSettings{
    82  				Enabled:    true,
    83  				DailyLimit: 10,
    84  			},
    85  			SubmitRevertSettings: &configpb.GerritConfig_RevertActionSettings{
    86  				Enabled:    true,
    87  				DailyLimit: 4,
    88  			},
    89  			MaxRevertibleCulpritAge: 21600, // 6 hours
    90  		}
    91  		canCreate, reason, err := CanCreateRevert(ctx, gerritCfg, pb.AnalysisType_COMPILE_FAILURE_ANALYSIS)
    92  		So(err, ShouldBeNil)
    93  		So(canCreate, ShouldEqual, true)
    94  		So(reason, ShouldEqual, "")
    95  	})
    96  
    97  	Convey("CanCreateRevert should be false when daily limit has been reached", t, func() {
    98  		gerritCfg := &configpb.GerritConfig{
    99  			ActionsEnabled: true,
   100  			CreateRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   101  				Enabled:    true,
   102  				DailyLimit: 0,
   103  			},
   104  			SubmitRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   105  				Enabled:    true,
   106  				DailyLimit: 4,
   107  			},
   108  			MaxRevertibleCulpritAge: 21600, // 6 hours
   109  		}
   110  		canCreate, reason, err := CanCreateRevert(ctx, gerritCfg, pb.AnalysisType_COMPILE_FAILURE_ANALYSIS)
   111  		So(err, ShouldBeNil)
   112  		So(canCreate, ShouldEqual, false)
   113  		So(reason, ShouldEqual, "LUCI Bisection's daily limit for revert creation"+
   114  			" (0) has been reached; 0 reverts have already been created")
   115  	})
   116  }
   117  
   118  func TestCanSubmitRevert(t *testing.T) {
   119  	ctx := memory.Use(context.Background())
   120  	testutil.UpdateIndices(ctx)
   121  
   122  	// Set test clock
   123  	cl := testclock.New(testclock.TestTimeUTC)
   124  	ctx = clock.Set(ctx, cl)
   125  
   126  	Convey("Disabling all Gerrit actions should override CanSubmitRevert to false", t, func() {
   127  		gerritCfg := &configpb.GerritConfig{
   128  			ActionsEnabled: false,
   129  			CreateRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   130  				Enabled:    true,
   131  				DailyLimit: 10,
   132  			},
   133  			SubmitRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   134  				Enabled:    true,
   135  				DailyLimit: 4,
   136  			},
   137  			MaxRevertibleCulpritAge: 21600, // 6 hours
   138  		}
   139  		canSubmit, reason, err := CanSubmitRevert(ctx, gerritCfg)
   140  		So(err, ShouldBeNil)
   141  		So(canSubmit, ShouldEqual, false)
   142  		So(reason, ShouldEqual, "all Gerrit actions are disabled")
   143  	})
   144  
   145  	Convey("CanSubmitRevert should be false when submit is disabled", t, func() {
   146  		gerritCfg := &configpb.GerritConfig{
   147  			ActionsEnabled: true,
   148  			CreateRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   149  				Enabled:    true,
   150  				DailyLimit: 10,
   151  			},
   152  			SubmitRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   153  				Enabled:    false,
   154  				DailyLimit: 4,
   155  			},
   156  			MaxRevertibleCulpritAge: 21600, // 6 hours
   157  		}
   158  		canSubmit, reason, err := CanSubmitRevert(ctx, gerritCfg)
   159  		So(err, ShouldBeNil)
   160  		So(canSubmit, ShouldEqual, false)
   161  		So(reason, ShouldEqual, "LUCI Bisection's revert submission has been disabled")
   162  	})
   163  
   164  	Convey("CanSubmitRevert should be true", t, func() {
   165  		gerritCfg := &configpb.GerritConfig{
   166  			ActionsEnabled: true,
   167  			CreateRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   168  				Enabled:    true,
   169  				DailyLimit: 10,
   170  			},
   171  			SubmitRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   172  				Enabled:    true,
   173  				DailyLimit: 4,
   174  			},
   175  			MaxRevertibleCulpritAge: 21600, // 6 hours
   176  		}
   177  		canSubmit, reason, err := CanSubmitRevert(ctx, gerritCfg)
   178  		So(err, ShouldBeNil)
   179  		So(canSubmit, ShouldEqual, true)
   180  		So(reason, ShouldEqual, "")
   181  	})
   182  
   183  	Convey("CanSubmitRevert should be false when daily limit has been reached", t, func() {
   184  		gerritCfg := &configpb.GerritConfig{
   185  			ActionsEnabled: true,
   186  			CreateRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   187  				Enabled:    true,
   188  				DailyLimit: 10,
   189  			},
   190  			SubmitRevertSettings: &configpb.GerritConfig_RevertActionSettings{
   191  				Enabled:    true,
   192  				DailyLimit: 0,
   193  			},
   194  			MaxRevertibleCulpritAge: 21600, // 6 hours
   195  		}
   196  		canSubmit, reason, err := CanSubmitRevert(ctx, gerritCfg)
   197  		So(err, ShouldBeNil)
   198  		So(canSubmit, ShouldEqual, false)
   199  		So(reason, ShouldEqual, "LUCI Bisection's daily limit for revert submission"+
   200  			" (0) has been reached; 0 reverts have already been submitted")
   201  	})
   202  }