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', |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 41 | connection=self.connection) |
James E. Blair | b3f5db1 | 2017-03-17 12:57:39 -0700 | [diff] [blame] | 42 | self.tenant = model.Tenant('tenant') |
| 43 | self.layout = model.Layout() |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 44 | self.project = model.Project('project', self.source) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 45 | self.tenant.addUntrustedProject(self.project) |
James E. Blair | b3f5db1 | 2017-03-17 12:57:39 -0700 | [diff] [blame] | 46 | self.pipeline = model.Pipeline('gate', self.layout) |
| 47 | self.layout.addPipeline(self.pipeline) |
| 48 | self.queue = model.ChangeQueue(self.pipeline) |
| 49 | |
James E. Blair | 18f86a3 | 2017-03-15 14:43:26 -0700 | [diff] [blame] | 50 | private_key_file = os.path.join(FIXTURE_DIR, 'private.pem') |
| 51 | with open(private_key_file, "rb") as f: |
James E. Blair | bf1a4f2 | 2017-03-17 10:59:37 -0700 | [diff] [blame] | 52 | self.project.private_key, self.project.public_key = \ |
| 53 | encryption.deserialize_rsa_keypair(f.read()) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 54 | self.context = model.SourceContext(self.project, 'master', |
| 55 | 'test', True) |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 56 | self.start_mark = yaml.Mark('name', 0, 0, 0, '', 0) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 57 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 58 | @property |
| 59 | def job(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 60 | tenant = model.Tenant('tenant') |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 61 | layout = model.Layout() |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 62 | job = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 63 | '_source_context': self.context, |
| 64 | '_start_mark': self.start_mark, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 65 | 'name': 'job', |
| 66 | 'irrelevant-files': [ |
| 67 | '^docs/.*$' |
| 68 | ]}) |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 69 | return job |
| 70 | |
| 71 | def test_change_matches_returns_false_for_matched_skip_if(self): |
| 72 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 73 | change.files = ['/COMMIT_MSG', 'docs/foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 74 | self.assertFalse(self.job.changeMatches(change)) |
| 75 | |
| 76 | def test_change_matches_returns_true_for_unmatched_skip_if(self): |
| 77 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 78 | change.files = ['/COMMIT_MSG', 'foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 79 | self.assertTrue(self.job.changeMatches(change)) |
| 80 | |
Maru Newby | 79427a4 | 2015-02-17 17:54:45 +0000 | [diff] [blame] | 81 | def test_job_sets_defaults_for_boolean_attributes(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 82 | self.assertIsNotNone(self.job.voting) |
| 83 | |
| 84 | def test_job_inheritance(self): |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 85 | # This is standard job inheritance. |
| 86 | |
| 87 | base_pre = model.PlaybookContext(self.context, 'base-pre') |
| 88 | base_run = model.PlaybookContext(self.context, 'base-run') |
| 89 | base_post = model.PlaybookContext(self.context, 'base-post') |
| 90 | |
| 91 | base = model.Job('base') |
| 92 | base.timeout = 30 |
| 93 | base.pre_run = [base_pre] |
| 94 | base.run = [base_run] |
| 95 | base.post_run = [base_post] |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 96 | base.auth = model.AuthContext() |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 97 | |
| 98 | py27 = model.Job('py27') |
| 99 | self.assertEqual(None, py27.timeout) |
| 100 | py27.inheritFrom(base) |
| 101 | self.assertEqual(30, py27.timeout) |
| 102 | self.assertEqual(['base-pre'], |
| 103 | [x.path for x in py27.pre_run]) |
| 104 | self.assertEqual(['base-run'], |
| 105 | [x.path for x in py27.run]) |
| 106 | self.assertEqual(['base-post'], |
| 107 | [x.path for x in py27.post_run]) |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 108 | self.assertEqual(None, py27.auth) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 109 | |
| 110 | def test_job_variants(self): |
| 111 | # This simulates freezing a job. |
| 112 | |
| 113 | py27_pre = model.PlaybookContext(self.context, 'py27-pre') |
| 114 | py27_run = model.PlaybookContext(self.context, 'py27-run') |
| 115 | py27_post = model.PlaybookContext(self.context, 'py27-post') |
| 116 | |
| 117 | py27 = model.Job('py27') |
| 118 | py27.timeout = 30 |
| 119 | py27.pre_run = [py27_pre] |
| 120 | py27.run = [py27_run] |
| 121 | py27.post_run = [py27_post] |
James E. Blair | 8525e2b | 2017-03-15 14:05:47 -0700 | [diff] [blame] | 122 | auth = model.AuthContext() |
| 123 | auth.secrets.append('foo') |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 124 | py27.auth = auth |
| 125 | |
| 126 | job = py27.copy() |
| 127 | self.assertEqual(30, job.timeout) |
| 128 | |
| 129 | # Apply the diablo variant |
| 130 | diablo = model.Job('py27') |
| 131 | diablo.timeout = 40 |
| 132 | job.applyVariant(diablo) |
| 133 | |
| 134 | self.assertEqual(40, job.timeout) |
| 135 | self.assertEqual(['py27-pre'], |
| 136 | [x.path for x in job.pre_run]) |
| 137 | self.assertEqual(['py27-run'], |
| 138 | [x.path for x in job.run]) |
| 139 | self.assertEqual(['py27-post'], |
| 140 | [x.path for x in job.post_run]) |
| 141 | self.assertEqual(auth, job.auth) |
| 142 | |
| 143 | # Set the job to final for the following checks |
| 144 | job.final = True |
| 145 | self.assertTrue(job.voting) |
| 146 | |
| 147 | good_final = model.Job('py27') |
| 148 | good_final.voting = False |
| 149 | job.applyVariant(good_final) |
| 150 | self.assertFalse(job.voting) |
| 151 | |
| 152 | bad_final = model.Job('py27') |
| 153 | bad_final.timeout = 600 |
| 154 | with testtools.ExpectedException( |
| 155 | Exception, |
| 156 | "Unable to modify final job"): |
| 157 | job.applyVariant(bad_final) |
| 158 | |
| 159 | def test_job_inheritance_configloader(self): |
| 160 | # TODO(jeblair): move this to a configloader test |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 161 | tenant = model.Tenant('tenant') |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 162 | layout = model.Layout() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 163 | |
| 164 | pipeline = model.Pipeline('gate', layout) |
| 165 | layout.addPipeline(pipeline) |
| 166 | queue = model.ChangeQueue(pipeline) |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 167 | project = model.Project('project', self.source) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 168 | tenant.addUntrustedProject(project) |
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() |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 435 | tenant.addUntrustedProject(self.project) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 436 | |
| 437 | pipeline = model.Pipeline('gate', layout) |
| 438 | layout.addPipeline(pipeline) |
| 439 | queue = model.ChangeQueue(pipeline) |
| 440 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 441 | base = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 442 | '_source_context': self.context, |
| 443 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 444 | 'name': 'base', |
| 445 | 'timeout': 30, |
| 446 | }) |
| 447 | layout.addJob(base) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 448 | python27 = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 449 | '_source_context': self.context, |
| 450 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 451 | 'name': 'python27', |
| 452 | 'parent': 'base', |
| 453 | 'timeout': 40, |
| 454 | }) |
| 455 | layout.addJob(python27) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 456 | python27diablo = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 457 | '_source_context': self.context, |
| 458 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 459 | 'name': 'python27', |
| 460 | 'branches': [ |
| 461 | 'stable/diablo' |
| 462 | ], |
| 463 | 'timeout': 50, |
| 464 | }) |
| 465 | layout.addJob(python27diablo) |
| 466 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 467 | project_config = configloader.ProjectParser.fromYaml(tenant, layout, [{ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 468 | '_source_context': self.context, |
| 469 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 470 | 'name': 'project', |
| 471 | 'gate': { |
| 472 | 'jobs': [ |
| 473 | {'python27': {'timeout': 70}} |
| 474 | ] |
| 475 | } |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 476 | }]) |
James E. Blair | f59f3cf | 2017-02-19 14:50:26 -0800 | [diff] [blame] | 477 | layout.addProjectConfig(project_config) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 478 | |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 479 | change = model.Change(self.project) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 480 | change.branch = 'master' |
| 481 | item = queue.enqueueChange(change) |
| 482 | item.current_build_set.layout = layout |
| 483 | |
| 484 | self.assertTrue(base.changeMatches(change)) |
| 485 | self.assertTrue(python27.changeMatches(change)) |
| 486 | self.assertFalse(python27diablo.changeMatches(change)) |
| 487 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 488 | item.freezeJobGraph() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 489 | self.assertEqual(len(item.getJobs()), 1) |
| 490 | job = item.getJobs()[0] |
| 491 | self.assertEqual(job.name, 'python27') |
| 492 | self.assertEqual(job.timeout, 70) |
| 493 | |
| 494 | change.branch = 'stable/diablo' |
| 495 | item = queue.enqueueChange(change) |
| 496 | item.current_build_set.layout = layout |
| 497 | |
| 498 | self.assertTrue(base.changeMatches(change)) |
| 499 | self.assertTrue(python27.changeMatches(change)) |
| 500 | self.assertTrue(python27diablo.changeMatches(change)) |
| 501 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 502 | item.freezeJobGraph() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 503 | self.assertEqual(len(item.getJobs()), 1) |
| 504 | job = item.getJobs()[0] |
| 505 | self.assertEqual(job.name, 'python27') |
| 506 | self.assertEqual(job.timeout, 70) |
| 507 | |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 508 | def test_inheritance_keeps_matchers(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 509 | tenant = model.Tenant('tenant') |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 510 | layout = model.Layout() |
| 511 | |
| 512 | pipeline = model.Pipeline('gate', layout) |
| 513 | layout.addPipeline(pipeline) |
| 514 | queue = model.ChangeQueue(pipeline) |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 515 | project = model.Project('project', self.source) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 516 | tenant.addUntrustedProject(project) |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 517 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 518 | base = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 519 | '_source_context': self.context, |
| 520 | '_start_mark': self.start_mark, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 521 | 'name': 'base', |
| 522 | 'timeout': 30, |
| 523 | }) |
| 524 | layout.addJob(base) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 525 | python27 = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 526 | '_source_context': self.context, |
| 527 | '_start_mark': self.start_mark, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 528 | 'name': 'python27', |
| 529 | 'parent': 'base', |
| 530 | 'timeout': 40, |
| 531 | 'irrelevant-files': ['^ignored-file$'], |
| 532 | }) |
| 533 | layout.addJob(python27) |
| 534 | |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 535 | project_config = configloader.ProjectParser.fromYaml(tenant, layout, [{ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 536 | '_source_context': self.context, |
| 537 | '_start_mark': self.start_mark, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 538 | 'name': 'project', |
| 539 | 'gate': { |
| 540 | 'jobs': [ |
| 541 | 'python27', |
| 542 | ] |
| 543 | } |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 544 | }]) |
James E. Blair | f59f3cf | 2017-02-19 14:50:26 -0800 | [diff] [blame] | 545 | layout.addProjectConfig(project_config) |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 546 | |
| 547 | change = model.Change(project) |
| 548 | change.branch = 'master' |
| 549 | change.files = ['/COMMIT_MSG', 'ignored-file'] |
| 550 | item = queue.enqueueChange(change) |
| 551 | item.current_build_set.layout = layout |
| 552 | |
| 553 | self.assertTrue(base.changeMatches(change)) |
| 554 | self.assertFalse(python27.changeMatches(change)) |
| 555 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 556 | item.freezeJobGraph() |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 557 | self.assertEqual([], item.getJobs()) |
| 558 | |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 559 | def test_job_source_project(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 560 | tenant = model.Tenant('tenant') |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 561 | layout = model.Layout() |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 562 | base_project = model.Project('base_project', self.source) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 563 | base_context = model.SourceContext(base_project, 'master', |
| 564 | 'test', True) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 565 | |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 566 | base = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 567 | '_source_context': base_context, |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 568 | '_start_mark': self.start_mark, |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 569 | 'name': 'base', |
| 570 | }) |
| 571 | layout.addJob(base) |
| 572 | |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 573 | other_project = model.Project('other_project', self.source) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 574 | other_context = model.SourceContext(other_project, 'master', |
| 575 | 'test', True) |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 576 | base2 = configloader.JobParser.fromYaml(tenant, layout, { |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 577 | '_source_context': other_context, |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 578 | '_start_mark': self.start_mark, |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 579 | 'name': 'base', |
| 580 | }) |
| 581 | with testtools.ExpectedException( |
| 582 | Exception, |
| 583 | "Job base in other_project is not permitted " |
| 584 | "to shadow job base in base_project"): |
| 585 | layout.addJob(base2) |
| 586 | |
James E. Blair | b3f5db1 | 2017-03-17 12:57:39 -0700 | [diff] [blame] | 587 | def test_job_allowed_projects(self): |
| 588 | job = configloader.JobParser.fromYaml(self.tenant, self.layout, { |
| 589 | '_source_context': self.context, |
| 590 | '_start_mark': self.start_mark, |
| 591 | 'name': 'job', |
| 592 | 'allowed-projects': ['project'], |
| 593 | }) |
| 594 | self.layout.addJob(job) |
| 595 | |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 596 | project2 = model.Project('project2', self.source) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 597 | self.tenant.addUntrustedProject(project2) |
James E. Blair | b3f5db1 | 2017-03-17 12:57:39 -0700 | [diff] [blame] | 598 | context2 = model.SourceContext(project2, 'master', |
| 599 | 'test', True) |
| 600 | |
| 601 | project2_config = configloader.ProjectParser.fromYaml( |
| 602 | self.tenant, self.layout, [{ |
| 603 | '_source_context': context2, |
| 604 | '_start_mark': self.start_mark, |
| 605 | 'name': 'project2', |
| 606 | 'gate': { |
| 607 | 'jobs': [ |
| 608 | 'job' |
| 609 | ] |
| 610 | } |
| 611 | }] |
| 612 | ) |
| 613 | self.layout.addProjectConfig(project2_config) |
| 614 | |
| 615 | change = model.Change(project2) |
| 616 | # Test master |
| 617 | change.branch = 'master' |
| 618 | item = self.queue.enqueueChange(change) |
| 619 | item.current_build_set.layout = self.layout |
| 620 | with testtools.ExpectedException( |
| 621 | Exception, |
| 622 | "Project project2 is not allowed to run job job"): |
| 623 | item.freezeJobGraph() |
| 624 | |
James E. Blair | d234836 | 2017-03-17 13:59:35 -0700 | [diff] [blame] | 625 | def test_job_pipeline_allow_secrets(self): |
| 626 | self.pipeline.allow_secrets = False |
| 627 | job = configloader.JobParser.fromYaml(self.tenant, self.layout, { |
| 628 | '_source_context': self.context, |
| 629 | '_start_mark': self.start_mark, |
| 630 | 'name': 'job', |
| 631 | }) |
| 632 | auth = model.AuthContext() |
| 633 | auth.secrets.append('foo') |
| 634 | job.auth = auth |
| 635 | |
| 636 | self.layout.addJob(job) |
| 637 | |
| 638 | project_config = configloader.ProjectParser.fromYaml( |
| 639 | self.tenant, self.layout, [{ |
| 640 | '_source_context': self.context, |
| 641 | '_start_mark': self.start_mark, |
| 642 | 'name': 'project', |
| 643 | 'gate': { |
| 644 | 'jobs': [ |
| 645 | 'job' |
| 646 | ] |
| 647 | } |
| 648 | }] |
| 649 | ) |
| 650 | self.layout.addProjectConfig(project_config) |
| 651 | |
| 652 | change = model.Change(self.project) |
| 653 | # Test master |
| 654 | change.branch = 'master' |
| 655 | item = self.queue.enqueueChange(change) |
| 656 | item.current_build_set.layout = self.layout |
| 657 | with testtools.ExpectedException( |
| 658 | Exception, |
| 659 | "Pipeline gate does not allow jobs with secrets"): |
| 660 | item.freezeJobGraph() |
| 661 | |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 662 | |
| 663 | class TestJobTimeData(BaseTestCase): |
| 664 | def setUp(self): |
| 665 | super(TestJobTimeData, self).setUp() |
| 666 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 667 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 668 | ).path |
| 669 | |
| 670 | def test_empty_timedata(self): |
| 671 | path = os.path.join(self.tmp_root, 'job-name') |
| 672 | self.assertFalse(os.path.exists(path)) |
| 673 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 674 | td = model.JobTimeData(path) |
| 675 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 676 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 677 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 678 | |
| 679 | def test_save_reload(self): |
| 680 | path = os.path.join(self.tmp_root, 'job-name') |
| 681 | self.assertFalse(os.path.exists(path)) |
| 682 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 683 | td = model.JobTimeData(path) |
| 684 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 685 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 686 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 687 | success_times = [] |
| 688 | failure_times = [] |
| 689 | results = [] |
| 690 | for x in range(10): |
| 691 | success_times.append(int(random.random() * 1000)) |
| 692 | failure_times.append(int(random.random() * 1000)) |
| 693 | results.append(0) |
| 694 | results.append(1) |
| 695 | random.shuffle(results) |
| 696 | s = f = 0 |
| 697 | for result in results: |
| 698 | if result: |
| 699 | td.add(failure_times[f], 'FAILURE') |
| 700 | f += 1 |
| 701 | else: |
| 702 | td.add(success_times[s], 'SUCCESS') |
| 703 | s += 1 |
| 704 | self.assertEqual(td.success_times, success_times) |
| 705 | self.assertEqual(td.failure_times, failure_times) |
| 706 | self.assertEqual(td.results, results[10:]) |
| 707 | td.save() |
| 708 | self.assertTrue(os.path.exists(path)) |
| 709 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 710 | td = model.JobTimeData(path) |
| 711 | td.load() |
| 712 | self.assertEqual(td.success_times, success_times) |
| 713 | self.assertEqual(td.failure_times, failure_times) |
| 714 | self.assertEqual(td.results, results[10:]) |
| 715 | |
| 716 | |
| 717 | class TestTimeDataBase(BaseTestCase): |
| 718 | def setUp(self): |
| 719 | super(TestTimeDataBase, self).setUp() |
| 720 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 721 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 722 | ).path |
| 723 | self.db = model.TimeDataBase(self.tmp_root) |
| 724 | |
| 725 | def test_timedatabase(self): |
| 726 | self.assertEqual(self.db.getEstimatedTime('job-name'), 0) |
| 727 | self.db.update('job-name', 50, 'SUCCESS') |
| 728 | self.assertEqual(self.db.getEstimatedTime('job-name'), 50) |
| 729 | self.db.update('job-name', 100, 'SUCCESS') |
| 730 | self.assertEqual(self.db.getEstimatedTime('job-name'), 75) |
| 731 | for x in range(10): |
| 732 | self.db.update('job-name', 100, 'SUCCESS') |
| 733 | self.assertEqual(self.db.getEstimatedTime('job-name'), 100) |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 734 | |
| 735 | |
| 736 | class TestGraph(BaseTestCase): |
| 737 | def test_job_graph_disallows_multiple_jobs_with_same_name(self): |
| 738 | graph = model.JobGraph() |
| 739 | job1 = model.Job('job') |
| 740 | job2 = model.Job('job') |
| 741 | graph.addJob(job1) |
| 742 | with testtools.ExpectedException(Exception, |
| 743 | "Job job already added"): |
| 744 | graph.addJob(job2) |
| 745 | |
| 746 | def test_job_graph_disallows_circular_dependencies(self): |
| 747 | graph = model.JobGraph() |
| 748 | jobs = [model.Job('job%d' % i) for i in range(0, 10)] |
| 749 | prevjob = None |
| 750 | for j in jobs[:3]: |
| 751 | if prevjob: |
| 752 | j.dependencies = frozenset([prevjob.name]) |
| 753 | graph.addJob(j) |
| 754 | prevjob = j |
| 755 | # 0 triggers 1 triggers 2 triggers 3... |
| 756 | |
| 757 | # Cannot depend on itself |
| 758 | with testtools.ExpectedException( |
| 759 | Exception, |
| 760 | "Dependency cycle detected in job jobX"): |
| 761 | j = model.Job('jobX') |
| 762 | j.dependencies = frozenset([j.name]) |
| 763 | graph.addJob(j) |
| 764 | |
| 765 | # Disallow circular dependencies |
| 766 | with testtools.ExpectedException( |
| 767 | Exception, |
| 768 | "Dependency cycle detected in job job3"): |
| 769 | jobs[4].dependencies = frozenset([jobs[3].name]) |
| 770 | graph.addJob(jobs[4]) |
| 771 | jobs[3].dependencies = frozenset([jobs[4].name]) |
| 772 | graph.addJob(jobs[3]) |
| 773 | |
| 774 | jobs[5].dependencies = frozenset([jobs[4].name]) |
| 775 | graph.addJob(jobs[5]) |
| 776 | |
| 777 | with testtools.ExpectedException( |
| 778 | Exception, |
| 779 | "Dependency cycle detected in job job3"): |
| 780 | jobs[3].dependencies = frozenset([jobs[5].name]) |
| 781 | graph.addJob(jobs[3]) |
| 782 | |
| 783 | jobs[3].dependencies = frozenset([jobs[2].name]) |
| 784 | graph.addJob(jobs[3]) |
| 785 | jobs[6].dependencies = frozenset([jobs[2].name]) |
| 786 | graph.addJob(jobs[6]) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 787 | |
| 788 | |
| 789 | class TestTenant(BaseTestCase): |
| 790 | def test_add_project(self): |
| 791 | tenant = model.Tenant('tenant') |
| 792 | connection1 = Dummy(connection_name='dummy_connection1') |
| 793 | source1 = Dummy(canonical_hostname='git1.example.com', |
| 794 | name='dummy', # TODOv3(jeblair): remove |
| 795 | connection=connection1) |
| 796 | |
| 797 | source1_project1 = model.Project('project1', source1) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 798 | tenant.addConfigProject(source1_project1) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 799 | d = {'project1': |
| 800 | {'git1.example.com': source1_project1}} |
| 801 | self.assertEqual(d, tenant.projects) |
| 802 | self.assertEqual((True, source1_project1), |
| 803 | tenant.getProject('project1')) |
| 804 | self.assertEqual((True, source1_project1), |
| 805 | tenant.getProject('git1.example.com/project1')) |
| 806 | |
| 807 | source1_project2 = model.Project('project2', source1) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 808 | tenant.addUntrustedProject(source1_project2) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 809 | d = {'project1': |
| 810 | {'git1.example.com': source1_project1}, |
| 811 | 'project2': |
| 812 | {'git1.example.com': source1_project2}} |
| 813 | self.assertEqual(d, tenant.projects) |
| 814 | self.assertEqual((False, source1_project2), |
| 815 | tenant.getProject('project2')) |
| 816 | self.assertEqual((False, source1_project2), |
| 817 | tenant.getProject('git1.example.com/project2')) |
| 818 | |
| 819 | connection2 = Dummy(connection_name='dummy_connection2') |
| 820 | source2 = Dummy(canonical_hostname='git2.example.com', |
| 821 | name='dummy', # TODOv3(jeblair): remove |
| 822 | connection=connection2) |
| 823 | |
| 824 | source2_project1 = model.Project('project1', source2) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 825 | tenant.addUntrustedProject(source2_project1) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 826 | d = {'project1': |
| 827 | {'git1.example.com': source1_project1, |
| 828 | 'git2.example.com': source2_project1}, |
| 829 | 'project2': |
| 830 | {'git1.example.com': source1_project2}} |
| 831 | self.assertEqual(d, tenant.projects) |
| 832 | with testtools.ExpectedException( |
| 833 | Exception, |
| 834 | "Project name 'project1' is ambiguous"): |
| 835 | tenant.getProject('project1') |
| 836 | self.assertEqual((False, source1_project2), |
| 837 | tenant.getProject('project2')) |
| 838 | self.assertEqual((True, source1_project1), |
| 839 | tenant.getProject('git1.example.com/project1')) |
| 840 | self.assertEqual((False, source2_project1), |
| 841 | tenant.getProject('git2.example.com/project1')) |
| 842 | |
| 843 | source2_project2 = model.Project('project2', source2) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 844 | tenant.addConfigProject(source2_project2) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 845 | d = {'project1': |
| 846 | {'git1.example.com': source1_project1, |
| 847 | 'git2.example.com': source2_project1}, |
| 848 | 'project2': |
| 849 | {'git1.example.com': source1_project2, |
| 850 | 'git2.example.com': source2_project2}} |
| 851 | self.assertEqual(d, tenant.projects) |
| 852 | with testtools.ExpectedException( |
| 853 | Exception, |
| 854 | "Project name 'project1' is ambiguous"): |
| 855 | tenant.getProject('project1') |
| 856 | with testtools.ExpectedException( |
| 857 | Exception, |
| 858 | "Project name 'project2' is ambiguous"): |
| 859 | tenant.getProject('project2') |
| 860 | self.assertEqual((True, source1_project1), |
| 861 | tenant.getProject('git1.example.com/project1')) |
| 862 | self.assertEqual((False, source2_project1), |
| 863 | tenant.getProject('git2.example.com/project1')) |
| 864 | self.assertEqual((False, source1_project2), |
| 865 | tenant.getProject('git1.example.com/project2')) |
| 866 | self.assertEqual((True, source2_project2), |
| 867 | tenant.getProject('git2.example.com/project2')) |
| 868 | |
| 869 | source1_project2b = model.Project('subpath/project2', source1) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 870 | tenant.addConfigProject(source1_project2b) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 871 | d = {'project1': |
| 872 | {'git1.example.com': source1_project1, |
| 873 | 'git2.example.com': source2_project1}, |
| 874 | 'project2': |
| 875 | {'git1.example.com': source1_project2, |
| 876 | 'git2.example.com': source2_project2}, |
| 877 | 'subpath/project2': |
| 878 | {'git1.example.com': source1_project2b}} |
| 879 | self.assertEqual(d, tenant.projects) |
| 880 | self.assertEqual((False, source1_project2), |
| 881 | tenant.getProject('git1.example.com/project2')) |
| 882 | self.assertEqual((True, source2_project2), |
| 883 | tenant.getProject('git2.example.com/project2')) |
| 884 | self.assertEqual((True, source1_project2b), |
| 885 | tenant.getProject('subpath/project2')) |
| 886 | self.assertEqual( |
| 887 | (True, source1_project2b), |
| 888 | tenant.getProject('git1.example.com/subpath/project2')) |
| 889 | |
| 890 | source2_project2b = model.Project('subpath/project2', source2) |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 891 | tenant.addConfigProject(source2_project2b) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 892 | d = {'project1': |
| 893 | {'git1.example.com': source1_project1, |
| 894 | 'git2.example.com': source2_project1}, |
| 895 | 'project2': |
| 896 | {'git1.example.com': source1_project2, |
| 897 | 'git2.example.com': source2_project2}, |
| 898 | 'subpath/project2': |
| 899 | {'git1.example.com': source1_project2b, |
| 900 | 'git2.example.com': source2_project2b}} |
| 901 | self.assertEqual(d, tenant.projects) |
| 902 | self.assertEqual((False, source1_project2), |
| 903 | tenant.getProject('git1.example.com/project2')) |
| 904 | self.assertEqual((True, source2_project2), |
| 905 | tenant.getProject('git2.example.com/project2')) |
| 906 | with testtools.ExpectedException( |
| 907 | Exception, |
| 908 | "Project name 'subpath/project2' is ambiguous"): |
| 909 | tenant.getProject('subpath/project2') |
| 910 | self.assertEqual( |
| 911 | (True, source1_project2b), |
| 912 | tenant.getProject('git1.example.com/subpath/project2')) |
| 913 | self.assertEqual( |
| 914 | (True, source2_project2b), |
| 915 | tenant.getProject('git2.example.com/subpath/project2')) |
| 916 | |
| 917 | with testtools.ExpectedException( |
| 918 | Exception, |
| 919 | "Project project1 is already in project index"): |
| 920 | tenant._addProject(source1_project1) |