summaryrefslogtreecommitdiff
path: root/tests/unit/test_github_crd.py
blob: 4d9aad0ac167501f8010bc29f3611ec37987b5b1 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Copyright (c) 2017 IBM Corp.
#
# 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.

from tests.base import ZuulTestCase, simple_layout


class TestGithubCrossRepoDeps(ZuulTestCase):
    """Test Github cross-repo dependencies"""
    config_file = 'zuul-github-driver.conf'
    scheduler_count = 1

    @simple_layout('layouts/crd-github.yaml', driver='github')
    def test_crd_independent(self):
        "Test cross-repo dependences on an independent pipeline"

        # Create a change in project1 that a project2 change will depend on
        A = self.fake_github.openFakePullRequest('org/project1', 'master', 'A')

        # Create a commit in B that sets the dependency on A
        msg = "Depends-On: https://github.com/org/project1/pull/%s" % A.number
        B = self.fake_github.openFakePullRequest('org/project2', 'master', 'B',
                                                 body=msg)

        # Make an event to re-use
        event = B.getPullRequestEditedEvent()

        self.fake_github.emitEvent(event)
        self.waitUntilSettled()

        # The changes for the job from project2 should include the project1
        # PR contet
        changes = self.getJobFromHistory(
            'project2-test', 'org/project2').changes

        self.assertEqual(changes, "%s,%s %s,%s" % (A.number,
                                                   A.head_sha,
                                                   B.number,
                                                   B.head_sha))

        # There should be no more changes in the queue
        tenant = self.scheds.first.sched.abide.tenants.get('tenant-one')
        self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)

    @simple_layout('layouts/crd-github.yaml', driver='github')
    def test_crd_dependent(self):
        "Test cross-repo dependences on a dependent pipeline"

        # Create a change in project3 that a project4 change will depend on
        A = self.fake_github.openFakePullRequest('org/project3', 'master', 'A')

        # Create a commit in B that sets the dependency on A
        msg = "Depends-On: https://github.com/org/project3/pull/%s" % A.number
        B = self.fake_github.openFakePullRequest('org/project4', 'master', 'B',
                                                 body=msg)

        # Make an event to re-use
        event = B.getPullRequestEditedEvent()

        self.fake_github.emitEvent(event)
        self.waitUntilSettled()

        # The changes for the job from project4 should include the project3
        # PR contet
        changes = self.getJobFromHistory(
            'project4-test', 'org/project4').changes

        self.assertEqual(changes, "%s,%s %s,%s" % (A.number,
                                                   A.head_sha,
                                                   B.number,
                                                   B.head_sha))

        self.assertTrue(A.is_merged)
        self.assertTrue(B.is_merged)

    @simple_layout('layouts/crd-github.yaml', driver='github')
    def test_crd_unshared_dependent(self):
        "Test cross-repo dependences on unshared dependent pipeline"

        # Create a change in project1 that a project2 change will depend on
        A = self.fake_github.openFakePullRequest('org/project5', 'master', 'A')

        # Create a commit in B that sets the dependency on A
        msg = "Depends-On: https://github.com/org/project5/pull/%s" % A.number
        B = self.fake_github.openFakePullRequest('org/project6', 'master', 'B',
                                                 body=msg)

        # Make an event for B
        event = B.getPullRequestEditedEvent()

        # Emit for B, which should not enqueue A because they do not share
        # A queue. Since B depends on A, and A isn't enqueue, B will not run
        self.fake_github.emitEvent(event)
        self.waitUntilSettled()

        self.assertEqual(0, len(self.history))

        # Enqueue A alone, let it finish
        self.fake_github.emitEvent(A.getPullRequestEditedEvent())
        self.waitUntilSettled()

        self.assertTrue(A.is_merged)
        self.assertFalse(B.is_merged)
        self.assertEqual(1, len(self.history))

        # With A merged, B should go through
        self.fake_github.emitEvent(event)
        self.waitUntilSettled()

        self.assertTrue(B.is_merged)
        self.assertEqual(2, len(self.history))

    @simple_layout('layouts/crd-github.yaml', driver='github')
    def test_crd_cycle(self):
        "Test cross-repo dependency cycles"

        # A -> B -> A
        msg = "Depends-On: https://github.com/org/project6/pull/2"
        A = self.fake_github.openFakePullRequest('org/project5', 'master', 'A',
                                                 body=msg)
        msg = "Depends-On: https://github.com/org/project5/pull/1"
        B = self.fake_github.openFakePullRequest('org/project6', 'master', 'B',
                                                 body=msg)

        self.fake_github.emitEvent(A.getPullRequestEditedEvent())
        self.waitUntilSettled()

        self.assertFalse(A.is_merged)
        self.assertFalse(B.is_merged)
        self.assertEqual(0, len(self.history))

    @simple_layout('layouts/crd-github.yaml', driver='github')
    def test_crd_needed_changes(self):
        "Test cross-repo needed changes discovery"

        # Given change A and B, where B depends on A, when A
        # completes B should be enqueued (using a shared queue)

        # Create a change in project3 that a project4 change will depend on
        A = self.fake_github.openFakePullRequest('org/project3', 'master', 'A')

        # Set B to depend on A
        msg = "Depends-On: https://github.com/org/project3/pull/%s" % A.number
        B = self.fake_github.openFakePullRequest('org/project4', 'master', 'B',
                                                 body=msg)

        # Enqueue A, which when finished should enqueue B
        self.fake_github.emitEvent(A.getPullRequestEditedEvent())
        self.waitUntilSettled()

        # The changes for the job from project4 should include the project3
        # PR contet
        changes = self.getJobFromHistory(
            'project4-test', 'org/project4').changes

        self.assertEqual(changes, "%s,%s %s,%s" % (A.number,
                                                   A.head_sha,
                                                   B.number,
                                                   B.head_sha))

        self.assertTrue(A.is_merged)
        self.assertTrue(B.is_merged)

    @simple_layout('layouts/github-message-update.yaml', driver='github')
    def test_crd_message_update(self):
        "Test a change is dequeued when the PR in its depends-on is updated"

        # Create a change in project1 that a project2 change will depend on
        A = self.fake_github.openFakePullRequest('org/project1', 'master', 'A')

        # Create a commit in B that sets the dependency on A
        msg = "Depends-On: https://github.com/org/project1/pull/%s" % A.number
        B = self.fake_github.openFakePullRequest('org/project2', 'master', 'B',
                                                 body=msg)

        # Create a commit in C that sets the dependency on B
        msg = "Depends-On: https://github.com/org/project2/pull/%s" % B.number
        C = self.fake_github.openFakePullRequest('org/project3', 'master', 'C',
                                                 body=msg)

        # A change we'll use later to replace A
        A1 = self.fake_github.openFakePullRequest(
            'org/project1', 'master', 'A1')

        self.executor_server.hold_jobs_in_build = True

        # Enqueue A,B,C
        self.fake_github.emitEvent(C.getReviewAddedEvent('approve'))
        self.waitUntilSettled()

        self.assertEqual(len(self.builds), 1)
        tenant = self.scheds.first.sched.abide.tenants.get('tenant-one')
        items = tenant.layout.pipelines['check'].getAllItems()
        self.assertEqual(len(items), 3)

        # Update B to point at A1 instead of A
        msg = "Depends-On: https://github.com/org/project1/pull/%s" % A1.number
        self.fake_github.emitEvent(B.editBody(msg))
        self.waitUntilSettled()

        # Release
        self.executor_server.hold_jobs_in_build = False
        self.executor_server.release()
        self.waitUntilSettled()

        # The job should be aborted since B was updated while enqueued.
        self.assertHistory([dict(name='project3-test', result='ABORTED')])