blob: fa670a4a8aba58a1eeec7dcf0966107faa8d2b8f [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. Blair4317e9f2016-07-15 10:05:47 -070059 project = model.Project('project')
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()
125 project = model.Project('project')
126
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': {
139 'password': {
140 'pypipassword': 'dummypassword'
141 }
142 }
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,
152 'password': {
153 'pypipassword': 'dummypassword'
154 }
155 }
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,
166 'password': {
167 'pypipassword': 'dummypassword'
168 }
169 }
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)
193 self.assertIn('password', in_repo_job_with_inherit.auth)
194 self.assertEquals(in_repo_job_with_inherit.auth['password'],
195 {'pypipassword': 'dummypassword'})
196 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. Blair4317e9f2016-07-15 10:05:47 -0700204 project = model.Project('project')
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
James E. Blair4317e9f2016-07-15 10:05:47 -0700268 def test_job_source_project(self):
269 layout = model.Layout()
270 base_project = model.Project('base_project')
271 base = configloader.JobParser.fromYaml(layout, {
272 '_source_project': base_project,
273 'name': 'base',
274 })
275 layout.addJob(base)
276
277 other_project = model.Project('other_project')
278 base2 = configloader.JobParser.fromYaml(layout, {
279 '_source_project': other_project,
280 'name': 'base',
281 })
282 with testtools.ExpectedException(
283 Exception,
284 "Job base in other_project is not permitted "
285 "to shadow job base in base_project"):
286 layout.addJob(base2)
287
James E. Blairce8a2132016-05-19 15:21:52 -0700288
289class TestJobTimeData(BaseTestCase):
290 def setUp(self):
291 super(TestJobTimeData, self).setUp()
292 self.tmp_root = self.useFixture(fixtures.TempDir(
293 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
294 ).path
295
296 def test_empty_timedata(self):
297 path = os.path.join(self.tmp_root, 'job-name')
298 self.assertFalse(os.path.exists(path))
299 self.assertFalse(os.path.exists(path + '.tmp'))
300 td = model.JobTimeData(path)
301 self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
302 self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
303 self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
304
305 def test_save_reload(self):
306 path = os.path.join(self.tmp_root, 'job-name')
307 self.assertFalse(os.path.exists(path))
308 self.assertFalse(os.path.exists(path + '.tmp'))
309 td = model.JobTimeData(path)
310 self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
311 self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
312 self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
313 success_times = []
314 failure_times = []
315 results = []
316 for x in range(10):
317 success_times.append(int(random.random() * 1000))
318 failure_times.append(int(random.random() * 1000))
319 results.append(0)
320 results.append(1)
321 random.shuffle(results)
322 s = f = 0
323 for result in results:
324 if result:
325 td.add(failure_times[f], 'FAILURE')
326 f += 1
327 else:
328 td.add(success_times[s], 'SUCCESS')
329 s += 1
330 self.assertEqual(td.success_times, success_times)
331 self.assertEqual(td.failure_times, failure_times)
332 self.assertEqual(td.results, results[10:])
333 td.save()
334 self.assertTrue(os.path.exists(path))
335 self.assertFalse(os.path.exists(path + '.tmp'))
336 td = model.JobTimeData(path)
337 td.load()
338 self.assertEqual(td.success_times, success_times)
339 self.assertEqual(td.failure_times, failure_times)
340 self.assertEqual(td.results, results[10:])
341
342
343class TestTimeDataBase(BaseTestCase):
344 def setUp(self):
345 super(TestTimeDataBase, self).setUp()
346 self.tmp_root = self.useFixture(fixtures.TempDir(
347 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
348 ).path
349 self.db = model.TimeDataBase(self.tmp_root)
350
351 def test_timedatabase(self):
352 self.assertEqual(self.db.getEstimatedTime('job-name'), 0)
353 self.db.update('job-name', 50, 'SUCCESS')
354 self.assertEqual(self.db.getEstimatedTime('job-name'), 50)
355 self.db.update('job-name', 100, 'SUCCESS')
356 self.assertEqual(self.db.getEstimatedTime('job-name'), 75)
357 for x in range(10):
358 self.db.update('job-name', 100, 'SUCCESS')
359 self.assertEqual(self.db.getEstimatedTime('job-name'), 100)