blob: db98d14c2d7f08e869fe58cac8cd3ed6b88b79f7 [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
Maru Newby3fe5f852015-01-13 04:22:14 +000024
25from tests.base import BaseTestCase
26
27
28class TestJob(BaseTestCase):
29
30 @property
31 def job(self):
James E. Blair83005782015-12-11 14:46:03 -080032 layout = model.Layout()
33 job = configloader.JobParser.fromYaml(layout, {
34 'name': 'job',
35 'irrelevant-files': [
36 '^docs/.*$'
37 ]})
Maru Newby3fe5f852015-01-13 04:22:14 +000038 return job
39
40 def test_change_matches_returns_false_for_matched_skip_if(self):
41 change = model.Change('project')
Alexander Evseevdbe6fab2015-11-19 12:46:34 +030042 change.files = ['/COMMIT_MSG', 'docs/foo']
Maru Newby3fe5f852015-01-13 04:22:14 +000043 self.assertFalse(self.job.changeMatches(change))
44
45 def test_change_matches_returns_true_for_unmatched_skip_if(self):
46 change = model.Change('project')
Alexander Evseevdbe6fab2015-11-19 12:46:34 +030047 change.files = ['/COMMIT_MSG', 'foo']
Maru Newby3fe5f852015-01-13 04:22:14 +000048 self.assertTrue(self.job.changeMatches(change))
49
Maru Newby79427a42015-02-17 17:54:45 +000050 def test_job_sets_defaults_for_boolean_attributes(self):
James E. Blair83005782015-12-11 14:46:03 -080051 self.assertIsNotNone(self.job.voting)
52
53 def test_job_inheritance(self):
54 layout = model.Layout()
James E. Blair8b1dc3f2016-07-05 16:49:00 -070055
56 pipeline = model.Pipeline('gate', layout)
57 layout.addPipeline(pipeline)
58 queue = model.ChangeQueue(pipeline)
James E. Blairc73c73a2017-01-20 15:15:15 -080059 project = model.Project('project', None)
James E. Blair8b1dc3f2016-07-05 16:49:00 -070060
James E. Blair83005782015-12-11 14:46:03 -080061 base = configloader.JobParser.fromYaml(layout, {
James E. Blair4317e9f2016-07-15 10:05:47 -070062 '_source_project': project,
James E. Blair83005782015-12-11 14:46:03 -080063 'name': 'base',
64 'timeout': 30,
65 })
66 layout.addJob(base)
67 python27 = configloader.JobParser.fromYaml(layout, {
James E. Blair4317e9f2016-07-15 10:05:47 -070068 '_source_project': project,
James E. Blair83005782015-12-11 14:46:03 -080069 'name': 'python27',
70 'parent': 'base',
71 'timeout': 40,
72 })
73 layout.addJob(python27)
74 python27diablo = configloader.JobParser.fromYaml(layout, {
James E. Blair4317e9f2016-07-15 10:05:47 -070075 '_source_project': project,
James E. Blair83005782015-12-11 14:46:03 -080076 'name': 'python27',
77 'branches': [
78 'stable/diablo'
79 ],
80 'timeout': 50,
81 })
82 layout.addJob(python27diablo)
83
James E. Blair8b1dc3f2016-07-05 16:49:00 -070084 project_config = configloader.ProjectParser.fromYaml(layout, {
85 'name': 'project',
86 'gate': {
87 'jobs': [
88 'python27'
89 ]
90 }
91 })
92 layout.addProjectConfig(project_config, update_pipeline=False)
James E. Blair83005782015-12-11 14:46:03 -080093
James E. Blair83005782015-12-11 14:46:03 -080094 change = model.Change(project)
95 change.branch = 'master'
96 item = queue.enqueueChange(change)
James E. Blair8b1dc3f2016-07-05 16:49:00 -070097 item.current_build_set.layout = layout
James E. Blair83005782015-12-11 14:46:03 -080098
99 self.assertTrue(base.changeMatches(change))
100 self.assertTrue(python27.changeMatches(change))
101 self.assertFalse(python27diablo.changeMatches(change))
102
103 item.freezeJobTree()
104 self.assertEqual(len(item.getJobs()), 1)
105 job = item.getJobs()[0]
106 self.assertEqual(job.name, 'python27')
107 self.assertEqual(job.timeout, 40)
108
109 change.branch = 'stable/diablo'
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700110 item = queue.enqueueChange(change)
111 item.current_build_set.layout = layout
James E. Blair83005782015-12-11 14:46:03 -0800112
113 self.assertTrue(base.changeMatches(change))
114 self.assertTrue(python27.changeMatches(change))
115 self.assertTrue(python27diablo.changeMatches(change))
116
117 item.freezeJobTree()
118 self.assertEqual(len(item.getJobs()), 1)
119 job = item.getJobs()[0]
120 self.assertEqual(job.name, 'python27')
121 self.assertEqual(job.timeout, 50)
James E. Blairce8a2132016-05-19 15:21:52 -0700122
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000123 def test_job_auth_inheritance(self):
124 layout = model.Layout()
James E. Blairc73c73a2017-01-20 15:15:15 -0800125 project = model.Project('project', None)
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000126
127 base = configloader.JobParser.fromYaml(layout, {
128 '_source_project': project,
129 'name': 'base',
130 'timeout': 30,
131 })
132 layout.addJob(base)
133 pypi_upload_without_inherit = configloader.JobParser.fromYaml(layout, {
134 '_source_project': project,
135 'name': 'pypi-upload-without-inherit',
136 'parent': 'base',
137 'timeout': 40,
138 'auth': {
Ricardo Carrillo Cruz12c892b2016-11-18 15:35:49 +0000139 'secrets': [
140 'pypi-credentials',
141 ]
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000142 }
143 })
144 layout.addJob(pypi_upload_without_inherit)
145 pypi_upload_with_inherit = configloader.JobParser.fromYaml(layout, {
146 '_source_project': project,
147 'name': 'pypi-upload-with-inherit',
148 'parent': 'base',
149 'timeout': 40,
150 'auth': {
151 'inherit': True,
Ricardo Carrillo Cruz12c892b2016-11-18 15:35:49 +0000152 'secrets': [
153 'pypi-credentials',
154 ]
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000155 }
156 })
157 layout.addJob(pypi_upload_with_inherit)
158 pypi_upload_with_inherit_false = configloader.JobParser.fromYaml(
159 layout, {
160 '_source_project': project,
161 'name': 'pypi-upload-with-inherit-false',
162 'parent': 'base',
163 'timeout': 40,
164 'auth': {
165 'inherit': False,
Ricardo Carrillo Cruz12c892b2016-11-18 15:35:49 +0000166 'secrets': [
167 'pypi-credentials',
168 ]
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000169 }
170 })
171 layout.addJob(pypi_upload_with_inherit_false)
172 in_repo_job_without_inherit = configloader.JobParser.fromYaml(layout, {
173 '_source_project': project,
174 'name': 'in-repo-job-without-inherit',
175 'parent': 'pypi-upload-without-inherit',
176 })
177 layout.addJob(in_repo_job_without_inherit)
178 in_repo_job_with_inherit = configloader.JobParser.fromYaml(layout, {
179 '_source_project': project,
180 'name': 'in-repo-job-with-inherit',
181 'parent': 'pypi-upload-with-inherit',
182 })
183 layout.addJob(in_repo_job_with_inherit)
184 in_repo_job_with_inherit_false = configloader.JobParser.fromYaml(
185 layout, {
186 '_source_project': project,
187 'name': 'in-repo-job-with-inherit-false',
188 'parent': 'pypi-upload-with-inherit-false',
189 })
190 layout.addJob(in_repo_job_with_inherit_false)
191
192 self.assertNotIn('auth', in_repo_job_without_inherit.auth)
Ricardo Carrillo Cruz12c892b2016-11-18 15:35:49 +0000193 self.assertIn('secrets', in_repo_job_with_inherit.auth)
194 self.assertEquals(in_repo_job_with_inherit.auth['secrets'],
195 ['pypi-credentials'])
Ricardo Carrillo Cruz4e94f612016-07-25 16:11:56 +0000196 self.assertNotIn('auth', in_repo_job_with_inherit_false.auth)
197
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700198 def test_job_inheritance_job_tree(self):
199 layout = model.Layout()
200
201 pipeline = model.Pipeline('gate', layout)
202 layout.addPipeline(pipeline)
203 queue = model.ChangeQueue(pipeline)
James E. Blairc73c73a2017-01-20 15:15:15 -0800204 project = model.Project('project', None)
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700205
206 base = configloader.JobParser.fromYaml(layout, {
James E. Blair4317e9f2016-07-15 10:05:47 -0700207 '_source_project': project,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700208 'name': 'base',
209 'timeout': 30,
210 })
211 layout.addJob(base)
212 python27 = configloader.JobParser.fromYaml(layout, {
James E. Blair4317e9f2016-07-15 10:05:47 -0700213 '_source_project': project,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700214 'name': 'python27',
215 'parent': 'base',
216 'timeout': 40,
217 })
218 layout.addJob(python27)
219 python27diablo = configloader.JobParser.fromYaml(layout, {
James E. Blair4317e9f2016-07-15 10:05:47 -0700220 '_source_project': project,
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700221 'name': 'python27',
222 'branches': [
223 'stable/diablo'
224 ],
225 'timeout': 50,
226 })
227 layout.addJob(python27diablo)
228
229 project_config = configloader.ProjectParser.fromYaml(layout, {
230 'name': 'project',
231 'gate': {
232 'jobs': [
233 {'python27': {'timeout': 70}}
234 ]
235 }
236 })
237 layout.addProjectConfig(project_config, update_pipeline=False)
238
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700239 change = model.Change(project)
240 change.branch = 'master'
241 item = queue.enqueueChange(change)
242 item.current_build_set.layout = layout
243
244 self.assertTrue(base.changeMatches(change))
245 self.assertTrue(python27.changeMatches(change))
246 self.assertFalse(python27diablo.changeMatches(change))
247
248 item.freezeJobTree()
249 self.assertEqual(len(item.getJobs()), 1)
250 job = item.getJobs()[0]
251 self.assertEqual(job.name, 'python27')
252 self.assertEqual(job.timeout, 70)
253
254 change.branch = 'stable/diablo'
255 item = queue.enqueueChange(change)
256 item.current_build_set.layout = layout
257
258 self.assertTrue(base.changeMatches(change))
259 self.assertTrue(python27.changeMatches(change))
260 self.assertTrue(python27diablo.changeMatches(change))
261
262 item.freezeJobTree()
263 self.assertEqual(len(item.getJobs()), 1)
264 job = item.getJobs()[0]
265 self.assertEqual(job.name, 'python27')
266 self.assertEqual(job.timeout, 70)
267
Clint Byrum85493602016-11-18 11:59:47 -0800268 def test_inheritance_keeps_matchers(self):
269 layout = model.Layout()
270
271 pipeline = model.Pipeline('gate', layout)
272 layout.addPipeline(pipeline)
273 queue = model.ChangeQueue(pipeline)
James E. Blairc73c73a2017-01-20 15:15:15 -0800274 project = model.Project('project', None)
Clint Byrum85493602016-11-18 11:59:47 -0800275
276 base = configloader.JobParser.fromYaml(layout, {
277 '_source_project': project,
278 'name': 'base',
279 'timeout': 30,
280 })
281 layout.addJob(base)
282 python27 = configloader.JobParser.fromYaml(layout, {
283 '_source_project': project,
284 'name': 'python27',
285 'parent': 'base',
286 'timeout': 40,
287 'irrelevant-files': ['^ignored-file$'],
288 })
289 layout.addJob(python27)
290
291 project_config = configloader.ProjectParser.fromYaml(layout, {
292 'name': 'project',
293 'gate': {
294 'jobs': [
295 'python27',
296 ]
297 }
298 })
299 layout.addProjectConfig(project_config, update_pipeline=False)
300
301 change = model.Change(project)
302 change.branch = 'master'
303 change.files = ['/COMMIT_MSG', 'ignored-file']
304 item = queue.enqueueChange(change)
305 item.current_build_set.layout = layout
306
307 self.assertTrue(base.changeMatches(change))
308 self.assertFalse(python27.changeMatches(change))
309
310 item.freezeJobTree()
311 self.assertEqual([], item.getJobs())
312
James E. Blair4317e9f2016-07-15 10:05:47 -0700313 def test_job_source_project(self):
314 layout = model.Layout()
James E. Blairc73c73a2017-01-20 15:15:15 -0800315 base_project = model.Project('base_project', None)
James E. Blair4317e9f2016-07-15 10:05:47 -0700316 base = configloader.JobParser.fromYaml(layout, {
317 '_source_project': base_project,
318 'name': 'base',
319 })
320 layout.addJob(base)
321
James E. Blairc73c73a2017-01-20 15:15:15 -0800322 other_project = model.Project('other_project', None)
James E. Blair4317e9f2016-07-15 10:05:47 -0700323 base2 = configloader.JobParser.fromYaml(layout, {
324 '_source_project': other_project,
325 'name': 'base',
326 })
327 with testtools.ExpectedException(
328 Exception,
329 "Job base in other_project is not permitted "
330 "to shadow job base in base_project"):
331 layout.addJob(base2)
332
James E. Blairce8a2132016-05-19 15:21:52 -0700333
334class TestJobTimeData(BaseTestCase):
335 def setUp(self):
336 super(TestJobTimeData, self).setUp()
337 self.tmp_root = self.useFixture(fixtures.TempDir(
338 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
339 ).path
340
341 def test_empty_timedata(self):
342 path = os.path.join(self.tmp_root, 'job-name')
343 self.assertFalse(os.path.exists(path))
344 self.assertFalse(os.path.exists(path + '.tmp'))
345 td = model.JobTimeData(path)
346 self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
347 self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
348 self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
349
350 def test_save_reload(self):
351 path = os.path.join(self.tmp_root, 'job-name')
352 self.assertFalse(os.path.exists(path))
353 self.assertFalse(os.path.exists(path + '.tmp'))
354 td = model.JobTimeData(path)
355 self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
356 self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
357 self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
358 success_times = []
359 failure_times = []
360 results = []
361 for x in range(10):
362 success_times.append(int(random.random() * 1000))
363 failure_times.append(int(random.random() * 1000))
364 results.append(0)
365 results.append(1)
366 random.shuffle(results)
367 s = f = 0
368 for result in results:
369 if result:
370 td.add(failure_times[f], 'FAILURE')
371 f += 1
372 else:
373 td.add(success_times[s], 'SUCCESS')
374 s += 1
375 self.assertEqual(td.success_times, success_times)
376 self.assertEqual(td.failure_times, failure_times)
377 self.assertEqual(td.results, results[10:])
378 td.save()
379 self.assertTrue(os.path.exists(path))
380 self.assertFalse(os.path.exists(path + '.tmp'))
381 td = model.JobTimeData(path)
382 td.load()
383 self.assertEqual(td.success_times, success_times)
384 self.assertEqual(td.failure_times, failure_times)
385 self.assertEqual(td.results, results[10:])
386
387
388class TestTimeDataBase(BaseTestCase):
389 def setUp(self):
390 super(TestTimeDataBase, self).setUp()
391 self.tmp_root = self.useFixture(fixtures.TempDir(
392 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
393 ).path
394 self.db = model.TimeDataBase(self.tmp_root)
395
396 def test_timedatabase(self):
397 self.assertEqual(self.db.getEstimatedTime('job-name'), 0)
398 self.db.update('job-name', 50, 'SUCCESS')
399 self.assertEqual(self.db.getEstimatedTime('job-name'), 50)
400 self.db.update('job-name', 100, 'SUCCESS')
401 self.assertEqual(self.db.getEstimatedTime('job-name'), 75)
402 for x in range(10):
403 self.db.update('job-name', 100, 'SUCCESS')
404 self.assertEqual(self.db.getEstimatedTime('job-name'), 100)