Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 1 | # 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 Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 15 | import sqlalchemy as sa |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 16 | |
| 17 | from tests.base import ZuulTestCase, ZuulDBTestCase |
| 18 | |
| 19 | |
| 20 | def _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 Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 27 | |
Joshua Hesketh | 352264b | 2015-08-11 23:42:08 +1000 | [diff] [blame] | 28 | |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 29 | class TestConnections(ZuulTestCase): |
Morgan Fainberg | 4245a42 | 2016-08-05 16:20:12 -0700 | [diff] [blame] | 30 | config_file = 'zuul-connections-same-gerrit.conf' |
| 31 | tenant_config_file = 'config/zuul-connections-same-gerrit/main.yaml' |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 32 | |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 33 | def test_multiple_gerrit_connections(self): |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 34 | "Test multiple connections to the one gerrit" |
| 35 | |
| 36 | A = self.fake_review_gerrit.addFakeChange('org/project', 'master', 'A') |
James E. Blair | 7fc8daa | 2016-08-08 15:37:15 -0700 | [diff] [blame] | 37 | self.addEvent('review_gerrit', A.getPatchsetCreatedEvent(1)) |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 38 | |
| 39 | self.waitUntilSettled() |
| 40 | |
| 41 | self.assertEqual(len(A.patchsets[-1]['approvals']), 1) |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 42 | self.assertEqual(A.patchsets[-1]['approvals'][0]['type'], 'verified') |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 43 | 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 Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 48 | self.executor_server.failJob('project-test2', B) |
James E. Blair | 7fc8daa | 2016-08-08 15:37:15 -0700 | [diff] [blame] | 49 | self.addEvent('review_gerrit', B.getPatchsetCreatedEvent(1)) |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 50 | |
| 51 | self.waitUntilSettled() |
| 52 | |
| 53 | self.assertEqual(len(B.patchsets[-1]['approvals']), 1) |
James E. Blair | 8b5408c | 2016-08-08 15:37:46 -0700 | [diff] [blame] | 54 | self.assertEqual(B.patchsets[-1]['approvals'][0]['type'], 'verified') |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 55 | self.assertEqual(B.patchsets[-1]['approvals'][0]['value'], '-1') |
| 56 | self.assertEqual(B.patchsets[-1]['approvals'][0]['by']['username'], |
| 57 | 'civoter') |
| 58 | |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 59 | |
| 60 | class 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 Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 65 | "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. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 70 | self.connections.connections['resultsdb'].engine) |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 71 | |
| 72 | self.assertEqual(9, len(insp.get_columns(buildset_table))) |
| 73 | self.assertEqual(10, len(insp.get_columns(build_table))) |
| 74 | |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 75 | def test_sql_results(self): |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 76 | "Test results are entered into an sql table" |
| 77 | # Grab the sa tables |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 78 | tenant = self.sched.abide.tenants.get('tenant-one') |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 79 | reporter = _get_reporter_from_connection_name( |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 80 | tenant.layout.pipelines['check'].success_actions, |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 81 | 'resultsdb' |
| 82 | ) |
| 83 | |
| 84 | # Add a success result |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 85 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 86 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 87 | self.waitUntilSettled() |
| 88 | |
| 89 | # Add a failed result for a negative score |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 90 | 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 Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 94 | self.waitUntilSettled() |
| 95 | |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 96 | conn = self.connections.connections['resultsdb'].engine.connect() |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 97 | 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']) |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 123 | self.assertEqual('https://server/job/project-merge/0/', |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 124 | buildset0_builds[0]['log_url']) |
| 125 | |
| 126 | self.assertEqual('check', buildset1['pipeline']) |
| 127 | self.assertEqual('org/project', buildset1['project']) |
| 128 | self.assertEqual(2, buildset1['change']) |
| 129 | self.assertEqual(1, buildset1['patchset']) |
| 130 | self.assertEqual(-1, buildset1['score']) |
| 131 | self.assertEqual('Build failed.', buildset1['message']) |
| 132 | |
| 133 | buildset1_builds = conn.execute( |
| 134 | sa.sql.select([reporter.connection.zuul_build_table]). |
| 135 | where( |
| 136 | reporter.connection.zuul_build_table.c.buildset_id == |
| 137 | buildset1['id'] |
| 138 | ) |
| 139 | ).fetchall() |
| 140 | |
| 141 | # Check the second last result, which should be the project-test1 job |
| 142 | # which failed |
| 143 | self.assertEqual('project-test1', buildset1_builds[-2]['job_name']) |
| 144 | self.assertEqual("FAILURE", buildset1_builds[-2]['result']) |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 145 | self.assertEqual('https://server/job/project-test1/0/', |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 146 | buildset1_builds[-2]['log_url']) |
| 147 | |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 148 | def test_multiple_sql_connections(self): |
| 149 | "Test putting results in different databases" |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 150 | self.updateConfigLayout( |
| 151 | 'tests/fixtures/layout-sql-reporter.yaml') |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 152 | |
| 153 | # Add a successful result |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 154 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 155 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 156 | self.waitUntilSettled() |
| 157 | |
| 158 | # Add a failed result |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 159 | B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B') |
| 160 | self.executor_server.failJob('project-test1', B) |
| 161 | self.fake_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 162 | self.waitUntilSettled() |
| 163 | |
| 164 | # Grab the sa tables for resultsdb |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 165 | tenant = self.sched.abide.tenants.get('tenant-one') |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 166 | reporter1 = _get_reporter_from_connection_name( |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 167 | tenant.layout.pipelines['check'].success_actions, |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 168 | 'resultsdb' |
| 169 | ) |
| 170 | |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 171 | conn = self.connections.connections['resultsdb'].engine.connect() |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 172 | buildsets_resultsdb = conn.execute(sa.sql.select( |
| 173 | [reporter1.connection.zuul_buildset_table])).fetchall() |
| 174 | # Should have been 2 buildset reported to the resultsdb (both success |
| 175 | # and failure report) |
| 176 | self.assertEqual(2, len(buildsets_resultsdb)) |
| 177 | |
| 178 | # The first one should have passed |
| 179 | self.assertEqual('check', buildsets_resultsdb[0]['pipeline']) |
| 180 | self.assertEqual('org/project', buildsets_resultsdb[0]['project']) |
| 181 | self.assertEqual(1, buildsets_resultsdb[0]['change']) |
| 182 | self.assertEqual(1, buildsets_resultsdb[0]['patchset']) |
| 183 | self.assertEqual(1, buildsets_resultsdb[0]['score']) |
| 184 | self.assertEqual('Build succeeded.', buildsets_resultsdb[0]['message']) |
| 185 | |
| 186 | # Grab the sa tables for resultsdb_failures |
| 187 | reporter2 = _get_reporter_from_connection_name( |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 188 | tenant.layout.pipelines['check'].failure_actions, |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 189 | 'resultsdb_failures' |
| 190 | ) |
| 191 | |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 192 | conn = self.connections.connections['resultsdb_failures'].\ |
| 193 | engine.connect() |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 194 | buildsets_resultsdb_failures = conn.execute(sa.sql.select( |
| 195 | [reporter2.connection.zuul_buildset_table])).fetchall() |
| 196 | # The failure db should only have 1 buildset failed |
| 197 | self.assertEqual(1, len(buildsets_resultsdb_failures)) |
| 198 | |
| 199 | self.assertEqual('check', buildsets_resultsdb_failures[0]['pipeline']) |
| 200 | self.assertEqual( |
| 201 | 'org/project', buildsets_resultsdb_failures[0]['project']) |
| 202 | self.assertEqual(2, buildsets_resultsdb_failures[0]['change']) |
| 203 | self.assertEqual(1, buildsets_resultsdb_failures[0]['patchset']) |
| 204 | self.assertEqual(-1, buildsets_resultsdb_failures[0]['score']) |
| 205 | self.assertEqual( |
| 206 | 'Build failed.', buildsets_resultsdb_failures[0]['message']) |
| 207 | |
| 208 | |
| 209 | class TestConnectionsBadSQL(ZuulDBTestCase): |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 210 | config_file = 'zuul-sql-driver-bad.conf' |
| 211 | tenant_config_file = 'config/sql-driver/main.yaml' |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 212 | |
| 213 | def test_unable_to_connect(self): |
| 214 | "Test the SQL reporter fails gracefully when unable to connect" |
| 215 | self.config.set('zuul', 'layout_config', |
| 216 | 'tests/fixtures/layout-sql-reporter.yaml') |
| 217 | self.sched.reconfigure(self.config) |
| 218 | |
| 219 | # Trigger a reporter. If no errors are raised, the reporter has been |
| 220 | # disabled correctly |
James E. Blair | 8284489 | 2017-03-06 10:55:26 -0800 | [diff] [blame] | 221 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 222 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
Joshua Hesketh | d78b448 | 2015-09-14 16:56:34 -0600 | [diff] [blame] | 223 | self.waitUntilSettled() |
| 224 | |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 225 | |
| 226 | class TestMultipleGerrits(ZuulTestCase): |
Jamie Lennox | d2e3733 | 2016-12-05 15:26:19 +1100 | [diff] [blame] | 227 | config_file = 'zuul-connections-multiple-gerrits.conf' |
| 228 | tenant_config_file = 'config/zuul-connections-multiple-gerrits/main.yaml' |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 229 | |
| 230 | def test_multiple_project_separate_gerrits(self): |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 231 | self.executor_server.hold_jobs_in_build = True |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 232 | |
| 233 | A = self.fake_another_gerrit.addFakeChange( |
Jamie Lennox | d2e3733 | 2016-12-05 15:26:19 +1100 | [diff] [blame] | 234 | 'org/project1', 'master', 'A') |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 235 | self.fake_another_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 236 | |
| 237 | self.waitUntilSettled() |
| 238 | |
Jamie Lennox | d2e3733 | 2016-12-05 15:26:19 +1100 | [diff] [blame] | 239 | self.assertBuilds([dict(name='project-test2', |
| 240 | changes='1,1', |
| 241 | project='org/project1', |
| 242 | pipeline='another_check')]) |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 243 | |
Jamie Lennox | d2e3733 | 2016-12-05 15:26:19 +1100 | [diff] [blame] | 244 | # NOTE(jamielennox): the tests back the git repo for both connections |
| 245 | # onto the same git repo on the file system. If we just create another |
| 246 | # fake change the fake_review_gerrit will try to create another 1,1 |
| 247 | # change and git will fail to create the ref. Arbitrarily set it to get |
| 248 | # around the problem. |
| 249 | self.fake_review_gerrit.change_number = 50 |
| 250 | |
| 251 | B = self.fake_review_gerrit.addFakeChange( |
| 252 | 'org/project1', 'master', 'B') |
| 253 | self.fake_review_gerrit.addEvent(B.getPatchsetCreatedEvent(1)) |
| 254 | |
| 255 | self.waitUntilSettled() |
| 256 | |
| 257 | self.assertBuilds([ |
| 258 | dict(name='project-test2', |
| 259 | changes='1,1', |
| 260 | project='org/project1', |
| 261 | pipeline='another_check'), |
| 262 | dict(name='project-test1', |
| 263 | changes='51,1', |
| 264 | project='org/project1', |
| 265 | pipeline='review_check'), |
| 266 | ]) |
| 267 | |
Paul Belanger | 174a827 | 2017-03-14 13:20:10 -0400 | [diff] [blame] | 268 | self.executor_server.hold_jobs_in_build = False |
| 269 | self.executor_server.release() |
Joshua Hesketh | acccffc | 2015-03-31 23:38:17 +1100 | [diff] [blame] | 270 | self.waitUntilSettled() |