sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/plugins/trigger/push_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 trigger 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/sirupsen/logrus" 24 clienttesting "k8s.io/client-go/testing" 25 26 "k8s.io/apimachinery/pkg/api/equality" 27 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 28 "k8s.io/apimachinery/pkg/util/diff" 29 prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1" 30 "sigs.k8s.io/prow/pkg/client/clientset/versioned/fake" 31 "sigs.k8s.io/prow/pkg/config" 32 "sigs.k8s.io/prow/pkg/github" 33 34 "sigs.k8s.io/prow/pkg/github/fakegithub" 35 ) 36 37 func TestCreateRefs(t *testing.T) { 38 pe := github.PushEvent{ 39 Ref: "refs/heads/master", 40 Repo: github.Repo{ 41 Owner: github.User{ 42 Name: "kubernetes", 43 }, 44 Name: "repo", 45 HTMLURL: "https://example.com/kubernetes/repo", 46 }, 47 After: "abcdef", 48 Compare: "https://example.com/kubernetes/repo/compare/abcdee...abcdef", 49 } 50 expected := prowapi.Refs{ 51 Org: "kubernetes", 52 Repo: "repo", 53 RepoLink: "https://example.com/kubernetes/repo", 54 BaseRef: "master", 55 BaseSHA: "abcdef", 56 BaseLink: "https://example.com/kubernetes/repo/compare/abcdee...abcdef", 57 } 58 if actual := createRefs(pe); !equality.Semantic.DeepEqual(expected, actual) { 59 t.Errorf("diff between expected and actual refs:%s", diff.ObjectReflectDiff(expected, actual)) 60 } 61 } 62 63 func TestHandlePE(t *testing.T) { 64 testCases := []struct { 65 name string 66 pe github.PushEvent 67 jobsToRun int 68 }{ 69 { 70 name: "branch deleted", 71 pe: github.PushEvent{ 72 Ref: "refs/heads/master", 73 Repo: github.Repo{ 74 Owner: github.User{Login: "org"}, 75 Name: "repo", 76 }, 77 Deleted: true, 78 }, 79 jobsToRun: 0, 80 }, 81 { 82 name: "null after sha", 83 pe: github.PushEvent{ 84 After: "0000000000000000000000000000000000000000", 85 Ref: "refs/heads/master", 86 Repo: github.Repo{ 87 Owner: github.User{Login: "org"}, 88 Name: "repo", 89 }, 90 }, 91 jobsToRun: 0, 92 }, 93 { 94 name: "no matching files", 95 pe: github.PushEvent{ 96 Ref: "refs/heads/master", 97 Commits: []github.Commit{ 98 { 99 Added: []string{"example.txt"}, 100 }, 101 }, 102 Repo: github.Repo{ 103 Owner: github.User{Login: "org"}, 104 Name: "repo", 105 }, 106 }, 107 }, 108 { 109 name: "one matching file", 110 pe: github.PushEvent{ 111 Ref: "refs/heads/master", 112 Commits: []github.Commit{ 113 { 114 Added: []string{"example.txt"}, 115 Modified: []string{"hack.sh"}, 116 }, 117 }, 118 Repo: github.Repo{ 119 Owner: github.User{Login: "org"}, 120 Name: "repo", 121 }, 122 }, 123 jobsToRun: 1, 124 }, 125 { 126 name: "no change matcher", 127 pe: github.PushEvent{ 128 Ref: "refs/heads/master", 129 Commits: []github.Commit{ 130 { 131 Added: []string{"example.txt"}, 132 }, 133 }, 134 Repo: github.Repo{ 135 Owner: github.User{Login: "org2"}, 136 Name: "repo2", 137 }, 138 }, 139 jobsToRun: 1, 140 }, 141 { 142 name: "branch name with a slash", 143 pe: github.PushEvent{ 144 Ref: "refs/heads/release/v1.14", 145 Commits: []github.Commit{ 146 { 147 Added: []string{"hack.sh"}, 148 }, 149 }, 150 Repo: github.Repo{ 151 Owner: github.User{Login: "org3"}, 152 Name: "repo3", 153 }, 154 }, 155 jobsToRun: 1, 156 }, 157 } 158 for _, tc := range testCases { 159 g := fakegithub.NewFakeClient() 160 fakeProwJobClient := fake.NewSimpleClientset() 161 c := Client{ 162 GitHubClient: g, 163 ProwJobClient: fakeProwJobClient.ProwV1().ProwJobs("prowjobs"), 164 Config: &config.Config{ProwConfig: config.ProwConfig{ProwJobNamespace: "prowjobs"}}, 165 Logger: logrus.WithField("plugin", PluginName), 166 } 167 postsubmits := map[string][]config.Postsubmit{ 168 "org/repo": { 169 { 170 JobBase: config.JobBase{ 171 Name: "pass-butter", 172 }, 173 RegexpChangeMatcher: config.RegexpChangeMatcher{ 174 RunIfChanged: "\\.sh$", 175 }, 176 }, 177 }, 178 "org2/repo2": { 179 { 180 JobBase: config.JobBase{ 181 Name: "pass-salt", 182 }, 183 }, 184 }, 185 "org3/repo3": { 186 { 187 JobBase: config.JobBase{ 188 Name: "pass-pepper", 189 }, 190 Brancher: config.Brancher{ 191 Branches: []string{"release/v1.14"}, 192 }, 193 }, 194 }, 195 } 196 if err := c.Config.SetPostsubmits(postsubmits); err != nil { 197 t.Fatalf("failed to set postsubmits: %v", err) 198 } 199 err := handlePE(c, tc.pe) 200 if err != nil { 201 t.Errorf("test %q: handlePE returned unexpected error %v", tc.name, err) 202 } 203 var numStarted int 204 for _, action := range fakeProwJobClient.Fake.Actions() { 205 switch action.(type) { 206 case clienttesting.CreateActionImpl: 207 numStarted++ 208 } 209 } 210 if numStarted != tc.jobsToRun { 211 t.Errorf("test %q: expected %d jobs to run, got %d", tc.name, tc.jobsToRun, numStarted) 212 } 213 } 214 } 215 216 func TestHandlePEScheduling(t *testing.T) { 217 job := config.JobBase{Name: "job"} 218 postsubmits := map[string][]config.Postsubmit{"org/repo": {{JobBase: job}}} 219 220 for _, tc := range []struct { 221 name string 222 enableScheduling bool 223 pe github.PushEvent 224 wantPJState prowapi.ProwJobState 225 }{ 226 { 227 name: "Create job in triggered state", 228 pe: github.PushEvent{ 229 Ref: "refs/heads/master", 230 Repo: github.Repo{ 231 Owner: github.User{Login: "org"}, 232 Name: "repo", 233 }, 234 }, 235 wantPJState: prowapi.TriggeredState, 236 }, 237 { 238 name: "Create job in scheduling state", 239 enableScheduling: true, 240 pe: github.PushEvent{ 241 Ref: "refs/heads/master", 242 Repo: github.Repo{ 243 Owner: github.User{Login: "org"}, 244 Name: "repo", 245 }, 246 }, 247 wantPJState: prowapi.SchedulingState, 248 }, 249 } { 250 t.Run(tc.name, func(t *testing.T) { 251 ghClient := fakegithub.NewFakeClient() 252 fakeProwJobClient := fake.NewSimpleClientset() 253 c := Client{ 254 GitHubClient: ghClient, 255 ProwJobClient: fakeProwJobClient.ProwV1().ProwJobs("prowjobs"), 256 Config: &config.Config{ProwConfig: config.ProwConfig{ 257 ProwJobNamespace: "prowjobs", 258 Scheduler: config.Scheduler{Enabled: tc.enableScheduling}, 259 }}, 260 Logger: logrus.WithField("plugin", PluginName), 261 } 262 263 c.Config.SetPostsubmits(postsubmits) 264 265 err := handlePE(c, tc.pe) 266 if err != nil { 267 t.Errorf("test %q: handlePE returned unexpected error %v", tc.name, err) 268 } 269 270 pjs, err := c.ProwJobClient.List(context.TODO(), v1.ListOptions{}) 271 if err != nil { 272 t.Fatalf("Couldn't get PJs from the fake client: %s", err) 273 } 274 275 if len(pjs.Items) != 1 { 276 t.Errorf("Expected 1 job but got %d", len(pjs.Items)) 277 } 278 279 resultPJ := pjs.Items[0] 280 if job.Name != resultPJ.Spec.Job { 281 t.Errorf("Expected job %s but got %s", job.Name, resultPJ.Spec.Job) 282 } 283 284 if tc.wantPJState != resultPJ.Status.State { 285 t.Errorf("Expected state %s but got %s", tc.wantPJState, resultPJ.Status.State) 286 } 287 }) 288 } 289 }