go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/gerrit/updater/fetcher_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 updater
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	gerritpb "go.chromium.org/luci/common/proto/gerrit"
    22  
    23  	"go.chromium.org/luci/cv/internal/changelist"
    24  	gf "go.chromium.org/luci/cv/internal/gerrit/gerritfake"
    25  
    26  	. "github.com/smartystreets/goconvey/convey"
    27  	. "go.chromium.org/luci/common/testing/assertions"
    28  )
    29  
    30  func TestRelatedChangeProcessing(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	Convey("setGitDeps works", t, func() {
    34  		ctx := context.Background()
    35  		f := fetcher{
    36  			change: 111,
    37  			host:   "host",
    38  			toUpdate: changelist.UpdateFields{
    39  				Snapshot: &changelist.Snapshot{Kind: &changelist.Snapshot_Gerrit{Gerrit: &changelist.Gerrit{}}},
    40  			},
    41  		}
    42  
    43  		Convey("No related changes", func() {
    44  			f.setGitDeps(ctx, nil)
    45  			So(f.toUpdate.Snapshot.GetGerrit().GetGitDeps(), ShouldBeNil)
    46  
    47  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{})
    48  			So(f.toUpdate.Snapshot.GetGerrit().GetGitDeps(), ShouldBeNil)
    49  		})
    50  
    51  		Convey("Just itself", func() {
    52  			// This isn't happening today, but CV shouldn't choke if Gerrit changes.
    53  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{
    54  				gf.RelatedChange(111, 3, 3), // No parents.
    55  			})
    56  			So(f.toUpdate.Snapshot.GetGerrit().GetGitDeps(), ShouldBeNil)
    57  
    58  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{
    59  				gf.RelatedChange(111, 3, 3, "107_2"),
    60  			})
    61  			So(f.toUpdate.Snapshot.GetGerrit().GetGitDeps(), ShouldBeNil)
    62  		})
    63  
    64  		Convey("Has related, but no deps", func() {
    65  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{
    66  				gf.RelatedChange(111, 3, 3, "107_2"),
    67  				gf.RelatedChange(114, 1, 3, "111_3"),
    68  				gf.RelatedChange(117, 2, 2, "114_1"),
    69  			})
    70  			So(f.toUpdate.Snapshot.GetGerrit().GetGitDeps(), ShouldBeNil)
    71  		})
    72  
    73  		Convey("Has related, but lacking this change crbug/1199471", func() {
    74  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{
    75  				gf.RelatedChange(114, 1, 3, "111_3"),
    76  				gf.RelatedChange(117, 2, 2, "114_1"),
    77  			})
    78  			So(f.toUpdate.Snapshot.GetErrors(), ShouldHaveLength, 1)
    79  			So(f.toUpdate.Snapshot.GetErrors()[0].GetCorruptGerritMetadata(), ShouldContainSubstring, "https://crbug.com/1199471")
    80  		})
    81  
    82  		Convey("Has related, and several times itself", func() {
    83  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{
    84  				gf.RelatedChange(111, 2, 2, "107_2"),
    85  				gf.RelatedChange(111, 3, 3, "107_2"),
    86  				gf.RelatedChange(114, 1, 3, "111_3"),
    87  			})
    88  			So(f.toUpdate.Snapshot.GetErrors()[0].GetCorruptGerritMetadata(), ShouldContainSubstring, "https://crbug.com/1199471")
    89  		})
    90  
    91  		Convey("1 parent", func() {
    92  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{
    93  				gf.RelatedChange(107, 1, 3, "104_2"),
    94  				gf.RelatedChange(111, 3, 3, "107_1"),
    95  				gf.RelatedChange(117, 2, 2, "114_1"),
    96  			})
    97  			So(f.toUpdate.Snapshot.GetGerrit().GetGitDeps(), ShouldResembleProto, []*changelist.GerritGitDep{
    98  				{Change: 107, Immediate: true},
    99  			})
   100  		})
   101  
   102  		Convey("Diamond", func() {
   103  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{
   104  				gf.RelatedChange(103, 2, 2),
   105  				gf.RelatedChange(104, 2, 2, "103_2"),
   106  				gf.RelatedChange(107, 1, 3, "104_2"),
   107  				gf.RelatedChange(108, 1, 3, "104_2"),
   108  				gf.RelatedChange(111, 3, 3, "107_1", "108_1"),
   109  				gf.RelatedChange(114, 1, 3, "111_3"),
   110  				gf.RelatedChange(117, 2, 2, "114_1"),
   111  			})
   112  			So(f.toUpdate.Snapshot.GetGerrit().GetGitDeps(), ShouldResembleProto, []*changelist.GerritGitDep{
   113  				{Change: 107, Immediate: true},
   114  				{Change: 108, Immediate: true},
   115  				{Change: 104, Immediate: false},
   116  				{Change: 103, Immediate: false},
   117  			})
   118  		})
   119  
   120  		Convey("Same revision, different changes", func() {
   121  			c104 := gf.RelatedChange(104, 1, 1, "103_2")
   122  			c105 := gf.RelatedChange(105, 1, 1, "103_2")
   123  			c105.GetCommit().Id = c104.GetCommit().GetId()
   124  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{
   125  				gf.RelatedChange(103, 2, 2),
   126  				c104,
   127  				c105, // should be ignored, somewhat arbitrarily.
   128  				gf.RelatedChange(111, 3, 3, "104_1"),
   129  			})
   130  			So(f.toUpdate.Snapshot.GetGerrit().GetGitDeps(), ShouldResembleProto, []*changelist.GerritGitDep{
   131  				{Change: 104, Immediate: true},
   132  				{Change: 103, Immediate: false},
   133  			})
   134  		})
   135  
   136  		Convey("2 parents which are the same change at different revisions", func() {
   137  			// Actually happened, see https://crbug.com/988309.
   138  			f.setGitDeps(ctx, []*gerritpb.GetRelatedChangesResponse_ChangeAndCommit{
   139  				gf.RelatedChange(104, 1, 2, "long-ago-merged1"),
   140  				gf.RelatedChange(107, 1, 1, "long-ago-merged2"),
   141  				gf.RelatedChange(104, 2, 2, "107_1"),
   142  				gf.RelatedChange(111, 3, 3, "104_1", "104_2"),
   143  			})
   144  			So(f.toUpdate.Snapshot.GetGerrit().GetGitDeps(), ShouldResembleProto, []*changelist.GerritGitDep{
   145  				{Change: 104, Immediate: true},
   146  				{Change: 107, Immediate: false},
   147  			})
   148  		})
   149  	})
   150  }