blob: 895483297ac6b3f523a574cee8cc2123d616c788 [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 Hesketh25695cb2017-03-06 12:50:04 +110016from unittest import skip
Joshua Heskethd78b4482015-09-14 16:56:34 -060017
18from tests.base import ZuulTestCase, ZuulDBTestCase
19
20
21def _get_reporter_from_connection_name(reporters, connection_name):
22 # Reporters are placed into lists for each action they may exist in.
23 # Search through the given list for the correct reporter by its conncetion
24 # name
25 for r in reporters:
26 if r.connection.connection_name == connection_name:
27 return r
Joshua Heskethacccffc2015-03-31 23:38:17 +110028
Joshua Hesketh352264b2015-08-11 23:42:08 +100029
Joshua Heskethacccffc2015-03-31 23:38:17 +110030class TestConnections(ZuulTestCase):
Morgan Fainberg4245a422016-08-05 16:20:12 -070031 config_file = 'zuul-connections-same-gerrit.conf'
32 tenant_config_file = 'config/zuul-connections-same-gerrit/main.yaml'
Joshua Heskethacccffc2015-03-31 23:38:17 +110033
Joshua Heskethd78b4482015-09-14 16:56:34 -060034 def test_multiple_gerrit_connections(self):
Joshua Heskethacccffc2015-03-31 23:38:17 +110035 "Test multiple connections to the one gerrit"
36
37 A = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair7fc8daa2016-08-08 15:37:15 -070038 self.addEvent('review_gerrit', A.getPatchsetCreatedEvent(1))
Joshua Heskethacccffc2015-03-31 23:38:17 +110039
40 self.waitUntilSettled()
41
42 self.assertEqual(len(A.patchsets[-1]['approvals']), 1)
James E. Blair8b5408c2016-08-08 15:37:46 -070043 self.assertEqual(A.patchsets[-1]['approvals'][0]['type'], 'verified')
Joshua Heskethacccffc2015-03-31 23:38:17 +110044 self.assertEqual(A.patchsets[-1]['approvals'][0]['value'], '1')
45 self.assertEqual(A.patchsets[-1]['approvals'][0]['by']['username'],
46 'jenkins')
47
48 B = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'B')
James E. Blaira5dba232016-08-08 15:53:24 -070049 self.launch_server.failJob('project-test2', B)
James E. Blair7fc8daa2016-08-08 15:37:15 -070050 self.addEvent('review_gerrit', B.getPatchsetCreatedEvent(1))
Joshua Heskethacccffc2015-03-31 23:38:17 +110051
52 self.waitUntilSettled()
53
54 self.assertEqual(len(B.patchsets[-1]['approvals']), 1)
James E. Blair8b5408c2016-08-08 15:37:46 -070055 self.assertEqual(B.patchsets[-1]['approvals'][0]['type'], 'verified')
Joshua Heskethacccffc2015-03-31 23:38:17 +110056 self.assertEqual(B.patchsets[-1]['approvals'][0]['value'], '-1')
57 self.assertEqual(B.patchsets[-1]['approvals'][0]['by']['username'],
58 'civoter')
59
Joshua Heskethd78b4482015-09-14 16:56:34 -060060 def _test_sql_tables_created(self, metadata_table=None):
61 "Test the tables for storing results are created properly"
62 buildset_table = 'zuul_buildset'
63 build_table = 'zuul_build'
64
65 insp = sa.engine.reflection.Inspector(
66 self.connections['resultsdb'].engine)
67
68 self.assertEqual(9, len(insp.get_columns(buildset_table)))
69 self.assertEqual(10, len(insp.get_columns(build_table)))
70
Joshua Hesketh25695cb2017-03-06 12:50:04 +110071 @skip("Disabled for early v3 development")
Joshua Heskethd78b4482015-09-14 16:56:34 -060072 def test_sql_tables_created(self):
73 "Test the default table is created"
74 self.config.set('zuul', 'layout_config',
75 'tests/fixtures/layout-sql-reporter.yaml')
76 self.sched.reconfigure(self.config)
77 self._test_sql_tables_created()
78
79 def _test_sql_results(self):
80 "Test results are entered into an sql table"
81 # Grab the sa tables
82 reporter = _get_reporter_from_connection_name(
83 self.sched.layout.pipelines['check'].success_actions,
84 'resultsdb'
85 )
86
87 # Add a success result
88 A = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'A')
89 self.fake_review_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
90 self.waitUntilSettled()
91
92 # Add a failed result for a negative score
93 B = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'B')
94 self.worker.addFailTest('project-test1', B)
95 self.fake_review_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
96 self.waitUntilSettled()
97
98 conn = self.connections['resultsdb'].engine.connect()
99 result = conn.execute(
100 sa.sql.select([reporter.connection.zuul_buildset_table]))
101
102 buildsets = result.fetchall()
103 self.assertEqual(2, len(buildsets))
104 buildset0 = buildsets[0]
105 buildset1 = buildsets[1]
106
107 self.assertEqual('check', buildset0['pipeline'])
108 self.assertEqual('org/project', buildset0['project'])
109 self.assertEqual(1, buildset0['change'])
110 self.assertEqual(1, buildset0['patchset'])
111 self.assertEqual(1, buildset0['score'])
112 self.assertEqual('Build succeeded.', buildset0['message'])
113
114 buildset0_builds = conn.execute(
115 sa.sql.select([reporter.connection.zuul_build_table]).
116 where(
117 reporter.connection.zuul_build_table.c.buildset_id ==
118 buildset0['id']
119 )
120 ).fetchall()
121
122 # Check the first result, which should be the project-merge job
123 self.assertEqual('project-merge', buildset0_builds[0]['job_name'])
124 self.assertEqual("SUCCESS", buildset0_builds[0]['result'])
125 self.assertEqual('http://logs.example.com/1/1/check/project-merge/0',
126 buildset0_builds[0]['log_url'])
127
128 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'])
147 self.assertEqual('http://logs.example.com/2/1/check/project-test1/4',
148 buildset1_builds[-2]['log_url'])
149
Joshua Hesketh25695cb2017-03-06 12:50:04 +1100150 @skip("Disabled for early v3 development")
Joshua Heskethd78b4482015-09-14 16:56:34 -0600151 def test_sql_results(self):
152 "Test results are entered into the default sql table"
153 self.config.set('zuul', 'layout_config',
154 'tests/fixtures/layout-sql-reporter.yaml')
155 self.sched.reconfigure(self.config)
156 self._test_sql_results()
157
Joshua Hesketh25695cb2017-03-06 12:50:04 +1100158 @skip("Disabled for early v3 development")
Joshua Heskethd78b4482015-09-14 16:56:34 -0600159 def test_multiple_sql_connections(self):
160 "Test putting results in different databases"
161 self.config.set('zuul', 'layout_config',
162 'tests/fixtures/layout-sql-reporter.yaml')
163 self.sched.reconfigure(self.config)
164
165 # Add a successful result
166 A = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'A')
167 self.fake_review_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
168 self.waitUntilSettled()
169
170 # Add a failed result
171 B = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'B')
172 self.worker.addFailTest('project-test1', B)
173 self.fake_review_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
174 self.waitUntilSettled()
175
176 # Grab the sa tables for resultsdb
177 reporter1 = _get_reporter_from_connection_name(
178 self.sched.layout.pipelines['check'].success_actions,
179 'resultsdb'
180 )
181
182 conn = self.connections['resultsdb'].engine.connect()
183 buildsets_resultsdb = conn.execute(sa.sql.select(
184 [reporter1.connection.zuul_buildset_table])).fetchall()
185 # Should have been 2 buildset reported to the resultsdb (both success
186 # and failure report)
187 self.assertEqual(2, len(buildsets_resultsdb))
188
189 # The first one should have passed
190 self.assertEqual('check', buildsets_resultsdb[0]['pipeline'])
191 self.assertEqual('org/project', buildsets_resultsdb[0]['project'])
192 self.assertEqual(1, buildsets_resultsdb[0]['change'])
193 self.assertEqual(1, buildsets_resultsdb[0]['patchset'])
194 self.assertEqual(1, buildsets_resultsdb[0]['score'])
195 self.assertEqual('Build succeeded.', buildsets_resultsdb[0]['message'])
196
197 # Grab the sa tables for resultsdb_failures
198 reporter2 = _get_reporter_from_connection_name(
199 self.sched.layout.pipelines['check'].failure_actions,
200 'resultsdb_failures'
201 )
202
203 conn = self.connections['resultsdb_failures'].engine.connect()
204 buildsets_resultsdb_failures = conn.execute(sa.sql.select(
205 [reporter2.connection.zuul_buildset_table])).fetchall()
206 # The failure db should only have 1 buildset failed
207 self.assertEqual(1, len(buildsets_resultsdb_failures))
208
209 self.assertEqual('check', buildsets_resultsdb_failures[0]['pipeline'])
210 self.assertEqual(
211 'org/project', buildsets_resultsdb_failures[0]['project'])
212 self.assertEqual(2, buildsets_resultsdb_failures[0]['change'])
213 self.assertEqual(1, buildsets_resultsdb_failures[0]['patchset'])
214 self.assertEqual(-1, buildsets_resultsdb_failures[0]['score'])
215 self.assertEqual(
216 'Build failed.', buildsets_resultsdb_failures[0]['message'])
217
218
219class TestConnectionsBadSQL(ZuulDBTestCase):
220 def setup_config(self, config_file='zuul-connections-bad-sql.conf'):
221 super(TestConnectionsBadSQL, self).setup_config(config_file)
222
Joshua Hesketh25695cb2017-03-06 12:50:04 +1100223 @skip("Disabled for early v3 development")
Joshua Heskethd78b4482015-09-14 16:56:34 -0600224 def test_unable_to_connect(self):
225 "Test the SQL reporter fails gracefully when unable to connect"
226 self.config.set('zuul', 'layout_config',
227 'tests/fixtures/layout-sql-reporter.yaml')
228 self.sched.reconfigure(self.config)
229
230 # Trigger a reporter. If no errors are raised, the reporter has been
231 # disabled correctly
232 A = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'A')
233 self.fake_review_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
234 self.waitUntilSettled()
235
Joshua Heskethacccffc2015-03-31 23:38:17 +1100236
237class TestMultipleGerrits(ZuulTestCase):
Jamie Lennoxd2e37332016-12-05 15:26:19 +1100238 config_file = 'zuul-connections-multiple-gerrits.conf'
239 tenant_config_file = 'config/zuul-connections-multiple-gerrits/main.yaml'
Joshua Heskethacccffc2015-03-31 23:38:17 +1100240
241 def test_multiple_project_separate_gerrits(self):
Jamie Lennoxd2e37332016-12-05 15:26:19 +1100242 self.launch_server.hold_jobs_in_build = True
Joshua Heskethacccffc2015-03-31 23:38:17 +1100243
244 A = self.fake_another_gerrit.addFakeChange(
Jamie Lennoxd2e37332016-12-05 15:26:19 +1100245 'org/project1', 'master', 'A')
Joshua Heskethacccffc2015-03-31 23:38:17 +1100246 self.fake_another_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
247
248 self.waitUntilSettled()
249
Jamie Lennoxd2e37332016-12-05 15:26:19 +1100250 self.assertBuilds([dict(name='project-test2',
251 changes='1,1',
252 project='org/project1',
253 pipeline='another_check')])
Joshua Heskethacccffc2015-03-31 23:38:17 +1100254
Jamie Lennoxd2e37332016-12-05 15:26:19 +1100255 # NOTE(jamielennox): the tests back the git repo for both connections
256 # onto the same git repo on the file system. If we just create another
257 # fake change the fake_review_gerrit will try to create another 1,1
258 # change and git will fail to create the ref. Arbitrarily set it to get
259 # around the problem.
260 self.fake_review_gerrit.change_number = 50
261
262 B = self.fake_review_gerrit.addFakeChange(
263 'org/project1', 'master', 'B')
264 self.fake_review_gerrit.addEvent(B.getPatchsetCreatedEvent(1))
265
266 self.waitUntilSettled()
267
268 self.assertBuilds([
269 dict(name='project-test2',
270 changes='1,1',
271 project='org/project1',
272 pipeline='another_check'),
273 dict(name='project-test1',
274 changes='51,1',
275 project='org/project1',
276 pipeline='review_check'),
277 ])
278
279 self.launch_server.hold_jobs_in_build = False
280 self.launch_server.release()
Joshua Heskethacccffc2015-03-31 23:38:17 +1100281 self.waitUntilSettled()