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