sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/plugins/hold/hold_test.go (about) 1 /* 2 Copyright 2017 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 hold 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/labels" 28 ) 29 30 func TestHandle(t *testing.T) { 31 var tests = []struct { 32 name string 33 body string 34 hasLabel bool 35 shouldLabel bool 36 shouldUnlabel bool 37 isPR bool 38 }{ 39 { 40 name: "nothing to do", 41 body: "noise", 42 hasLabel: false, 43 shouldLabel: false, 44 shouldUnlabel: false, 45 isPR: true, 46 }, 47 { 48 name: "quoted hold in a reply", 49 body: "> /hold", 50 hasLabel: false, 51 shouldLabel: false, 52 shouldUnlabel: false, 53 isPR: true, 54 }, 55 { 56 name: "requested hold", 57 body: "/hold", 58 hasLabel: false, 59 shouldLabel: true, 60 shouldUnlabel: false, 61 isPR: true, 62 }, 63 { 64 name: "requested hold with reason", 65 body: "/hold for further review", 66 hasLabel: false, 67 shouldLabel: true, 68 shouldUnlabel: false, 69 isPR: true, 70 }, 71 { 72 name: "requested hold, Label already exists", 73 body: "/hold", 74 hasLabel: true, 75 shouldLabel: false, 76 shouldUnlabel: false, 77 isPR: true, 78 }, 79 { 80 name: "requested hold with reason, Label already exists", 81 body: "/hold for further review", 82 hasLabel: true, 83 shouldLabel: false, 84 shouldUnlabel: false, 85 isPR: true, 86 }, 87 { 88 name: "requested hold cancel", 89 body: "/hold cancel", 90 hasLabel: true, 91 shouldLabel: false, 92 shouldUnlabel: true, 93 isPR: true, 94 }, 95 { 96 name: "requested hold cancel with whitespace", 97 body: "/hold cancel ", 98 hasLabel: true, 99 shouldLabel: false, 100 shouldUnlabel: true, 101 isPR: true, 102 }, 103 { 104 name: "requested hold cancel, Label already gone", 105 body: "/hold cancel", 106 hasLabel: false, 107 shouldLabel: false, 108 shouldUnlabel: false, 109 isPR: true, 110 }, 111 { 112 name: "requested unhold", 113 body: "/unhold", 114 hasLabel: true, 115 shouldLabel: false, 116 shouldUnlabel: true, 117 isPR: true, 118 }, 119 { 120 name: "requested unhold with whitespace", 121 body: "/unhold ", 122 hasLabel: true, 123 shouldLabel: false, 124 shouldUnlabel: true, 125 isPR: true, 126 }, 127 { 128 name: "requested unhold, Label already gone", 129 body: "/unhold", 130 hasLabel: false, 131 shouldLabel: false, 132 shouldUnlabel: false, 133 isPR: true, 134 }, 135 { 136 name: "requested hold for issues", 137 body: "/hold", 138 hasLabel: false, 139 shouldLabel: false, 140 shouldUnlabel: false, 141 isPR: false, 142 }, 143 { 144 name: "requested unhold for issues", 145 body: "/unhold", 146 hasLabel: true, 147 shouldLabel: false, 148 shouldUnlabel: false, 149 isPR: false, 150 }, 151 { 152 name: "requested remove hold label", 153 body: "/remove-hold", 154 hasLabel: true, 155 shouldLabel: false, 156 shouldUnlabel: true, 157 isPR: true, 158 }, 159 { 160 name: "requested remove hold label with whitespaces in between", 161 body: "/remove - hold", 162 hasLabel: false, 163 shouldLabel: false, 164 shouldUnlabel: false, 165 isPR: true, 166 }, 167 { 168 name: "requested remove hold label with no separating hyphen", 169 body: "/removehold", 170 hasLabel: false, 171 shouldLabel: false, 172 shouldUnlabel: false, 173 isPR: true, 174 }, 175 } 176 177 for _, tc := range tests { 178 fc := fakegithub.NewFakeClient() 179 fc.IssueComments = make(map[int][]github.IssueComment) 180 181 e := &github.GenericCommentEvent{ 182 Action: github.GenericCommentActionCreated, 183 Body: tc.body, 184 Number: 1, 185 Repo: github.Repo{Owner: github.User{Login: "org"}, Name: "repo"}, 186 IsPR: tc.isPR, 187 } 188 hasLabel := func(label string, issueLabels []github.Label) bool { 189 return tc.hasLabel 190 } 191 192 if err := handle(fc, logrus.WithField("plugin", PluginName), e, hasLabel); err != nil { 193 t.Errorf("For case %s, didn't expect error from hold: %v", tc.name, err) 194 continue 195 } 196 197 fakeLabel := fmt.Sprintf("org/repo#1:%s", labels.Hold) 198 if tc.shouldLabel { 199 if len(fc.IssueLabelsAdded) != 1 || fc.IssueLabelsAdded[0] != fakeLabel { 200 t.Errorf("For case %s: expected to add %q Label but instead added: %v", tc.name, labels.Hold, fc.IssueLabelsAdded) 201 } 202 } else if len(fc.IssueLabelsAdded) > 0 { 203 t.Errorf("For case %s, expected to not add %q Label but added: %v", tc.name, labels.Hold, fc.IssueLabelsAdded) 204 } 205 if tc.shouldUnlabel { 206 if len(fc.IssueLabelsRemoved) != 1 || fc.IssueLabelsRemoved[0] != fakeLabel { 207 t.Errorf("For case %s: expected to remove %q Label but instead removed: %v", tc.name, labels.Hold, fc.IssueLabelsRemoved) 208 } 209 } else if len(fc.IssueLabelsRemoved) > 0 { 210 t.Errorf("For case %s, expected to not remove %q Label but removed: %v", tc.name, labels.Hold, fc.IssueLabelsRemoved) 211 } 212 } 213 }