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