blob: c457ff092aac1f9ef8888d5705d2710e07cc95d1 [file] [log] [blame]
Maru Newby3fe5f852015-01-13 04:22:14 +00001# 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 Hesketh0aa7e8b2016-07-14 00:12:25 +100015
James E. Blairce8a2132016-05-19 15:21:52 -070016import os
17import random
18
19import fixtures
James E. Blair4317e9f2016-07-15 10:05:47 -070020import testtools
James E. Blairce8a2132016-05-19 15:21:52 -070021
Maru Newby3fe5f852015-01-13 04:22:14 +000022from zuul import model
James E. Blair83005782015-12-11 14:46:03 -080023from zuul import configloader
James E. Blairbf1a4f22017-03-17 10:59:37 -070024from zuul.lib import encryption
Clint Byrum88fdfde2017-03-28 16:22:42 -070025from zuul.lib import yamlutil as yaml
Maru Newby3fe5f852015-01-13 04:22:14 +000026
James E. Blair18f86a32017-03-15 14:43:26 -070027from tests.base import BaseTestCase, FIXTURE_DIR
Maru Newby3fe5f852015-01-13 04:22:14 +000028
29
James E. Blair0a899752017-03-29 13:22:16 -070030class Dummy(object):
31 def __init__(self, **kw):
32 for k, v in kw.items():
33 setattr(self, k, v)
James E. Blairb3f5db12017-03-17 12:57:39 -070034
35
Maru Newby3fe5f852015-01-13 04:22:14 +000036class TestJob(BaseTestCase):
James E. Blaira7f51ca2017-02-07 16:01:26 -080037 def setUp(self):
38 super(TestJob, self).setUp()
James E. Blair0a899752017-03-29 13:22:16 -070039 self.connection = Dummy(connection_name='dummy_connection')
40 self.source = Dummy(canonical_hostname='git.example.com',
James E. Blair0a899752017-03-29 13:22:16 -070041 connection=self.connection)
James E. Blairb3f5db12017-03-17 12:57:39 -070042 self.tenant = model.Tenant('tenant')
James E. Blair6459db12017-06-29 14:57:20 -070043 self.layout = model.Layout(self.tenant)
James E. Blair0a899752017-03-29 13:22:16 -070044 self.project = model.Project('project', self.source)
James E. Blair08d9b782017-06-29 14:22:48 -070045 self.tpc = model.TenantProjectConfig(self.project)
46 self.tenant.addUntrustedProject(self.tpc)
James E. Blairb3f5db12017-03-17 12:57:39 -070047 self.pipeline = model.Pipeline('gate', self.layout)
48 self.layout.addPipeline(self.pipeline)
49 self.queue = model.ChangeQueue(self.pipeline)
50
James E. Blair18f86a32017-03-15 14:43:26 -070051 private_key_file = os.path.join(FIXTURE_DIR, 'private.pem')
52 with open(private_key_file, "rb") as f:
James E. Blairbf1a4f22017-03-17 10:59:37 -070053 self.project.private_key, self.project.public_key = \
54 encryption.deserialize_rsa_keypair(f.read())
James E. Blair6f140c72017-03-03 10:32:07 -080055 self.context = model.SourceContext(self.project, 'master',
56 'test', True)
James E. Blair892cca62017-08-09 11:36:58 -070057 self.untrusted_context = model.SourceContext(self.project, 'master',
58 'test', False)
James E. Blair1cebebf2017-07-14 11:39:03 -070059 m = yaml.Mark('name', 0, 0, 0, '', 0)
60 self.start_mark = configloader.ZuulMark(m, m, '')
James E. Blaira7f51ca2017-02-07 16:01:26 -080061
Maru Newby3fe5f852015-01-13 04:22:14 +000062 @property
63 def job(self):
James E. Blair5ac93842017-01-20 06:47:34 -080064 tenant = model.Tenant('tenant')
James E. Blair6459db12017-06-29 14:57:20 -070065 layout = model.Layout(tenant)
James E. Blair5ac93842017-01-20 06:47:34 -080066 job = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -080067 '_source_context': self.context,
68 '_start_mark': self.start_mark,
James E. Blair83005782015-12-11 14:46:03 -080069 'name': 'job',
James E. Blair2bab6e72017-08-07 09:52:45 -070070 'parent': None,
James E. Blair83005782015-12-11 14:46:03 -080071 'irrelevant-files': [
72 '^docs/.*$'
73 ]})
Maru Newby3fe5f852015-01-13 04:22:14 +000074 return job
75
76 def test_change_matches_returns_false_for_matched_skip_if(self):
77 change = model.Change('project')
Alexander Evseevdbe6fab2015-11-19 12:46:34 +030078 change.files = ['/COMMIT_MSG', 'docs/foo']
Maru Newby3fe5f852015-01-13 04:22:14 +000079 self.assertFalse(self.job.changeMatches(change))
80
Jan Hruban570d01c2016-03-10 21:51:32 +010081 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 Newby3fe5f852015-01-13 04:22:14 +000086 def test_change_matches_returns_true_for_unmatched_skip_if(self):
87 change = model.Change('project')
Alexander Evseevdbe6fab2015-11-19 12:46:34 +030088 change.files = ['/COMMIT_MSG', 'foo']
Maru Newby3fe5f852015-01-13 04:22:14 +000089 self.assertTrue(self.job.changeMatches(change))
90
Jan Hruban570d01c2016-03-10 21:51:32 +010091 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 Newby79427a42015-02-17 17:54:45 +000096 def test_job_sets_defaults_for_boolean_attributes(self):
James E. Blair83005782015-12-11 14:46:03 -080097 self.assertIsNotNone(self.job.voting)
98
99 def test_job_inheritance(self):
James E. Blaira7f51ca2017-02-07 16:01:26 -0800100 # This is standard job inheritance.
101
James E. Blair892cca62017-08-09 11:36:58 -0700102 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. Blaira7f51ca2017-02-07 16:01:26 -0800105
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. Blaira7f51ca2017-02-07 16:01:26 -0800111
112 py27 = model.Job('py27')
Monty Taylor38b553a2017-06-05 13:06:10 -0500113 self.assertIsNone(py27.timeout)
James E. Blaira7f51ca2017-02-07 16:01:26 -0800114 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. Blaira7f51ca2017-02-07 16:01:26 -0800122
123 def test_job_variants(self):
124 # This simulates freezing a job.
125
James E. Blair892cca62017-08-09 11:36:58 -0700126 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. Blaira7f51ca2017-02-07 16:01:26 -0800131
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. Blaira7f51ca2017-02-07 16:01:26 -0800137
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. Blair892cca62017-08-09 11:36:58 -0700153 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. Blaira7f51ca2017-02-07 16:01:26 -0800156
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. Blair5ac93842017-01-20 06:47:34 -0800175 tenant = model.Tenant('tenant')
James E. Blair6459db12017-06-29 14:57:20 -0700176 layout = model.Layout(tenant)
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700177
178 pipeline = model.Pipeline('gate', layout)
179 layout.addPipeline(pipeline)
180 queue = model.ChangeQueue(pipeline)
James E. Blair0a899752017-03-29 13:22:16 -0700181 project = model.Project('project', self.source)
James E. Blair08d9b782017-06-29 14:22:48 -0700182 tpc = model.TenantProjectConfig(project)
183 tenant.addUntrustedProject(tpc)
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700184
James E. Blair5ac93842017-01-20 06:47:34 -0800185 base = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800186 '_source_context': self.context,
187 '_start_mark': self.start_mark,
James E. Blair83005782015-12-11 14:46:03 -0800188 'name': 'base',
James E. Blair2bab6e72017-08-07 09:52:45 -0700189 'parent': None,
James E. Blair83005782015-12-11 14:46:03 -0800190 'timeout': 30,
James E. Blaira7f51ca2017-02-07 16:01:26 -0800191 'pre-run': 'base-pre',
192 'post-run': 'base-post',
James E. Blair7e3e6882017-09-20 15:47:13 -0700193 'nodeset': {
194 'nodes': [{
195 'name': 'controller',
196 'label': 'base',
197 }],
198 },
James E. Blair83005782015-12-11 14:46:03 -0800199 })
200 layout.addJob(base)
James E. Blair5ac93842017-01-20 06:47:34 -0800201 python27 = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800202 '_source_context': self.context,
203 '_start_mark': self.start_mark,
James E. Blair83005782015-12-11 14:46:03 -0800204 'name': 'python27',
205 'parent': 'base',
James E. Blaira7f51ca2017-02-07 16:01:26 -0800206 'pre-run': 'py27-pre',
Paul Belanger3ae0eee2017-08-23 13:12:00 -0400207 'post-run': ['py27-post-a', 'py27-post-b'],
James E. Blair7e3e6882017-09-20 15:47:13 -0700208 'nodeset': {
209 'nodes': [{
210 'name': 'controller',
211 'label': 'new',
212 }],
213 },
James E. Blair83005782015-12-11 14:46:03 -0800214 'timeout': 40,
215 })
216 layout.addJob(python27)
James E. Blair5ac93842017-01-20 06:47:34 -0800217 python27diablo = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800218 '_source_context': self.context,
219 '_start_mark': self.start_mark,
James E. Blair83005782015-12-11 14:46:03 -0800220 'name': 'python27',
221 'branches': [
222 'stable/diablo'
223 ],
James E. Blaira7f51ca2017-02-07 16:01:26 -0800224 'pre-run': 'py27-diablo-pre',
225 'run': 'py27-diablo',
226 'post-run': 'py27-diablo-post',
James E. Blair7e3e6882017-09-20 15:47:13 -0700227 'nodeset': {
228 'nodes': [{
229 'name': 'controller',
230 'label': 'old',
231 }],
232 },
James E. Blair83005782015-12-11 14:46:03 -0800233 'timeout': 50,
234 })
235 layout.addJob(python27diablo)
236
James E. Blair5ac93842017-01-20 06:47:34 -0800237 python27essex = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800238 '_source_context': self.context,
239 '_start_mark': self.start_mark,
James E. Blaira7f51ca2017-02-07 16:01:26 -0800240 'name': 'python27',
241 'branches': [
242 'stable/essex'
243 ],
244 'pre-run': 'py27-essex-pre',
245 'post-run': 'py27-essex-post',
246 })
247 layout.addJob(python27essex)
248
James E. Blairff555742017-02-19 11:34:27 -0800249 project_config = configloader.ProjectParser.fromYaml(tenant, layout, [{
James E. Blairec7ff302017-03-04 07:31:32 -0800250 '_source_context': self.context,
251 '_start_mark': self.start_mark,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700252 'name': 'project',
253 'gate': {
254 'jobs': [
255 'python27'
256 ]
257 }
James E. Blairff555742017-02-19 11:34:27 -0800258 }])
James E. Blairf59f3cf2017-02-19 14:50:26 -0800259 layout.addProjectConfig(project_config)
James E. Blair83005782015-12-11 14:46:03 -0800260
James E. Blair83005782015-12-11 14:46:03 -0800261 change = model.Change(project)
James E. Blair1774dd52017-02-03 10:52:32 -0800262 # Test master
James E. Blair83005782015-12-11 14:46:03 -0800263 change.branch = 'master'
264 item = queue.enqueueChange(change)
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700265 item.current_build_set.layout = layout
James E. Blair83005782015-12-11 14:46:03 -0800266
267 self.assertTrue(base.changeMatches(change))
268 self.assertTrue(python27.changeMatches(change))
269 self.assertFalse(python27diablo.changeMatches(change))
James E. Blaira7f51ca2017-02-07 16:01:26 -0800270 self.assertFalse(python27essex.changeMatches(change))
James E. Blair83005782015-12-11 14:46:03 -0800271
Fredrik Medleyf8aec832015-09-28 13:40:20 +0200272 item.freezeJobGraph()
James E. Blair83005782015-12-11 14:46:03 -0800273 self.assertEqual(len(item.getJobs()), 1)
274 job = item.getJobs()[0]
275 self.assertEqual(job.name, 'python27')
276 self.assertEqual(job.timeout, 40)
James E. Blair1774dd52017-02-03 10:52:32 -0800277 nodes = job.nodeset.getNodes()
278 self.assertEqual(len(nodes), 1)
James E. Blair16d96a02017-06-08 11:32:56 -0700279 self.assertEqual(nodes[0].label, 'new')
James E. Blaira7f51ca2017-02-07 16:01:26 -0800280 self.assertEqual([x.path for x in job.pre_run],
Tobias Henkel165450e2017-06-26 22:53:45 +0200281 ['base-pre',
282 'py27-pre'])
James E. Blaira7f51ca2017-02-07 16:01:26 -0800283 self.assertEqual([x.path for x in job.post_run],
Paul Belanger3ae0eee2017-08-23 13:12:00 -0400284 ['py27-post-a',
285 'py27-post-b',
Tobias Henkel165450e2017-06-26 22:53:45 +0200286 'base-post'])
James E. Blaira7f51ca2017-02-07 16:01:26 -0800287 self.assertEqual([x.path for x in job.run],
288 ['playbooks/python27',
289 'playbooks/base'])
James E. Blair83005782015-12-11 14:46:03 -0800290
James E. Blair1774dd52017-02-03 10:52:32 -0800291 # Test diablo
James E. Blair83005782015-12-11 14:46:03 -0800292 change.branch = 'stable/diablo'
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700293 item = queue.enqueueChange(change)
294 item.current_build_set.layout = layout
James E. Blair83005782015-12-11 14:46:03 -0800295
296 self.assertTrue(base.changeMatches(change))
297 self.assertTrue(python27.changeMatches(change))
298 self.assertTrue(python27diablo.changeMatches(change))
James E. Blaira7f51ca2017-02-07 16:01:26 -0800299 self.assertFalse(python27essex.changeMatches(change))
James E. Blair83005782015-12-11 14:46:03 -0800300
Fredrik Medleyf8aec832015-09-28 13:40:20 +0200301 item.freezeJobGraph()
James E. Blair83005782015-12-11 14:46:03 -0800302 self.assertEqual(len(item.getJobs()), 1)
303 job = item.getJobs()[0]
304 self.assertEqual(job.name, 'python27')
305 self.assertEqual(job.timeout, 50)
James E. Blair1774dd52017-02-03 10:52:32 -0800306 nodes = job.nodeset.getNodes()
307 self.assertEqual(len(nodes), 1)
James E. Blair16d96a02017-06-08 11:32:56 -0700308 self.assertEqual(nodes[0].label, 'old')
James E. Blaira7f51ca2017-02-07 16:01:26 -0800309 self.assertEqual([x.path for x in job.pre_run],
Tobias Henkel165450e2017-06-26 22:53:45 +0200310 ['base-pre',
311 'py27-pre',
312 'py27-diablo-pre'])
James E. Blaira7f51ca2017-02-07 16:01:26 -0800313 self.assertEqual([x.path for x in job.post_run],
Tobias Henkel165450e2017-06-26 22:53:45 +0200314 ['py27-diablo-post',
Paul Belanger3ae0eee2017-08-23 13:12:00 -0400315 'py27-post-a',
316 'py27-post-b',
Tobias Henkel165450e2017-06-26 22:53:45 +0200317 'base-post'])
James E. Blaira7f51ca2017-02-07 16:01:26 -0800318 self.assertEqual([x.path for x in job.run],
Tobias Henkel165450e2017-06-26 22:53:45 +0200319 ['py27-diablo']),
James E. Blaira7f51ca2017-02-07 16:01:26 -0800320
321 # Test essex
322 change.branch = 'stable/essex'
323 item = queue.enqueueChange(change)
324 item.current_build_set.layout = layout
325
326 self.assertTrue(base.changeMatches(change))
327 self.assertTrue(python27.changeMatches(change))
328 self.assertFalse(python27diablo.changeMatches(change))
329 self.assertTrue(python27essex.changeMatches(change))
330
Fredrik Medleyf8aec832015-09-28 13:40:20 +0200331 item.freezeJobGraph()
James E. Blaira7f51ca2017-02-07 16:01:26 -0800332 self.assertEqual(len(item.getJobs()), 1)
333 job = item.getJobs()[0]
334 self.assertEqual(job.name, 'python27')
335 self.assertEqual([x.path for x in job.pre_run],
Tobias Henkel165450e2017-06-26 22:53:45 +0200336 ['base-pre',
337 'py27-pre',
338 'py27-essex-pre'])
James E. Blaira7f51ca2017-02-07 16:01:26 -0800339 self.assertEqual([x.path for x in job.post_run],
Tobias Henkel165450e2017-06-26 22:53:45 +0200340 ['py27-essex-post',
Paul Belanger3ae0eee2017-08-23 13:12:00 -0400341 'py27-post-a',
342 'py27-post-b',
Tobias Henkel165450e2017-06-26 22:53:45 +0200343 'base-post'])
James E. Blaira7f51ca2017-02-07 16:01:26 -0800344 self.assertEqual([x.path for x in job.run],
345 ['playbooks/python27',
346 'playbooks/base'])
James E. Blairce8a2132016-05-19 15:21:52 -0700347
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000348 def test_job_auth_inheritance(self):
James E. Blair6459db12017-06-29 14:57:20 -0700349 tenant = self.tenant
350 layout = self.layout
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000351
James E. Blair01f83b72017-03-15 13:03:40 -0700352 conf = yaml.safe_load('''
353- secret:
James E. Blair892cca62017-08-09 11:36:58 -0700354 name: trusted-secret
James E. Blair01f83b72017-03-15 13:03:40 -0700355 data:
356 username: test-username
James E. Blair9118c012017-08-03 11:19:16 -0700357 longpassword: !encrypted/pkcs1-oaep
358 - BFhtdnm8uXx7kn79RFL/zJywmzLkT1GY78P3bOtp4WghUFWobkifSu7ZpaV4NeO0s71Y
359 Usi1wGZZL0LveZjUN0t6OU1VZKSG8R5Ly7urjaSo1pPVIq5Rtt/H7W14Lecd+cUeKb4j
360 oeusC9drN3AA8a4oykcVpt1wVqUnTbMGC9ARMCQP6eopcs1l7tzMseprW4RDNhIuz3CR
361 gd0QBMPl6VDoFgBPB8vxtJw+3m0rqBYZCLZgCXekqlny8s2s92nJMuUABbJOEcDRarzi
362 bDsSXsfJt1y+5n7yOURsC7lovMg4GF/vCl/0YMKjBO5bpv9EM5fToeKYyPGSKQoHOnCY
363 ceb3cAVcv5UawcCic8XjhEhp4K7WPdYf2HVAC/qtxhbpjTxG4U5Q/SoppOJ60WqEkQvb
364 Xs6n5Dvy7xmph6GWmU/bAv3eUK3pdD3xa2Ue1lHWz3U+rsYraI+AKYsMYx3RBlfAmCeC
365 1ve2BXPrqnOo7G8tnUvfdYPbK4Aakk0ds/AVqFHEZN+S6hRBmBjLaRFWZ3QSO1NjbBxW
366 naHKZYT7nkrJm8AMCgZU0ZArFLpaufKCeiK5ECSsDxic4FIsY1OkWT42qEUfL0Wd+150
367 AKGNZpPJnnP3QYY4W/MWcKH/zdO400+zWN52WevbSqZy90tqKDJrBkMl1ydqbuw1E4ZH
368 vIs=
369 - BFhtdnm8uXx7kn79RFL/zJywmzLkT1GY78P3bOtp4WghUFWobkifSu7ZpaV4NeO0s71Y
370 Usi1wGZZL0LveZjUN0t6OU1VZKSG8R5Ly7urjaSo1pPVIq5Rtt/H7W14Lecd+cUeKb4j
371 oeusC9drN3AA8a4oykcVpt1wVqUnTbMGC9ARMCQP6eopcs1l7tzMseprW4RDNhIuz3CR
372 gd0QBMPl6VDoFgBPB8vxtJw+3m0rqBYZCLZgCXekqlny8s2s92nJMuUABbJOEcDRarzi
373 bDsSXsfJt1y+5n7yOURsC7lovMg4GF/vCl/0YMKjBO5bpv9EM5fToeKYyPGSKQoHOnCY
374 ceb3cAVcv5UawcCic8XjhEhp4K7WPdYf2HVAC/qtxhbpjTxG4U5Q/SoppOJ60WqEkQvb
375 Xs6n5Dvy7xmph6GWmU/bAv3eUK3pdD3xa2Ue1lHWz3U+rsYraI+AKYsMYx3RBlfAmCeC
376 1ve2BXPrqnOo7G8tnUvfdYPbK4Aakk0ds/AVqFHEZN+S6hRBmBjLaRFWZ3QSO1NjbBxW
377 naHKZYT7nkrJm8AMCgZU0ZArFLpaufKCeiK5ECSsDxic4FIsY1OkWT42qEUfL0Wd+150
378 AKGNZpPJnnP3QYY4W/MWcKH/zdO400+zWN52WevbSqZy90tqKDJrBkMl1ydqbuw1E4ZH
379 vIs=
James E. Blair717e8e92017-03-17 11:03:27 -0700380 password: !encrypted/pkcs1-oaep |
James E. Blair9118c012017-08-03 11:19:16 -0700381 BFhtdnm8uXx7kn79RFL/zJywmzLkT1GY78P3bOtp4WghUFWobkifSu7ZpaV4NeO0s71Y
382 Usi1wGZZL0LveZjUN0t6OU1VZKSG8R5Ly7urjaSo1pPVIq5Rtt/H7W14Lecd+cUeKb4j
383 oeusC9drN3AA8a4oykcVpt1wVqUnTbMGC9ARMCQP6eopcs1l7tzMseprW4RDNhIuz3CR
384 gd0QBMPl6VDoFgBPB8vxtJw+3m0rqBYZCLZgCXekqlny8s2s92nJMuUABbJOEcDRarzi
385 bDsSXsfJt1y+5n7yOURsC7lovMg4GF/vCl/0YMKjBO5bpv9EM5fToeKYyPGSKQoHOnCY
386 ceb3cAVcv5UawcCic8XjhEhp4K7WPdYf2HVAC/qtxhbpjTxG4U5Q/SoppOJ60WqEkQvb
387 Xs6n5Dvy7xmph6GWmU/bAv3eUK3pdD3xa2Ue1lHWz3U+rsYraI+AKYsMYx3RBlfAmCeC
388 1ve2BXPrqnOo7G8tnUvfdYPbK4Aakk0ds/AVqFHEZN+S6hRBmBjLaRFWZ3QSO1NjbBxW
389 naHKZYT7nkrJm8AMCgZU0ZArFLpaufKCeiK5ECSsDxic4FIsY1OkWT42qEUfL0Wd+150
390 AKGNZpPJnnP3QYY4W/MWcKH/zdO400+zWN52WevbSqZy90tqKDJrBkMl1ydqbuw1E4ZH
391 vIs=
James E. Blair01f83b72017-03-15 13:03:40 -0700392''')[0]['secret']
393
394 conf['_source_context'] = self.context
395 conf['_start_mark'] = self.start_mark
396
James E. Blair892cca62017-08-09 11:36:58 -0700397 trusted_secret = configloader.SecretParser.fromYaml(layout, conf)
398 layout.addSecret(trusted_secret)
399
400 conf['name'] = 'untrusted-secret'
401 conf['_source_context'] = self.untrusted_context
402
403 untrusted_secret = configloader.SecretParser.fromYaml(layout, conf)
404 layout.addSecret(untrusted_secret)
James E. Blair01f83b72017-03-15 13:03:40 -0700405
James E. Blair6459db12017-06-29 14:57:20 -0700406 base = configloader.JobParser.fromYaml(self.tenant, self.layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800407 '_source_context': self.context,
408 '_start_mark': self.start_mark,
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000409 'name': 'base',
James E. Blair2bab6e72017-08-07 09:52:45 -0700410 'parent': None,
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000411 'timeout': 30,
412 })
413 layout.addJob(base)
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000414
James E. Blair892cca62017-08-09 11:36:58 -0700415 trusted_secrets_job = configloader.JobParser.fromYaml(
416 tenant, layout, {
417 '_source_context': self.context,
418 '_start_mark': self.start_mark,
419 'name': 'trusted-secrets',
420 'parent': 'base',
421 'timeout': 40,
James E. Blaire19e88a2017-08-09 15:14:29 -0700422 'secrets': [
423 'trusted-secret',
424 ]
James E. Blair892cca62017-08-09 11:36:58 -0700425 })
426 layout.addJob(trusted_secrets_job)
427 untrusted_secrets_job = configloader.JobParser.fromYaml(
428 tenant, layout, {
429 '_source_context': self.untrusted_context,
430 '_start_mark': self.start_mark,
431 'name': 'untrusted-secrets',
432 'parent': 'base',
433 'timeout': 40,
James E. Blaire19e88a2017-08-09 15:14:29 -0700434 'secrets': [
435 'untrusted-secret',
436 ]
James E. Blair892cca62017-08-09 11:36:58 -0700437 })
438 layout.addJob(untrusted_secrets_job)
439 trusted_secrets_trusted_child_job = configloader.JobParser.fromYaml(
440 tenant, layout, {
441 '_source_context': self.context,
442 '_start_mark': self.start_mark,
443 'name': 'trusted-secrets-trusted-child',
444 'parent': 'trusted-secrets',
445 })
446 layout.addJob(trusted_secrets_trusted_child_job)
447 trusted_secrets_untrusted_child_job = configloader.JobParser.fromYaml(
448 tenant, layout, {
449 '_source_context': self.untrusted_context,
450 '_start_mark': self.start_mark,
451 'name': 'trusted-secrets-untrusted-child',
452 'parent': 'trusted-secrets',
453 })
454 layout.addJob(trusted_secrets_untrusted_child_job)
455 untrusted_secrets_trusted_child_job = configloader.JobParser.fromYaml(
456 tenant, layout, {
457 '_source_context': self.context,
458 '_start_mark': self.start_mark,
459 'name': 'untrusted-secrets-trusted-child',
460 'parent': 'untrusted-secrets',
461 })
462 layout.addJob(untrusted_secrets_trusted_child_job)
463 untrusted_secrets_untrusted_child_job = \
464 configloader.JobParser.fromYaml(
465 tenant, layout, {
466 '_source_context': self.untrusted_context,
467 '_start_mark': self.start_mark,
468 'name': 'untrusted-secrets-untrusted-child',
469 'parent': 'untrusted-secrets',
470 })
471 layout.addJob(untrusted_secrets_untrusted_child_job)
472
James E. Blair8eb564a2017-08-10 09:21:41 -0700473 self.assertIsNone(trusted_secrets_job.post_review)
474 self.assertTrue(untrusted_secrets_job.post_review)
James E. Blair892cca62017-08-09 11:36:58 -0700475 self.assertIsNone(
James E. Blair8eb564a2017-08-10 09:21:41 -0700476 trusted_secrets_trusted_child_job.post_review)
James E. Blair892cca62017-08-09 11:36:58 -0700477 self.assertIsNone(
James E. Blair8eb564a2017-08-10 09:21:41 -0700478 trusted_secrets_untrusted_child_job.post_review)
James E. Blair892cca62017-08-09 11:36:58 -0700479 self.assertTrue(
James E. Blair8eb564a2017-08-10 09:21:41 -0700480 untrusted_secrets_trusted_child_job.post_review)
James E. Blair892cca62017-08-09 11:36:58 -0700481 self.assertTrue(
James E. Blair8eb564a2017-08-10 09:21:41 -0700482 untrusted_secrets_untrusted_child_job.post_review)
James E. Blair892cca62017-08-09 11:36:58 -0700483
484 self.assertEqual(trusted_secrets_job.implied_run[0].secrets[0].name,
485 'trusted-secret')
486 self.assertEqual(trusted_secrets_job.implied_run[0].secrets[0].
James E. Blair9118c012017-08-03 11:19:16 -0700487 secret_data['longpassword'],
488 'test-passwordtest-password')
James E. Blair892cca62017-08-09 11:36:58 -0700489 self.assertEqual(trusted_secrets_job.implied_run[0].secrets[0].
James E. Blair9118c012017-08-03 11:19:16 -0700490 secret_data['password'],
491 'test-password')
James E. Blair892cca62017-08-09 11:36:58 -0700492 self.assertEqual(
493 len(trusted_secrets_trusted_child_job.implied_run[0].secrets), 0)
494 self.assertEqual(
495 len(trusted_secrets_untrusted_child_job.implied_run[0].secrets), 0)
496
497 self.assertEqual(untrusted_secrets_job.implied_run[0].secrets[0].name,
498 'untrusted-secret')
499 self.assertEqual(
500 len(untrusted_secrets_trusted_child_job.implied_run[0].secrets), 0)
501 self.assertEqual(
502 len(untrusted_secrets_untrusted_child_job.implied_run[0].secrets),
503 0)
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000504
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700505 def test_job_inheritance_job_tree(self):
James E. Blair5ac93842017-01-20 06:47:34 -0800506 tenant = model.Tenant('tenant')
James E. Blair6459db12017-06-29 14:57:20 -0700507 layout = model.Layout(tenant)
James E. Blair08d9b782017-06-29 14:22:48 -0700508 tpc = model.TenantProjectConfig(self.project)
509 tenant.addUntrustedProject(tpc)
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700510
511 pipeline = model.Pipeline('gate', layout)
512 layout.addPipeline(pipeline)
513 queue = model.ChangeQueue(pipeline)
514
James E. Blair5ac93842017-01-20 06:47:34 -0800515 base = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800516 '_source_context': self.context,
517 '_start_mark': self.start_mark,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700518 'name': 'base',
James E. Blair2bab6e72017-08-07 09:52:45 -0700519 'parent': None,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700520 'timeout': 30,
521 })
522 layout.addJob(base)
James E. Blair5ac93842017-01-20 06:47:34 -0800523 python27 = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800524 '_source_context': self.context,
525 '_start_mark': self.start_mark,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700526 'name': 'python27',
527 'parent': 'base',
528 'timeout': 40,
529 })
530 layout.addJob(python27)
James E. Blair5ac93842017-01-20 06:47:34 -0800531 python27diablo = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800532 '_source_context': self.context,
533 '_start_mark': self.start_mark,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700534 'name': 'python27',
535 'branches': [
536 'stable/diablo'
537 ],
538 'timeout': 50,
539 })
540 layout.addJob(python27diablo)
541
James E. Blairff555742017-02-19 11:34:27 -0800542 project_config = configloader.ProjectParser.fromYaml(tenant, layout, [{
James E. Blairec7ff302017-03-04 07:31:32 -0800543 '_source_context': self.context,
544 '_start_mark': self.start_mark,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700545 'name': 'project',
546 'gate': {
547 'jobs': [
548 {'python27': {'timeout': 70}}
549 ]
550 }
James E. Blairff555742017-02-19 11:34:27 -0800551 }])
James E. Blairf59f3cf2017-02-19 14:50:26 -0800552 layout.addProjectConfig(project_config)
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700553
James E. Blairec7ff302017-03-04 07:31:32 -0800554 change = model.Change(self.project)
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700555 change.branch = 'master'
556 item = queue.enqueueChange(change)
557 item.current_build_set.layout = layout
558
559 self.assertTrue(base.changeMatches(change))
560 self.assertTrue(python27.changeMatches(change))
561 self.assertFalse(python27diablo.changeMatches(change))
562
Fredrik Medleyf8aec832015-09-28 13:40:20 +0200563 item.freezeJobGraph()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700564 self.assertEqual(len(item.getJobs()), 1)
565 job = item.getJobs()[0]
566 self.assertEqual(job.name, 'python27')
567 self.assertEqual(job.timeout, 70)
568
569 change.branch = 'stable/diablo'
570 item = queue.enqueueChange(change)
571 item.current_build_set.layout = layout
572
573 self.assertTrue(base.changeMatches(change))
574 self.assertTrue(python27.changeMatches(change))
575 self.assertTrue(python27diablo.changeMatches(change))
576
Fredrik Medleyf8aec832015-09-28 13:40:20 +0200577 item.freezeJobGraph()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700578 self.assertEqual(len(item.getJobs()), 1)
579 job = item.getJobs()[0]
580 self.assertEqual(job.name, 'python27')
581 self.assertEqual(job.timeout, 70)
582
Clint Byrum85493602016-11-18 11:59:47 -0800583 def test_inheritance_keeps_matchers(self):
James E. Blair5ac93842017-01-20 06:47:34 -0800584 tenant = model.Tenant('tenant')
James E. Blair6459db12017-06-29 14:57:20 -0700585 layout = model.Layout(tenant)
Clint Byrum85493602016-11-18 11:59:47 -0800586
587 pipeline = model.Pipeline('gate', layout)
588 layout.addPipeline(pipeline)
589 queue = model.ChangeQueue(pipeline)
James E. Blair0a899752017-03-29 13:22:16 -0700590 project = model.Project('project', self.source)
James E. Blair08d9b782017-06-29 14:22:48 -0700591 tpc = model.TenantProjectConfig(project)
592 tenant.addUntrustedProject(tpc)
Clint Byrum85493602016-11-18 11:59:47 -0800593
James E. Blair5ac93842017-01-20 06:47:34 -0800594 base = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800595 '_source_context': self.context,
596 '_start_mark': self.start_mark,
Clint Byrum85493602016-11-18 11:59:47 -0800597 'name': 'base',
James E. Blair2bab6e72017-08-07 09:52:45 -0700598 'parent': None,
Clint Byrum85493602016-11-18 11:59:47 -0800599 'timeout': 30,
600 })
601 layout.addJob(base)
James E. Blair5ac93842017-01-20 06:47:34 -0800602 python27 = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blairec7ff302017-03-04 07:31:32 -0800603 '_source_context': self.context,
604 '_start_mark': self.start_mark,
Clint Byrum85493602016-11-18 11:59:47 -0800605 'name': 'python27',
606 'parent': 'base',
607 'timeout': 40,
608 'irrelevant-files': ['^ignored-file$'],
609 })
610 layout.addJob(python27)
611
James E. Blairff555742017-02-19 11:34:27 -0800612 project_config = configloader.ProjectParser.fromYaml(tenant, layout, [{
James E. Blairec7ff302017-03-04 07:31:32 -0800613 '_source_context': self.context,
614 '_start_mark': self.start_mark,
Clint Byrum85493602016-11-18 11:59:47 -0800615 'name': 'project',
616 'gate': {
617 'jobs': [
618 'python27',
619 ]
620 }
James E. Blairff555742017-02-19 11:34:27 -0800621 }])
James E. Blairf59f3cf2017-02-19 14:50:26 -0800622 layout.addProjectConfig(project_config)
Clint Byrum85493602016-11-18 11:59:47 -0800623
624 change = model.Change(project)
625 change.branch = 'master'
626 change.files = ['/COMMIT_MSG', 'ignored-file']
627 item = queue.enqueueChange(change)
628 item.current_build_set.layout = layout
629
630 self.assertTrue(base.changeMatches(change))
631 self.assertFalse(python27.changeMatches(change))
632
Fredrik Medleyf8aec832015-09-28 13:40:20 +0200633 item.freezeJobGraph()
Clint Byrum85493602016-11-18 11:59:47 -0800634 self.assertEqual([], item.getJobs())
635
James E. Blair4317e9f2016-07-15 10:05:47 -0700636 def test_job_source_project(self):
James E. Blair6459db12017-06-29 14:57:20 -0700637 tenant = self.tenant
638 layout = self.layout
James E. Blair0a899752017-03-29 13:22:16 -0700639 base_project = model.Project('base_project', self.source)
James E. Blair6f140c72017-03-03 10:32:07 -0800640 base_context = model.SourceContext(base_project, 'master',
641 'test', True)
James E. Blair6459db12017-06-29 14:57:20 -0700642 tpc = model.TenantProjectConfig(base_project)
643 tenant.addUntrustedProject(tpc)
James E. Blaircdab2032017-02-01 09:09:29 -0800644
James E. Blair5ac93842017-01-20 06:47:34 -0800645 base = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blaircdab2032017-02-01 09:09:29 -0800646 '_source_context': base_context,
James E. Blairec7ff302017-03-04 07:31:32 -0800647 '_start_mark': self.start_mark,
James E. Blair2bab6e72017-08-07 09:52:45 -0700648 'parent': None,
James E. Blair4317e9f2016-07-15 10:05:47 -0700649 'name': 'base',
650 })
651 layout.addJob(base)
652
James E. Blair0a899752017-03-29 13:22:16 -0700653 other_project = model.Project('other_project', self.source)
James E. Blair6f140c72017-03-03 10:32:07 -0800654 other_context = model.SourceContext(other_project, 'master',
655 'test', True)
James E. Blair6459db12017-06-29 14:57:20 -0700656 tpc = model.TenantProjectConfig(other_project)
657 tenant.addUntrustedProject(tpc)
James E. Blair5ac93842017-01-20 06:47:34 -0800658 base2 = configloader.JobParser.fromYaml(tenant, layout, {
James E. Blaircdab2032017-02-01 09:09:29 -0800659 '_source_context': other_context,
James E. Blairec7ff302017-03-04 07:31:32 -0800660 '_start_mark': self.start_mark,
James E. Blair4317e9f2016-07-15 10:05:47 -0700661 'name': 'base',
662 })
663 with testtools.ExpectedException(
664 Exception,
665 "Job base in other_project is not permitted "
666 "to shadow job base in base_project"):
667 layout.addJob(base2)
668
James E. Blairb3f5db12017-03-17 12:57:39 -0700669 def test_job_allowed_projects(self):
670 job = configloader.JobParser.fromYaml(self.tenant, self.layout, {
671 '_source_context': self.context,
672 '_start_mark': self.start_mark,
673 'name': 'job',
James E. Blair2bab6e72017-08-07 09:52:45 -0700674 'parent': None,
James E. Blairb3f5db12017-03-17 12:57:39 -0700675 'allowed-projects': ['project'],
676 })
677 self.layout.addJob(job)
678
James E. Blair0a899752017-03-29 13:22:16 -0700679 project2 = model.Project('project2', self.source)
James E. Blair08d9b782017-06-29 14:22:48 -0700680 tpc2 = model.TenantProjectConfig(project2)
681 self.tenant.addUntrustedProject(tpc2)
James E. Blairb3f5db12017-03-17 12:57:39 -0700682 context2 = model.SourceContext(project2, 'master',
683 'test', True)
684
685 project2_config = configloader.ProjectParser.fromYaml(
686 self.tenant, self.layout, [{
687 '_source_context': context2,
688 '_start_mark': self.start_mark,
689 'name': 'project2',
690 'gate': {
691 'jobs': [
692 'job'
693 ]
694 }
695 }]
696 )
697 self.layout.addProjectConfig(project2_config)
698
699 change = model.Change(project2)
700 # Test master
701 change.branch = 'master'
702 item = self.queue.enqueueChange(change)
703 item.current_build_set.layout = self.layout
704 with testtools.ExpectedException(
705 Exception,
706 "Project project2 is not allowed to run job job"):
707 item.freezeJobGraph()
708
James E. Blair8eb564a2017-08-10 09:21:41 -0700709 def test_job_pipeline_allow_untrusted_secrets(self):
710 self.pipeline.post_review = False
James E. Blaird2348362017-03-17 13:59:35 -0700711 job = configloader.JobParser.fromYaml(self.tenant, self.layout, {
712 '_source_context': self.context,
713 '_start_mark': self.start_mark,
714 'name': 'job',
James E. Blair2bab6e72017-08-07 09:52:45 -0700715 'parent': None,
James E. Blaird2348362017-03-17 13:59:35 -0700716 })
James E. Blair8eb564a2017-08-10 09:21:41 -0700717 job.post_review = True
James E. Blaird2348362017-03-17 13:59:35 -0700718
719 self.layout.addJob(job)
720
721 project_config = configloader.ProjectParser.fromYaml(
722 self.tenant, self.layout, [{
723 '_source_context': self.context,
724 '_start_mark': self.start_mark,
725 'name': 'project',
726 'gate': {
727 'jobs': [
728 'job'
729 ]
730 }
731 }]
732 )
733 self.layout.addProjectConfig(project_config)
734
735 change = model.Change(self.project)
736 # Test master
737 change.branch = 'master'
738 item = self.queue.enqueueChange(change)
739 item.current_build_set.layout = self.layout
740 with testtools.ExpectedException(
741 Exception,
James E. Blair8eb564a2017-08-10 09:21:41 -0700742 "Pre-review pipeline gate does not allow post-review job"):
James E. Blaird2348362017-03-17 13:59:35 -0700743 item.freezeJobGraph()
744
James E. Blairce8a2132016-05-19 15:21:52 -0700745
746class TestJobTimeData(BaseTestCase):
747 def setUp(self):
748 super(TestJobTimeData, self).setUp()
749 self.tmp_root = self.useFixture(fixtures.TempDir(
750 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
751 ).path
752
753 def test_empty_timedata(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
762 def test_save_reload(self):
763 path = os.path.join(self.tmp_root, 'job-name')
764 self.assertFalse(os.path.exists(path))
765 self.assertFalse(os.path.exists(path + '.tmp'))
766 td = model.JobTimeData(path)
767 self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
768 self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
769 self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
770 success_times = []
771 failure_times = []
772 results = []
773 for x in range(10):
774 success_times.append(int(random.random() * 1000))
775 failure_times.append(int(random.random() * 1000))
776 results.append(0)
777 results.append(1)
778 random.shuffle(results)
779 s = f = 0
780 for result in results:
781 if result:
782 td.add(failure_times[f], 'FAILURE')
783 f += 1
784 else:
785 td.add(success_times[s], 'SUCCESS')
786 s += 1
787 self.assertEqual(td.success_times, success_times)
788 self.assertEqual(td.failure_times, failure_times)
789 self.assertEqual(td.results, results[10:])
790 td.save()
791 self.assertTrue(os.path.exists(path))
792 self.assertFalse(os.path.exists(path + '.tmp'))
793 td = model.JobTimeData(path)
794 td.load()
795 self.assertEqual(td.success_times, success_times)
796 self.assertEqual(td.failure_times, failure_times)
797 self.assertEqual(td.results, results[10:])
798
799
800class TestTimeDataBase(BaseTestCase):
801 def setUp(self):
802 super(TestTimeDataBase, self).setUp()
803 self.tmp_root = self.useFixture(fixtures.TempDir(
804 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
805 ).path
806 self.db = model.TimeDataBase(self.tmp_root)
807
808 def test_timedatabase(self):
James E. Blairae0f23c2017-09-13 10:55:15 -0600809 pipeline = Dummy(layout=Dummy(tenant=Dummy(name='test-tenant')))
810 change = Dummy(project=Dummy(canonical_name='git.example.com/foo/bar'))
811 job = Dummy(name='job-name')
812 item = Dummy(pipeline=pipeline,
813 change=change)
814 build = Dummy(build_set=Dummy(item=item),
815 job=job)
816
817 self.assertEqual(self.db.getEstimatedTime(build), 0)
818 self.db.update(build, 50, 'SUCCESS')
819 self.assertEqual(self.db.getEstimatedTime(build), 50)
820 self.db.update(build, 100, 'SUCCESS')
821 self.assertEqual(self.db.getEstimatedTime(build), 75)
James E. Blairce8a2132016-05-19 15:21:52 -0700822 for x in range(10):
James E. Blairae0f23c2017-09-13 10:55:15 -0600823 self.db.update(build, 100, 'SUCCESS')
824 self.assertEqual(self.db.getEstimatedTime(build), 100)
Fredrik Medleyf8aec832015-09-28 13:40:20 +0200825
826
827class TestGraph(BaseTestCase):
828 def test_job_graph_disallows_multiple_jobs_with_same_name(self):
829 graph = model.JobGraph()
830 job1 = model.Job('job')
831 job2 = model.Job('job')
832 graph.addJob(job1)
833 with testtools.ExpectedException(Exception,
834 "Job job already added"):
835 graph.addJob(job2)
836
837 def test_job_graph_disallows_circular_dependencies(self):
838 graph = model.JobGraph()
839 jobs = [model.Job('job%d' % i) for i in range(0, 10)]
840 prevjob = None
841 for j in jobs[:3]:
842 if prevjob:
843 j.dependencies = frozenset([prevjob.name])
844 graph.addJob(j)
845 prevjob = j
846 # 0 triggers 1 triggers 2 triggers 3...
847
848 # Cannot depend on itself
849 with testtools.ExpectedException(
850 Exception,
851 "Dependency cycle detected in job jobX"):
852 j = model.Job('jobX')
853 j.dependencies = frozenset([j.name])
854 graph.addJob(j)
855
856 # Disallow circular dependencies
857 with testtools.ExpectedException(
858 Exception,
859 "Dependency cycle detected in job job3"):
860 jobs[4].dependencies = frozenset([jobs[3].name])
861 graph.addJob(jobs[4])
862 jobs[3].dependencies = frozenset([jobs[4].name])
863 graph.addJob(jobs[3])
864
865 jobs[5].dependencies = frozenset([jobs[4].name])
866 graph.addJob(jobs[5])
867
868 with testtools.ExpectedException(
869 Exception,
870 "Dependency cycle detected in job job3"):
871 jobs[3].dependencies = frozenset([jobs[5].name])
872 graph.addJob(jobs[3])
873
874 jobs[3].dependencies = frozenset([jobs[2].name])
875 graph.addJob(jobs[3])
876 jobs[6].dependencies = frozenset([jobs[2].name])
877 graph.addJob(jobs[6])
James E. Blairc2a54fd2017-03-29 15:19:26 -0700878
879
880class TestTenant(BaseTestCase):
881 def test_add_project(self):
882 tenant = model.Tenant('tenant')
883 connection1 = Dummy(connection_name='dummy_connection1')
884 source1 = Dummy(canonical_hostname='git1.example.com',
885 name='dummy', # TODOv3(jeblair): remove
886 connection=connection1)
887
888 source1_project1 = model.Project('project1', source1)
James E. Blair08d9b782017-06-29 14:22:48 -0700889 source1_project1_tpc = model.TenantProjectConfig(source1_project1)
890 tenant.addConfigProject(source1_project1_tpc)
James E. Blairc2a54fd2017-03-29 15:19:26 -0700891 d = {'project1':
892 {'git1.example.com': source1_project1}}
893 self.assertEqual(d, tenant.projects)
894 self.assertEqual((True, source1_project1),
895 tenant.getProject('project1'))
896 self.assertEqual((True, source1_project1),
897 tenant.getProject('git1.example.com/project1'))
898
899 source1_project2 = model.Project('project2', source1)
James E. Blair08d9b782017-06-29 14:22:48 -0700900 tpc = model.TenantProjectConfig(source1_project2)
901 tenant.addUntrustedProject(tpc)
James E. Blairc2a54fd2017-03-29 15:19:26 -0700902 d = {'project1':
903 {'git1.example.com': source1_project1},
904 'project2':
905 {'git1.example.com': source1_project2}}
906 self.assertEqual(d, tenant.projects)
907 self.assertEqual((False, source1_project2),
908 tenant.getProject('project2'))
909 self.assertEqual((False, source1_project2),
910 tenant.getProject('git1.example.com/project2'))
911
912 connection2 = Dummy(connection_name='dummy_connection2')
913 source2 = Dummy(canonical_hostname='git2.example.com',
914 name='dummy', # TODOv3(jeblair): remove
915 connection=connection2)
916
917 source2_project1 = model.Project('project1', source2)
James E. Blair08d9b782017-06-29 14:22:48 -0700918 tpc = model.TenantProjectConfig(source2_project1)
919 tenant.addUntrustedProject(tpc)
James E. Blairc2a54fd2017-03-29 15:19:26 -0700920 d = {'project1':
921 {'git1.example.com': source1_project1,
922 'git2.example.com': source2_project1},
923 'project2':
924 {'git1.example.com': source1_project2}}
925 self.assertEqual(d, tenant.projects)
926 with testtools.ExpectedException(
927 Exception,
928 "Project name 'project1' is ambiguous"):
929 tenant.getProject('project1')
930 self.assertEqual((False, source1_project2),
931 tenant.getProject('project2'))
932 self.assertEqual((True, source1_project1),
933 tenant.getProject('git1.example.com/project1'))
934 self.assertEqual((False, source2_project1),
935 tenant.getProject('git2.example.com/project1'))
936
937 source2_project2 = model.Project('project2', source2)
James E. Blair08d9b782017-06-29 14:22:48 -0700938 tpc = model.TenantProjectConfig(source2_project2)
939 tenant.addConfigProject(tpc)
James E. Blairc2a54fd2017-03-29 15:19:26 -0700940 d = {'project1':
941 {'git1.example.com': source1_project1,
942 'git2.example.com': source2_project1},
943 'project2':
944 {'git1.example.com': source1_project2,
945 'git2.example.com': source2_project2}}
946 self.assertEqual(d, tenant.projects)
947 with testtools.ExpectedException(
948 Exception,
949 "Project name 'project1' is ambiguous"):
950 tenant.getProject('project1')
951 with testtools.ExpectedException(
952 Exception,
953 "Project name 'project2' is ambiguous"):
954 tenant.getProject('project2')
955 self.assertEqual((True, source1_project1),
956 tenant.getProject('git1.example.com/project1'))
957 self.assertEqual((False, source2_project1),
958 tenant.getProject('git2.example.com/project1'))
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
964 source1_project2b = model.Project('subpath/project2', source1)
James E. Blair08d9b782017-06-29 14:22:48 -0700965 tpc = model.TenantProjectConfig(source1_project2b)
966 tenant.addConfigProject(tpc)
James E. Blairc2a54fd2017-03-29 15:19:26 -0700967 d = {'project1':
968 {'git1.example.com': source1_project1,
969 'git2.example.com': source2_project1},
970 'project2':
971 {'git1.example.com': source1_project2,
972 'git2.example.com': source2_project2},
973 'subpath/project2':
974 {'git1.example.com': source1_project2b}}
975 self.assertEqual(d, tenant.projects)
976 self.assertEqual((False, source1_project2),
977 tenant.getProject('git1.example.com/project2'))
978 self.assertEqual((True, source2_project2),
979 tenant.getProject('git2.example.com/project2'))
980 self.assertEqual((True, source1_project2b),
981 tenant.getProject('subpath/project2'))
982 self.assertEqual(
983 (True, source1_project2b),
984 tenant.getProject('git1.example.com/subpath/project2'))
985
986 source2_project2b = model.Project('subpath/project2', source2)
James E. Blair08d9b782017-06-29 14:22:48 -0700987 tpc = model.TenantProjectConfig(source2_project2b)
988 tenant.addConfigProject(tpc)
James E. Blairc2a54fd2017-03-29 15:19:26 -0700989 d = {'project1':
990 {'git1.example.com': source1_project1,
991 'git2.example.com': source2_project1},
992 'project2':
993 {'git1.example.com': source1_project2,
994 'git2.example.com': source2_project2},
995 'subpath/project2':
996 {'git1.example.com': source1_project2b,
997 'git2.example.com': source2_project2b}}
998 self.assertEqual(d, tenant.projects)
999 self.assertEqual((False, source1_project2),
1000 tenant.getProject('git1.example.com/project2'))
1001 self.assertEqual((True, source2_project2),
1002 tenant.getProject('git2.example.com/project2'))
1003 with testtools.ExpectedException(
1004 Exception,
1005 "Project name 'subpath/project2' is ambiguous"):
1006 tenant.getProject('subpath/project2')
1007 self.assertEqual(
1008 (True, source1_project2b),
1009 tenant.getProject('git1.example.com/subpath/project2'))
1010 self.assertEqual(
1011 (True, source2_project2b),
1012 tenant.getProject('git2.example.com/subpath/project2'))
1013
1014 with testtools.ExpectedException(
1015 Exception,
1016 "Project project1 is already in project index"):
James E. Blair08d9b782017-06-29 14:22:48 -07001017 tenant._addProject(source1_project1_tpc)