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