James E. Blair | 45de61c | 2017-02-14 13:23:14 -0800 | [diff] [blame] | 1 | # Copyright 2016 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 | |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 15 | |
| 16 | import os |
| 17 | import time |
| 18 | import yaml |
| 19 | |
| 20 | from tests.base import ZuulTestCase, simple_layout |
James E. Blair | 45de61c | 2017-02-14 13:23:14 -0800 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class TestGitDriver(ZuulTestCase): |
| 24 | config_file = 'zuul-git-driver.conf' |
| 25 | tenant_config_file = 'config/git-driver/main.yaml' |
| 26 | |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 27 | def setUp(self): |
| 28 | super(TestGitDriver, self).setUp() |
| 29 | self.git_connection = self.sched.connections.getSource('git').\ |
| 30 | connection |
| 31 | |
James E. Blair | 45de61c | 2017-02-14 13:23:14 -0800 | [diff] [blame] | 32 | def setup_config(self): |
| 33 | super(TestGitDriver, self).setup_config() |
| 34 | self.config.set('connection git', 'baseurl', self.upstream_root) |
| 35 | |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 36 | def test_basic(self): |
James E. Blair | 45de61c | 2017-02-14 13:23:14 -0800 | [diff] [blame] | 37 | tenant = self.sched.abide.tenants.get('tenant-one') |
| 38 | # Check that we have the git source for common-config and the |
| 39 | # gerrit source for the project. |
James E. Blair | 109da3f | 2017-04-04 14:39:43 -0700 | [diff] [blame] | 40 | self.assertEqual('git', tenant.config_projects[0].source.name) |
| 41 | self.assertEqual('common-config', tenant.config_projects[0].name) |
| 42 | self.assertEqual('gerrit', tenant.untrusted_projects[0].source.name) |
| 43 | self.assertEqual('org/project', tenant.untrusted_projects[0].name) |
James E. Blair | 45de61c | 2017-02-14 13:23:14 -0800 | [diff] [blame] | 44 | |
| 45 | # The configuration for this test is accessed via the git |
| 46 | # driver (in common-config), rather than the gerrit driver, so |
| 47 | # if the job runs, it worked. |
| 48 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 49 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 50 | self.waitUntilSettled() |
| 51 | self.assertEqual(len(self.history), 1) |
| 52 | self.assertEqual(A.reported, 1) |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 53 | |
| 54 | def test_config_refreshed(self): |
| 55 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 56 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 57 | self.waitUntilSettled() |
| 58 | self.assertEqual(len(self.history), 1) |
| 59 | self.assertEqual(A.reported, 1) |
| 60 | self.assertEqual(self.history[0].name, 'project-test1') |
| 61 | |
| 62 | # Update zuul.yaml to force a tenant reconfiguration |
| 63 | path = os.path.join(self.upstream_root, 'common-config', 'zuul.yaml') |
| 64 | config = yaml.load(open(path, 'r').read()) |
| 65 | change = { |
| 66 | 'name': 'org/project', |
| 67 | 'check': { |
| 68 | 'jobs': [ |
| 69 | 'project-test2' |
| 70 | ] |
| 71 | } |
| 72 | } |
| 73 | config[4]['project'] = change |
| 74 | files = {'zuul.yaml': yaml.dump(config)} |
| 75 | self.addCommitToRepo( |
| 76 | 'common-config', 'Change zuul.yaml configuration', files) |
| 77 | |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 78 | # Wait for the tenant reconfiguration to happen |
| 79 | count = self.waitForEvent() |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 80 | self.waitUntilSettled() |
| 81 | |
| 82 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 83 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 84 | self.waitUntilSettled() |
| 85 | self.assertEqual(len(self.history), 2) |
| 86 | self.assertEqual(A.reported, 1) |
| 87 | # We make sure the new job has run |
| 88 | self.assertEqual(self.history[1].name, 'project-test2') |
| 89 | |
| 90 | # Let's stop the git Watcher to let us merge some changes commits |
| 91 | # We want to verify that config changes are detected for commits |
| 92 | # on the range oldrev..newrev |
| 93 | self.sched.connections.getSource('git').connection.w_pause = True |
| 94 | # Add a config change |
| 95 | change = { |
| 96 | 'name': 'org/project', |
| 97 | 'check': { |
| 98 | 'jobs': [ |
| 99 | 'project-test1' |
| 100 | ] |
| 101 | } |
| 102 | } |
| 103 | config[4]['project'] = change |
| 104 | files = {'zuul.yaml': yaml.dump(config)} |
| 105 | self.addCommitToRepo( |
| 106 | 'common-config', 'Change zuul.yaml configuration', files) |
| 107 | # Add two other changes |
| 108 | self.addCommitToRepo( |
| 109 | 'common-config', 'Adding f1', |
| 110 | {'f1': "Content"}) |
| 111 | self.addCommitToRepo( |
| 112 | 'common-config', 'Adding f2', |
| 113 | {'f2': "Content"}) |
| 114 | # Restart the git watcher |
| 115 | self.sched.connections.getSource('git').connection.w_pause = False |
| 116 | |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 117 | # Wait for the tenant reconfiguration to happen |
| 118 | self.waitForEvent(count) |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 119 | self.waitUntilSettled() |
| 120 | |
| 121 | A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A') |
| 122 | self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1)) |
| 123 | self.waitUntilSettled() |
| 124 | self.assertEqual(len(self.history), 3) |
| 125 | self.assertEqual(A.reported, 1) |
| 126 | # We make sure the new job has run |
| 127 | self.assertEqual(self.history[2].name, 'project-test1') |
| 128 | |
| 129 | def ensure_watcher_has_context(self): |
| 130 | # Make sure watcher have read initial refs shas |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 131 | delay = 0.1 |
| 132 | max_delay = 1 |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 133 | while not self.git_connection.projects_refs: |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 134 | time.sleep(delay) |
| 135 | max_delay -= delay |
| 136 | if max_delay <= 0: |
| 137 | raise Exception("Timeout waiting for initial read") |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 138 | return self.git_connection.watcher_thread._event_count |
| 139 | |
| 140 | def waitForEvent(self, initial_count=0): |
| 141 | delay = 0.1 |
| 142 | max_delay = 1 |
| 143 | while self.git_connection.watcher_thread._event_count <= initial_count: |
| 144 | time.sleep(delay) |
| 145 | max_delay -= delay |
| 146 | if max_delay <= 0: |
| 147 | raise Exception("Timeout waiting for event") |
| 148 | return self.git_connection.watcher_thread._event_count |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 149 | |
| 150 | @simple_layout('layouts/basic-git.yaml', driver='git') |
| 151 | def test_ref_updated_event(self): |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 152 | count = self.ensure_watcher_has_context() |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 153 | # Add a commit to trigger a ref-updated event |
| 154 | self.addCommitToRepo( |
| 155 | 'org/project', 'A change for ref-updated', {'f1': 'Content'}) |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 156 | # Wait for the git watcher to detect the ref-update event |
| 157 | self.waitForEvent(count) |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 158 | self.waitUntilSettled() |
| 159 | self.assertEqual(len(self.history), 1) |
| 160 | self.assertEqual('SUCCESS', |
| 161 | self.getJobFromHistory('post-job').result) |
| 162 | |
| 163 | @simple_layout('layouts/basic-git.yaml', driver='git') |
| 164 | def test_ref_created(self): |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 165 | count = self.ensure_watcher_has_context() |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 166 | # Tag HEAD to trigger a ref-updated event |
| 167 | self.addTagToRepo( |
| 168 | 'org/project', 'atag', 'HEAD') |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 169 | # Wait for the git watcher to detect the ref-update event |
| 170 | self.waitForEvent(count) |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 171 | self.waitUntilSettled() |
| 172 | self.assertEqual(len(self.history), 1) |
| 173 | self.assertEqual('SUCCESS', |
| 174 | self.getJobFromHistory('tag-job').result) |
| 175 | |
| 176 | @simple_layout('layouts/basic-git.yaml', driver='git') |
| 177 | def test_ref_deleted(self): |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 178 | count = self.ensure_watcher_has_context() |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 179 | # Delete default tag init to trigger a ref-updated event |
| 180 | self.delTagFromRepo( |
| 181 | 'org/project', 'init') |
James E. Blair | 185b970 | 2018-01-17 07:57:54 -0800 | [diff] [blame^] | 182 | # Wait for the git watcher to detect the ref-update event |
| 183 | self.waitForEvent(count) |
Fabien Boucher | 194a2bf | 2017-12-02 18:17:58 +0100 | [diff] [blame] | 184 | self.waitUntilSettled() |
| 185 | # Make sure no job as run as ignore-delete is True by default |
| 186 | self.assertEqual(len(self.history), 0) |