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 | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 21 | import yaml |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 22 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 23 | from zuul import model |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 24 | from zuul import configloader |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 25 | from zuul.lib import encryption |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 26 | |
James E. Blair | 18f86a3 | 2017-03-15 14:43:26 -0700 | [diff] [blame] | 27 | from tests.base import BaseTestCase, FIXTURE_DIR |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 28 | |
| 29 | |
James E. Blair | b3f5db1 | 2017-03-17 12:57:39 -0700 | [diff] [blame^] | 30 | class FakeSource(object): |
| 31 | def __init__(self, name): |
| 32 | self.name = name |
| 33 | |
| 34 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 35 | class TestJob(BaseTestCase): |
| 36 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 37 | def setUp(self): |
| 38 | super(TestJob, self).setUp() |
James E. Blair | b3f5db1 | 2017-03-17 12:57:39 -0700 | [diff] [blame^] | 39 | self.tenant = model.Tenant('tenant') |
| 40 | self.layout = model.Layout() |
| 41 | self.project = model.Project('project', 'connection') |
| 42 | self.source = FakeSource('connection') |
| 43 | self.tenant.addProjectRepo(self.source, self.project) |
| 44 | self.pipeline = model.Pipeline('gate', self.layout) |
| 45 | self.layout.addPipeline(self.pipeline) |
| 46 | self.queue = model.ChangeQueue(self.pipeline) |
| 47 | |
James E. Blair | 18f86a3 | 2017-03-15 14:43:26 -0700 | [diff] [blame] | 48 | private_key_file = os.path.join(FIXTURE_DIR, 'private.pem') |
| 49 | with open(private_key_file, "rb") as f: |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 50 | self.project.private_key, self.project.public_key = \ |
| 51 | encryption.deserialize_rsa_keypair(f.read()) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 52 | self.context = model.SourceContext(self.project, 'master', |
| 53 | 'test', True) |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 54 | self.start_mark = yaml.Mark('name', 0, 0, 0, '', 0) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 55 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 56 | @property |
| 57 | def job(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 58 | tenant = model.Tenant('tenant') |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 59 | layout = model.Layout() |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 60 | job = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 61 | '_source_context': self.context, |
| 62 | '_start_mark': self.start_mark, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 63 | 'name': 'job', |
| 64 | 'irrelevant-files': [ |
| 65 | '^docs/.*$' |
| 66 | ]}) |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 67 | return job |
| 68 | |
| 69 | def test_change_matches_returns_false_for_matched_skip_if(self): |
| 70 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 71 | change.files = ['/COMMIT_MSG', 'docs/foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 72 | self.assertFalse(self.job.changeMatches(change)) |
| 73 | |
| 74 | def test_change_matches_returns_true_for_unmatched_skip_if(self): |
| 75 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 76 | change.files = ['/COMMIT_MSG', 'foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 77 | self.assertTrue(self.job.changeMatches(change)) |
| 78 | |
Maru Newby | 79427a4 | 2015-02-17 17:54:45 +0000 | [diff] [blame] | 79 | def test_job_sets_defaults_for_boolean_attributes(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 80 | self.assertIsNotNone(self.job.voting) |
| 81 | |
| 82 | def test_job_inheritance(self): |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 83 | # This is standard job inheritance. |
| 84 | |
| 85 | base_pre = model.PlaybookContext(self.context, 'base-pre') |
| 86 | base_run = model.PlaybookContext(self.context, 'base-run') |
| 87 | base_post = model.PlaybookContext(self.context, 'base-post') |
| 88 | |
| 89 | base = model.Job('base') |
| 90 | base.timeout = 30 |
| 91 | base.pre_run = [base_pre] |
| 92 | base.run = [base_run] |
| 93 | base.post_run = [base_post] |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 94 | base.auth = model.AuthContext() |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 95 | |
| 96 | py27 = model.Job('py27') |
| 97 | self.assertEqual(None, py27.timeout) |
| 98 | py27.inheritFrom(base) |
| 99 | self.assertEqual(30, py27.timeout) |
| 100 | self.assertEqual(['base-pre'], |
| 101 | [x.path for x in py27.pre_run]) |
| 102 | self.assertEqual(['base-run'], |
| 103 | [x.path for x in py27.run]) |
| 104 | self.assertEqual(['base-post'], |
| 105 | [x.path for x in py27.post_run]) |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 106 | self.assertEqual(None, py27.auth) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 107 | |
| 108 | def test_job_variants(self): |
| 109 | # This simulates freezing a job. |
| 110 | |
| 111 | py27_pre = model.PlaybookContext(self.context, 'py27-pre') |
| 112 | py27_run = model.PlaybookContext(self.context, 'py27-run') |
| 113 | py27_post = model.PlaybookContext(self.context, 'py27-post') |
| 114 | |
| 115 | py27 = model.Job('py27') |
| 116 | py27.timeout = 30 |
| 117 | py27.pre_run = [py27_pre] |
| 118 | py27.run = [py27_run] |
| 119 | py27.post_run = [py27_post] |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 120 | auth = model.AuthContext() |
| 121 | auth.secrets.append('foo') |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 122 | py27.auth = auth |
| 123 | |
| 124 | job = py27.copy() |
| 125 | self.assertEqual(30, job.timeout) |
| 126 | |
| 127 | # Apply the diablo variant |
| 128 | diablo = model.Job('py27') |
| 129 | diablo.timeout = 40 |
| 130 | job.applyVariant(diablo) |
| 131 | |
| 132 | self.assertEqual(40, job.timeout) |
| 133 | self.assertEqual(['py27-pre'], |
| 134 | [x.path for x in job.pre_run]) |
| 135 | self.assertEqual(['py27-run'], |
| 136 | [x.path for x in job.run]) |
| 137 | self.assertEqual(['py27-post'], |
| 138 | [x.path for x in job.post_run]) |
| 139 | self.assertEqual(auth, job.auth) |
| 140 | |
| 141 | # Set the job to final for the following checks |
| 142 | job.final = True |
| 143 | self.assertTrue(job.voting) |
| 144 | |
| 145 | good_final = model.Job('py27') |
| 146 | good_final.voting = False |
| 147 | job.applyVariant(good_final) |
| 148 | self.assertFalse(job.voting) |
| 149 | |
| 150 | bad_final = model.Job('py27') |
| 151 | bad_final.timeout = 600 |
| 152 | with testtools.ExpectedException( |
| 153 | Exception, |
| 154 | "Unable to modify final job"): |
| 155 | job.applyVariant(bad_final) |
| 156 | |
| 157 | def test_job_inheritance_configloader(self): |
| 158 | # TODO(jeblair): move this to a configloader test |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 159 | tenant = model.Tenant('tenant') |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 160 | layout = model.Layout() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 161 | |
| 162 | pipeline = model.Pipeline('gate', layout) |
| 163 | layout.addPipeline(pipeline) |
| 164 | queue = model.ChangeQueue(pipeline) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 165 | project = model.Project('project', None) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 166 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 167 | base = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 168 | '_source_context': self.context, |
| 169 | '_start_mark': self.start_mark, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 170 | 'name': 'base', |
| 171 | 'timeout': 30, |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 172 | 'pre-run': 'base-pre', |
| 173 | 'post-run': 'base-post', |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 174 | 'nodes': [{ |
| 175 | 'name': 'controller', |
| 176 | 'image': 'base', |
| 177 | }], |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 178 | }) |
| 179 | layout.addJob(base) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 180 | python27 = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 181 | '_source_context': self.context, |
| 182 | '_start_mark': self.start_mark, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 183 | 'name': 'python27', |
| 184 | 'parent': 'base', |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 185 | 'pre-run': 'py27-pre', |
| 186 | 'post-run': 'py27-post', |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 187 | 'nodes': [{ |
| 188 | 'name': 'controller', |
| 189 | 'image': 'new', |
| 190 | }], |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 191 | 'timeout': 40, |
| 192 | }) |
| 193 | layout.addJob(python27) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 194 | python27diablo = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 195 | '_source_context': self.context, |
| 196 | '_start_mark': self.start_mark, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 197 | 'name': 'python27', |
| 198 | 'branches': [ |
| 199 | 'stable/diablo' |
| 200 | ], |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 201 | 'pre-run': 'py27-diablo-pre', |
| 202 | 'run': 'py27-diablo', |
| 203 | 'post-run': 'py27-diablo-post', |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 204 | 'nodes': [{ |
| 205 | 'name': 'controller', |
| 206 | 'image': 'old', |
| 207 | }], |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 208 | 'timeout': 50, |
| 209 | }) |
| 210 | layout.addJob(python27diablo) |
| 211 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 212 | python27essex = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 213 | '_source_context': self.context, |
| 214 | '_start_mark': self.start_mark, |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 215 | 'name': 'python27', |
| 216 | 'branches': [ |
| 217 | 'stable/essex' |
| 218 | ], |
| 219 | 'pre-run': 'py27-essex-pre', |
| 220 | 'post-run': 'py27-essex-post', |
| 221 | }) |
| 222 | layout.addJob(python27essex) |
| 223 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 224 | project_config = configloader.ProjectParser.fromYaml(tenant, layout, [{ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 225 | '_source_context': self.context, |
| 226 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 227 | 'name': 'project', |
| 228 | 'gate': { |
| 229 | 'jobs': [ |
| 230 | 'python27' |
| 231 | ] |
| 232 | } |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 233 | }]) |
James E. Blair | f59f3cf | 2017-02-19 14:50:26 -0800 | [diff] [blame] | 234 | layout.addProjectConfig(project_config) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 235 | |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 236 | change = model.Change(project) |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 237 | # Test master |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 238 | change.branch = 'master' |
| 239 | item = queue.enqueueChange(change) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 240 | item.current_build_set.layout = layout |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 241 | |
| 242 | self.assertTrue(base.changeMatches(change)) |
| 243 | self.assertTrue(python27.changeMatches(change)) |
| 244 | self.assertFalse(python27diablo.changeMatches(change)) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 245 | self.assertFalse(python27essex.changeMatches(change)) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 246 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 247 | item.freezeJobGraph() |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 248 | self.assertEqual(len(item.getJobs()), 1) |
| 249 | job = item.getJobs()[0] |
| 250 | self.assertEqual(job.name, 'python27') |
| 251 | self.assertEqual(job.timeout, 40) |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 252 | nodes = job.nodeset.getNodes() |
| 253 | self.assertEqual(len(nodes), 1) |
| 254 | self.assertEqual(nodes[0].image, 'new') |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 255 | self.assertEqual([x.path for x in job.pre_run], |
| 256 | ['playbooks/base-pre', |
| 257 | 'playbooks/py27-pre']) |
| 258 | self.assertEqual([x.path for x in job.post_run], |
| 259 | ['playbooks/py27-post', |
| 260 | 'playbooks/base-post']) |
| 261 | self.assertEqual([x.path for x in job.run], |
| 262 | ['playbooks/python27', |
| 263 | 'playbooks/base']) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 264 | |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 265 | # Test diablo |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 266 | change.branch = 'stable/diablo' |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 267 | item = queue.enqueueChange(change) |
| 268 | item.current_build_set.layout = layout |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 269 | |
| 270 | self.assertTrue(base.changeMatches(change)) |
| 271 | self.assertTrue(python27.changeMatches(change)) |
| 272 | self.assertTrue(python27diablo.changeMatches(change)) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 273 | self.assertFalse(python27essex.changeMatches(change)) |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 274 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 275 | item.freezeJobGraph() |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 276 | self.assertEqual(len(item.getJobs()), 1) |
| 277 | job = item.getJobs()[0] |
| 278 | self.assertEqual(job.name, 'python27') |
| 279 | self.assertEqual(job.timeout, 50) |
James E. Blair | 1774dd5 | 2017-02-03 10:52:32 -0800 | [diff] [blame] | 280 | nodes = job.nodeset.getNodes() |
| 281 | self.assertEqual(len(nodes), 1) |
| 282 | self.assertEqual(nodes[0].image, 'old') |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 283 | self.assertEqual([x.path for x in job.pre_run], |
| 284 | ['playbooks/base-pre', |
| 285 | 'playbooks/py27-pre', |
| 286 | 'playbooks/py27-diablo-pre']) |
| 287 | self.assertEqual([x.path for x in job.post_run], |
| 288 | ['playbooks/py27-diablo-post', |
| 289 | 'playbooks/py27-post', |
| 290 | 'playbooks/base-post']) |
| 291 | self.assertEqual([x.path for x in job.run], |
| 292 | ['playbooks/py27-diablo']), |
| 293 | |
| 294 | # Test essex |
| 295 | change.branch = 'stable/essex' |
| 296 | item = queue.enqueueChange(change) |
| 297 | item.current_build_set.layout = layout |
| 298 | |
| 299 | self.assertTrue(base.changeMatches(change)) |
| 300 | self.assertTrue(python27.changeMatches(change)) |
| 301 | self.assertFalse(python27diablo.changeMatches(change)) |
| 302 | self.assertTrue(python27essex.changeMatches(change)) |
| 303 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 304 | item.freezeJobGraph() |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 305 | self.assertEqual(len(item.getJobs()), 1) |
| 306 | job = item.getJobs()[0] |
| 307 | self.assertEqual(job.name, 'python27') |
| 308 | self.assertEqual([x.path for x in job.pre_run], |
| 309 | ['playbooks/base-pre', |
| 310 | 'playbooks/py27-pre', |
| 311 | 'playbooks/py27-essex-pre']) |
| 312 | self.assertEqual([x.path for x in job.post_run], |
| 313 | ['playbooks/py27-essex-post', |
| 314 | 'playbooks/py27-post', |
| 315 | 'playbooks/base-post']) |
| 316 | self.assertEqual([x.path for x in job.run], |
| 317 | ['playbooks/python27', |
| 318 | 'playbooks/base']) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 319 | |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 320 | def test_job_auth_inheritance(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 321 | tenant = model.Tenant('tenant') |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 322 | layout = model.Layout() |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 323 | |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 324 | conf = yaml.safe_load(''' |
| 325 | - secret: |
| 326 | name: pypi-credentials |
| 327 | data: |
| 328 | username: test-username |
James E. Blair | 717e8e9 | 2017-03-17 11:03:27 -0700 | [diff] [blame] | 329 | password: !encrypted/pkcs1-oaep | |
James E. Blair | 01f83b7 | 2017-03-15 13:03:40 -0700 | [diff] [blame] | 330 | BFhtdnm8uXx7kn79RFL/zJywmzLkT1GY78P3bOtp4WghUFWobkifSu7ZpaV4NeO0s71YUsi1wGZZ |
| 331 | L0LveZjUN0t6OU1VZKSG8R5Ly7urjaSo1pPVIq5Rtt/H7W14Lecd+cUeKb4joeusC9drN3AA8a4o |
| 332 | ykcVpt1wVqUnTbMGC9ARMCQP6eopcs1l7tzMseprW4RDNhIuz3CRgd0QBMPl6VDoFgBPB8vxtJw+ |
| 333 | 3m0rqBYZCLZgCXekqlny8s2s92nJMuUABbJOEcDRarzibDsSXsfJt1y+5n7yOURsC7lovMg4GF/v |
| 334 | Cl/0YMKjBO5bpv9EM5fToeKYyPGSKQoHOnCYceb3cAVcv5UawcCic8XjhEhp4K7WPdYf2HVAC/qt |
| 335 | xhbpjTxG4U5Q/SoppOJ60WqEkQvbXs6n5Dvy7xmph6GWmU/bAv3eUK3pdD3xa2Ue1lHWz3U+rsYr |
| 336 | aI+AKYsMYx3RBlfAmCeC1ve2BXPrqnOo7G8tnUvfdYPbK4Aakk0ds/AVqFHEZN+S6hRBmBjLaRFW |
| 337 | Z3QSO1NjbBxWnaHKZYT7nkrJm8AMCgZU0ZArFLpaufKCeiK5ECSsDxic4FIsY1OkWT42qEUfL0Wd |
| 338 | +150AKGNZpPJnnP3QYY4W/MWcKH/zdO400+zWN52WevbSqZy90tqKDJrBkMl1ydqbuw1E4ZHvIs= |
| 339 | ''')[0]['secret'] |
| 340 | |
| 341 | conf['_source_context'] = self.context |
| 342 | conf['_start_mark'] = self.start_mark |
| 343 | |
| 344 | secret = configloader.SecretParser.fromYaml(layout, conf) |
| 345 | layout.addSecret(secret) |
| 346 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 347 | base = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 348 | '_source_context': self.context, |
| 349 | '_start_mark': self.start_mark, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 350 | 'name': 'base', |
| 351 | 'timeout': 30, |
| 352 | }) |
| 353 | layout.addJob(base) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 354 | pypi_upload_without_inherit = configloader.JobParser.fromYaml( |
| 355 | tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 356 | '_source_context': self.context, |
| 357 | '_start_mark': self.start_mark, |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 358 | 'name': 'pypi-upload-without-inherit', |
| 359 | 'parent': 'base', |
| 360 | 'timeout': 40, |
| 361 | 'auth': { |
| 362 | 'secrets': [ |
| 363 | 'pypi-credentials', |
| 364 | ] |
| 365 | } |
| 366 | }) |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 367 | layout.addJob(pypi_upload_without_inherit) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 368 | pypi_upload_with_inherit = configloader.JobParser.fromYaml( |
| 369 | tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 370 | '_source_context': self.context, |
| 371 | '_start_mark': self.start_mark, |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 372 | 'name': 'pypi-upload-with-inherit', |
| 373 | 'parent': 'base', |
| 374 | 'timeout': 40, |
| 375 | 'auth': { |
| 376 | 'inherit': True, |
| 377 | 'secrets': [ |
| 378 | 'pypi-credentials', |
| 379 | ] |
| 380 | } |
| 381 | }) |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 382 | layout.addJob(pypi_upload_with_inherit) |
| 383 | pypi_upload_with_inherit_false = configloader.JobParser.fromYaml( |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 384 | tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 385 | '_source_context': self.context, |
| 386 | '_start_mark': self.start_mark, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 387 | 'name': 'pypi-upload-with-inherit-false', |
| 388 | 'parent': 'base', |
| 389 | 'timeout': 40, |
| 390 | 'auth': { |
| 391 | 'inherit': False, |
Ricardo Carrillo Cruz | 12c892b | 2016-11-18 15:35:49 +0000 | [diff] [blame] | 392 | 'secrets': [ |
| 393 | 'pypi-credentials', |
| 394 | ] |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 395 | } |
| 396 | }) |
| 397 | layout.addJob(pypi_upload_with_inherit_false) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 398 | in_repo_job_without_inherit = configloader.JobParser.fromYaml( |
| 399 | tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 400 | '_source_context': self.context, |
| 401 | '_start_mark': self.start_mark, |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 402 | 'name': 'in-repo-job-without-inherit', |
| 403 | 'parent': 'pypi-upload-without-inherit', |
| 404 | }) |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 405 | layout.addJob(in_repo_job_without_inherit) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 406 | in_repo_job_with_inherit = configloader.JobParser.fromYaml( |
| 407 | tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 408 | '_source_context': self.context, |
| 409 | '_start_mark': self.start_mark, |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 410 | 'name': 'in-repo-job-with-inherit', |
| 411 | 'parent': 'pypi-upload-with-inherit', |
| 412 | }) |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 413 | layout.addJob(in_repo_job_with_inherit) |
| 414 | in_repo_job_with_inherit_false = configloader.JobParser.fromYaml( |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 415 | tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 416 | '_source_context': self.context, |
| 417 | '_start_mark': self.start_mark, |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 418 | 'name': 'in-repo-job-with-inherit-false', |
| 419 | 'parent': 'pypi-upload-with-inherit-false', |
| 420 | }) |
| 421 | layout.addJob(in_repo_job_with_inherit_false) |
| 422 | |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 423 | self.assertEqual(None, in_repo_job_without_inherit.auth) |
| 424 | self.assertEqual(1, len(in_repo_job_with_inherit.auth.secrets)) |
| 425 | self.assertEqual(in_repo_job_with_inherit.auth.secrets[0].name, |
| 426 | 'pypi-credentials') |
| 427 | self.assertEqual(None, in_repo_job_with_inherit_false.auth) |
Ricardo Carrillo Cruz | 4e94f61 | 2016-07-25 16:11:56 +0000 | [diff] [blame] | 428 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 429 | def test_job_inheritance_job_tree(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 430 | tenant = model.Tenant('tenant') |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 431 | layout = model.Layout() |
| 432 | |
| 433 | pipeline = model.Pipeline('gate', layout) |
| 434 | layout.addPipeline(pipeline) |
| 435 | queue = model.ChangeQueue(pipeline) |
| 436 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 437 | base = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 438 | '_source_context': self.context, |
| 439 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 440 | 'name': 'base', |
| 441 | 'timeout': 30, |
| 442 | }) |
| 443 | layout.addJob(base) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 444 | python27 = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 445 | '_source_context': self.context, |
| 446 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 447 | 'name': 'python27', |
| 448 | 'parent': 'base', |
| 449 | 'timeout': 40, |
| 450 | }) |
| 451 | layout.addJob(python27) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 452 | python27diablo = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 453 | '_source_context': self.context, |
| 454 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 455 | 'name': 'python27', |
| 456 | 'branches': [ |
| 457 | 'stable/diablo' |
| 458 | ], |
| 459 | 'timeout': 50, |
| 460 | }) |
| 461 | layout.addJob(python27diablo) |
| 462 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 463 | project_config = configloader.ProjectParser.fromYaml(tenant, layout, [{ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 464 | '_source_context': self.context, |
| 465 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 466 | 'name': 'project', |
| 467 | 'gate': { |
| 468 | 'jobs': [ |
| 469 | {'python27': {'timeout': 70}} |
| 470 | ] |
| 471 | } |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 472 | }]) |
James E. Blair | f59f3cf | 2017-02-19 14:50:26 -0800 | [diff] [blame] | 473 | layout.addProjectConfig(project_config) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 474 | |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 475 | change = model.Change(self.project) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 476 | change.branch = 'master' |
| 477 | item = queue.enqueueChange(change) |
| 478 | item.current_build_set.layout = layout |
| 479 | |
| 480 | self.assertTrue(base.changeMatches(change)) |
| 481 | self.assertTrue(python27.changeMatches(change)) |
| 482 | self.assertFalse(python27diablo.changeMatches(change)) |
| 483 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 484 | item.freezeJobGraph() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 485 | self.assertEqual(len(item.getJobs()), 1) |
| 486 | job = item.getJobs()[0] |
| 487 | self.assertEqual(job.name, 'python27') |
| 488 | self.assertEqual(job.timeout, 70) |
| 489 | |
| 490 | change.branch = 'stable/diablo' |
| 491 | item = queue.enqueueChange(change) |
| 492 | item.current_build_set.layout = layout |
| 493 | |
| 494 | self.assertTrue(base.changeMatches(change)) |
| 495 | self.assertTrue(python27.changeMatches(change)) |
| 496 | self.assertTrue(python27diablo.changeMatches(change)) |
| 497 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 498 | item.freezeJobGraph() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 499 | self.assertEqual(len(item.getJobs()), 1) |
| 500 | job = item.getJobs()[0] |
| 501 | self.assertEqual(job.name, 'python27') |
| 502 | self.assertEqual(job.timeout, 70) |
| 503 | |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 504 | def test_inheritance_keeps_matchers(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 505 | tenant = model.Tenant('tenant') |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 506 | layout = model.Layout() |
| 507 | |
| 508 | pipeline = model.Pipeline('gate', layout) |
| 509 | layout.addPipeline(pipeline) |
| 510 | queue = model.ChangeQueue(pipeline) |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 511 | project = model.Project('project', None) |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 512 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 513 | base = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 514 | '_source_context': self.context, |
| 515 | '_start_mark': self.start_mark, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 516 | 'name': 'base', |
| 517 | 'timeout': 30, |
| 518 | }) |
| 519 | layout.addJob(base) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 520 | python27 = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 521 | '_source_context': self.context, |
| 522 | '_start_mark': self.start_mark, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 523 | 'name': 'python27', |
| 524 | 'parent': 'base', |
| 525 | 'timeout': 40, |
| 526 | 'irrelevant-files': ['^ignored-file$'], |
| 527 | }) |
| 528 | layout.addJob(python27) |
| 529 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 530 | project_config = configloader.ProjectParser.fromYaml(tenant, layout, [{ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 531 | '_source_context': self.context, |
| 532 | '_start_mark': self.start_mark, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 533 | 'name': 'project', |
| 534 | 'gate': { |
| 535 | 'jobs': [ |
| 536 | 'python27', |
| 537 | ] |
| 538 | } |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 539 | }]) |
James E. Blair | f59f3cf | 2017-02-19 14:50:26 -0800 | [diff] [blame] | 540 | layout.addProjectConfig(project_config) |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 541 | |
| 542 | change = model.Change(project) |
| 543 | change.branch = 'master' |
| 544 | change.files = ['/COMMIT_MSG', 'ignored-file'] |
| 545 | item = queue.enqueueChange(change) |
| 546 | item.current_build_set.layout = layout |
| 547 | |
| 548 | self.assertTrue(base.changeMatches(change)) |
| 549 | self.assertFalse(python27.changeMatches(change)) |
| 550 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 551 | item.freezeJobGraph() |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 552 | self.assertEqual([], item.getJobs()) |
| 553 | |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 554 | def test_job_source_project(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 555 | tenant = model.Tenant('tenant') |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 556 | layout = model.Layout() |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 557 | base_project = model.Project('base_project', None) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 558 | base_context = model.SourceContext(base_project, 'master', |
| 559 | 'test', True) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 560 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 561 | base = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 562 | '_source_context': base_context, |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 563 | '_start_mark': self.start_mark, |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 564 | 'name': 'base', |
| 565 | }) |
| 566 | layout.addJob(base) |
| 567 | |
James E. Blair | c73c73a | 2017-01-20 15:15:15 -0800 | [diff] [blame] | 568 | other_project = model.Project('other_project', None) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 569 | other_context = model.SourceContext(other_project, 'master', |
| 570 | 'test', True) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 571 | base2 = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 572 | '_source_context': other_context, |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 573 | '_start_mark': self.start_mark, |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 574 | 'name': 'base', |
| 575 | }) |
| 576 | with testtools.ExpectedException( |
| 577 | Exception, |
| 578 | "Job base in other_project is not permitted " |
| 579 | "to shadow job base in base_project"): |
| 580 | layout.addJob(base2) |
| 581 | |
James E. Blair | b3f5db1 | 2017-03-17 12:57:39 -0700 | [diff] [blame^] | 582 | def test_job_allowed_projects(self): |
| 583 | job = configloader.JobParser.fromYaml(self.tenant, self.layout, { |
| 584 | '_source_context': self.context, |
| 585 | '_start_mark': self.start_mark, |
| 586 | 'name': 'job', |
| 587 | 'allowed-projects': ['project'], |
| 588 | }) |
| 589 | self.layout.addJob(job) |
| 590 | |
| 591 | project2 = model.Project('project2', None) |
| 592 | context2 = model.SourceContext(project2, 'master', |
| 593 | 'test', True) |
| 594 | |
| 595 | project2_config = configloader.ProjectParser.fromYaml( |
| 596 | self.tenant, self.layout, [{ |
| 597 | '_source_context': context2, |
| 598 | '_start_mark': self.start_mark, |
| 599 | 'name': 'project2', |
| 600 | 'gate': { |
| 601 | 'jobs': [ |
| 602 | 'job' |
| 603 | ] |
| 604 | } |
| 605 | }] |
| 606 | ) |
| 607 | self.layout.addProjectConfig(project2_config) |
| 608 | |
| 609 | change = model.Change(project2) |
| 610 | # Test master |
| 611 | change.branch = 'master' |
| 612 | item = self.queue.enqueueChange(change) |
| 613 | item.current_build_set.layout = self.layout |
| 614 | with testtools.ExpectedException( |
| 615 | Exception, |
| 616 | "Project project2 is not allowed to run job job"): |
| 617 | item.freezeJobGraph() |
| 618 | |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 619 | |
| 620 | class TestJobTimeData(BaseTestCase): |
| 621 | def setUp(self): |
| 622 | super(TestJobTimeData, self).setUp() |
| 623 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 624 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 625 | ).path |
| 626 | |
| 627 | def test_empty_timedata(self): |
| 628 | path = os.path.join(self.tmp_root, 'job-name') |
| 629 | self.assertFalse(os.path.exists(path)) |
| 630 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 631 | td = model.JobTimeData(path) |
| 632 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 633 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 634 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 635 | |
| 636 | def test_save_reload(self): |
| 637 | path = os.path.join(self.tmp_root, 'job-name') |
| 638 | self.assertFalse(os.path.exists(path)) |
| 639 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 640 | td = model.JobTimeData(path) |
| 641 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 642 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 643 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 644 | success_times = [] |
| 645 | failure_times = [] |
| 646 | results = [] |
| 647 | for x in range(10): |
| 648 | success_times.append(int(random.random() * 1000)) |
| 649 | failure_times.append(int(random.random() * 1000)) |
| 650 | results.append(0) |
| 651 | results.append(1) |
| 652 | random.shuffle(results) |
| 653 | s = f = 0 |
| 654 | for result in results: |
| 655 | if result: |
| 656 | td.add(failure_times[f], 'FAILURE') |
| 657 | f += 1 |
| 658 | else: |
| 659 | td.add(success_times[s], 'SUCCESS') |
| 660 | s += 1 |
| 661 | self.assertEqual(td.success_times, success_times) |
| 662 | self.assertEqual(td.failure_times, failure_times) |
| 663 | self.assertEqual(td.results, results[10:]) |
| 664 | td.save() |
| 665 | self.assertTrue(os.path.exists(path)) |
| 666 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 667 | td = model.JobTimeData(path) |
| 668 | td.load() |
| 669 | self.assertEqual(td.success_times, success_times) |
| 670 | self.assertEqual(td.failure_times, failure_times) |
| 671 | self.assertEqual(td.results, results[10:]) |
| 672 | |
| 673 | |
| 674 | class TestTimeDataBase(BaseTestCase): |
| 675 | def setUp(self): |
| 676 | super(TestTimeDataBase, self).setUp() |
| 677 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 678 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 679 | ).path |
| 680 | self.db = model.TimeDataBase(self.tmp_root) |
| 681 | |
| 682 | def test_timedatabase(self): |
| 683 | self.assertEqual(self.db.getEstimatedTime('job-name'), 0) |
| 684 | self.db.update('job-name', 50, 'SUCCESS') |
| 685 | self.assertEqual(self.db.getEstimatedTime('job-name'), 50) |
| 686 | self.db.update('job-name', 100, 'SUCCESS') |
| 687 | self.assertEqual(self.db.getEstimatedTime('job-name'), 75) |
| 688 | for x in range(10): |
| 689 | self.db.update('job-name', 100, 'SUCCESS') |
| 690 | self.assertEqual(self.db.getEstimatedTime('job-name'), 100) |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 691 | |
| 692 | |
| 693 | class TestGraph(BaseTestCase): |
| 694 | def test_job_graph_disallows_multiple_jobs_with_same_name(self): |
| 695 | graph = model.JobGraph() |
| 696 | job1 = model.Job('job') |
| 697 | job2 = model.Job('job') |
| 698 | graph.addJob(job1) |
| 699 | with testtools.ExpectedException(Exception, |
| 700 | "Job job already added"): |
| 701 | graph.addJob(job2) |
| 702 | |
| 703 | def test_job_graph_disallows_circular_dependencies(self): |
| 704 | graph = model.JobGraph() |
| 705 | jobs = [model.Job('job%d' % i) for i in range(0, 10)] |
| 706 | prevjob = None |
| 707 | for j in jobs[:3]: |
| 708 | if prevjob: |
| 709 | j.dependencies = frozenset([prevjob.name]) |
| 710 | graph.addJob(j) |
| 711 | prevjob = j |
| 712 | # 0 triggers 1 triggers 2 triggers 3... |
| 713 | |
| 714 | # Cannot depend on itself |
| 715 | with testtools.ExpectedException( |
| 716 | Exception, |
| 717 | "Dependency cycle detected in job jobX"): |
| 718 | j = model.Job('jobX') |
| 719 | j.dependencies = frozenset([j.name]) |
| 720 | graph.addJob(j) |
| 721 | |
| 722 | # Disallow circular dependencies |
| 723 | with testtools.ExpectedException( |
| 724 | Exception, |
| 725 | "Dependency cycle detected in job job3"): |
| 726 | jobs[4].dependencies = frozenset([jobs[3].name]) |
| 727 | graph.addJob(jobs[4]) |
| 728 | jobs[3].dependencies = frozenset([jobs[4].name]) |
| 729 | graph.addJob(jobs[3]) |
| 730 | |
| 731 | jobs[5].dependencies = frozenset([jobs[4].name]) |
| 732 | graph.addJob(jobs[5]) |
| 733 | |
| 734 | with testtools.ExpectedException( |
| 735 | Exception, |
| 736 | "Dependency cycle detected in job job3"): |
| 737 | jobs[3].dependencies = frozenset([jobs[5].name]) |
| 738 | graph.addJob(jobs[3]) |
| 739 | |
| 740 | jobs[3].dependencies = frozenset([jobs[2].name]) |
| 741 | graph.addJob(jobs[3]) |
| 742 | jobs[6].dependencies = frozenset([jobs[2].name]) |
| 743 | graph.addJob(jobs[6]) |