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