blob: 145c1191853896c47c89817125c72f8e013c4b6d [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
20
Maru Newby3fe5f852015-01-13 04:22:14 +000021from zuul import model
James E. Blair83005782015-12-11 14:46:03 -080022from zuul import configloader
Maru Newby3fe5f852015-01-13 04:22:14 +000023
24from tests.base import BaseTestCase
25
26
27class TestJob(BaseTestCase):
28
29 @property
30 def job(self):
James E. Blair83005782015-12-11 14:46:03 -080031 layout = model.Layout()
32 job = configloader.JobParser.fromYaml(layout, {
33 'name': 'job',
34 'irrelevant-files': [
35 '^docs/.*$'
36 ]})
Maru Newby3fe5f852015-01-13 04:22:14 +000037 return job
38
39 def test_change_matches_returns_false_for_matched_skip_if(self):
40 change = model.Change('project')
Alexander Evseevdbe6fab2015-11-19 12:46:34 +030041 change.files = ['/COMMIT_MSG', 'docs/foo']
Maru Newby3fe5f852015-01-13 04:22:14 +000042 self.assertFalse(self.job.changeMatches(change))
43
44 def test_change_matches_returns_true_for_unmatched_skip_if(self):
45 change = model.Change('project')
Alexander Evseevdbe6fab2015-11-19 12:46:34 +030046 change.files = ['/COMMIT_MSG', 'foo']
Maru Newby3fe5f852015-01-13 04:22:14 +000047 self.assertTrue(self.job.changeMatches(change))
48
Maru Newby79427a42015-02-17 17:54:45 +000049 def test_job_sets_defaults_for_boolean_attributes(self):
James E. Blair83005782015-12-11 14:46:03 -080050 self.assertIsNotNone(self.job.voting)
51
52 def test_job_inheritance(self):
53 layout = model.Layout()
54 base = configloader.JobParser.fromYaml(layout, {
55 'name': 'base',
56 'timeout': 30,
57 })
58 layout.addJob(base)
59 python27 = configloader.JobParser.fromYaml(layout, {
60 'name': 'python27',
61 'parent': 'base',
62 'timeout': 40,
63 })
64 layout.addJob(python27)
65 python27diablo = configloader.JobParser.fromYaml(layout, {
66 'name': 'python27',
67 'branches': [
68 'stable/diablo'
69 ],
70 'timeout': 50,
71 })
72 layout.addJob(python27diablo)
73
74 pipeline = model.Pipeline('gate', layout)
75 layout.addPipeline(pipeline)
76 queue = model.ChangeQueue(pipeline)
77
78 project = model.Project('project')
79 tree = pipeline.addProject(project)
80 tree.addJob(layout.getJob('python27'))
81
82 change = model.Change(project)
83 change.branch = 'master'
84 item = queue.enqueueChange(change)
85
86 self.assertTrue(base.changeMatches(change))
87 self.assertTrue(python27.changeMatches(change))
88 self.assertFalse(python27diablo.changeMatches(change))
89
90 item.freezeJobTree()
91 self.assertEqual(len(item.getJobs()), 1)
92 job = item.getJobs()[0]
93 self.assertEqual(job.name, 'python27')
94 self.assertEqual(job.timeout, 40)
95
96 change.branch = 'stable/diablo'
97
98 self.assertTrue(base.changeMatches(change))
99 self.assertTrue(python27.changeMatches(change))
100 self.assertTrue(python27diablo.changeMatches(change))
101
102 item.freezeJobTree()
103 self.assertEqual(len(item.getJobs()), 1)
104 job = item.getJobs()[0]
105 self.assertEqual(job.name, 'python27')
106 self.assertEqual(job.timeout, 50)
James E. Blairce8a2132016-05-19 15:21:52 -0700107
108
109class TestJobTimeData(BaseTestCase):
110 def setUp(self):
111 super(TestJobTimeData, self).setUp()
112 self.tmp_root = self.useFixture(fixtures.TempDir(
113 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
114 ).path
115
116 def test_empty_timedata(self):
117 path = os.path.join(self.tmp_root, 'job-name')
118 self.assertFalse(os.path.exists(path))
119 self.assertFalse(os.path.exists(path + '.tmp'))
120 td = model.JobTimeData(path)
121 self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
122 self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
123 self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
124
125 def test_save_reload(self):
126 path = os.path.join(self.tmp_root, 'job-name')
127 self.assertFalse(os.path.exists(path))
128 self.assertFalse(os.path.exists(path + '.tmp'))
129 td = model.JobTimeData(path)
130 self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
131 self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
132 self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
133 success_times = []
134 failure_times = []
135 results = []
136 for x in range(10):
137 success_times.append(int(random.random() * 1000))
138 failure_times.append(int(random.random() * 1000))
139 results.append(0)
140 results.append(1)
141 random.shuffle(results)
142 s = f = 0
143 for result in results:
144 if result:
145 td.add(failure_times[f], 'FAILURE')
146 f += 1
147 else:
148 td.add(success_times[s], 'SUCCESS')
149 s += 1
150 self.assertEqual(td.success_times, success_times)
151 self.assertEqual(td.failure_times, failure_times)
152 self.assertEqual(td.results, results[10:])
153 td.save()
154 self.assertTrue(os.path.exists(path))
155 self.assertFalse(os.path.exists(path + '.tmp'))
156 td = model.JobTimeData(path)
157 td.load()
158 self.assertEqual(td.success_times, success_times)
159 self.assertEqual(td.failure_times, failure_times)
160 self.assertEqual(td.results, results[10:])
161
162
163class TestTimeDataBase(BaseTestCase):
164 def setUp(self):
165 super(TestTimeDataBase, self).setUp()
166 self.tmp_root = self.useFixture(fixtures.TempDir(
167 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
168 ).path
169 self.db = model.TimeDataBase(self.tmp_root)
170
171 def test_timedatabase(self):
172 self.assertEqual(self.db.getEstimatedTime('job-name'), 0)
173 self.db.update('job-name', 50, 'SUCCESS')
174 self.assertEqual(self.db.getEstimatedTime('job-name'), 50)
175 self.db.update('job-name', 100, 'SUCCESS')
176 self.assertEqual(self.db.getEstimatedTime('job-name'), 75)
177 for x in range(10):
178 self.db.update('job-name', 100, 'SUCCESS')
179 self.assertEqual(self.db.getEstimatedTime('job-name'), 100)