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