github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/plugins/branchcleaner/branchcleaner_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package branchcleaner 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/sirupsen/logrus" 24 25 "sigs.k8s.io/prow/pkg/github" 26 "sigs.k8s.io/prow/pkg/github/fakegithub" 27 "sigs.k8s.io/prow/pkg/plugins" 28 ) 29 30 func TestBranchCleaner(t *testing.T) { 31 baseRepoOrg := "my-org" 32 baseRepoRepo := "repo" 33 baseRepoFullName := fmt.Sprintf("%s/%s", baseRepoOrg, baseRepoRepo) 34 35 testcases := []struct { 36 name string 37 prAction github.PullRequestEventAction 38 merged bool 39 headRepoFullName string 40 srcBranchName string 41 preservedBranches map[string][]string 42 branchDeleteExpected bool 43 }{ 44 { 45 name: "Opened PR nothing to do", 46 prAction: github.PullRequestActionOpened, 47 srcBranchName: "my-feature1", 48 merged: false, 49 branchDeleteExpected: false, 50 }, 51 { 52 name: "Closed PR unmerged nothing to do", 53 prAction: github.PullRequestActionClosed, 54 srcBranchName: "my-feature2", 55 merged: false, 56 branchDeleteExpected: false, 57 }, 58 { 59 name: "PR from different repo nothing to do", 60 prAction: github.PullRequestActionClosed, 61 srcBranchName: "my-fix1", 62 merged: true, 63 headRepoFullName: "different-org/repo", 64 branchDeleteExpected: false, 65 }, 66 { 67 name: "PR from same repo with preserved branch in repo", 68 prAction: github.PullRequestActionClosed, 69 srcBranchName: "betatest", 70 preservedBranches: map[string][]string{ 71 "my-org": { 72 "release", "betatest", 73 }, 74 }, 75 merged: true, 76 headRepoFullName: "my-org/repo", 77 branchDeleteExpected: false, 78 }, 79 { 80 name: "PR from same repo with preserved branch regex match", 81 prAction: github.PullRequestActionClosed, 82 srcBranchName: "betatest-1", 83 preservedBranches: map[string][]string{ 84 "my-org": { 85 "release", "betatest-.*", 86 }, 87 }, 88 merged: true, 89 headRepoFullName: "my-org/repo", 90 branchDeleteExpected: false, 91 }, 92 { 93 name: "PR from same repo without preserved branch regex match", 94 prAction: github.PullRequestActionClosed, 95 srcBranchName: "betatest1", 96 preservedBranches: map[string][]string{ 97 "my-org": { 98 "release", "betatest-.*", 99 }, 100 }, 101 merged: true, 102 headRepoFullName: "my-org/repo", 103 branchDeleteExpected: true, 104 }, 105 { 106 name: "PR from same repo with preserved branch in org", 107 prAction: github.PullRequestActionClosed, 108 srcBranchName: "betatest", 109 preservedBranches: map[string][]string{ 110 "my-org/repo": { 111 "release", "betatest", 112 }, 113 }, 114 merged: true, 115 headRepoFullName: "my-org/repo", 116 branchDeleteExpected: false, 117 }, 118 { 119 name: "PR from same repo with other repo preserved branch", 120 prAction: github.PullRequestActionClosed, 121 srcBranchName: "release", 122 preservedBranches: map[string][]string{ 123 "my-org/other-repo": { 124 "release", "betatest", 125 }, 126 }, 127 merged: true, 128 headRepoFullName: "my-org/repo", 129 branchDeleteExpected: true, 130 }, 131 { 132 name: "PR from same repo without preserved branch", 133 prAction: github.PullRequestActionClosed, 134 srcBranchName: "release", 135 merged: true, 136 headRepoFullName: "my-org/repo", 137 branchDeleteExpected: true, 138 }, 139 { 140 name: "PR from same repo delete head ref", 141 prAction: github.PullRequestActionClosed, 142 srcBranchName: "my-chore1", 143 merged: true, 144 headRepoFullName: "my-org/repo", 145 branchDeleteExpected: true, 146 }, 147 } 148 149 mergeSHA := "abc" 150 prNumber := 1 151 152 for _, tc := range testcases { 153 154 t.Run(tc.name, func(t *testing.T) { 155 log := logrus.WithField("plugin", pluginName) 156 event := github.PullRequestEvent{ 157 Action: tc.prAction, 158 Number: prNumber, 159 PullRequest: github.PullRequest{ 160 Base: github.PullRequestBranch{ 161 Ref: "master", 162 Repo: github.Repo{ 163 DefaultBranch: "master", 164 FullName: baseRepoFullName, 165 Name: baseRepoRepo, 166 Owner: github.User{Login: baseRepoOrg}, 167 }, 168 }, 169 Head: github.PullRequestBranch{ 170 Ref: tc.srcBranchName, 171 Repo: github.Repo{ 172 FullName: tc.headRepoFullName, 173 }, 174 }, 175 Merged: tc.merged}, 176 } 177 if tc.merged { 178 event.PullRequest.MergeSHA = &mergeSHA 179 } 180 181 fgc := fakegithub.NewFakeClient() 182 fgc.PullRequests = map[int]*github.PullRequest{ 183 prNumber: { 184 Number: prNumber, 185 }, 186 } 187 if err := handle(fgc, log, plugins.BranchCleaner{ 188 PreservedBranches: tc.preservedBranches, 189 }, event); err != nil { 190 t.Fatalf("error in handle: %v", err) 191 } 192 if tc.branchDeleteExpected != (len(fgc.RefsDeleted) == 1) { 193 t.Fatalf("branchDeleteExpected: %v, refsDeleted: %d", tc.branchDeleteExpected, len(fgc.RefsDeleted)) 194 } 195 196 if tc.branchDeleteExpected { 197 if fgc.RefsDeleted[0].Org != event.PullRequest.Base.Repo.Owner.Login { 198 t.Errorf("Expected org of deleted ref to be %s but was %s", event.PullRequest.Base.Repo.Owner.Login, fgc.RefsDeleted[0].Org) 199 } 200 if fgc.RefsDeleted[0].Repo != event.PullRequest.Base.Repo.Name { 201 t.Errorf("Expected repo of deleted ref to be %s but was %s", baseRepoRepo, fgc.RefsDeleted[0].Repo) 202 } 203 expectedRefName := fmt.Sprintf("heads/%s", event.PullRequest.Head.Ref) 204 if fgc.RefsDeleted[0].Ref != expectedRefName { 205 t.Errorf("Expected name of deleted ref to be %s but was %s", expectedRefName, fgc.RefsDeleted[0].Ref) 206 } 207 } 208 209 }) 210 211 } 212 }