blob: 8d9d12734a69323e32c18933284783fcb2f19457 [file] [log] [blame]
Joshua Hesketh352264b2015-08-11 23:42:08 +10001# Copyright 2014 Rackspace Australia
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 Heskethd78b4482015-09-14 16:56:34 -060015import sqlalchemy as sa
Joshua Heskethd78b4482015-09-14 16:56:34 -060016
17from tests.base import ZuulTestCase, ZuulDBTestCase
18
19
20def _get_reporter_from_connection_name(reporters, connection_name):
21 # Reporters are placed into lists for each action they may exist in.
22 # Search through the given list for the correct reporter by its conncetion
23 # name
24 for r in reporters:
25 if r.connection.connection_name == connection_name:
26 return r
Joshua Heskethacccffc2015-03-31 23:38:17 +110027
Joshua Hesketh352264b2015-08-11 23:42:08 +100028
Joshua Heskethacccffc2015-03-31 23:38:17 +110029class TestConnections(ZuulTestCase):
Morgan Fainberg4245a422016-08-05 16:20:12 -070030 config_file = 'zuul-connections-same-gerrit.conf'
31 tenant_config_file = 'config/zuul-connections-same-gerrit/main.yaml'
Joshua Heskethacccffc2015-03-31 23:38:17 +110032
Joshua Heskethd78b4482015-09-14 16:56:34 -060033 def test_multiple_gerrit_connections(self):
Joshua Heskethacccffc2015-03-31 23:38:17 +110034 "Test multiple connections to the one gerrit"
35
36 A = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair7fc8daa2016-08-08 15:37:15 -070037 self.addEvent('review_gerrit', A.getPatchsetCreatedEvent(1))
Joshua Heskethacccffc2015-03-31 23:38:17 +110038
39 self.waitUntilSettled()
40
41 self.assertEqual(len(A.patchsets[-1]['approvals']), 1)
James E. Blair8b5408c2016-08-08 15:37:46 -070042 self.assertEqual(A.patchsets[-1]['approvals'][0]['type'], 'verified')
Joshua Heskethacccffc2015-03-31 23:38:17 +110043 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1')
44 self.assertEqual(A.patchsets[-1]['approvals'][0]['by']['username'],
45 'jenkins')
46
47 B = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'B')
Paul Belanger174a8272017-03-14 13:20:10 -040048 self.executor_server.failJob('project-test2', B)
James E. Blair7fc8daa2016-08-08 15:37:15 -070049 self.addEvent('review_gerrit', B.getPatchsetCreatedEvent(1))
Joshua Heskethacccffc2015-03-31 23:38:17 +110050
51 self.waitUntilSettled()
52
53 self.assertEqual(len(B.patchsets[-1]['approvals']), 1)
James E. Blair8b5408c2016-08-08 15:37:46 -070054 self.assertEqual(B.patchsets[-1]['approvals'][0]['type'], 'verified')
Joshua Heskethacccffc2015-03-31 23:38:17 +110055 self.assertEqual(B.patchsets[-1]['approvals'][0]['value'], '-1')
56 self.assertEqual(B.patchsets[-1]['approvals'][0]['by']['username'],
57 'civoter')
58
James E. Blair82844892017-03-06 10:55:26 -080059
60class TestSQLConnection(ZuulDBTestCase):
61 config_file = 'zuul-sql-driver.conf'
62 tenant_config_file = 'config/sql-driver/main.yaml'
63
64 def test_sql_tables_created(self, metadata_table=None):
Joshua Heskethd78b4482015-09-14 16:56:34 -060065 "Test the tables for storing results are created properly"
66 buildset_table = 'zuul_buildset'
67 build_table = 'zuul_build'
68
69 insp = sa.engine.reflection.Inspector(
James E. Blair82844892017-03-06 10:55:26 -080070 self.connections.connections['resultsdb'].engine)
Joshua Heskethd78b4482015-09-14 16:56:34 -060071
72 self.assertEqual(9, len(insp.get_columns(buildset_table)))
73 self.assertEqual(10, len(insp.get_columns(build_table)))
74
James E. Blair82844892017-03-06 10:55:26 -080075 def test_sql_results(self):
Joshua Heskethd78b4482015-09-14 16:56:34 -060076 "Test results are entered into an sql table"
77 # Grab the sa tables
James E. Blair82844892017-03-06 10:55:26 -080078 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethd78b4482015-09-14 16:56:34 -060079 reporter = _get_reporter_from_connection_name(
James E. Blair82844892017-03-06 10:55:26 -080080 tenant.layout.pipelines['check'].success_actions,
Joshua Heskethd78b4482015-09-14 16:56:34 -060081 'resultsdb'
82 )
83
84 # Add a success result
James E. Blair82844892017-03-06 10:55:26 -080085 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
86 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Joshua Heskethd78b4482015-09-14 16:56:34 -060087 self.waitUntilSettled()
88
89 # Add a failed result for a negative score
James E. Blair82844892017-03-06 10:55:26 -080090 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
91
92 self.executor_server.failJob('project-test1', B)
93 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
Joshua Heskethd78b4482015-09-14 16:56:34 -060094 self.waitUntilSettled()
95
James E. Blair82844892017-03-06 10:55:26 -080096 conn = self.connections.connections['resultsdb'].engine.connect()
Joshua Heskethd78b4482015-09-14 16:56:34 -060097 result = conn.execute(
98 sa.sql.select([reporter.connection.zuul_buildset_table]))
99
100 buildsets = result.fetchall()
101 self.assertEqual(2, len(buildsets))
102 buildset0 = buildsets[0]
103 buildset1 = buildsets[1]
104
105 self.assertEqual('check', buildset0['pipeline'])
106 self.assertEqual('org/project', buildset0['project'])
107 self.assertEqual(1, buildset0['change'])
108 self.assertEqual(1, buildset0['patchset'])
109 self.assertEqual(1, buildset0['score'])
110 self.assertEqual('Build succeeded.', buildset0['message'])
111
112 buildset0_builds = conn.execute(
113 sa.sql.select([reporter.connection.zuul_build_table]).
114 where(
115 reporter.connection.zuul_build_table.c.buildset_id ==
116 buildset0['id']
117 )
118 ).fetchall()
119
120 # Check the first result, which should be the project-merge job
121 self.assertEqual('project-merge', buildset0_builds[0]['job_name'])
122 self.assertEqual("SUCCESS", buildset0_builds[0]['result'])
Monty Taylorde8242c2017-02-23 20:29:53 -0600123 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -0500124 'finger://{hostname}/{uuid}'.format(
125 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -0600126 uuid=buildset0_builds[0]['uuid']),
127 buildset0_builds[0]['log_url'])
Joshua Heskethd78b4482015-09-14 16:56:34 -0600128 self.assertEqual('check', buildset1['pipeline'])
129 self.assertEqual('org/project', buildset1['project'])
130 self.assertEqual(2, buildset1['change'])
131 self.assertEqual(1, buildset1['patchset'])
132 self.assertEqual(-1, buildset1['score'])
133 self.assertEqual('Build failed.', buildset1['message'])
134
135 buildset1_builds = conn.execute(
136 sa.sql.select([reporter.connection.zuul_build_table]).
137 where(
138 reporter.connection.zuul_build_table.c.buildset_id ==
139 buildset1['id']
140 )
141 ).fetchall()
142
143 # Check the second last result, which should be the project-test1 job
144 # which failed
145 self.assertEqual('project-test1', buildset1_builds[-2]['job_name'])
146 self.assertEqual("FAILURE", buildset1_builds[-2]['result'])
Monty Taylorde8242c2017-02-23 20:29:53 -0600147 self.assertEqual(
Monty Taylor51139a02016-05-24 11:28:10 -0500148 'finger://{hostname}/{uuid}'.format(
149 hostname=self.executor_server.hostname,
Monty Taylorde8242c2017-02-23 20:29:53 -0600150 uuid=buildset1_builds[-2]['uuid']),
151 buildset1_builds[-2]['log_url'])
Joshua Heskethd78b4482015-09-14 16:56:34 -0600152
Joshua Heskethd78b4482015-09-14 16:56:34 -0600153 def test_multiple_sql_connections(self):
154 "Test putting results in different databases"
Joshua Heskethd78b4482015-09-14 16:56:34 -0600155 # Add a successful result
James E. Blair82844892017-03-06 10:55:26 -0800156 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
157 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Joshua Heskethd78b4482015-09-14 16:56:34 -0600158 self.waitUntilSettled()
159
160 # Add a failed result
James E. Blair82844892017-03-06 10:55:26 -0800161 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
162 self.executor_server.failJob('project-test1', B)
163 self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
Joshua Heskethd78b4482015-09-14 16:56:34 -0600164 self.waitUntilSettled()
165
166 # Grab the sa tables for resultsdb
James E. Blair82844892017-03-06 10:55:26 -0800167 tenant = self.sched.abide.tenants.get('tenant-one')
Joshua Heskethd78b4482015-09-14 16:56:34 -0600168 reporter1 = _get_reporter_from_connection_name(
James E. Blair82844892017-03-06 10:55:26 -0800169 tenant.layout.pipelines['check'].success_actions,
Joshua Heskethd78b4482015-09-14 16:56:34 -0600170 'resultsdb'
171 )
172
James E. Blair82844892017-03-06 10:55:26 -0800173 conn = self.connections.connections['resultsdb'].engine.connect()
Joshua Heskethd78b4482015-09-14 16:56:34 -0600174 buildsets_resultsdb = conn.execute(sa.sql.select(
175 [reporter1.connection.zuul_buildset_table])).fetchall()
176 # Should have been 2 buildset reported to the resultsdb (both success
177 # and failure report)
178 self.assertEqual(2, len(buildsets_resultsdb))
179
180 # The first one should have passed
181 self.assertEqual('check', buildsets_resultsdb[0]['pipeline'])
182 self.assertEqual('org/project', buildsets_resultsdb[0]['project'])
183 self.assertEqual(1, buildsets_resultsdb[0]['change'])
184 self.assertEqual(1, buildsets_resultsdb[0]['patchset'])
185 self.assertEqual(1, buildsets_resultsdb[0]['score'])
186 self.assertEqual('Build succeeded.', buildsets_resultsdb[0]['message'])
187
188 # Grab the sa tables for resultsdb_failures
189 reporter2 = _get_reporter_from_connection_name(
James E. Blair82844892017-03-06 10:55:26 -0800190 tenant.layout.pipelines['check'].failure_actions,
Joshua Heskethd78b4482015-09-14 16:56:34 -0600191 'resultsdb_failures'
192 )
193
James E. Blair82844892017-03-06 10:55:26 -0800194 conn = self.connections.connections['resultsdb_failures'].\
195 engine.connect()
Joshua Heskethd78b4482015-09-14 16:56:34 -0600196 buildsets_resultsdb_failures = conn.execute(sa.sql.select(
197 [reporter2.connection.zuul_buildset_table])).fetchall()
198 # The failure db should only have 1 buildset failed
199 self.assertEqual(1, len(buildsets_resultsdb_failures))
200
201 self.assertEqual('check', buildsets_resultsdb_failures[0]['pipeline'])
202 self.assertEqual(
203 'org/project', buildsets_resultsdb_failures[0]['project'])
204 self.assertEqual(2, buildsets_resultsdb_failures[0]['change'])
205 self.assertEqual(1, buildsets_resultsdb_failures[0]['patchset'])
206 self.assertEqual(-1, buildsets_resultsdb_failures[0]['score'])
207 self.assertEqual(
208 'Build failed.', buildsets_resultsdb_failures[0]['message'])
209
210
211class TestConnectionsBadSQL(ZuulDBTestCase):
James E. Blair82844892017-03-06 10:55:26 -0800212 config_file = 'zuul-sql-driver-bad.conf'
213 tenant_config_file = 'config/sql-driver/main.yaml'
Joshua Heskethd78b4482015-09-14 16:56:34 -0600214
215 def test_unable_to_connect(self):
216 "Test the SQL reporter fails gracefully when unable to connect"
217 self.config.set('zuul', 'layout_config',
218 'tests/fixtures/layout-sql-reporter.yaml')
219 self.sched.reconfigure(self.config)
220
221 # Trigger a reporter. If no errors are raised, the reporter has been
222 # disabled correctly
James E. Blair82844892017-03-06 10:55:26 -0800223 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
224 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
Joshua Heskethd78b4482015-09-14 16:56:34 -0600225 self.waitUntilSettled()
226
Joshua Heskethacccffc2015-03-31 23:38:17 +1100227
228class TestMultipleGerrits(ZuulTestCase):
Jamie Lennoxd2e37332016-12-05 15:26:19 +1100229 config_file = 'zuul-connections-multiple-gerrits.conf'
230 tenant_config_file = 'config/zuul-connections-multiple-gerrits/main.yaml'
Joshua Heskethacccffc2015-03-31 23:38:17 +1100231
232 def test_multiple_project_separate_gerrits(self):
Paul Belanger174a8272017-03-14 13:20:10 -0400233 self.executor_server.hold_jobs_in_build = True
Joshua Heskethacccffc2015-03-31 23:38:17 +1100234
235 A = self.fake_another_gerrit.addFakeChange(
Jamie Lennoxd2e37332016-12-05 15:26:19 +1100236 'org/project1', 'master', 'A')
Joshua Heskethacccffc2015-03-31 23:38:17 +1100237 self.fake_another_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
238
239 self.waitUntilSettled()
240
Jamie Lennoxd2e37332016-12-05 15:26:19 +1100241 self.assertBuilds([dict(name='project-test2',
242 changes='1,1',
243 project='org/project1',
244 pipeline='another_check')])
Joshua Heskethacccffc2015-03-31 23:38:17 +1100245
Jamie Lennoxd2e37332016-12-05 15:26:19 +1100246 # NOTE(jamielennox): the tests back the git repo for both connections
247 # onto the same git repo on the file system. If we just create another
248 # fake change the fake_review_gerrit will try to create another 1,1
249 # change and git will fail to create the ref. Arbitrarily set it to get
250 # around the problem.
251 self.fake_review_gerrit.change_number = 50
252
253 B = self.fake_review_gerrit.addFakeChange(
254 'org/project1', 'master', 'B')
255 self.fake_review_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
256
257 self.waitUntilSettled()
258
259 self.assertBuilds([
260 dict(name='project-test2',
261 changes='1,1',
262 project='org/project1',
263 pipeline='another_check'),
264 dict(name='project-test1',
265 changes='51,1',
266 project='org/project1',
267 pipeline='review_check'),
268 ])
269
Paul Belanger174a8272017-03-14 13:20:10 -0400270 self.executor_server.hold_jobs_in_build = False
271 self.executor_server.release()
Joshua Heskethacccffc2015-03-31 23:38:17 +1100272 self.waitUntilSettled()
Tobias Henkel7df274b2017-05-26 17:41:11 +0200273
Jamie Lennoxb09f4212017-05-03 13:51:38 +1000274 def test_multiple_project_separate_gerrits_common_pipeline(self):
275 self.executor_server.hold_jobs_in_build = True
276
277 A = self.fake_another_gerrit.addFakeChange(
278 'org/project2', 'master', 'A')
279 self.fake_another_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
280
281 self.waitUntilSettled()
282
283 self.assertBuilds([dict(name='project-test2',
284 changes='1,1',
285 project='org/project2',
286 pipeline='common_check')])
287
288 # NOTE(jamielennox): the tests back the git repo for both connections
289 # onto the same git repo on the file system. If we just create another
290 # fake change the fake_review_gerrit will try to create another 1,1
291 # change and git will fail to create the ref. Arbitrarily set it to get
292 # around the problem.
293 self.fake_review_gerrit.change_number = 50
294
295 B = self.fake_review_gerrit.addFakeChange(
296 'org/project2', 'master', 'B')
297 self.fake_review_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
298
299 self.waitUntilSettled()
300
301 self.assertBuilds([
302 dict(name='project-test2',
303 changes='1,1',
304 project='org/project2',
305 pipeline='common_check'),
306 dict(name='project-test1',
307 changes='51,1',
308 project='org/project2',
309 pipeline='common_check'),
310 ])
311
312 self.executor_server.hold_jobs_in_build = False
313 self.executor_server.release()
314 self.waitUntilSettled()
315
Tobias Henkel7df274b2017-05-26 17:41:11 +0200316
317class TestConnectionsMerger(ZuulTestCase):
318 config_file = 'zuul-connections-merger.conf'
319 tenant_config_file = 'config/single-tenant/main.yaml'
320
321 def configure_connections(self):
322 super(TestConnectionsMerger, self).configure_connections(True)
323
324 def test_connections_merger(self):
325 "Test merger only configures source connections"
326
327 self.assertIn("gerrit", self.connections.connections)
328 self.assertIn("github", self.connections.connections)
329 self.assertNotIn("smtp", self.connections.connections)
330 self.assertNotIn("sql", self.connections.connections)
331 self.assertNotIn("timer", self.connections.connections)
332 self.assertNotIn("zuul", self.connections.connections)