blob: ac19383ff84a11a064c4c28abebae381bc16e80e [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
James E. Blairce8a2132016-05-19 15:21:52 -070015import os
16import random
17
18import fixtures
19
Maru Newby3fe5f852015-01-13 04:22:14 +000020from zuul import change_matcher as cm
21from zuul import model
22
23from tests.base import BaseTestCase
24
25
26class TestJob(BaseTestCase):
27
28 @property
29 def job(self):
30 job = model.Job('job')
31 job.skip_if_matcher = cm.MatchAll([
32 cm.ProjectMatcher('^project$'),
33 cm.MatchAllFiles([cm.FileMatcher('^docs/.*$')]),
34 ])
35 return job
36
37 def test_change_matches_returns_false_for_matched_skip_if(self):
38 change = model.Change('project')
39 change.files = ['docs/foo']
40 self.assertFalse(self.job.changeMatches(change))
41
42 def test_change_matches_returns_true_for_unmatched_skip_if(self):
43 change = model.Change('project')
44 change.files = ['foo']
45 self.assertTrue(self.job.changeMatches(change))
46
47 def test_copy_retains_skip_if(self):
48 job = model.Job('job')
49 job.copy(self.job)
50 self.assertTrue(job.skip_if_matcher)
Maru Newby79427a42015-02-17 17:54:45 +000051
52 def _assert_job_booleans_are_not_none(self, job):
53 self.assertIsNotNone(job.voting)
54 self.assertIsNotNone(job.hold_following_changes)
55
56 def test_job_sets_defaults_for_boolean_attributes(self):
57 job = model.Job('job')
58 self._assert_job_booleans_are_not_none(job)
59
60 def test_metajob_does_not_set_defaults_for_boolean_attributes(self):
61 job = model.Job('^job')
62 self.assertIsNone(job.voting)
63 self.assertIsNone(job.hold_following_changes)
64
65 def test_metajob_copy_does_not_set_undefined_boolean_attributes(self):
66 job = model.Job('job')
67 metajob = model.Job('^job')
68 job.copy(metajob)
69 self._assert_job_booleans_are_not_none(job)
James E. Blairce8a2132016-05-19 15:21:52 -070070
71
72class TestJobTimeData(BaseTestCase):
73 def setUp(self):
74 super(TestJobTimeData, self).setUp()
75 self.tmp_root = self.useFixture(fixtures.TempDir(
76 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
77 ).path
78
79 def test_empty_timedata(self):
80 path = os.path.join(self.tmp_root, 'job-name')
81 self.assertFalse(os.path.exists(path))
82 self.assertFalse(os.path.exists(path + '.tmp'))
83 td = model.JobTimeData(path)
84 self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
85 self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
86 self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
87
88 def test_save_reload(self):
89 path = os.path.join(self.tmp_root, 'job-name')
90 self.assertFalse(os.path.exists(path))
91 self.assertFalse(os.path.exists(path + '.tmp'))
92 td = model.JobTimeData(path)
93 self.assertEqual(td.success_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
94 self.assertEqual(td.failure_times, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
95 self.assertEqual(td.results, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
96 success_times = []
97 failure_times = []
98 results = []
99 for x in range(10):
100 success_times.append(int(random.random() * 1000))
101 failure_times.append(int(random.random() * 1000))
102 results.append(0)
103 results.append(1)
104 random.shuffle(results)
105 s = f = 0
106 for result in results:
107 if result:
108 td.add(failure_times[f], 'FAILURE')
109 f += 1
110 else:
111 td.add(success_times[s], 'SUCCESS')
112 s += 1
113 self.assertEqual(td.success_times, success_times)
114 self.assertEqual(td.failure_times, failure_times)
115 self.assertEqual(td.results, results[10:])
116 td.save()
117 self.assertTrue(os.path.exists(path))
118 self.assertFalse(os.path.exists(path + '.tmp'))
119 td = model.JobTimeData(path)
120 td.load()
121 self.assertEqual(td.success_times, success_times)
122 self.assertEqual(td.failure_times, failure_times)
123 self.assertEqual(td.results, results[10:])
124
125
126class TestTimeDataBase(BaseTestCase):
127 def setUp(self):
128 super(TestTimeDataBase, self).setUp()
129 self.tmp_root = self.useFixture(fixtures.TempDir(
130 rootdir=os.environ.get("ZUUL_TEST_ROOT"))
131 ).path
132 self.db = model.TimeDataBase(self.tmp_root)
133
134 def test_timedatabase(self):
135 self.assertEqual(self.db.getEstimatedTime('job-name'), 0)
136 self.db.update('job-name', 50, 'SUCCESS')
137 self.assertEqual(self.db.getEstimatedTime('job-name'), 50)
138 self.db.update('job-name', 100, 'SUCCESS')
139 self.assertEqual(self.db.getEstimatedTime('job-name'), 75)
140 for x in range(10):
141 self.db.update('job-name', 100, 'SUCCESS')
142 self.assertEqual(self.db.getEstimatedTime('job-name'), 100)