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 | 892cca6 | 2017-08-09 11:36:58 -0700 | [diff] [blame] | 57 | self.untrusted_context = model.SourceContext(self.project, 'master', |
| 58 | 'test', False) |
James E. Blair | 1cebebf | 2017-07-14 11:39:03 -0700 | [diff] [blame] | 59 | m = yaml.Mark('name', 0, 0, 0, '', 0) |
| 60 | self.start_mark = configloader.ZuulMark(m, m, '') |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 61 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 62 | @property |
| 63 | def job(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 64 | tenant = model.Tenant('tenant') |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 65 | layout = model.Layout(tenant) |
James E. Blair | 7e9cc27 | 2018-02-15 14:13:46 -0800 | [diff] [blame^] | 66 | job_parser = configloader.JobParser(tenant, layout) |
| 67 | job = job_parser.fromYaml({ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 68 | '_source_context': self.context, |
| 69 | '_start_mark': self.start_mark, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 70 | 'name': 'job', |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 71 | 'parent': None, |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 72 | 'irrelevant-files': [ |
| 73 | '^docs/.*$' |
| 74 | ]}) |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 75 | return job |
| 76 | |
| 77 | def test_change_matches_returns_false_for_matched_skip_if(self): |
| 78 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 79 | change.files = ['/COMMIT_MSG', 'docs/foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 80 | self.assertFalse(self.job.changeMatches(change)) |
| 81 | |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 82 | def test_change_matches_returns_false_for_single_matched_skip_if(self): |
| 83 | change = model.Change('project') |
| 84 | change.files = ['docs/foo'] |
| 85 | self.assertFalse(self.job.changeMatches(change)) |
| 86 | |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 87 | def test_change_matches_returns_true_for_unmatched_skip_if(self): |
| 88 | change = model.Change('project') |
Alexander Evseev | dbe6fab | 2015-11-19 12:46:34 +0300 | [diff] [blame] | 89 | change.files = ['/COMMIT_MSG', 'foo'] |
Maru Newby | 3fe5f85 | 2015-01-13 04:22:14 +0000 | [diff] [blame] | 90 | self.assertTrue(self.job.changeMatches(change)) |
| 91 | |
Jan Hruban | 570d01c | 2016-03-10 21:51:32 +0100 | [diff] [blame] | 92 | def test_change_matches_returns_true_for_single_unmatched_skip_if(self): |
| 93 | change = model.Change('project') |
| 94 | change.files = ['foo'] |
| 95 | self.assertTrue(self.job.changeMatches(change)) |
| 96 | |
Maru Newby | 79427a4 | 2015-02-17 17:54:45 +0000 | [diff] [blame] | 97 | def test_job_sets_defaults_for_boolean_attributes(self): |
James E. Blair | 8300578 | 2015-12-11 14:46:03 -0800 | [diff] [blame] | 98 | self.assertIsNotNone(self.job.voting) |
| 99 | |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 100 | def test_job_variants(self): |
| 101 | # This simulates freezing a job. |
| 102 | |
James E. Blair | 892cca6 | 2017-08-09 11:36:58 -0700 | [diff] [blame] | 103 | secrets = ['foo'] |
| 104 | py27_pre = model.PlaybookContext(self.context, 'py27-pre', [], secrets) |
| 105 | py27_run = model.PlaybookContext(self.context, 'py27-run', [], secrets) |
| 106 | py27_post = model.PlaybookContext(self.context, 'py27-post', [], |
| 107 | secrets) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 108 | |
| 109 | py27 = model.Job('py27') |
| 110 | py27.timeout = 30 |
| 111 | py27.pre_run = [py27_pre] |
| 112 | py27.run = [py27_run] |
| 113 | py27.post_run = [py27_post] |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 114 | |
| 115 | job = py27.copy() |
| 116 | self.assertEqual(30, job.timeout) |
| 117 | |
| 118 | # Apply the diablo variant |
| 119 | diablo = model.Job('py27') |
| 120 | diablo.timeout = 40 |
| 121 | job.applyVariant(diablo) |
| 122 | |
| 123 | self.assertEqual(40, job.timeout) |
| 124 | self.assertEqual(['py27-pre'], |
| 125 | [x.path for x in job.pre_run]) |
| 126 | self.assertEqual(['py27-run'], |
| 127 | [x.path for x in job.run]) |
| 128 | self.assertEqual(['py27-post'], |
| 129 | [x.path for x in job.post_run]) |
James E. Blair | 892cca6 | 2017-08-09 11:36:58 -0700 | [diff] [blame] | 130 | self.assertEqual(secrets, job.pre_run[0].secrets) |
| 131 | self.assertEqual(secrets, job.run[0].secrets) |
| 132 | self.assertEqual(secrets, job.post_run[0].secrets) |
James E. Blair | a7f51ca | 2017-02-07 16:01:26 -0800 | [diff] [blame] | 133 | |
| 134 | # Set the job to final for the following checks |
| 135 | job.final = True |
| 136 | self.assertTrue(job.voting) |
| 137 | |
| 138 | good_final = model.Job('py27') |
| 139 | good_final.voting = False |
| 140 | job.applyVariant(good_final) |
| 141 | self.assertFalse(job.voting) |
| 142 | |
| 143 | bad_final = model.Job('py27') |
| 144 | bad_final.timeout = 600 |
| 145 | with testtools.ExpectedException( |
| 146 | Exception, |
| 147 | "Unable to modify final job"): |
| 148 | job.applyVariant(bad_final) |
| 149 | |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 150 | def test_job_inheritance_job_tree(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 151 | tenant = model.Tenant('tenant') |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 152 | layout = model.Layout(tenant) |
James E. Blair | af8b208 | 2017-10-03 15:38:27 -0700 | [diff] [blame] | 153 | |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 154 | tpc = model.TenantProjectConfig(self.project) |
| 155 | tenant.addUntrustedProject(tpc) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 156 | |
| 157 | pipeline = model.Pipeline('gate', layout) |
| 158 | layout.addPipeline(pipeline) |
| 159 | queue = model.ChangeQueue(pipeline) |
| 160 | |
James E. Blair | 7e9cc27 | 2018-02-15 14:13:46 -0800 | [diff] [blame^] | 161 | job_parser = configloader.JobParser(tenant, layout) |
| 162 | base = job_parser.fromYaml({ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 163 | '_source_context': self.context, |
| 164 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 165 | 'name': 'base', |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 166 | 'parent': None, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 167 | 'timeout': 30, |
| 168 | }) |
| 169 | layout.addJob(base) |
James E. Blair | 7e9cc27 | 2018-02-15 14:13:46 -0800 | [diff] [blame^] | 170 | python27 = job_parser.fromYaml({ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 171 | '_source_context': self.context, |
| 172 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 173 | 'name': 'python27', |
| 174 | 'parent': 'base', |
| 175 | 'timeout': 40, |
| 176 | }) |
| 177 | layout.addJob(python27) |
James E. Blair | 7e9cc27 | 2018-02-15 14:13:46 -0800 | [diff] [blame^] | 178 | python27diablo = job_parser.fromYaml({ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 179 | '_source_context': self.context, |
| 180 | '_start_mark': self.start_mark, |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 181 | 'name': 'python27', |
| 182 | 'branches': [ |
| 183 | 'stable/diablo' |
| 184 | ], |
| 185 | 'timeout': 50, |
| 186 | }) |
| 187 | layout.addJob(python27diablo) |
| 188 | |
James E. Blair | af8b208 | 2017-10-03 15:38:27 -0700 | [diff] [blame] | 189 | project_template_parser = configloader.ProjectTemplateParser( |
| 190 | tenant, layout) |
| 191 | project_parser = configloader.ProjectParser( |
| 192 | tenant, layout, project_template_parser) |
| 193 | project_config = project_parser.fromYaml([{ |
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 | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 196 | 'name': 'project', |
| 197 | 'gate': { |
| 198 | 'jobs': [ |
James E. Blair | 0029267 | 2017-10-26 15:29:39 -0700 | [diff] [blame] | 199 | {'python27': {'timeout': 70, |
| 200 | 'run': 'playbooks/python27.yaml'}} |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 201 | ] |
| 202 | } |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 203 | }]) |
James E. Blair | f59f3cf | 2017-02-19 14:50:26 -0800 | [diff] [blame] | 204 | layout.addProjectConfig(project_config) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 205 | |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 206 | change = model.Change(self.project) |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 207 | change.branch = 'master' |
| 208 | item = queue.enqueueChange(change) |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 209 | item.layout = layout |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 210 | |
| 211 | self.assertTrue(base.changeMatches(change)) |
| 212 | self.assertTrue(python27.changeMatches(change)) |
| 213 | self.assertFalse(python27diablo.changeMatches(change)) |
| 214 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 215 | item.freezeJobGraph() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 216 | self.assertEqual(len(item.getJobs()), 1) |
| 217 | job = item.getJobs()[0] |
| 218 | self.assertEqual(job.name, 'python27') |
| 219 | self.assertEqual(job.timeout, 70) |
| 220 | |
| 221 | change.branch = 'stable/diablo' |
| 222 | item = queue.enqueueChange(change) |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 223 | item.layout = layout |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 224 | |
| 225 | self.assertTrue(base.changeMatches(change)) |
| 226 | self.assertTrue(python27.changeMatches(change)) |
| 227 | self.assertTrue(python27diablo.changeMatches(change)) |
| 228 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 229 | item.freezeJobGraph() |
James E. Blair | 8b1dc3f | 2016-07-05 16:49:00 -0700 | [diff] [blame] | 230 | self.assertEqual(len(item.getJobs()), 1) |
| 231 | job = item.getJobs()[0] |
| 232 | self.assertEqual(job.name, 'python27') |
| 233 | self.assertEqual(job.timeout, 70) |
| 234 | |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 235 | def test_inheritance_keeps_matchers(self): |
James E. Blair | 5ac9384 | 2017-01-20 06:47:34 -0800 | [diff] [blame] | 236 | tenant = model.Tenant('tenant') |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 237 | layout = model.Layout(tenant) |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 238 | |
| 239 | pipeline = model.Pipeline('gate', layout) |
| 240 | layout.addPipeline(pipeline) |
| 241 | queue = model.ChangeQueue(pipeline) |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 242 | project = model.Project('project', self.source) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 243 | tpc = model.TenantProjectConfig(project) |
| 244 | tenant.addUntrustedProject(tpc) |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 245 | |
James E. Blair | 7e9cc27 | 2018-02-15 14:13:46 -0800 | [diff] [blame^] | 246 | job_parser = configloader.JobParser(tenant, layout) |
| 247 | base = job_parser.fromYaml({ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 248 | '_source_context': self.context, |
| 249 | '_start_mark': self.start_mark, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 250 | 'name': 'base', |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 251 | 'parent': None, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 252 | 'timeout': 30, |
| 253 | }) |
| 254 | layout.addJob(base) |
James E. Blair | 7e9cc27 | 2018-02-15 14:13:46 -0800 | [diff] [blame^] | 255 | python27 = job_parser.fromYaml({ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 256 | '_source_context': self.context, |
| 257 | '_start_mark': self.start_mark, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 258 | 'name': 'python27', |
| 259 | 'parent': 'base', |
| 260 | 'timeout': 40, |
| 261 | 'irrelevant-files': ['^ignored-file$'], |
| 262 | }) |
| 263 | layout.addJob(python27) |
| 264 | |
James E. Blair | af8b208 | 2017-10-03 15:38:27 -0700 | [diff] [blame] | 265 | project_template_parser = configloader.ProjectTemplateParser( |
| 266 | tenant, layout) |
| 267 | project_parser = configloader.ProjectParser( |
| 268 | tenant, layout, project_template_parser) |
| 269 | project_config = project_parser.fromYaml([{ |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 270 | '_source_context': self.context, |
| 271 | '_start_mark': self.start_mark, |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 272 | 'name': 'project', |
| 273 | 'gate': { |
| 274 | 'jobs': [ |
| 275 | 'python27', |
| 276 | ] |
| 277 | } |
James E. Blair | ff55574 | 2017-02-19 11:34:27 -0800 | [diff] [blame] | 278 | }]) |
James E. Blair | f59f3cf | 2017-02-19 14:50:26 -0800 | [diff] [blame] | 279 | layout.addProjectConfig(project_config) |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 280 | |
| 281 | change = model.Change(project) |
| 282 | change.branch = 'master' |
| 283 | change.files = ['/COMMIT_MSG', 'ignored-file'] |
| 284 | item = queue.enqueueChange(change) |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 285 | item.layout = layout |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 286 | |
| 287 | self.assertTrue(base.changeMatches(change)) |
| 288 | self.assertFalse(python27.changeMatches(change)) |
| 289 | |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 290 | item.freezeJobGraph() |
Clint Byrum | 8549360 | 2016-11-18 11:59:47 -0800 | [diff] [blame] | 291 | self.assertEqual([], item.getJobs()) |
| 292 | |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 293 | def test_job_source_project(self): |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 294 | tenant = self.tenant |
| 295 | layout = self.layout |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 296 | base_project = model.Project('base_project', self.source) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 297 | base_context = model.SourceContext(base_project, 'master', |
| 298 | 'test', True) |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 299 | tpc = model.TenantProjectConfig(base_project) |
| 300 | tenant.addUntrustedProject(tpc) |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 301 | |
James E. Blair | 7e9cc27 | 2018-02-15 14:13:46 -0800 | [diff] [blame^] | 302 | job_parser = configloader.JobParser(tenant, layout) |
| 303 | base = job_parser.fromYaml({ |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 304 | '_source_context': base_context, |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 305 | '_start_mark': self.start_mark, |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 306 | 'parent': None, |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 307 | 'name': 'base', |
| 308 | }) |
| 309 | layout.addJob(base) |
| 310 | |
James E. Blair | 0a89975 | 2017-03-29 13:22:16 -0700 | [diff] [blame] | 311 | other_project = model.Project('other_project', self.source) |
James E. Blair | 6f140c7 | 2017-03-03 10:32:07 -0800 | [diff] [blame] | 312 | other_context = model.SourceContext(other_project, 'master', |
| 313 | 'test', True) |
James E. Blair | 6459db1 | 2017-06-29 14:57:20 -0700 | [diff] [blame] | 314 | tpc = model.TenantProjectConfig(other_project) |
| 315 | tenant.addUntrustedProject(tpc) |
James E. Blair | 7e9cc27 | 2018-02-15 14:13:46 -0800 | [diff] [blame^] | 316 | base2 = job_parser.fromYaml({ |
James E. Blair | cdab203 | 2017-02-01 09:09:29 -0800 | [diff] [blame] | 317 | '_source_context': other_context, |
James E. Blair | ec7ff30 | 2017-03-04 07:31:32 -0800 | [diff] [blame] | 318 | '_start_mark': self.start_mark, |
James E. Blair | 4317e9f | 2016-07-15 10:05:47 -0700 | [diff] [blame] | 319 | 'name': 'base', |
| 320 | }) |
| 321 | with testtools.ExpectedException( |
| 322 | Exception, |
| 323 | "Job base in other_project is not permitted " |
| 324 | "to shadow job base in base_project"): |
| 325 | layout.addJob(base2) |
| 326 | |
James E. Blair | 8eb564a | 2017-08-10 09:21:41 -0700 | [diff] [blame] | 327 | def test_job_pipeline_allow_untrusted_secrets(self): |
| 328 | self.pipeline.post_review = False |
James E. Blair | 7e9cc27 | 2018-02-15 14:13:46 -0800 | [diff] [blame^] | 329 | job_parser = configloader.JobParser(self.tenant, self.layout) |
| 330 | job = job_parser.fromYaml({ |
James E. Blair | d234836 | 2017-03-17 13:59:35 -0700 | [diff] [blame] | 331 | '_source_context': self.context, |
| 332 | '_start_mark': self.start_mark, |
| 333 | 'name': 'job', |
James E. Blair | 2bab6e7 | 2017-08-07 09:52:45 -0700 | [diff] [blame] | 334 | 'parent': None, |
James E. Blair | d234836 | 2017-03-17 13:59:35 -0700 | [diff] [blame] | 335 | }) |
James E. Blair | 8eb564a | 2017-08-10 09:21:41 -0700 | [diff] [blame] | 336 | job.post_review = True |
James E. Blair | d234836 | 2017-03-17 13:59:35 -0700 | [diff] [blame] | 337 | |
| 338 | self.layout.addJob(job) |
| 339 | |
James E. Blair | af8b208 | 2017-10-03 15:38:27 -0700 | [diff] [blame] | 340 | project_template_parser = configloader.ProjectTemplateParser( |
| 341 | self.tenant, self.layout) |
| 342 | project_parser = configloader.ProjectParser( |
| 343 | self.tenant, self.layout, project_template_parser) |
| 344 | project_config = project_parser.fromYaml( |
| 345 | [{ |
James E. Blair | d234836 | 2017-03-17 13:59:35 -0700 | [diff] [blame] | 346 | '_source_context': self.context, |
| 347 | '_start_mark': self.start_mark, |
| 348 | 'name': 'project', |
| 349 | 'gate': { |
| 350 | 'jobs': [ |
| 351 | 'job' |
| 352 | ] |
| 353 | } |
| 354 | }] |
| 355 | ) |
| 356 | self.layout.addProjectConfig(project_config) |
| 357 | |
| 358 | change = model.Change(self.project) |
| 359 | # Test master |
| 360 | change.branch = 'master' |
| 361 | item = self.queue.enqueueChange(change) |
James E. Blair | 29a24fd | 2017-10-02 15:04:56 -0700 | [diff] [blame] | 362 | item.layout = self.layout |
James E. Blair | d234836 | 2017-03-17 13:59:35 -0700 | [diff] [blame] | 363 | with testtools.ExpectedException( |
| 364 | Exception, |
James E. Blair | 8eb564a | 2017-08-10 09:21:41 -0700 | [diff] [blame] | 365 | "Pre-review pipeline gate does not allow post-review job"): |
James E. Blair | d234836 | 2017-03-17 13:59:35 -0700 | [diff] [blame] | 366 | item.freezeJobGraph() |
| 367 | |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 368 | |
| 369 | class TestJobTimeData(BaseTestCase): |
| 370 | def setUp(self): |
| 371 | super(TestJobTimeData, self).setUp() |
| 372 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 373 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 374 | ).path |
| 375 | |
| 376 | def test_empty_timedata(self): |
| 377 | path = os.path.join(self.tmp_root, 'job-name') |
| 378 | self.assertFalse(os.path.exists(path)) |
| 379 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 380 | td = model.JobTimeData(path) |
| 381 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 382 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 383 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 384 | |
| 385 | def test_save_reload(self): |
| 386 | path = os.path.join(self.tmp_root, 'job-name') |
| 387 | self.assertFalse(os.path.exists(path)) |
| 388 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 389 | td = model.JobTimeData(path) |
| 390 | self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 391 | self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 392 | self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) |
| 393 | success_times = [] |
| 394 | failure_times = [] |
| 395 | results = [] |
| 396 | for x in range(10): |
| 397 | success_times.append(int(random.random() * 1000)) |
| 398 | failure_times.append(int(random.random() * 1000)) |
| 399 | results.append(0) |
| 400 | results.append(1) |
| 401 | random.shuffle(results) |
| 402 | s = f = 0 |
| 403 | for result in results: |
| 404 | if result: |
| 405 | td.add(failure_times[f], 'FAILURE') |
| 406 | f += 1 |
| 407 | else: |
| 408 | td.add(success_times[s], 'SUCCESS') |
| 409 | s += 1 |
| 410 | self.assertEqual(td.success_times, success_times) |
| 411 | self.assertEqual(td.failure_times, failure_times) |
| 412 | self.assertEqual(td.results, results[10:]) |
| 413 | td.save() |
| 414 | self.assertTrue(os.path.exists(path)) |
| 415 | self.assertFalse(os.path.exists(path + '.tmp')) |
| 416 | td = model.JobTimeData(path) |
| 417 | td.load() |
| 418 | self.assertEqual(td.success_times, success_times) |
| 419 | self.assertEqual(td.failure_times, failure_times) |
| 420 | self.assertEqual(td.results, results[10:]) |
| 421 | |
| 422 | |
| 423 | class TestTimeDataBase(BaseTestCase): |
| 424 | def setUp(self): |
| 425 | super(TestTimeDataBase, self).setUp() |
| 426 | self.tmp_root = self.useFixture(fixtures.TempDir( |
| 427 | rootdir=os.environ.get("ZUUL_TEST_ROOT")) |
| 428 | ).path |
| 429 | self.db = model.TimeDataBase(self.tmp_root) |
| 430 | |
| 431 | def test_timedatabase(self): |
James E. Blair | ae0f23c | 2017-09-13 10:55:15 -0600 | [diff] [blame] | 432 | pipeline = Dummy(layout=Dummy(tenant=Dummy(name='test-tenant'))) |
| 433 | change = Dummy(project=Dummy(canonical_name='git.example.com/foo/bar')) |
| 434 | job = Dummy(name='job-name') |
| 435 | item = Dummy(pipeline=pipeline, |
| 436 | change=change) |
| 437 | build = Dummy(build_set=Dummy(item=item), |
| 438 | job=job) |
| 439 | |
| 440 | self.assertEqual(self.db.getEstimatedTime(build), 0) |
| 441 | self.db.update(build, 50, 'SUCCESS') |
| 442 | self.assertEqual(self.db.getEstimatedTime(build), 50) |
| 443 | self.db.update(build, 100, 'SUCCESS') |
| 444 | self.assertEqual(self.db.getEstimatedTime(build), 75) |
James E. Blair | ce8a213 | 2016-05-19 15:21:52 -0700 | [diff] [blame] | 445 | for x in range(10): |
James E. Blair | ae0f23c | 2017-09-13 10:55:15 -0600 | [diff] [blame] | 446 | self.db.update(build, 100, 'SUCCESS') |
| 447 | self.assertEqual(self.db.getEstimatedTime(build), 100) |
Fredrik Medley | f8aec83 | 2015-09-28 13:40:20 +0200 | [diff] [blame] | 448 | |
| 449 | |
| 450 | class TestGraph(BaseTestCase): |
| 451 | def test_job_graph_disallows_multiple_jobs_with_same_name(self): |
| 452 | graph = model.JobGraph() |
| 453 | job1 = model.Job('job') |
| 454 | job2 = model.Job('job') |
| 455 | graph.addJob(job1) |
| 456 | with testtools.ExpectedException(Exception, |
| 457 | "Job job already added"): |
| 458 | graph.addJob(job2) |
| 459 | |
| 460 | def test_job_graph_disallows_circular_dependencies(self): |
| 461 | graph = model.JobGraph() |
| 462 | jobs = [model.Job('job%d' % i) for i in range(0, 10)] |
| 463 | prevjob = None |
| 464 | for j in jobs[:3]: |
| 465 | if prevjob: |
| 466 | j.dependencies = frozenset([prevjob.name]) |
| 467 | graph.addJob(j) |
| 468 | prevjob = j |
| 469 | # 0 triggers 1 triggers 2 triggers 3... |
| 470 | |
| 471 | # Cannot depend on itself |
| 472 | with testtools.ExpectedException( |
| 473 | Exception, |
| 474 | "Dependency cycle detected in job jobX"): |
| 475 | j = model.Job('jobX') |
| 476 | j.dependencies = frozenset([j.name]) |
| 477 | graph.addJob(j) |
| 478 | |
| 479 | # Disallow circular dependencies |
| 480 | with testtools.ExpectedException( |
| 481 | Exception, |
| 482 | "Dependency cycle detected in job job3"): |
| 483 | jobs[4].dependencies = frozenset([jobs[3].name]) |
| 484 | graph.addJob(jobs[4]) |
| 485 | jobs[3].dependencies = frozenset([jobs[4].name]) |
| 486 | graph.addJob(jobs[3]) |
| 487 | |
| 488 | jobs[5].dependencies = frozenset([jobs[4].name]) |
| 489 | graph.addJob(jobs[5]) |
| 490 | |
| 491 | with testtools.ExpectedException( |
| 492 | Exception, |
| 493 | "Dependency cycle detected in job job3"): |
| 494 | jobs[3].dependencies = frozenset([jobs[5].name]) |
| 495 | graph.addJob(jobs[3]) |
| 496 | |
| 497 | jobs[3].dependencies = frozenset([jobs[2].name]) |
| 498 | graph.addJob(jobs[3]) |
| 499 | jobs[6].dependencies = frozenset([jobs[2].name]) |
| 500 | graph.addJob(jobs[6]) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 501 | |
| 502 | |
| 503 | class TestTenant(BaseTestCase): |
| 504 | def test_add_project(self): |
| 505 | tenant = model.Tenant('tenant') |
| 506 | connection1 = Dummy(connection_name='dummy_connection1') |
| 507 | source1 = Dummy(canonical_hostname='git1.example.com', |
| 508 | name='dummy', # TODOv3(jeblair): remove |
| 509 | connection=connection1) |
| 510 | |
| 511 | source1_project1 = model.Project('project1', source1) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 512 | source1_project1_tpc = model.TenantProjectConfig(source1_project1) |
| 513 | tenant.addConfigProject(source1_project1_tpc) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 514 | d = {'project1': |
| 515 | {'git1.example.com': source1_project1}} |
| 516 | self.assertEqual(d, tenant.projects) |
| 517 | self.assertEqual((True, source1_project1), |
| 518 | tenant.getProject('project1')) |
| 519 | self.assertEqual((True, source1_project1), |
| 520 | tenant.getProject('git1.example.com/project1')) |
| 521 | |
| 522 | source1_project2 = model.Project('project2', source1) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 523 | tpc = model.TenantProjectConfig(source1_project2) |
| 524 | tenant.addUntrustedProject(tpc) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 525 | d = {'project1': |
| 526 | {'git1.example.com': source1_project1}, |
| 527 | 'project2': |
| 528 | {'git1.example.com': source1_project2}} |
| 529 | self.assertEqual(d, tenant.projects) |
| 530 | self.assertEqual((False, source1_project2), |
| 531 | tenant.getProject('project2')) |
| 532 | self.assertEqual((False, source1_project2), |
| 533 | tenant.getProject('git1.example.com/project2')) |
| 534 | |
| 535 | connection2 = Dummy(connection_name='dummy_connection2') |
| 536 | source2 = Dummy(canonical_hostname='git2.example.com', |
| 537 | name='dummy', # TODOv3(jeblair): remove |
| 538 | connection=connection2) |
| 539 | |
| 540 | source2_project1 = model.Project('project1', source2) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 541 | tpc = model.TenantProjectConfig(source2_project1) |
| 542 | tenant.addUntrustedProject(tpc) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 543 | d = {'project1': |
| 544 | {'git1.example.com': source1_project1, |
| 545 | 'git2.example.com': source2_project1}, |
| 546 | 'project2': |
| 547 | {'git1.example.com': source1_project2}} |
| 548 | self.assertEqual(d, tenant.projects) |
| 549 | with testtools.ExpectedException( |
| 550 | Exception, |
| 551 | "Project name 'project1' is ambiguous"): |
| 552 | tenant.getProject('project1') |
| 553 | self.assertEqual((False, source1_project2), |
| 554 | tenant.getProject('project2')) |
| 555 | self.assertEqual((True, source1_project1), |
| 556 | tenant.getProject('git1.example.com/project1')) |
| 557 | self.assertEqual((False, source2_project1), |
| 558 | tenant.getProject('git2.example.com/project1')) |
| 559 | |
| 560 | source2_project2 = model.Project('project2', source2) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 561 | tpc = model.TenantProjectConfig(source2_project2) |
| 562 | tenant.addConfigProject(tpc) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 563 | d = {'project1': |
| 564 | {'git1.example.com': source1_project1, |
| 565 | 'git2.example.com': source2_project1}, |
| 566 | 'project2': |
| 567 | {'git1.example.com': source1_project2, |
| 568 | 'git2.example.com': source2_project2}} |
| 569 | self.assertEqual(d, tenant.projects) |
| 570 | with testtools.ExpectedException( |
| 571 | Exception, |
| 572 | "Project name 'project1' is ambiguous"): |
| 573 | tenant.getProject('project1') |
| 574 | with testtools.ExpectedException( |
| 575 | Exception, |
| 576 | "Project name 'project2' is ambiguous"): |
| 577 | tenant.getProject('project2') |
| 578 | self.assertEqual((True, source1_project1), |
| 579 | tenant.getProject('git1.example.com/project1')) |
| 580 | self.assertEqual((False, source2_project1), |
| 581 | tenant.getProject('git2.example.com/project1')) |
| 582 | self.assertEqual((False, source1_project2), |
| 583 | tenant.getProject('git1.example.com/project2')) |
| 584 | self.assertEqual((True, source2_project2), |
| 585 | tenant.getProject('git2.example.com/project2')) |
| 586 | |
| 587 | source1_project2b = model.Project('subpath/project2', source1) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 588 | tpc = model.TenantProjectConfig(source1_project2b) |
| 589 | tenant.addConfigProject(tpc) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 590 | d = {'project1': |
| 591 | {'git1.example.com': source1_project1, |
| 592 | 'git2.example.com': source2_project1}, |
| 593 | 'project2': |
| 594 | {'git1.example.com': source1_project2, |
| 595 | 'git2.example.com': source2_project2}, |
| 596 | 'subpath/project2': |
| 597 | {'git1.example.com': source1_project2b}} |
| 598 | self.assertEqual(d, tenant.projects) |
| 599 | self.assertEqual((False, source1_project2), |
| 600 | tenant.getProject('git1.example.com/project2')) |
| 601 | self.assertEqual((True, source2_project2), |
| 602 | tenant.getProject('git2.example.com/project2')) |
| 603 | self.assertEqual((True, source1_project2b), |
| 604 | tenant.getProject('subpath/project2')) |
| 605 | self.assertEqual( |
| 606 | (True, source1_project2b), |
| 607 | tenant.getProject('git1.example.com/subpath/project2')) |
| 608 | |
| 609 | source2_project2b = model.Project('subpath/project2', source2) |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 610 | tpc = model.TenantProjectConfig(source2_project2b) |
| 611 | tenant.addConfigProject(tpc) |
James E. Blair | c2a54fd | 2017-03-29 15:19:26 -0700 | [diff] [blame] | 612 | d = {'project1': |
| 613 | {'git1.example.com': source1_project1, |
| 614 | 'git2.example.com': source2_project1}, |
| 615 | 'project2': |
| 616 | {'git1.example.com': source1_project2, |
| 617 | 'git2.example.com': source2_project2}, |
| 618 | 'subpath/project2': |
| 619 | {'git1.example.com': source1_project2b, |
| 620 | 'git2.example.com': source2_project2b}} |
| 621 | self.assertEqual(d, tenant.projects) |
| 622 | self.assertEqual((False, source1_project2), |
| 623 | tenant.getProject('git1.example.com/project2')) |
| 624 | self.assertEqual((True, source2_project2), |
| 625 | tenant.getProject('git2.example.com/project2')) |
| 626 | with testtools.ExpectedException( |
| 627 | Exception, |
| 628 | "Project name 'subpath/project2' is ambiguous"): |
| 629 | tenant.getProject('subpath/project2') |
| 630 | self.assertEqual( |
| 631 | (True, source1_project2b), |
| 632 | tenant.getProject('git1.example.com/subpath/project2')) |
| 633 | self.assertEqual( |
| 634 | (True, source2_project2b), |
| 635 | tenant.getProject('git2.example.com/subpath/project2')) |
| 636 | |
| 637 | with testtools.ExpectedException( |
| 638 | Exception, |
| 639 | "Project project1 is already in project index"): |
James E. Blair | 08d9b78 | 2017-06-29 14:22:48 -0700 | [diff] [blame] | 640 | tenant._addProject(source1_project1_tpc) |