blob: bc7d499f97a455e8aaf20547cc6a6268375fae17 [file] [log] [blame]
Jesse Keatinga41566f2017-06-14 18:17:51 -07001#!/usr/bin/env python
2# Copyright (c) 2017 IBM Corp.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16from tests.base import ZuulTestCase, simple_layout
17
18
19class TestGithubCrossRepoDeps(ZuulTestCase):
20 """Test Github cross-repo dependencies"""
21 config_file = 'zuul-github-driver.conf'
22
23 @simple_layout('layouts/crd-github.yaml', driver='github')
24 def test_crd_independent(self):
25 "Test cross-repo dependences on an independent pipeline"
26
27 # Create a change in project1 that a project2 change will depend on
28 A = self.fake_github.openFakePullRequest('org/project1', 'master', 'A')
29
30 # Create a commit in B that sets the dependency on A
31 msg = "Depends-On: https://github.com/org/project1/pull/%s" % A.number
32 B = self.fake_github.openFakePullRequest('org/project2', 'master', 'B',
33 body=msg)
34
35 # Make an event to re-use
36 event = B.getPullRequestEditedEvent()
37
38 self.fake_github.emitEvent(event)
39 self.waitUntilSettled()
40
41 # The changes for the job from project2 should include the project1
42 # PR contet
43 changes = self.getJobFromHistory(
44 'project2-test', 'org/project2').changes
45
46 self.assertEqual(changes, "%s,%s %s,%s" % (A.number,
47 A.head_sha,
48 B.number,
49 B.head_sha))
50
51 # There should be no more changes in the queue
52 tenant = self.sched.abide.tenants.get('tenant-one')
53 self.assertEqual(len(tenant.layout.pipelines['check'].queues), 0)
54
55 @simple_layout('layouts/crd-github.yaml', driver='github')
56 def test_crd_dependent(self):
57 "Test cross-repo dependences on a dependent pipeline"
58
59 # Create a change in project3 that a project4 change will depend on
60 A = self.fake_github.openFakePullRequest('org/project3', 'master', 'A')
61
62 # Create a commit in B that sets the dependency on A
63 msg = "Depends-On: https://github.com/org/project3/pull/%s" % A.number
64 B = self.fake_github.openFakePullRequest('org/project4', 'master', 'B',
65 body=msg)
66
67 # Make an event to re-use
68 event = B.getPullRequestEditedEvent()
69
70 self.fake_github.emitEvent(event)
71 self.waitUntilSettled()
72
73 # The changes for the job from project4 should include the project3
74 # PR contet
75 changes = self.getJobFromHistory(
76 'project4-test', 'org/project4').changes
77
78 self.assertEqual(changes, "%s,%s %s,%s" % (A.number,
79 A.head_sha,
80 B.number,
81 B.head_sha))
82
83 self.assertTrue(A.is_merged)
84 self.assertTrue(B.is_merged)
85
86 @simple_layout('layouts/crd-github.yaml', driver='github')
87 def test_crd_unshared_dependent(self):
88 "Test cross-repo dependences on unshared dependent pipeline"
89
90 # Create a change in project1 that a project2 change will depend on
91 A = self.fake_github.openFakePullRequest('org/project5', 'master', 'A')
92
93 # Create a commit in B that sets the dependency on A
94 msg = "Depends-On: https://github.com/org/project5/pull/%s" % A.number
95 B = self.fake_github.openFakePullRequest('org/project6', 'master', 'B',
96 body=msg)
97
98 # Make an event for B
99 event = B.getPullRequestEditedEvent()
100
101 # Emit for B, which should not enqueue A because they do not share
102 # A queue. Since B depends on A, and A isn't enqueue, B will not run
103 self.fake_github.emitEvent(event)
104 self.waitUntilSettled()
105
106 self.assertEqual(0, len(self.history))
107
108 # Enqueue A alone, let it finish
109 self.fake_github.emitEvent(A.getPullRequestEditedEvent())
110 self.waitUntilSettled()
111
112 self.assertTrue(A.is_merged)
113 self.assertFalse(B.is_merged)
114 self.assertEqual(1, len(self.history))
115
116 # With A merged, B should go through
117 self.fake_github.emitEvent(event)
118 self.waitUntilSettled()
119
120 self.assertTrue(B.is_merged)
121 self.assertEqual(2, len(self.history))
122
123 @simple_layout('layouts/crd-github.yaml', driver='github')
124 def test_crd_cycle(self):
125 "Test cross-repo dependency cycles"
126
127 # A -> B -> A
128 msg = "Depends-On: https://github.com/org/project6/pull/2"
129 A = self.fake_github.openFakePullRequest('org/project5', 'master', 'A',
130 body=msg)
131 msg = "Depends-On: https://github.com/org/project5/pull/1"
132 B = self.fake_github.openFakePullRequest('org/project6', 'master', 'B',
133 body=msg)
134
135 self.fake_github.emitEvent(A.getPullRequestEditedEvent())
136 self.waitUntilSettled()
137
138 self.assertFalse(A.is_merged)
139 self.assertFalse(B.is_merged)
140 self.assertEqual(0, len(self.history))
141
142 @simple_layout('layouts/crd-github.yaml', driver='github')
143 def test_crd_needed_changes(self):
144 "Test cross-repo needed changes discovery"
145
146 # Given change A and B, where B depends on A, when A
147 # completes B should be enqueued (using a shared queue)
148
149 # Create a change in project3 that a project4 change will depend on
150 A = self.fake_github.openFakePullRequest('org/project3', 'master', 'A')
151
152 # Set B to depend on A
153 msg = "Depends-On: https://github.com/org/project3/pull/%s" % A.number
154 B = self.fake_github.openFakePullRequest('org/project4', 'master', 'B',
155 body=msg)
156
157 # Enqueue A, which when finished should enqueue B
158 self.fake_github.emitEvent(A.getPullRequestEditedEvent())
159 self.waitUntilSettled()
160
161 # The changes for the job from project4 should include the project3
162 # PR contet
163 changes = self.getJobFromHistory(
164 'project4-test', 'org/project4').changes
165
166 self.assertEqual(changes, "%s,%s %s,%s" % (A.number,
167 A.head_sha,
168 B.number,
169 B.head_sha))
170
171 self.assertTrue(A.is_merged)
172 self.assertTrue(B.is_merged)