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