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 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 30 | def setUp(self): |
| 31 | super(TestJob, self).setUp() |
| 32 | self.project = model.Project('project', None) |
| 33 | self.context = model.SourceContext(self.project, 'master', True) |
| 34 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 35 | @property |
| 36 | def job(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 37 | layout = model.Layout() |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 38 | project = model.Project('project', None) |
| 39 | context = model.SourceContext(project, 'master', True) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 40 | job = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 41 | '_source_context': context, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 42 | 'name': 'job', |
| 43 | 'irrelevant-files': [ |
| 44 | '^docs/.*$' |
| 45 | ]}) |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 46 | return job |
| 47 | |
| 48 | def test_change_matches_returns_false_for_matched_skip_if(self): |
| 49 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 50 | change.files = ['/COMMIT_MSG', 'docs/foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 51 | self.assertFalse(self.job.changeMatches(change)) |
| 52 | |
| 53 | def test_change_matches_returns_true_for_unmatched_skip_if(self): |
| 54 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 55 | change.files = ['/COMMIT_MSG', 'foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 56 | self.assertTrue(self.job.changeMatches(change)) |
| 57 | |
Maru Newby | 79427a4 | 2015-02-17 17:54:45 +0000 | [diff] [blame] | 58 | def test_job_sets_defaults_for_boolean_attributes(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 59 | self.assertIsNotNone(self.job.voting) |
| 60 | |
| 61 | def test_job_inheritance(self): |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 62 | # This is standard job inheritance. |
| 63 | |
| 64 | base_pre = model.PlaybookContext(self.context, 'base-pre') |
| 65 | base_run = model.PlaybookContext(self.context, 'base-run') |
| 66 | base_post = model.PlaybookContext(self.context, 'base-post') |
| 67 | |
| 68 | base = model.Job('base') |
| 69 | base.timeout = 30 |
| 70 | base.pre_run = [base_pre] |
| 71 | base.run = [base_run] |
| 72 | base.post_run = [base_post] |
| 73 | base.auth = dict(foo='bar', inherit=False) |
| 74 | |
| 75 | py27 = model.Job('py27') |
| 76 | self.assertEqual(None, py27.timeout) |
| 77 | py27.inheritFrom(base) |
| 78 | self.assertEqual(30, py27.timeout) |
| 79 | self.assertEqual(['base-pre'], |
| 80 | [x.path for x in py27.pre_run]) |
| 81 | self.assertEqual(['base-run'], |
| 82 | [x.path for x in py27.run]) |
| 83 | self.assertEqual(['base-post'], |
| 84 | [x.path for x in py27.post_run]) |
| 85 | self.assertEqual({}, py27.auth) |
| 86 | |
| 87 | def test_job_variants(self): |
| 88 | # This simulates freezing a job. |
| 89 | |
| 90 | py27_pre = model.PlaybookContext(self.context, 'py27-pre') |
| 91 | py27_run = model.PlaybookContext(self.context, 'py27-run') |
| 92 | py27_post = model.PlaybookContext(self.context, 'py27-post') |
| 93 | |
| 94 | py27 = model.Job('py27') |
| 95 | py27.timeout = 30 |
| 96 | py27.pre_run = [py27_pre] |
| 97 | py27.run = [py27_run] |
| 98 | py27.post_run = [py27_post] |
| 99 | auth = dict(foo='bar', inherit=False) |
| 100 | py27.auth = auth |
| 101 | |
| 102 | job = py27.copy() |
| 103 | self.assertEqual(30, job.timeout) |
| 104 | |
| 105 | # Apply the diablo variant |
| 106 | diablo = model.Job('py27') |
| 107 | diablo.timeout = 40 |
| 108 | job.applyVariant(diablo) |
| 109 | |
| 110 | self.assertEqual(40, job.timeout) |
| 111 | self.assertEqual(['py27-pre'], |
| 112 | [x.path for x in job.pre_run]) |
| 113 | self.assertEqual(['py27-run'], |
| 114 | [x.path for x in job.run]) |
| 115 | self.assertEqual(['py27-post'], |
| 116 | [x.path for x in job.post_run]) |
| 117 | self.assertEqual(auth, job.auth) |
| 118 | |
| 119 | # Set the job to final for the following checks |
| 120 | job.final = True |
| 121 | self.assertTrue(job.voting) |
| 122 | |
| 123 | good_final = model.Job('py27') |
| 124 | good_final.voting = False |
| 125 | job.applyVariant(good_final) |
| 126 | self.assertFalse(job.voting) |
| 127 | |
| 128 | bad_final = model.Job('py27') |
| 129 | bad_final.timeout = 600 |
| 130 | with testtools.ExpectedException( |
| 131 | Exception, |
| 132 | "Unable to modify final job"): |
| 133 | job.applyVariant(bad_final) |
| 134 | |
| 135 | def test_job_inheritance_configloader(self): |
| 136 | # TODO(jeblair): move this to a configloader test |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 137 | layout = model.Layout() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 138 | |
| 139 | pipeline = model.Pipeline('gate', layout) |
| 140 | layout.addPipeline(pipeline) |
| 141 | queue = model.ChangeQueue(pipeline) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 142 | project = model.Project('project', None) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 143 | context = model.SourceContext(project, 'master', True) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 144 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 145 | base = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 146 | '_source_context': context, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 147 | 'name': 'base', |
| 148 | 'timeout': 30, |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 149 | 'pre-run': 'base-pre', |
| 150 | 'post-run': 'base-post', |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 151 | 'nodes': [{ |
| 152 | 'name': 'controller', |
| 153 | 'image': 'base', |
| 154 | }], |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 155 | }) |
| 156 | layout.addJob(base) |
| 157 | python27 = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 158 | '_source_context': context, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 159 | 'name': 'python27', |
| 160 | 'parent': 'base', |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 161 | 'pre-run': 'py27-pre', |
| 162 | 'post-run': 'py27-post', |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 163 | 'nodes': [{ |
| 164 | 'name': 'controller', |
| 165 | 'image': 'new', |
| 166 | }], |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 167 | 'timeout': 40, |
| 168 | }) |
| 169 | layout.addJob(python27) |
| 170 | python27diablo = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 171 | '_source_context': context, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 172 | 'name': 'python27', |
| 173 | 'branches': [ |
| 174 | 'stable/diablo' |
| 175 | ], |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 176 | 'pre-run': 'py27-diablo-pre', |
| 177 | 'run': 'py27-diablo', |
| 178 | 'post-run': 'py27-diablo-post', |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 179 | 'nodes': [{ |
| 180 | 'name': 'controller', |
| 181 | 'image': 'old', |
| 182 | }], |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 183 | 'timeout': 50, |
| 184 | }) |
| 185 | layout.addJob(python27diablo) |
| 186 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 187 | python27essex = configloader.JobParser.fromYaml(layout, { |
| 188 | '_source_context': context, |
| 189 | 'name': 'python27', |
| 190 | 'branches': [ |
| 191 | 'stable/essex' |
| 192 | ], |
| 193 | 'pre-run': 'py27-essex-pre', |
| 194 | 'post-run': 'py27-essex-post', |
| 195 | }) |
| 196 | layout.addJob(python27essex) |
| 197 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 198 | project_config = configloader.ProjectParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 199 | '_source_context': context, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 200 | 'name': 'project', |
| 201 | 'gate': { |
| 202 | 'jobs': [ |
| 203 | 'python27' |
| 204 | ] |
| 205 | } |
| 206 | }) |
| 207 | layout.addProjectConfig(project_config, update_pipeline=False) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 208 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 209 | change = model.Change(project) |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 210 | # Test master |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 211 | change.branch = 'master' |
| 212 | item = queue.enqueueChange(change) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 213 | item.current_build_set.layout = layout |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 214 | |
| 215 | self.assertTrue(base.changeMatches(change)) |
| 216 | self.assertTrue(python27.changeMatches(change)) |
| 217 | self.assertFalse(python27diablo.changeMatches(change)) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 218 | self.assertFalse(python27essex.changeMatches(change)) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 219 | |
| 220 | item.freezeJobTree() |
| 221 | self.assertEqual(len(item.getJobs()), 1) |
| 222 | job = item.getJobs()[0] |
| 223 | self.assertEqual(job.name, 'python27') |
| 224 | self.assertEqual(job.timeout, 40) |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 225 | nodes = job.nodeset.getNodes() |
| 226 | self.assertEqual(len(nodes), 1) |
| 227 | self.assertEqual(nodes[0].image, 'new') |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 228 | self.assertEqual([x.path for x in job.pre_run], |
| 229 | ['playbooks/base-pre', |
| 230 | 'playbooks/py27-pre']) |
| 231 | self.assertEqual([x.path for x in job.post_run], |
| 232 | ['playbooks/py27-post', |
| 233 | 'playbooks/base-post']) |
| 234 | self.assertEqual([x.path for x in job.run], |
| 235 | ['playbooks/python27', |
| 236 | 'playbooks/base']) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 237 | |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 238 | # Test diablo |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 239 | change.branch = 'stable/diablo' |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 240 | item = queue.enqueueChange(change) |
| 241 | item.current_build_set.layout = layout |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 242 | |
| 243 | self.assertTrue(base.changeMatches(change)) |
| 244 | self.assertTrue(python27.changeMatches(change)) |
| 245 | self.assertTrue(python27diablo.changeMatches(change)) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 246 | self.assertFalse(python27essex.changeMatches(change)) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 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, 50) |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 253 | nodes = job.nodeset.getNodes() |
| 254 | self.assertEqual(len(nodes), 1) |
| 255 | self.assertEqual(nodes[0].image, 'old') |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 256 | self.assertEqual([x.path for x in job.pre_run], |
| 257 | ['playbooks/base-pre', |
| 258 | 'playbooks/py27-pre', |
| 259 | 'playbooks/py27-diablo-pre']) |
| 260 | self.assertEqual([x.path for x in job.post_run], |
| 261 | ['playbooks/py27-diablo-post', |
| 262 | 'playbooks/py27-post', |
| 263 | 'playbooks/base-post']) |
| 264 | self.assertEqual([x.path for x in job.run], |
| 265 | ['playbooks/py27-diablo']), |
| 266 | |
| 267 | # Test essex |
| 268 | change.branch = 'stable/essex' |
| 269 | item = queue.enqueueChange(change) |
| 270 | item.current_build_set.layout = layout |
| 271 | |
| 272 | self.assertTrue(base.changeMatches(change)) |
| 273 | self.assertTrue(python27.changeMatches(change)) |
| 274 | self.assertFalse(python27diablo.changeMatches(change)) |
| 275 | self.assertTrue(python27essex.changeMatches(change)) |
| 276 | |
| 277 | item.freezeJobTree() |
| 278 | self.assertEqual(len(item.getJobs()), 1) |
| 279 | job = item.getJobs()[0] |
| 280 | self.assertEqual(job.name, 'python27') |
| 281 | self.assertEqual([x.path for x in job.pre_run], |
| 282 | ['playbooks/base-pre', |
| 283 | 'playbooks/py27-pre', |
| 284 | 'playbooks/py27-essex-pre']) |
| 285 | self.assertEqual([x.path for x in job.post_run], |
| 286 | ['playbooks/py27-essex-post', |
| 287 | 'playbooks/py27-post', |
| 288 | 'playbooks/base-post']) |
| 289 | self.assertEqual([x.path for x in job.run], |
| 290 | ['playbooks/python27', |
| 291 | 'playbooks/base']) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 292 | |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 293 | def test_job_auth_inheritance(self): |
| 294 | layout = model.Layout() |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 295 | project = model.Project('project', None) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 296 | context = model.SourceContext(project, 'master', True) |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 297 | |
| 298 | base = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 299 | '_source_context': context, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 300 | 'name': 'base', |
| 301 | 'timeout': 30, |
| 302 | }) |
| 303 | layout.addJob(base) |
| 304 | pypi_upload_without_inherit = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 305 | '_source_context': context, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 306 | 'name': 'pypi-upload-without-inherit', |
| 307 | 'parent': 'base', |
| 308 | 'timeout': 40, |
| 309 | 'auth': { |
Ricardo Carrillo Cruz | 12c892b | 2016-11-18 15:35:49 +0000 | [diff] [blame] | 310 | 'secrets': [ |
| 311 | 'pypi-credentials', |
| 312 | ] |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 313 | } |
| 314 | }) |
| 315 | layout.addJob(pypi_upload_without_inherit) |
| 316 | pypi_upload_with_inherit = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 317 | '_source_context': context, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 318 | 'name': 'pypi-upload-with-inherit', |
| 319 | 'parent': 'base', |
| 320 | 'timeout': 40, |
| 321 | 'auth': { |
| 322 | 'inherit': True, |
Ricardo Carrillo Cruz | 12c892b | 2016-11-18 15:35:49 +0000 | [diff] [blame] | 323 | 'secrets': [ |
| 324 | 'pypi-credentials', |
| 325 | ] |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 326 | } |
| 327 | }) |
| 328 | layout.addJob(pypi_upload_with_inherit) |
| 329 | pypi_upload_with_inherit_false = configloader.JobParser.fromYaml( |
| 330 | layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 331 | '_source_context': context, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 332 | 'name': 'pypi-upload-with-inherit-false', |
| 333 | 'parent': 'base', |
| 334 | 'timeout': 40, |
| 335 | 'auth': { |
| 336 | 'inherit': False, |
Ricardo Carrillo Cruz | 12c892b | 2016-11-18 15:35:49 +0000 | [diff] [blame] | 337 | 'secrets': [ |
| 338 | 'pypi-credentials', |
| 339 | ] |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 340 | } |
| 341 | }) |
| 342 | layout.addJob(pypi_upload_with_inherit_false) |
| 343 | in_repo_job_without_inherit = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 344 | '_source_context': context, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 345 | 'name': 'in-repo-job-without-inherit', |
| 346 | 'parent': 'pypi-upload-without-inherit', |
| 347 | }) |
| 348 | layout.addJob(in_repo_job_without_inherit) |
| 349 | in_repo_job_with_inherit = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 350 | '_source_context': context, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 351 | 'name': 'in-repo-job-with-inherit', |
| 352 | 'parent': 'pypi-upload-with-inherit', |
| 353 | }) |
| 354 | layout.addJob(in_repo_job_with_inherit) |
| 355 | in_repo_job_with_inherit_false = configloader.JobParser.fromYaml( |
| 356 | layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 357 | '_source_context': context, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 358 | 'name': 'in-repo-job-with-inherit-false', |
| 359 | 'parent': 'pypi-upload-with-inherit-false', |
| 360 | }) |
| 361 | layout.addJob(in_repo_job_with_inherit_false) |
| 362 | |
| 363 | self.assertNotIn('auth', in_repo_job_without_inherit.auth) |
Ricardo Carrillo Cruz | 12c892b | 2016-11-18 15:35:49 +0000 | [diff] [blame] | 364 | self.assertIn('secrets', in_repo_job_with_inherit.auth) |
| 365 | self.assertEquals(in_repo_job_with_inherit.auth['secrets'], |
| 366 | ['pypi-credentials']) |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 367 | self.assertNotIn('auth', in_repo_job_with_inherit_false.auth) |
| 368 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 369 | def test_job_inheritance_job_tree(self): |
| 370 | layout = model.Layout() |
| 371 | |
| 372 | pipeline = model.Pipeline('gate', layout) |
| 373 | layout.addPipeline(pipeline) |
| 374 | queue = model.ChangeQueue(pipeline) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 375 | project = model.Project('project', None) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 376 | context = model.SourceContext(project, 'master', True) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 377 | |
| 378 | base = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 379 | '_source_context': context, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 380 | 'name': 'base', |
| 381 | 'timeout': 30, |
| 382 | }) |
| 383 | layout.addJob(base) |
| 384 | python27 = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 385 | '_source_context': context, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 386 | 'name': 'python27', |
| 387 | 'parent': 'base', |
| 388 | 'timeout': 40, |
| 389 | }) |
| 390 | layout.addJob(python27) |
| 391 | python27diablo = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 392 | '_source_context': context, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 393 | 'name': 'python27', |
| 394 | 'branches': [ |
| 395 | 'stable/diablo' |
| 396 | ], |
| 397 | 'timeout': 50, |
| 398 | }) |
| 399 | layout.addJob(python27diablo) |
| 400 | |
| 401 | project_config = configloader.ProjectParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 402 | '_source_context': context, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 403 | 'name': 'project', |
| 404 | 'gate': { |
| 405 | 'jobs': [ |
| 406 | {'python27': {'timeout': 70}} |
| 407 | ] |
| 408 | } |
| 409 | }) |
| 410 | layout.addProjectConfig(project_config, update_pipeline=False) |
| 411 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 412 | change = model.Change(project) |
| 413 | change.branch = 'master' |
| 414 | item = queue.enqueueChange(change) |
| 415 | item.current_build_set.layout = layout |
| 416 | |
| 417 | self.assertTrue(base.changeMatches(change)) |
| 418 | self.assertTrue(python27.changeMatches(change)) |
| 419 | self.assertFalse(python27diablo.changeMatches(change)) |
| 420 | |
| 421 | item.freezeJobTree() |
| 422 | self.assertEqual(len(item.getJobs()), 1) |
| 423 | job = item.getJobs()[0] |
| 424 | self.assertEqual(job.name, 'python27') |
| 425 | self.assertEqual(job.timeout, 70) |
| 426 | |
| 427 | change.branch = 'stable/diablo' |
| 428 | item = queue.enqueueChange(change) |
| 429 | item.current_build_set.layout = layout |
| 430 | |
| 431 | self.assertTrue(base.changeMatches(change)) |
| 432 | self.assertTrue(python27.changeMatches(change)) |
| 433 | self.assertTrue(python27diablo.changeMatches(change)) |
| 434 | |
| 435 | item.freezeJobTree() |
| 436 | self.assertEqual(len(item.getJobs()), 1) |
| 437 | job = item.getJobs()[0] |
| 438 | self.assertEqual(job.name, 'python27') |
| 439 | self.assertEqual(job.timeout, 70) |
| 440 | |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 441 | def test_inheritance_keeps_matchers(self): |
| 442 | layout = model.Layout() |
| 443 | |
| 444 | pipeline = model.Pipeline('gate', layout) |
| 445 | layout.addPipeline(pipeline) |
| 446 | queue = model.ChangeQueue(pipeline) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 447 | project = model.Project('project', None) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 448 | context = model.SourceContext(project, 'master', True) |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 449 | |
| 450 | base = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 451 | '_source_context': context, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 452 | 'name': 'base', |
| 453 | 'timeout': 30, |
| 454 | }) |
| 455 | layout.addJob(base) |
| 456 | python27 = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 457 | '_source_context': context, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 458 | 'name': 'python27', |
| 459 | 'parent': 'base', |
| 460 | 'timeout': 40, |
| 461 | 'irrelevant-files': ['^ignored-file$'], |
| 462 | }) |
| 463 | layout.addJob(python27) |
| 464 | |
| 465 | project_config = configloader.ProjectParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 466 | '_source_context': context, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 467 | 'name': 'project', |
| 468 | 'gate': { |
| 469 | 'jobs': [ |
| 470 | 'python27', |
| 471 | ] |
| 472 | } |
| 473 | }) |
| 474 | layout.addProjectConfig(project_config, update_pipeline=False) |
| 475 | |
| 476 | change = model.Change(project) |
| 477 | change.branch = 'master' |
| 478 | change.files = ['/COMMIT_MSG', 'ignored-file'] |
| 479 | item = queue.enqueueChange(change) |
| 480 | item.current_build_set.layout = layout |
| 481 | |
| 482 | self.assertTrue(base.changeMatches(change)) |
| 483 | self.assertFalse(python27.changeMatches(change)) |
| 484 | |
| 485 | item.freezeJobTree() |
| 486 | self.assertEqual([], item.getJobs()) |
| 487 | |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 488 | def test_job_source_project(self): |
| 489 | layout = model.Layout() |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 490 | base_project = model.Project('base_project', None) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 491 | base_context = model.SourceContext(base_project, 'master', True) |
| 492 | |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 493 | base = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 494 | '_source_context': base_context, |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 495 | 'name': 'base', |
| 496 | }) |
| 497 | layout.addJob(base) |
| 498 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 499 | other_project = model.Project('other_project', None) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 500 | other_context = model.SourceContext(other_project, 'master', True) |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 501 | base2 = configloader.JobParser.fromYaml(layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 502 | '_source_context': other_context, |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 503 | 'name': 'base', |
| 504 | }) |
| 505 | with testtools.ExpectedException( |
| 506 | Exception, |
| 507 | "Job base in other_project is not permitted " |
| 508 | "to shadow job base in base_project"): |
| 509 | layout.addJob(base2) |
| 510 | |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 511 | |
| 512 | class TestJobTimeData(BaseTestCase): |
| 513 | def setUp(self): |
| 514 | super(TestJobTimeData, self).setUp() |
| 515 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 516 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 517 | ).path |
| 518 | |
| 519 | def test_empty_timedata(self): |
| 520 | path = os.path.join(self.tmp_root, 'job-name') |
| 521 | self.assertFalse(os.path.exists(path)) |
| 522 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 523 | td = model.JobTimeData(path) |
| 524 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 525 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 526 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 527 | |
| 528 | def test_save_reload(self): |
| 529 | path = os.path.join(self.tmp_root, 'job-name') |
| 530 | self.assertFalse(os.path.exists(path)) |
| 531 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 532 | td = model.JobTimeData(path) |
| 533 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 534 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 535 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 536 | success_times = [] |
| 537 | failure_times = [] |
| 538 | results = [] |
| 539 | for x in range(10): |
| 540 | success_times.append(int(random.random() * 1000)) |
| 541 | failure_times.append(int(random.random() * 1000)) |
| 542 | results.append(0) |
| 543 | results.append(1) |
| 544 | random.shuffle(results) |
| 545 | s = f = 0 |
| 546 | for result in results: |
| 547 | if result: |
| 548 | td.add(failure_times[f], 'FAILURE') |
| 549 | f += 1 |
| 550 | else: |
| 551 | td.add(success_times[s], 'SUCCESS') |
| 552 | s += 1 |
| 553 | self.assertEqual(td.success_times, success_times) |
| 554 | self.assertEqual(td.failure_times, failure_times) |
| 555 | self.assertEqual(td.results, results[10:]) |
| 556 | td.save() |
| 557 | self.assertTrue(os.path.exists(path)) |
| 558 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 559 | td = model.JobTimeData(path) |
| 560 | td.load() |
| 561 | self.assertEqual(td.success_times, success_times) |
| 562 | self.assertEqual(td.failure_times, failure_times) |
| 563 | self.assertEqual(td.results, results[10:]) |
| 564 | |
| 565 | |
| 566 | class TestTimeDataBase(BaseTestCase): |
| 567 | def setUp(self): |
| 568 | super(TestTimeDataBase, self).setUp() |
| 569 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 570 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 571 | ).path |
| 572 | self.db = model.TimeDataBase(self.tmp_root) |
| 573 | |
| 574 | def test_timedatabase(self): |
| 575 | self.assertEqual(self.db.getEstimatedTime('job-name'), 0) |
| 576 | self.db.update('job-name', 50, 'SUCCESS') |
| 577 | self.assertEqual(self.db.getEstimatedTime('job-name'), 50) |
| 578 | self.db.update('job-name', 100, 'SUCCESS') |
| 579 | self.assertEqual(self.db.getEstimatedTime('job-name'), 75) |
| 580 | for x in range(10): |
| 581 | self.db.update('job-name', 100, 'SUCCESS') |
| 582 | self.assertEqual(self.db.getEstimatedTime('job-name'), 100) |