go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/rpc/admin/upgrade_cls_test.go (about)

     1  // Copyright 2021 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 admin
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  	"time"
    21  
    22  	gerritpb "go.chromium.org/luci/common/proto/gerrit"
    23  	"go.chromium.org/luci/gae/service/datastore"
    24  	"go.chromium.org/luci/server/auth"
    25  	"go.chromium.org/luci/server/auth/authtest"
    26  	"go.chromium.org/luci/server/dsmapper"
    27  	"go.chromium.org/luci/server/dsmapper/dsmapperpb"
    28  	"go.chromium.org/luci/server/tq/tqtesting"
    29  
    30  	"go.chromium.org/luci/cv/internal/changelist"
    31  	"go.chromium.org/luci/cv/internal/common"
    32  	"go.chromium.org/luci/cv/internal/cvtesting"
    33  	gf "go.chromium.org/luci/cv/internal/gerrit/gerritfake"
    34  	adminpb "go.chromium.org/luci/cv/internal/rpc/admin/api"
    35  	"go.chromium.org/luci/cv/internal/run"
    36  
    37  	. "github.com/smartystreets/goconvey/convey"
    38  )
    39  
    40  func TestUpgradeCLs(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	Convey("Upgrade all RunCLs to not contain CL description", t, func() {
    44  		ct := cvtesting.Test{}
    45  		ctx, cancel := ct.SetUp(t)
    46  		defer cancel()
    47  
    48  		mkCL := func(id common.CLID, ci *gerritpb.ChangeInfo) *run.RunCL {
    49  			cl := &run.RunCL{
    50  				ID:  id,
    51  				Run: datastore.MakeKey(ctx, common.RunKind, fmt.Sprintf("prj/%d", id)),
    52  				Detail: &changelist.Snapshot{
    53  					Kind: &changelist.Snapshot_Gerrit{Gerrit: &changelist.Gerrit{
    54  						Info: ci,
    55  					}},
    56  				},
    57  			}
    58  			So(datastore.Put(ctx, cl), ShouldBeNil)
    59  			return cl
    60  		}
    61  
    62  		clDesc := func(cl *run.RunCL, patchset int32) string {
    63  			ci := cl.Detail.GetGerrit().GetInfo()
    64  			for _, revInfo := range ci.GetRevisions() {
    65  				if revInfo.GetNumber() == patchset {
    66  					return revInfo.GetCommit().GetMessage()
    67  				}
    68  			}
    69  			return ""
    70  		}
    71  
    72  		cl1 := mkCL(1, gf.CI(1, gf.PS(1), gf.Desc("First")))
    73  		cl2 := mkCL(2, gf.CI(2, gf.PS(1), gf.Desc("PS#1 blah"), gf.PS(2), gf.Desc("PS#2 foo")))
    74  
    75  		// Check test setup.
    76  		So(clDesc(cl1, 1), ShouldResemble, "First")
    77  		So(clDesc(cl2, 1), ShouldResemble, "PS#1 blah")
    78  		So(clDesc(cl2, 2), ShouldResemble, "PS#2 foo")
    79  
    80  		verify := func() {
    81  			So(datastore.Get(ctx, cl1, cl2), ShouldBeNil)
    82  
    83  			So(clDesc(cl1, 1), ShouldBeEmpty)
    84  
    85  			So(cl2.Detail.GetGerrit().GetInfo().GetRevisions(), ShouldHaveLength, 2)
    86  			So(clDesc(cl2, 1), ShouldBeEmpty)
    87  			So(clDesc(cl2, 2), ShouldBeEmpty)
    88  		}
    89  
    90  		// Run the migration.
    91  		ct.Clock.Add(time.Minute)
    92  		ctrl := &dsmapper.Controller{}
    93  		ctrl.Install(ct.TQDispatcher)
    94  		a := New(ct.TQDispatcher, ctrl, nil, nil, nil)
    95  		ctx = auth.WithState(ctx, &authtest.FakeState{
    96  			Identity:       "user:admin@example.com",
    97  			IdentityGroups: []string{allowGroup},
    98  		})
    99  		jobID, err := a.DSMLaunchJob(ctx, &adminpb.DSMLaunchJobRequest{Name: "runcl-description"})
   100  		So(err, ShouldBeNil)
   101  		ct.TQ.Run(ctx, tqtesting.StopWhenDrained())
   102  		jobInfo, err := a.DSMGetJob(ctx, jobID)
   103  		So(err, ShouldBeNil)
   104  		So(jobInfo.GetInfo().GetState(), ShouldEqual, dsmapperpb.State_SUCCESS)
   105  
   106  		verify()
   107  	})
   108  }