summaryrefslogtreecommitdiff
path: root/tests/unit/test_git_driver.py
blob: 95fca30d3060cab93d8fcc71aff6d41149fd7d5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright 2016 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


import os
import time
import yaml

from tests.base import ZuulTestCase, simple_layout


class TestGitDriver(ZuulTestCase):
    config_file = 'zuul-git-driver.conf'
    tenant_config_file = 'config/git-driver/main.yaml'

    def setUp(self):
        super(TestGitDriver, self).setUp()
        self.git_connection = self.scheds.first.sched.connections\
            .getSource('git').connection

    def setup_config(self, config_file: str):
        config = super(TestGitDriver, self).setup_config(config_file)
        config.set('connection git', 'baseurl', self.upstream_root)
        return config

    def test_basic(self):
        tenant = self.scheds.first.sched.abide.tenants.get('tenant-one')
        # Check that we have the git source for common-config and the
        # gerrit source for the project.
        self.assertEqual('git', tenant.config_projects[0].source.name)
        self.assertEqual('common-config', tenant.config_projects[0].name)
        self.assertEqual('gerrit', tenant.untrusted_projects[0].source.name)
        self.assertEqual('org/project', tenant.untrusted_projects[0].name)

        # The configuration for this test is accessed via the git
        # driver (in common-config), rather than the gerrit driver, so
        # if the job runs, it worked.
        A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
        self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
        self.waitUntilSettled()
        self.assertEqual(len(self.history), 1)
        self.assertEqual(A.reported, 1)

    def test_config_refreshed(self):
        A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
        self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
        self.waitUntilSettled()
        self.assertEqual(len(self.history), 1)
        self.assertEqual(A.reported, 1)
        self.assertEqual(self.history[0].name, 'project-test1')

        # Update zuul.yaml to force a tenant reconfiguration
        path = os.path.join(self.upstream_root, 'common-config', 'zuul.yaml')
        with open(path, 'r') as f:
            config = yaml.safe_load(f)
        change = {
            'name': 'org/project',
            'check': {
                'jobs': [
                    'project-test2'
                ]
            }
        }
        config[4]['project'] = change
        files = {'zuul.yaml': yaml.dump(config)}
        self.addCommitToRepo(
            'common-config', 'Change zuul.yaml configuration', files)

        # Wait for the tenant reconfiguration to happen
        count = self.waitForEvent()
        self.waitUntilSettled()

        A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
        self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
        self.waitUntilSettled()
        self.assertEqual(len(self.history), 2)
        self.assertEqual(A.reported, 1)
        # We make sure the new job has run
        self.assertEqual(self.history[1].name, 'project-test2')

        # Let's stop the git Watcher to let us merge some changes commits
        # We want to verify that config changes are detected for commits
        # on the range oldrev..newrev
        self.scheds.first.sched.connections.getSource('git').connection\
            .watcher_thread._pause = True
        # Add a config change
        change = {
            'name': 'org/project',
            'check': {
                'jobs': [
                    'project-test1'
                ]
            }
        }
        config[4]['project'] = change
        files = {'zuul.yaml': yaml.dump(config)}
        self.addCommitToRepo(
            'common-config', 'Change zuul.yaml configuration', files)
        # Add two other changes
        self.addCommitToRepo(
            'common-config', 'Adding f1',
            {'f1': "Content"})
        self.addCommitToRepo(
            'common-config', 'Adding f2',
            {'f2': "Content"})
        # Restart the git watcher
        self.scheds.first.sched.connections.getSource('git').connection\
            .watcher_thread._pause = False

        # Wait for the tenant reconfiguration to happen
        self.waitForEvent(count)
        self.waitUntilSettled()

        A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
        self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
        self.waitUntilSettled()
        self.assertEqual(len(self.history), 3)
        self.assertEqual(A.reported, 1)
        # We make sure the new job has run
        self.assertEqual(self.history[2].name, 'project-test1')

    def ensure_watcher_has_context(self):
        # Make sure watcher have read initial refs shas
        delay = 0.1
        max_delay = 1
        while not self.git_connection.watcher_thread.projects_refs:
            time.sleep(delay)
            max_delay -= delay
            if max_delay <= 0:
                raise Exception("Timeout waiting for initial read")
        return self.git_connection.watcher_thread._event_count

    def waitForEvent(self, initial_count=0):
        delay = 0.1
        max_delay = 5
        while self.git_connection.watcher_thread._event_count <= initial_count:
            time.sleep(delay)
            max_delay -= delay
            if max_delay <= 0:
                raise Exception("Timeout waiting for event")
        return self.git_connection.watcher_thread._event_count

    @simple_layout('layouts/basic-git.yaml', driver='git')
    def test_ref_updated_event(self):
        count = self.ensure_watcher_has_context()
        # Add a commit to trigger a ref-updated event
        self.addCommitToRepo(
            'org/project', 'A change for ref-updated', {'f1': 'Content'})
        # Wait for the git watcher to detect the ref-update event
        self.waitForEvent(count)
        self.waitUntilSettled()
        self.assertEqual(len(self.history), 1)
        self.assertEqual('SUCCESS',
                         self.getJobFromHistory('post-job').result)

    @simple_layout('layouts/basic-git.yaml', driver='git')
    def test_ref_created(self):
        count = self.ensure_watcher_has_context()
        # Tag HEAD to trigger a ref-updated event
        self.addTagToRepo(
            'org/project', 'atag', 'HEAD')
        # Wait for the git watcher to detect the ref-update event
        self.waitForEvent(count)
        self.waitUntilSettled()
        self.assertEqual(len(self.history), 1)
        self.assertEqual('SUCCESS',
                         self.getJobFromHistory('tag-job').result)

    @simple_layout('layouts/basic-git.yaml', driver='git')
    def test_ref_deleted(self):
        count = self.ensure_watcher_has_context()
        # Delete default tag init to trigger a ref-updated event
        self.delTagFromRepo(
            'org/project', 'init')
        # Wait for the git watcher to detect the ref-update event
        self.waitForEvent(count)
        self.waitUntilSettled()
        # Make sure no job as run as ignore-delete is True by default
        self.assertEqual(len(self.history), 0)