blob: ea7e85a9749970e73b93a776c37464cade3d632b [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. Blairff555742017-02-19 11:34:27 -080020from tests.base import AnsibleZuulTestCase, ZuulTestCase
James E. Blair59fdbac2015-12-07 17:08:06 -080021
James E. Blair59fdbac2015-12-07 17:08:06 -080022
James E. Blair3f876d52016-07-22 13:07:14 -070023class TestMultipleTenants(AnsibleZuulTestCase):
James E. Blair59fdbac2015-12-07 17:08:06 -080024 # A temporary class to hold new tests while others are disabled
25
James E. Blair2a629ec2015-12-22 15:32:02 -080026 tenant_config_file = 'config/multi-tenant/main.yaml'
James E. Blair59fdbac2015-12-07 17:08:06 -080027
James E. Blair83005782015-12-11 14:46:03 -080028 def test_multiple_tenants(self):
James E. Blair96f26942015-12-09 10:15:59 -080029 A = self.fake_gerrit.addFakeChange('org/project1', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070030 A.addApproval('code-review', 2)
31 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair59fdbac2015-12-07 17:08:06 -080032 self.waitUntilSettled()
James E. Blair96f26942015-12-09 10:15:59 -080033 self.assertEqual(self.getJobFromHistory('project1-test1').result,
James E. Blair59fdbac2015-12-07 17:08:06 -080034 'SUCCESS')
James E. Blair96c6bf82016-01-15 16:20:40 -080035 self.assertEqual(self.getJobFromHistory('python27').result,
36 'SUCCESS')
James E. Blair59fdbac2015-12-07 17:08:06 -080037 self.assertEqual(A.data['status'], 'MERGED')
James E. Blair96f26942015-12-09 10:15:59 -080038 self.assertEqual(A.reported, 2,
39 "A should report start and success")
40 self.assertIn('tenant-one-gate', A.messages[1],
41 "A should transit tenant-one gate")
42 self.assertNotIn('tenant-two-gate', A.messages[1],
43 "A should *not* transit tenant-two gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080044
James E. Blair96f26942015-12-09 10:15:59 -080045 B = self.fake_gerrit.addFakeChange('org/project2', 'master', 'B')
James E. Blair8b5408c2016-08-08 15:37:46 -070046 B.addApproval('code-review', 2)
47 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
James E. Blair96f26942015-12-09 10:15:59 -080048 self.waitUntilSettled()
James E. Blair96c6bf82016-01-15 16:20:40 -080049 self.assertEqual(self.getJobFromHistory('python27',
50 'org/project2').result,
51 'SUCCESS')
James E. Blair96f26942015-12-09 10:15:59 -080052 self.assertEqual(self.getJobFromHistory('project2-test1').result,
53 'SUCCESS')
54 self.assertEqual(B.data['status'], 'MERGED')
55 self.assertEqual(B.reported, 2,
56 "B should report start and success")
57 self.assertIn('tenant-two-gate', B.messages[1],
58 "B should transit tenant-two gate")
59 self.assertNotIn('tenant-one-gate', B.messages[1],
60 "B should *not* transit tenant-one gate")
James E. Blair59fdbac2015-12-07 17:08:06 -080061
James E. Blair96f26942015-12-09 10:15:59 -080062 self.assertEqual(A.reported, 2, "Activity in tenant two should"
63 "not affect tenant one")
James E. Blair14abdf42015-12-09 16:11:53 -080064
James E. Blair83005782015-12-11 14:46:03 -080065
James E. Blairff555742017-02-19 11:34:27 -080066class TestInRepoConfig(ZuulTestCase):
James E. Blair83005782015-12-11 14:46:03 -080067 # A temporary class to hold new tests while others are disabled
68
James E. Blair2a629ec2015-12-22 15:32:02 -080069 tenant_config_file = 'config/in-repo/main.yaml'
James E. Blair83005782015-12-11 14:46:03 -080070
James E. Blair83005782015-12-11 14:46:03 -080071 def test_in_repo_config(self):
James E. Blair14abdf42015-12-09 16:11:53 -080072 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
James E. Blair8b5408c2016-08-08 15:37:46 -070073 A.addApproval('code-review', 2)
74 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair14abdf42015-12-09 16:11:53 -080075 self.waitUntilSettled()
76 self.assertEqual(self.getJobFromHistory('project-test1').result,
77 'SUCCESS')
78 self.assertEqual(A.data['status'], 'MERGED')
79 self.assertEqual(A.reported, 2,
80 "A should report start and success")
81 self.assertIn('tenant-one-gate', A.messages[1],
82 "A should transit tenant-one gate")
James E. Blairb97ed802015-12-21 15:55:35 -080083
James E. Blair8b1dc3f2016-07-05 16:49:00 -070084 def test_dynamic_config(self):
85 in_repo_conf = textwrap.dedent(
86 """
87 - job:
88 name: project-test2
89
90 - project:
91 name: org/project
92 tenant-one-gate:
93 jobs:
94 - project-test2
95 """)
96
James E. Blairc73c73a2017-01-20 15:15:15 -080097 in_repo_playbook = textwrap.dedent(
98 """
99 - hosts: all
100 tasks: []
101 """)
102
103 file_dict = {'.zuul.yaml': in_repo_conf,
104 'playbooks/project-test2.yaml': in_repo_playbook}
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700105 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
James E. Blairc73c73a2017-01-20 15:15:15 -0800106 files=file_dict)
James E. Blair8b5408c2016-08-08 15:37:46 -0700107 A.addApproval('code-review', 2)
108 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700109 self.waitUntilSettled()
James E. Blair8b1dc3f2016-07-05 16:49:00 -0700110 self.assertEqual(A.data['status'], 'MERGED')
111 self.assertEqual(A.reported, 2,
112 "A should report start and success")
113 self.assertIn('tenant-one-gate', A.messages[1],
114 "A should transit tenant-one gate")
James E. Blair646322f2017-01-27 15:50:34 -0800115 self.assertHistory([
116 dict(name='project-test2', result='SUCCESS', changes='1,1')])
117
James E. Blairc2a5ed72017-02-20 14:12:01 -0500118 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
119
James E. Blair646322f2017-01-27 15:50:34 -0800120 # Now that the config change is landed, it should be live for
121 # subsequent changes.
122 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
123 B.addApproval('code-review', 2)
124 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
125 self.waitUntilSettled()
126 self.assertEqual(self.getJobFromHistory('project-test2').result,
127 'SUCCESS')
128 self.assertHistory([
129 dict(name='project-test2', result='SUCCESS', changes='1,1'),
130 dict(name='project-test2', result='SUCCESS', changes='2,1')])
James E. Blairc73c73a2017-01-20 15:15:15 -0800131
James E. Blairff555742017-02-19 11:34:27 -0800132 def test_in_repo_branch(self):
133 in_repo_conf = textwrap.dedent(
134 """
135 - job:
136 name: project-test2
137
138 - project:
139 name: org/project
140 tenant-one-gate:
141 jobs:
142 - project-test2
143 """)
144
145 in_repo_playbook = textwrap.dedent(
146 """
147 - hosts: all
148 tasks: []
149 """)
150
151 file_dict = {'.zuul.yaml': in_repo_conf,
152 'playbooks/project-test2.yaml': in_repo_playbook}
153 self.create_branch('org/project', 'stable')
154 A = self.fake_gerrit.addFakeChange('org/project', 'stable', 'A',
155 files=file_dict)
156 A.addApproval('code-review', 2)
157 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
158 self.waitUntilSettled()
159 self.assertEqual(A.data['status'], 'MERGED')
160 self.assertEqual(A.reported, 2,
161 "A should report start and success")
162 self.assertIn('tenant-one-gate', A.messages[1],
163 "A should transit tenant-one gate")
164 self.assertHistory([
165 dict(name='project-test2', result='SUCCESS', changes='1,1')])
166 self.fake_gerrit.addEvent(A.getChangeMergedEvent())
167
168 # The config change should not affect master.
169 B = self.fake_gerrit.addFakeChange('org/project', 'master', 'B')
170 B.addApproval('code-review', 2)
171 self.fake_gerrit.addEvent(B.addApproval('approved', 1))
172 self.waitUntilSettled()
173 self.assertHistory([
174 dict(name='project-test2', result='SUCCESS', changes='1,1'),
175 dict(name='project-test1', result='SUCCESS', changes='2,1')])
176
177 # The config change should be live for further changes on
178 # stable.
179 C = self.fake_gerrit.addFakeChange('org/project', 'stable', 'C')
180 C.addApproval('code-review', 2)
181 self.fake_gerrit.addEvent(C.addApproval('approved', 1))
182 self.waitUntilSettled()
183 self.assertHistory([
184 dict(name='project-test2', result='SUCCESS', changes='1,1'),
185 dict(name='project-test1', result='SUCCESS', changes='2,1'),
186 dict(name='project-test2', result='SUCCESS', changes='3,1')])
187
James E. Blair149b69c2017-03-02 10:48:16 -0800188 def test_untrusted_syntax_error(self):
James E. Blaire53250c2017-03-01 14:34:36 -0800189 in_repo_conf = textwrap.dedent(
190 """
191 - job:
192 name: project-test2
193 foo: error
194 """)
195
196 file_dict = {'.zuul.yaml': in_repo_conf}
197 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A',
198 files=file_dict)
199 A.addApproval('code-review', 2)
200 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
201 self.waitUntilSettled()
202
203 self.assertEqual(A.data['status'], 'NEW')
204 self.assertEqual(A.reported, 2,
205 "A should report start and failure")
206 self.assertIn('syntax error', A.messages[1],
207 "A should have a syntax error reported")
208
James E. Blair149b69c2017-03-02 10:48:16 -0800209 def test_trusted_syntax_error(self):
210 in_repo_conf = textwrap.dedent(
211 """
212 - job:
213 name: project-test2
214 foo: error
215 """)
216
217 file_dict = {'zuul.yaml': in_repo_conf}
218 A = self.fake_gerrit.addFakeChange('common-config', 'master', 'A',
219 files=file_dict)
220 A.addApproval('code-review', 2)
221 self.fake_gerrit.addEvent(A.addApproval('approved', 1))
222 self.waitUntilSettled()
223
224 self.assertEqual(A.data['status'], 'NEW')
225 self.assertEqual(A.reported, 2,
226 "A should report start and failure")
227 self.assertIn('syntax error', A.messages[1],
228 "A should have a syntax error reported")
229
James E. Blairc73c73a2017-01-20 15:15:15 -0800230
231class TestAnsible(AnsibleZuulTestCase):
232 # A temporary class to hold new tests while others are disabled
233
234 tenant_config_file = 'config/ansible/main.yaml'
235
236 def test_playbook(self):
237 A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
238 self.fake_gerrit.addEvent(A.getPatchsetCreatedEvent(1))
239 self.waitUntilSettled()
Paul Belanger96618ed2017-03-01 09:42:33 -0500240 build = self.getJobFromHistory('timeout')
241 self.assertEqual(build.result, 'ABORTED')
Monty Taylorc231d932017-02-03 09:57:15 -0600242 build = self.getJobFromHistory('faillocal')
243 self.assertEqual(build.result, 'FAILURE')
James E. Blaira92cbc82017-01-23 14:56:49 -0800244 build = self.getJobFromHistory('python27')
245 self.assertEqual(build.result, 'SUCCESS')
246 flag_path = os.path.join(self.test_root, build.uuid + '.flag')
247 self.assertTrue(os.path.exists(flag_path))
Monty Taylorc231d932017-02-03 09:57:15 -0600248 copied_path = os.path.join(self.test_root, build.uuid +
249 '.copied')
250 self.assertTrue(os.path.exists(copied_path))
251 failed_path = os.path.join(self.test_root, build.uuid +
252 '.failed')
253 self.assertFalse(os.path.exists(failed_path))
James E. Blair66b274e2017-01-31 14:47:52 -0800254 pre_flag_path = os.path.join(self.test_root, build.uuid +
255 '.pre.flag')
256 self.assertTrue(os.path.exists(pre_flag_path))
257 post_flag_path = os.path.join(self.test_root, build.uuid +
258 '.post.flag')
259 self.assertTrue(os.path.exists(post_flag_path))
James E. Blair5ac93842017-01-20 06:47:34 -0800260 bare_role_flag_path = os.path.join(self.test_root,
261 build.uuid + '.bare-role.flag')
262 self.assertTrue(os.path.exists(bare_role_flag_path))