Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 1 | # Copyright 2015 Red Hat, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # 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, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Joshua Hesketh | 0aa7e8b | 2016-07-14 00:12:25 +1000 | [diff] [blame] | 15 | |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 16 | import os |
| 17 | import random |
| 18 | |
| 19 | import fixtures |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 20 | import testtools |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 21 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 22 | from zuul import model |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 23 | from zuul import configloader |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 24 | |
| 25 | from tests.base import BaseTestCase |
| 26 | |
| 27 | |
| 28 | class TestJob(BaseTestCase): |
| 29 | |
| 30 | @property |
| 31 | def job(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 32 | layout = model.Layout() |
| 33 | job = configloader.JobParser.fromYaml(layout, { |
| 34 | 'name': 'job', |
| 35 | 'irrelevant-files': [ |
| 36 | '^docs/.*$' |
| 37 | ]}) |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 38 | return job |
| 39 | |
| 40 | def test_change_matches_returns_false_for_matched_skip_if(self): |
| 41 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 42 | change.files = ['/COMMIT_MSG', 'docs/foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 43 | self.assertFalse(self.job.changeMatches(change)) |
| 44 | |
| 45 | def test_change_matches_returns_true_for_unmatched_skip_if(self): |
| 46 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 47 | change.files = ['/COMMIT_MSG', 'foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 48 | self.assertTrue(self.job.changeMatches(change)) |
| 49 | |
Maru Newby | 79427a4 | 2015-02-17 17:54:45 +0000 | [diff] [blame] | 50 | def test_job_sets_defaults_for_boolean_attributes(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 51 | self.assertIsNotNone(self.job.voting) |
| 52 | |
| 53 | def test_job_inheritance(self): |
| 54 | layout = model.Layout() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 55 | |
| 56 | pipeline = model.Pipeline('gate', layout) |
| 57 | layout.addPipeline(pipeline) |
| 58 | queue = model.ChangeQueue(pipeline) |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 59 | project = model.Project('project') |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 60 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 61 | base = configloader.JobParser.fromYaml(layout, { |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 62 | '_source_project': project, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 63 | 'name': 'base', |
| 64 | 'timeout': 30, |
| 65 | }) |
| 66 | layout.addJob(base) |
| 67 | python27 = configloader.JobParser.fromYaml(layout, { |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 68 | '_source_project': project, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 69 | 'name': 'python27', |
| 70 | 'parent': 'base', |
| 71 | 'timeout': 40, |
| 72 | }) |
| 73 | layout.addJob(python27) |
| 74 | python27diablo = configloader.JobParser.fromYaml(layout, { |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 75 | '_source_project': project, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 76 | 'name': 'python27', |
| 77 | 'branches': [ |
| 78 | 'stable/diablo' |
| 79 | ], |
| 80 | 'timeout': 50, |
| 81 | }) |
| 82 | layout.addJob(python27diablo) |
| 83 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 84 | project_config = configloader.ProjectParser.fromYaml(layout, { |
| 85 | 'name': 'project', |
| 86 | 'gate': { |
| 87 | 'jobs': [ |
| 88 | 'python27' |
| 89 | ] |
| 90 | } |
| 91 | }) |
| 92 | layout.addProjectConfig(project_config, update_pipeline=False) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 93 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 94 | change = model.Change(project) |
| 95 | change.branch = 'master' |
| 96 | item = queue.enqueueChange(change) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 97 | item.current_build_set.layout = layout |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 98 | |
| 99 | self.assertTrue(base.changeMatches(change)) |
| 100 | self.assertTrue(python27.changeMatches(change)) |
| 101 | self.assertFalse(python27diablo.changeMatches(change)) |
| 102 | |
| 103 | item.freezeJobTree() |
| 104 | self.assertEqual(len(item.getJobs()), 1) |
| 105 | job = item.getJobs()[0] |
| 106 | self.assertEqual(job.name, 'python27') |
| 107 | self.assertEqual(job.timeout, 40) |
| 108 | |
| 109 | change.branch = 'stable/diablo' |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 110 | item = queue.enqueueChange(change) |
| 111 | item.current_build_set.layout = layout |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 112 | |
| 113 | self.assertTrue(base.changeMatches(change)) |
| 114 | self.assertTrue(python27.changeMatches(change)) |
| 115 | self.assertTrue(python27diablo.changeMatches(change)) |
| 116 | |
| 117 | item.freezeJobTree() |
| 118 | self.assertEqual(len(item.getJobs()), 1) |
| 119 | job = item.getJobs()[0] |
| 120 | self.assertEqual(job.name, 'python27') |
| 121 | self.assertEqual(job.timeout, 50) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 122 | |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 123 | def test_job_auth_inheritance(self): |
| 124 | layout = model.Layout() |
| 125 | project = model.Project('project') |
| 126 | |
| 127 | base = configloader.JobParser.fromYaml(layout, { |
| 128 | '_source_project': project, |
| 129 | 'name': 'base', |
| 130 | 'timeout': 30, |
| 131 | }) |
| 132 | layout.addJob(base) |
| 133 | pypi_upload_without_inherit = configloader.JobParser.fromYaml(layout, { |
| 134 | '_source_project': project, |
| 135 | 'name': 'pypi-upload-without-inherit', |
| 136 | 'parent': 'base', |
| 137 | 'timeout': 40, |
| 138 | 'auth': { |
Ricardo Carrillo Cruz | 12c892b | 2016-11-18 15:35:49 +0000 | [diff] [blame] | 139 | 'secrets': [ |
| 140 | 'pypi-credentials', |
| 141 | ] |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 142 | } |
| 143 | }) |
| 144 | layout.addJob(pypi_upload_without_inherit) |
| 145 | pypi_upload_with_inherit = configloader.JobParser.fromYaml(layout, { |
| 146 | '_source_project': project, |
| 147 | 'name': 'pypi-upload-with-inherit', |
| 148 | 'parent': 'base', |
| 149 | 'timeout': 40, |
| 150 | 'auth': { |
| 151 | 'inherit': True, |
Ricardo Carrillo Cruz | 12c892b | 2016-11-18 15:35:49 +0000 | [diff] [blame] | 152 | 'secrets': [ |
| 153 | 'pypi-credentials', |
| 154 | ] |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 155 | } |
| 156 | }) |
| 157 | layout.addJob(pypi_upload_with_inherit) |
| 158 | pypi_upload_with_inherit_false = configloader.JobParser.fromYaml( |
| 159 | layout, { |
| 160 | '_source_project': project, |
| 161 | 'name': 'pypi-upload-with-inherit-false', |
| 162 | 'parent': 'base', |
| 163 | 'timeout': 40, |
| 164 | 'auth': { |
| 165 | 'inherit': False, |
Ricardo Carrillo Cruz | 12c892b | 2016-11-18 15:35:49 +0000 | [diff] [blame] | 166 | 'secrets': [ |
| 167 | 'pypi-credentials', |
| 168 | ] |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 169 | } |
| 170 | }) |
| 171 | layout.addJob(pypi_upload_with_inherit_false) |
| 172 | in_repo_job_without_inherit = configloader.JobParser.fromYaml(layout, { |
| 173 | '_source_project': project, |
| 174 | 'name': 'in-repo-job-without-inherit', |
| 175 | 'parent': 'pypi-upload-without-inherit', |
| 176 | }) |
| 177 | layout.addJob(in_repo_job_without_inherit) |
| 178 | in_repo_job_with_inherit = configloader.JobParser.fromYaml(layout, { |
| 179 | '_source_project': project, |
| 180 | 'name': 'in-repo-job-with-inherit', |
| 181 | 'parent': 'pypi-upload-with-inherit', |
| 182 | }) |
| 183 | layout.addJob(in_repo_job_with_inherit) |
| 184 | in_repo_job_with_inherit_false = configloader.JobParser.fromYaml( |
| 185 | layout, { |
| 186 | '_source_project': project, |
| 187 | 'name': 'in-repo-job-with-inherit-false', |
| 188 | 'parent': 'pypi-upload-with-inherit-false', |
| 189 | }) |
| 190 | layout.addJob(in_repo_job_with_inherit_false) |
| 191 | |
| 192 | self.assertNotIn('auth', in_repo_job_without_inherit.auth) |
Ricardo Carrillo Cruz | 12c892b | 2016-11-18 15:35:49 +0000 | [diff] [blame] | 193 | self.assertIn('secrets', in_repo_job_with_inherit.auth) |
| 194 | self.assertEquals(in_repo_job_with_inherit.auth['secrets'], |
| 195 | ['pypi-credentials']) |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 196 | self.assertNotIn('auth', in_repo_job_with_inherit_false.auth) |
| 197 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 198 | def test_job_inheritance_job_tree(self): |
| 199 | layout = model.Layout() |
| 200 | |
| 201 | pipeline = model.Pipeline('gate', layout) |
| 202 | layout.addPipeline(pipeline) |
| 203 | queue = model.ChangeQueue(pipeline) |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 204 | project = model.Project('project') |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 205 | |
| 206 | base = configloader.JobParser.fromYaml(layout, { |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 207 | '_source_project': project, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 208 | 'name': 'base', |
| 209 | 'timeout': 30, |
| 210 | }) |
| 211 | layout.addJob(base) |
| 212 | python27 = configloader.JobParser.fromYaml(layout, { |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 213 | '_source_project': project, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 214 | 'name': 'python27', |
| 215 | 'parent': 'base', |
| 216 | 'timeout': 40, |
| 217 | }) |
| 218 | layout.addJob(python27) |
| 219 | python27diablo = configloader.JobParser.fromYaml(layout, { |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 220 | '_source_project': project, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 221 | 'name': 'python27', |
| 222 | 'branches': [ |
| 223 | 'stable/diablo' |
| 224 | ], |
| 225 | 'timeout': 50, |
| 226 | }) |
| 227 | layout.addJob(python27diablo) |
| 228 | |
| 229 | project_config = configloader.ProjectParser.fromYaml(layout, { |
| 230 | 'name': 'project', |
| 231 | 'gate': { |
| 232 | 'jobs': [ |
| 233 | {'python27': {'timeout': 70}} |
| 234 | ] |
| 235 | } |
| 236 | }) |
| 237 | layout.addProjectConfig(project_config, update_pipeline=False) |
| 238 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 239 | change = model.Change(project) |
| 240 | change.branch = 'master' |
| 241 | item = queue.enqueueChange(change) |
| 242 | item.current_build_set.layout = layout |
| 243 | |
| 244 | self.assertTrue(base.changeMatches(change)) |
| 245 | self.assertTrue(python27.changeMatches(change)) |
| 246 | self.assertFalse(python27diablo.changeMatches(change)) |
| 247 | |
| 248 | item.freezeJobTree() |
| 249 | self.assertEqual(len(item.getJobs()), 1) |
| 250 | job = item.getJobs()[0] |
| 251 | self.assertEqual(job.name, 'python27') |
| 252 | self.assertEqual(job.timeout, 70) |
| 253 | |
| 254 | change.branch = 'stable/diablo' |
| 255 | item = queue.enqueueChange(change) |
| 256 | item.current_build_set.layout = layout |
| 257 | |
| 258 | self.assertTrue(base.changeMatches(change)) |
| 259 | self.assertTrue(python27.changeMatches(change)) |
| 260 | self.assertTrue(python27diablo.changeMatches(change)) |
| 261 | |
| 262 | item.freezeJobTree() |
| 263 | self.assertEqual(len(item.getJobs()), 1) |
| 264 | job = item.getJobs()[0] |
| 265 | self.assertEqual(job.name, 'python27') |
| 266 | self.assertEqual(job.timeout, 70) |
| 267 | |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 268 | def test_inheritance_keeps_matchers(self): |
| 269 | layout = model.Layout() |
| 270 | |
| 271 | pipeline = model.Pipeline('gate', layout) |
| 272 | layout.addPipeline(pipeline) |
| 273 | queue = model.ChangeQueue(pipeline) |
| 274 | project = model.Project('project') |
| 275 | |
| 276 | base = configloader.JobParser.fromYaml(layout, { |
| 277 | '_source_project': project, |
| 278 | 'name': 'base', |
| 279 | 'timeout': 30, |
| 280 | }) |
| 281 | layout.addJob(base) |
| 282 | python27 = configloader.JobParser.fromYaml(layout, { |
| 283 | '_source_project': project, |
| 284 | 'name': 'python27', |
| 285 | 'parent': 'base', |
| 286 | 'timeout': 40, |
| 287 | 'irrelevant-files': ['^ignored-file$'], |
| 288 | }) |
| 289 | layout.addJob(python27) |
| 290 | |
| 291 | project_config = configloader.ProjectParser.fromYaml(layout, { |
| 292 | 'name': 'project', |
| 293 | 'gate': { |
| 294 | 'jobs': [ |
| 295 | 'python27', |
| 296 | ] |
| 297 | } |
| 298 | }) |
| 299 | layout.addProjectConfig(project_config, update_pipeline=False) |
| 300 | |
| 301 | change = model.Change(project) |
| 302 | change.branch = 'master' |
| 303 | change.files = ['/COMMIT_MSG', 'ignored-file'] |
| 304 | item = queue.enqueueChange(change) |
| 305 | item.current_build_set.layout = layout |
| 306 | |
| 307 | self.assertTrue(base.changeMatches(change)) |
| 308 | self.assertFalse(python27.changeMatches(change)) |
| 309 | |
| 310 | item.freezeJobTree() |
| 311 | self.assertEqual([], item.getJobs()) |
| 312 | |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 313 | def test_job_source_project(self): |
| 314 | layout = model.Layout() |
| 315 | base_project = model.Project('base_project') |
| 316 | base = configloader.JobParser.fromYaml(layout, { |
| 317 | '_source_project': base_project, |
| 318 | 'name': 'base', |
| 319 | }) |
| 320 | layout.addJob(base) |
| 321 | |
| 322 | other_project = model.Project('other_project') |
| 323 | base2 = configloader.JobParser.fromYaml(layout, { |
| 324 | '_source_project': other_project, |
| 325 | 'name': 'base', |
| 326 | }) |
| 327 | with testtools.ExpectedException( |
| 328 | Exception, |
| 329 | "Job base in other_project is not permitted " |
| 330 | "to shadow job base in base_project"): |
| 331 | layout.addJob(base2) |
| 332 | |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 333 | |
| 334 | class TestJobTimeData(BaseTestCase): |
| 335 | def setUp(self): |
| 336 | super(TestJobTimeData, self).setUp() |
| 337 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 338 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 339 | ).path |
| 340 | |
| 341 | def test_empty_timedata(self): |
| 342 | path = os.path.join(self.tmp_root, 'job-name') |
| 343 | self.assertFalse(os.path.exists(path)) |
| 344 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 345 | td = model.JobTimeData(path) |
| 346 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 347 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 348 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 349 | |
| 350 | def test_save_reload(self): |
| 351 | path = os.path.join(self.tmp_root, 'job-name') |
| 352 | self.assertFalse(os.path.exists(path)) |
| 353 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 354 | td = model.JobTimeData(path) |
| 355 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 356 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 357 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 358 | success_times = [] |
| 359 | failure_times = [] |
| 360 | results = [] |
| 361 | for x in range(10): |
| 362 | success_times.append(int(random.random() * 1000)) |
| 363 | failure_times.append(int(random.random() * 1000)) |
| 364 | results.append(0) |
| 365 | results.append(1) |
| 366 | random.shuffle(results) |
| 367 | s = f = 0 |
| 368 | for result in results: |
| 369 | if result: |
| 370 | td.add(failure_times[f], 'FAILURE') |
| 371 | f += 1 |
| 372 | else: |
| 373 | td.add(success_times[s], 'SUCCESS') |
| 374 | s += 1 |
| 375 | self.assertEqual(td.success_times, success_times) |
| 376 | self.assertEqual(td.failure_times, failure_times) |
| 377 | self.assertEqual(td.results, results[10:]) |
| 378 | td.save() |
| 379 | self.assertTrue(os.path.exists(path)) |
| 380 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 381 | td = model.JobTimeData(path) |
| 382 | td.load() |
| 383 | self.assertEqual(td.success_times, success_times) |
| 384 | self.assertEqual(td.failure_times, failure_times) |
| 385 | self.assertEqual(td.results, results[10:]) |
| 386 | |
| 387 | |
| 388 | class TestTimeDataBase(BaseTestCase): |
| 389 | def setUp(self): |
| 390 | super(TestTimeDataBase, self).setUp() |
| 391 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 392 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 393 | ).path |
| 394 | self.db = model.TimeDataBase(self.tmp_root) |
| 395 | |
| 396 | def test_timedatabase(self): |
| 397 | self.assertEqual(self.db.getEstimatedTime('job-name'), 0) |
| 398 | self.db.update('job-name', 50, 'SUCCESS') |
| 399 | self.assertEqual(self.db.getEstimatedTime('job-name'), 50) |
| 400 | self.db.update('job-name', 100, 'SUCCESS') |
| 401 | self.assertEqual(self.db.getEstimatedTime('job-name'), 75) |
| 402 | for x in range(10): |
| 403 | self.db.update('job-name', 100, 'SUCCESS') |
| 404 | self.assertEqual(self.db.getEstimatedTime('job-name'), 100) |