summaryrefslogtreecommitdiff
path: root/tests/unit/test_git_driver.py
blob: b9e6c6e921a01e01478d01cab17d9264e3ba4c2f (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
# 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_config(self):
        super(TestGitDriver, self).setup_config()
        self.config.set('connection git', 'baseurl', self.upstream_root)

    def test_basic(self):
        tenant = self.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')
        config = yaml.load(open(path, 'r').read())
        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)

        # Let some time for the tenant reconfiguration to happen
        time.sleep(2)
        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.sched.connections.getSource('git').connection.w_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.sched.connections.getSource('git').connection.w_pause = False

        # Let some time for the tenant reconfiguration to happen
        time.sleep(2)
        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
        cnx = self.sched.connections.getSource('git').connection
        delay = 0.1
        max_delay = 1
        while not cnx.projects_refs:
            time.sleep(delay)
            max_delay -= delay
            if max_delay <= 0:
                raise Exception("Timeout waiting for initial read")

    @simple_layout('layouts/basic-git.yaml', driver='git')
    def test_ref_updated_event(self):
        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'})
        # Let some time for the git watcher to detect the ref-update event
        time.sleep(0.2)
        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):
        self.ensure_watcher_has_context()
        # Tag HEAD to trigger a ref-updated event
        self.addTagToRepo(
            'org/project', 'atag', 'HEAD')
        # Let some time for the git watcher to detect the ref-update event
        time.sleep(0.2)
        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):
        self.ensure_watcher_has_context()
        # Delete default tag init to trigger a ref-updated event
        self.delTagFromRepo(
            'org/project', 'init')
        # Let some time for the git watcher to detect the ref-update event
        time.sleep(0.2)
        self.waitUntilSettled()
        # Make sure no job as run as ignore-delete is True by default
        self.assertEqual(len(self.history), 0)