blob: 6ed9de4f30af66736697c2c78cd0e8c47ef93816 [file] [log] [blame]
James E. Blair59fdbac2015-12-07 17:08:06 -08001#!/usr/bin/env python
2
3# Copyright 2012 Hewlett-Packard Development Company, L.P.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
James E. Blaira92cbc82017-01-23 14:56:49 -080017import os
James E. Blair14abdf42015-12-09 16:11:53 -080018import textwrap
James E. Blair59fdbac2015-12-07 17:08:06 -080019
James E. Blairb9c0d772017-03-03 14:34:49 -080020import testtools
21
22import zuul.configloader
James E. Blairff555742017-02-19 11:34:27 -080023from tests.base import AnsibleZuulTestCase, ZuulTestCase
James E. Blair59fdbac2015-12-07 17:08:06 -080024
James E. Blair59fdbac2015-12-07 17:08:06 -080025
James E. Blair3f876d52016-07-22 13:07:14 -070026class TestMultipleTenants(AnsibleZuulTestCase):
James E. Blair59fdbac2015-12-07 17:08:06 -080027 # A temporary class to hold new tests while others are disabled
28
James E. Blair2a629ec2015-12-22 15:32:02 -080029 tenant_config_file = 'config/multi-tenant/main.yaml'
James E. Blair59fdbac2015-12-07 17:08:06 -080030
James E. Blair83005782015-12-11 14:46:03 -080031 def test_multiple_tenants(self):
James E. Blair96f26942015-12-09 10:15:59 -080032 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070033 A.addApproval('code-review', 2)
34 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair59fdbac2015-12-07 17:08:06 -080035 self.waitUntilSettled()
James E. Blair96f26942015-12-09 10:15:59 -080036 self.assertEqual(self.getJobFromHistory('project1-test1').result,
James E. Blair59fdbac2015-12-07 17:08:06 -080037 'SUCCESS')
James E. Blair96c6bf82016-01-15 16:20:40 -080038 self.assertEqual(self.getJobFromHistory('python27').result,
39 'SUCCESS')
James E. Blair59fdbac2015-12-07 17:08:06 -080040 self.assertEqual(A.data['status'], 'MERGED')
James E. Blair96f26942015-12-09 10:15:59 -080041 self.assertEqual(A.reported, 2,
42 "A should report start and success")
43 self.assertIn('tenant-one-gate', A.messages[1],
44 "A should transit tenant-one gate")
45 self.assertNotIn('tenant-two-gate', A.messages[1],
46 "A should *not* transit tenant-two gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080047
James E. Blair96f26942015-12-09 10:15:59 -080048 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -070049 B.addApproval('code-review', 2)
50 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96f26942015-12-09 10:15:59 -080051 self.waitUntilSettled()
James E. Blair96c6bf82016-01-15 16:20:40 -080052 self.assertEqual(self.getJobFromHistory('python27',
53 'org/project2').result,
54 'SUCCESS')
James E. Blair96f26942015-12-09 10:15:59 -080055 self.assertEqual(self.getJobFromHistory('project2-test1').result,
56 'SUCCESS')
57 self.assertEqual(B.data['status'], 'MERGED')
58 self.assertEqual(B.reported, 2,
59 "B should report start and success")
60 self.assertIn('tenant-two-gate', B.messages[1],
61 "B should transit tenant-two gate")
62 self.assertNotIn('tenant-one-gate', B.messages[1],
63 "B should *not* transit tenant-one gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080064
James E. Blair96f26942015-12-09 10:15:59 -080065 self.assertEqual(A.reported, 2, "Activity in tenant two should"
66 "not affect tenant one")
James E. Blair14abdf42015-12-09 16:11:53 -080067
James E. Blair83005782015-12-11 14:46:03 -080068
James E. Blairff555742017-02-19 11:34:27 -080069class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -080070 # A temporary class to hold new tests while others are disabled
71
James E. Blair2a629ec2015-12-22 15:32:02 -080072 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -080073
James E. Blair83005782015-12-11 14:46:03 -080074 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -080075 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070076 A.addApproval('code-review', 2)
77 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -080078 self.waitUntilSettled()
79 self.assertEqual(self.getJobFromHistory('project-test1').result,
80 'SUCCESS')
81 self.assertEqual(A.data['status'], 'MERGED')
82 self.assertEqual(A.reported, 2,
83 "A should report start and success")
84 self.assertIn('tenant-one-gate', A.messages[1],
85 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -080086
James E. Blair8b1dc3f2016-07-05 16:49:00 -070087 def test_dynamic_config(self):
88 in_repo_conf = textwrap.dedent(
89 """
90 - job:
91 name: project-test2
92
93 - project:
94 name: org/project
95 tenant-one-gate:
96 jobs:
97 - project-test2
98 """)
99
James E. Blairc73c73a2017-01-20 15:15:15 -0800100 in_repo_playbook = textwrap.dedent(
101 """
102 - hosts: all
103 tasks: []
104 """)
105
106 file_dict = {'.zuul.yaml': in_repo_conf,
107 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700108 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800109 files=file_dict)
James E. Blair8b5408c2016-08-08 15:37:46 -0700110 A.addApproval('code-review', 2)
111 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700112 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700113 self.assertEqual(A.data['status'], 'MERGED')
114 self.assertEqual(A.reported, 2,
115 "A should report start and success")
116 self.assertIn('tenant-one-gate', A.messages[1],
117 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800118 self.assertHistory([
119 dict(name='project-test2', result='SUCCESS', changes='1,1')])
120
James E. Blairc2a5ed72017-02-20 14:12:01 -0500121 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
122
James E. Blair646322f2017-01-27 15:50:34 -0800123 # Now that the config change is landed, it should be live for
124 # subsequent changes.
125 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
126 B.addApproval('code-review', 2)
127 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
128 self.waitUntilSettled()
129 self.assertEqual(self.getJobFromHistory('project-test2').result,
130 'SUCCESS')
131 self.assertHistory([
132 dict(name='project-test2', result='SUCCESS', changes='1,1'),
133 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800134
James E. Blairff555742017-02-19 11:34:27 -0800135 def test_in_repo_branch(self):
136 in_repo_conf = textwrap.dedent(
137 """
138 - job:
139 name: project-test2
140
141 - project:
142 name: org/project
143 tenant-one-gate:
144 jobs:
145 - project-test2
146 """)
147
148 in_repo_playbook = textwrap.dedent(
149 """
150 - hosts: all
151 tasks: []
152 """)
153
154 file_dict = {'.zuul.yaml': in_repo_conf,
155 'playbooks/project-test2.yaml': in_repo_playbook}
156 self.create_branch('org/project', 'stable')
157 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
158 files=file_dict)
159 A.addApproval('code-review', 2)
160 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
161 self.waitUntilSettled()
162 self.assertEqual(A.data['status'], 'MERGED')
163 self.assertEqual(A.reported, 2,
164 "A should report start and success")
165 self.assertIn('tenant-one-gate', A.messages[1],
166 "A should transit tenant-one gate")
167 self.assertHistory([
168 dict(name='project-test2', result='SUCCESS', changes='1,1')])
169 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
170
171 # The config change should not affect master.
172 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
173 B.addApproval('code-review', 2)
174 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
175 self.waitUntilSettled()
176 self.assertHistory([
177 dict(name='project-test2', result='SUCCESS', changes='1,1'),
178 dict(name='project-test1', result='SUCCESS', changes='2,1')])
179
180 # The config change should be live for further changes on
181 # stable.
182 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
183 C.addApproval('code-review', 2)
184 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
185 self.waitUntilSettled()
186 self.assertHistory([
187 dict(name='project-test2', result='SUCCESS', changes='1,1'),
188 dict(name='project-test1', result='SUCCESS', changes='2,1'),
189 dict(name='project-test2', result='SUCCESS', changes='3,1')])
190
James E. Blair149b69c2017-03-02 10:48:16 -0800191 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800192 in_repo_conf = textwrap.dedent(
193 """
194 - job:
195 name: project-test2
196 foo: error
197 """)
198
199 file_dict = {'.zuul.yaml': in_repo_conf}
200 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
201 files=file_dict)
202 A.addApproval('code-review', 2)
203 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
204 self.waitUntilSettled()
205
206 self.assertEqual(A.data['status'], 'NEW')
207 self.assertEqual(A.reported, 2,
208 "A should report start and failure")
209 self.assertIn('syntax error', A.messages[1],
210 "A should have a syntax error reported")
211
James E. Blair149b69c2017-03-02 10:48:16 -0800212 def test_trusted_syntax_error(self):
213 in_repo_conf = textwrap.dedent(
214 """
215 - job:
216 name: project-test2
217 foo: error
218 """)
219
220 file_dict = {'zuul.yaml': in_repo_conf}
221 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
222 files=file_dict)
223 A.addApproval('code-review', 2)
224 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
225 self.waitUntilSettled()
226
227 self.assertEqual(A.data['status'], 'NEW')
228 self.assertEqual(A.reported, 2,
229 "A should report start and failure")
230 self.assertIn('syntax error', A.messages[1],
231 "A should have a syntax error reported")
232
James E. Blair6f140c72017-03-03 10:32:07 -0800233 def test_untrusted_yaml_error(self):
234 in_repo_conf = textwrap.dedent(
235 """
236 - job:
237 foo: error
238 """)
239
240 file_dict = {'.zuul.yaml': in_repo_conf}
241 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
242 files=file_dict)
243 A.addApproval('code-review', 2)
244 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
245 self.waitUntilSettled()
246
247 self.assertEqual(A.data['status'], 'NEW')
248 self.assertEqual(A.reported, 2,
249 "A should report start and failure")
250 self.assertIn('syntax error', A.messages[1],
251 "A should have a syntax error reported")
252
James E. Blairc73c73a2017-01-20 15:15:15 -0800253
254class TestAnsible(AnsibleZuulTestCase):
255 # A temporary class to hold new tests while others are disabled
256
257 tenant_config_file = 'config/ansible/main.yaml'
258
259 def test_playbook(self):
260 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
261 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
262 self.waitUntilSettled()
Paul Belanger96618ed2017-03-01 09:42:33 -0500263 build = self.getJobFromHistory('timeout')
264 self.assertEqual(build.result, 'ABORTED')
Monty Taylorc231d932017-02-03 09:57:15 -0600265 build = self.getJobFromHistory('faillocal')
266 self.assertEqual(build.result, 'FAILURE')
James E. Blaira92cbc82017-01-23 14:56:49 -0800267 build = self.getJobFromHistory('python27')
268 self.assertEqual(build.result, 'SUCCESS')
269 flag_path = os.path.join(self.test_root, build.uuid + '.flag')
270 self.assertTrue(os.path.exists(flag_path))
Monty Taylorc231d932017-02-03 09:57:15 -0600271 copied_path = os.path.join(self.test_root, build.uuid +
272 '.copied')
273 self.assertTrue(os.path.exists(copied_path))
274 failed_path = os.path.join(self.test_root, build.uuid +
275 '.failed')
276 self.assertFalse(os.path.exists(failed_path))
James E. Blair66b274e2017-01-31 14:47:52 -0800277 pre_flag_path = os.path.join(self.test_root, build.uuid +
278 '.pre.flag')
279 self.assertTrue(os.path.exists(pre_flag_path))
280 post_flag_path = os.path.join(self.test_root, build.uuid +
281 '.post.flag')
282 self.assertTrue(os.path.exists(post_flag_path))
James E. Blair5ac93842017-01-20 06:47:34 -0800283 bare_role_flag_path = os.path.join(self.test_root,
284 build.uuid + '.bare-role.flag')
285 self.assertTrue(os.path.exists(bare_role_flag_path))
James E. Blairb9c0d772017-03-03 14:34:49 -0800286
287
288class TestBrokenConfig(ZuulTestCase):
289 # Test that we get an appropriate syntax error if we start with a
290 # broken config.
291
292 tenant_config_file = 'config/broken/main.yaml'
293
294 def setUp(self):
295 with testtools.ExpectedException(
296 zuul.configloader.ConfigurationSyntaxError,
297 "\nZuul encountered a syntax error"):
298 super(TestBrokenConfig, self).setUp()
299
300 def test_broken_config_on_startup(self):
301 pass