1 | # -*- coding: utf-8 -*- |
---|
2 | # |
---|
3 | # Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de> |
---|
4 | # Copyright (C) 2007-2010 Edgewall Software |
---|
5 | # All rights reserved. |
---|
6 | # |
---|
7 | # This software is licensed as described in the file COPYING, which |
---|
8 | # you should have received as part of this distribution. The terms |
---|
9 | # are also available at http://bitten.edgewall.org/wiki/License. |
---|
10 | |
---|
11 | import os |
---|
12 | import re |
---|
13 | import shutil |
---|
14 | from StringIO import StringIO |
---|
15 | import tempfile |
---|
16 | import unittest |
---|
17 | import cgi |
---|
18 | from Cookie import SimpleCookie as Cookie |
---|
19 | |
---|
20 | from trac.attachment import Attachment |
---|
21 | from trac.db import DatabaseManager |
---|
22 | from trac.perm import PermissionCache, PermissionSystem |
---|
23 | from trac.test import EnvironmentStub, Mock |
---|
24 | from trac.util.datefmt import to_datetime, utc |
---|
25 | from trac.web.api import RequestDone |
---|
26 | from trac.web.href import Href |
---|
27 | |
---|
28 | from bitten.master import BuildMaster |
---|
29 | from bitten.slave import encode_multipart_formdata |
---|
30 | from bitten.model import BuildConfig, TargetPlatform, Build, BuildStep, \ |
---|
31 | BuildLog, Report, schema |
---|
32 | from bitten import PROTOCOL_VERSION |
---|
33 | |
---|
34 | class BuildMasterTestCase(unittest.TestCase): |
---|
35 | |
---|
36 | def setUp(self): |
---|
37 | self.env = EnvironmentStub(enable=['trac.*', 'bitten.*']) |
---|
38 | self.env.path = tempfile.mkdtemp() |
---|
39 | logs_dir = self.env.config.get("bitten", "logs_dir") |
---|
40 | if os.path.isabs(logs_dir): |
---|
41 | raise ValueError("Should not have absolute logs directory for temporary test") |
---|
42 | logs_dir = os.path.join(self.env.path, logs_dir) |
---|
43 | os.makedirs(logs_dir) |
---|
44 | |
---|
45 | PermissionSystem(self.env).grant_permission('hal', 'BUILD_EXEC') |
---|
46 | |
---|
47 | # Create tables |
---|
48 | db = self.env.get_db_cnx() |
---|
49 | cursor = db.cursor() |
---|
50 | connector, _ = DatabaseManager(self.env)._get_connector() |
---|
51 | for table in schema: |
---|
52 | for stmt in connector.to_sql(table): |
---|
53 | cursor.execute(stmt) |
---|
54 | |
---|
55 | # Hook up a dummy repository |
---|
56 | self.repos = Mock(get_changeset=lambda rev: Mock(author = 'author')) |
---|
57 | self.env.get_repository = lambda authname=None: self.repos # 0.11 |
---|
58 | try: # 0.12+ |
---|
59 | from trac.core import Component, implements |
---|
60 | from trac.versioncontrol.api import IRepositoryConnector, \ |
---|
61 | IRepositoryProvider |
---|
62 | class DummyRepos(Component): |
---|
63 | implements(IRepositoryConnector, IRepositoryProvider) |
---|
64 | def get_supported_types(self): |
---|
65 | yield ('dummy', 9) |
---|
66 | def get_repository(this, repos_type, repos_dir, params): |
---|
67 | return self.repos # Note: 'this' vs 'self' usage |
---|
68 | def get_repositories(self): |
---|
69 | yield ('', {'dir': 'dummy_dir', 'type': 'dummy'}) |
---|
70 | self.dummy = DummyRepos |
---|
71 | except ImportError: |
---|
72 | self.dummy = None # not supported, will use get_repository() |
---|
73 | |
---|
74 | def tearDown(self): |
---|
75 | if self.dummy: # remove from components list + interfaces dict |
---|
76 | self.env.__metaclass__._components.remove(self.dummy) |
---|
77 | for key in self.env.__metaclass__._registry.keys(): |
---|
78 | if self.dummy in self.env.__metaclass__._registry[key]: |
---|
79 | self.env.__metaclass__._registry[key].remove(self.dummy) |
---|
80 | shutil.rmtree(self.env.path) |
---|
81 | |
---|
82 | def test_create_build(self): |
---|
83 | BuildConfig(self.env, 'test', path='somepath', active=True).insert() |
---|
84 | platform = TargetPlatform(self.env, config='test', name="Unix") |
---|
85 | platform.rules.append(('family', 'posix')) |
---|
86 | platform.insert() |
---|
87 | |
---|
88 | self.repos = Mock( |
---|
89 | get_node=lambda path, rev=None: Mock( |
---|
90 | get_entries=lambda: [Mock(), Mock()], |
---|
91 | get_history=lambda: [('somepath', 123, 'edit'), |
---|
92 | ('somepath', 121, 'edit'), |
---|
93 | ('somepath', 120, 'edit')] |
---|
94 | ), |
---|
95 | get_changeset=lambda rev: Mock(date=to_datetime(42, utc)), |
---|
96 | normalize_path=lambda path: path, |
---|
97 | rev_older_than=lambda rev1, rev2: rev1 < rev2 |
---|
98 | ) |
---|
99 | |
---|
100 | inheaders = {'Content-Type': 'application/x-bitten+xml'} |
---|
101 | inbody = StringIO("""<slave name="hal" version="%d"> |
---|
102 | <platform>Power Macintosh</platform> |
---|
103 | <os family="posix" version="8.1.0">Darwin</os> |
---|
104 | <package name="java" version="2.4.3"/> |
---|
105 | </slave>""" % PROTOCOL_VERSION) |
---|
106 | outheaders = {} |
---|
107 | outbody = StringIO() |
---|
108 | req = Mock(method='POST', base_path='', path_info='/builds', |
---|
109 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
110 | remote_addr='127.0.0.1', args={}, |
---|
111 | perm=PermissionCache(self.env, 'hal'), |
---|
112 | get_header=lambda x: inheaders.get(x), read=inbody.read, |
---|
113 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
114 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
115 | write=outbody.write, |
---|
116 | incookie=Cookie('trac_auth=')) |
---|
117 | |
---|
118 | module = BuildMaster(self.env) |
---|
119 | assert module.match_request(req) |
---|
120 | |
---|
121 | self.assertRaises(RequestDone, module.process_request, req) |
---|
122 | |
---|
123 | self.assertEqual(201, outheaders['Status']) |
---|
124 | self.assertEqual('text/plain', outheaders['Content-Type']) |
---|
125 | location = outheaders['Location'] |
---|
126 | mo = re.match('http://example.org/trac/builds/(\d+)', location) |
---|
127 | assert mo, 'Location was %r' % location |
---|
128 | self.assertEqual('Build pending', outbody.getvalue()) |
---|
129 | build = Build.fetch(self.env, int(mo.group(1))) |
---|
130 | self.assertEqual(Build.IN_PROGRESS, build.status) |
---|
131 | self.assertEqual('hal', build.slave) |
---|
132 | |
---|
133 | def test_create_build_invalid_xml(self): |
---|
134 | inheaders = {'Content-Type': 'application/x-bitten+xml'} |
---|
135 | inbody = StringIO('<slave></salve>') |
---|
136 | outheaders = {} |
---|
137 | outbody = StringIO() |
---|
138 | req = Mock(method='POST', base_path='', path_info='/builds', |
---|
139 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
140 | perm=PermissionCache(self.env, 'hal'), |
---|
141 | get_header=lambda x: inheaders.get(x), read=inbody.read, |
---|
142 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
143 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
144 | write=outbody.write, |
---|
145 | incookie=Cookie('trac_auth=')) |
---|
146 | |
---|
147 | module = BuildMaster(self.env) |
---|
148 | assert module.match_request(req) |
---|
149 | |
---|
150 | self.assertRaises(RequestDone, module.process_request, req) |
---|
151 | |
---|
152 | self.assertEquals(400, outheaders['Status']) |
---|
153 | self.assertEqual('XML parser error', outbody.getvalue()) |
---|
154 | |
---|
155 | def test_create_build_no_post(self): |
---|
156 | outheaders = {} |
---|
157 | outbody = StringIO() |
---|
158 | req = Mock(method='GET', base_path='', path_info='/builds', |
---|
159 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
160 | perm=PermissionCache(self.env, 'hal'), |
---|
161 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
162 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
163 | write=outbody.write, |
---|
164 | incookie=Cookie('trac_auth=')) |
---|
165 | |
---|
166 | module = BuildMaster(self.env) |
---|
167 | assert module.match_request(req) |
---|
168 | |
---|
169 | self.assertRaises(RequestDone, module.process_request, req) |
---|
170 | |
---|
171 | self.assertEquals(200, outheaders['Status']) |
---|
172 | self.assertEquals('Only POST allowed for build creation.', |
---|
173 | outbody.getvalue()) |
---|
174 | |
---|
175 | def test_create_build_no_match(self): |
---|
176 | inheaders = {'Content-Type': 'application/x-bitten+xml'} |
---|
177 | inbody = StringIO("""<slave name="hal" version="%d"> |
---|
178 | <platform>Power Macintosh</platform> |
---|
179 | <os family="posix" version="8.1.0">Darwin</os> |
---|
180 | </slave>""" % PROTOCOL_VERSION) |
---|
181 | outheaders = {} |
---|
182 | outbody = StringIO() |
---|
183 | req = Mock(method='POST', base_path='', path_info='/builds', |
---|
184 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
185 | perm=PermissionCache(self.env, 'hal'), |
---|
186 | get_header=lambda x: inheaders.get(x), read=inbody.read, |
---|
187 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
188 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
189 | write=outbody.write, |
---|
190 | incookie=Cookie('trac_auth=')) |
---|
191 | |
---|
192 | module = BuildMaster(self.env) |
---|
193 | assert module.match_request(req) |
---|
194 | |
---|
195 | self.assertRaises(RequestDone, module.process_request, req) |
---|
196 | |
---|
197 | self.assertEqual(204, outheaders['Status']) |
---|
198 | self.assertEqual('', outbody.getvalue()) |
---|
199 | |
---|
200 | def test_create_build_protocol_wrong_version(self): |
---|
201 | inheaders = {'Content-Type': 'application/x-bitten+xml'} |
---|
202 | inbody = StringIO("""<slave name="hal" version="%d"> |
---|
203 | <platform>Power Macintosh</platform> |
---|
204 | <os family="posix" version="8.1.0">Darwin</os> |
---|
205 | </slave>""" % (PROTOCOL_VERSION-2,)) |
---|
206 | outheaders = {} |
---|
207 | outbody = StringIO() |
---|
208 | req = Mock(method='POST', base_path='', path_info='/builds', |
---|
209 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
210 | perm=PermissionCache(self.env, 'hal'), |
---|
211 | get_header=lambda x: inheaders.get(x), read=inbody.read, |
---|
212 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
213 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
214 | write=outbody.write, |
---|
215 | incookie=Cookie('trac_auth=')) |
---|
216 | |
---|
217 | module = BuildMaster(self.env) |
---|
218 | assert module.match_request(req) |
---|
219 | |
---|
220 | self.assertRaises(RequestDone, module.process_request, req) |
---|
221 | |
---|
222 | self.assertEqual(400, outheaders['Status']) |
---|
223 | self.assertEqual('Master-Slave version mismatch: master=%d, slave=%d' \ |
---|
224 | % (PROTOCOL_VERSION, PROTOCOL_VERSION-2), |
---|
225 | outbody.getvalue()) |
---|
226 | |
---|
227 | def test_create_build_protocol_no_version(self): |
---|
228 | inheaders = {'Content-Type': 'application/x-bitten+xml'} |
---|
229 | inbody = StringIO("""<slave name="hal"> |
---|
230 | <platform>Power Macintosh</platform> |
---|
231 | <os family="posix" version="8.1.0">Darwin</os> |
---|
232 | </slave>""") |
---|
233 | outheaders = {} |
---|
234 | outbody = StringIO() |
---|
235 | req = Mock(method='POST', base_path='', path_info='/builds', |
---|
236 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
237 | perm=PermissionCache(self.env, 'hal'), |
---|
238 | get_header=lambda x: inheaders.get(x), read=inbody.read, |
---|
239 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
240 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
241 | write=outbody.write, |
---|
242 | incookie=Cookie('trac_auth=')) |
---|
243 | |
---|
244 | module = BuildMaster(self.env) |
---|
245 | assert module.match_request(req) |
---|
246 | |
---|
247 | self.assertRaises(RequestDone, module.process_request, req) |
---|
248 | |
---|
249 | self.assertEqual(400, outheaders['Status']) |
---|
250 | self.assertEqual('Master-Slave version mismatch: master=%d, slave=1' \ |
---|
251 | % (PROTOCOL_VERSION,), |
---|
252 | outbody.getvalue()) |
---|
253 | |
---|
254 | def test_cancel_build(self): |
---|
255 | config = BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
256 | recipe='<build></build>') |
---|
257 | config.insert() |
---|
258 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
259 | status=Build.IN_PROGRESS, started=42) |
---|
260 | build.insert() |
---|
261 | |
---|
262 | outheaders = {} |
---|
263 | outbody = StringIO() |
---|
264 | req = Mock(method='DELETE', base_path='', |
---|
265 | path_info='/builds/%d' % build.id, |
---|
266 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
267 | perm=PermissionCache(self.env, 'hal'), |
---|
268 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
269 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
270 | write=outbody.write, |
---|
271 | incookie=Cookie('trac_auth=')) |
---|
272 | |
---|
273 | module = BuildMaster(self.env) |
---|
274 | assert module.match_request(req) |
---|
275 | |
---|
276 | self.assertRaises(RequestDone, module.process_request, req) |
---|
277 | |
---|
278 | self.assertEqual(204, outheaders['Status']) |
---|
279 | self.assertEqual('', outbody.getvalue()) |
---|
280 | |
---|
281 | # Make sure the started timestamp has been set |
---|
282 | build = Build.fetch(self.env, build.id) |
---|
283 | self.assertEqual(Build.PENDING, build.status) |
---|
284 | assert not build.started |
---|
285 | |
---|
286 | def test_initiate_build(self): |
---|
287 | config = BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
288 | recipe='<build><step id="s1"></step></build>') |
---|
289 | config.insert() |
---|
290 | platform = TargetPlatform(self.env, config='test', name="Unix") |
---|
291 | platform.rules.append(('family', 'posix')) |
---|
292 | platform.insert() |
---|
293 | build = Build(self.env, 'test', '123', platform.id, slave='hal', |
---|
294 | rev_time=42) |
---|
295 | build.insert() |
---|
296 | |
---|
297 | outheaders = {} |
---|
298 | outbody = StringIO() |
---|
299 | |
---|
300 | req = Mock(method='GET', base_path='', |
---|
301 | path_info='/builds/%d' % build.id, |
---|
302 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
303 | authname='hal', |
---|
304 | perm=PermissionCache(self.env, 'hal'), |
---|
305 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
306 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
307 | write=outbody.write, |
---|
308 | form_token="12345", |
---|
309 | incookie=Cookie('trac_auth=')) |
---|
310 | |
---|
311 | module = BuildMaster(self.env) |
---|
312 | assert module.match_request(req) |
---|
313 | |
---|
314 | self.assertRaises(RequestDone, module.process_request, req) |
---|
315 | self.assertEqual(200, outheaders['Status']) |
---|
316 | self.assertEqual('163', outheaders['Content-Length']) |
---|
317 | self.assertEqual('application/x-bitten+xml', |
---|
318 | outheaders['Content-Type']) |
---|
319 | self.assertEqual('attachment; filename=recipe_test_r123.xml', |
---|
320 | outheaders['Content-Disposition']) |
---|
321 | self.assertEqual('<build build="1" config="test" form_token="12345" ' |
---|
322 | 'name="hal" path="somepath" platform="Unix" ' |
---|
323 | 'reponame="" repopath="somepath" ' |
---|
324 | 'revision="123"><step id="s1"/></build>', |
---|
325 | outbody.getvalue()) |
---|
326 | |
---|
327 | # Make sure the started timestamp has been set |
---|
328 | build = Build.fetch(self.env, build.id) |
---|
329 | assert build.started |
---|
330 | |
---|
331 | def test_initiate_build_no_such_build(self): |
---|
332 | outheaders = {} |
---|
333 | outbody = StringIO() |
---|
334 | req = Mock(method='GET', base_path='', |
---|
335 | path_info='/builds/123', href=Href('/trac'), |
---|
336 | remote_addr='127.0.0.1', args={}, |
---|
337 | perm=PermissionCache(self.env, 'hal'), |
---|
338 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
339 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
340 | write=outbody.write, |
---|
341 | incookie=Cookie('trac_auth=')) |
---|
342 | |
---|
343 | module = BuildMaster(self.env) |
---|
344 | assert module.match_request(req) |
---|
345 | |
---|
346 | self.assertRaises(RequestDone, module.process_request, req) |
---|
347 | |
---|
348 | self.assertEquals(404, outheaders['Status']) |
---|
349 | self.assertEquals('No such build (123)', outbody.getvalue()) |
---|
350 | |
---|
351 | def test_process_unknown_collection(self): |
---|
352 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
353 | recipe='<build></build>').insert() |
---|
354 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42) |
---|
355 | build.insert() |
---|
356 | |
---|
357 | outheaders = {} |
---|
358 | outbody = StringIO() |
---|
359 | req = Mock(method='POST', base_path='', |
---|
360 | path_info='/builds/%d/files/' % build.id, |
---|
361 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
362 | perm=PermissionCache(self.env, 'hal'), |
---|
363 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
364 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
365 | write=outbody.write, |
---|
366 | incookie=Cookie('trac_auth=')) |
---|
367 | |
---|
368 | module = BuildMaster(self.env) |
---|
369 | assert module.match_request(req) |
---|
370 | |
---|
371 | self.assertRaises(RequestDone, module.process_request, req) |
---|
372 | |
---|
373 | self.assertEqual(404, outheaders['Status']) |
---|
374 | self.assertEqual("No such collection 'files'", outbody.getvalue()) |
---|
375 | |
---|
376 | def test_process_build_step_success(self): |
---|
377 | recipe = """<build> |
---|
378 | <step id="foo"> |
---|
379 | </step> |
---|
380 | </build>""" |
---|
381 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
382 | recipe=recipe).insert() |
---|
383 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
384 | started=42, status=Build.IN_PROGRESS) |
---|
385 | build.slave_info[Build.TOKEN] = '123'; |
---|
386 | build.insert() |
---|
387 | |
---|
388 | inbody = StringIO("""<result step="foo" status="success" |
---|
389 | time="2007-04-01T15:30:00.0000" |
---|
390 | duration="3.45"> |
---|
391 | </result>""") |
---|
392 | outheaders = {} |
---|
393 | outbody = StringIO() |
---|
394 | req = Mock(method='POST', base_path='', |
---|
395 | path_info='/builds/%d/steps/' % build.id, |
---|
396 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
397 | remote_addr='127.0.0.1', args={}, |
---|
398 | perm=PermissionCache(self.env, 'hal'), |
---|
399 | read=inbody.read, |
---|
400 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
401 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
402 | write=outbody.write, |
---|
403 | incookie=Cookie('trac_auth=123')) |
---|
404 | module = BuildMaster(self.env) |
---|
405 | |
---|
406 | module._start_new_step(build, 'foo').insert() |
---|
407 | |
---|
408 | assert module.match_request(req) |
---|
409 | |
---|
410 | self.assertRaises(RequestDone, module.process_request, req) |
---|
411 | |
---|
412 | self.assertEqual(201, outheaders['Status']) |
---|
413 | self.assertEqual('20', outheaders['Content-Length']) |
---|
414 | self.assertEqual('text/plain', outheaders['Content-Type']) |
---|
415 | self.assertEqual('Build step processed', outbody.getvalue()) |
---|
416 | |
---|
417 | build = Build.fetch(self.env, build.id) |
---|
418 | self.assertEqual(Build.SUCCESS, build.status) |
---|
419 | assert build.stopped |
---|
420 | assert build.stopped > build.started |
---|
421 | |
---|
422 | steps = list(BuildStep.select(self.env, build.id)) |
---|
423 | self.assertEqual(1, len(steps)) |
---|
424 | self.assertEqual('foo', steps[0].name) |
---|
425 | self.assertEqual(BuildStep.SUCCESS, steps[0].status) |
---|
426 | |
---|
427 | def test_process_build_step_success_with_log(self): |
---|
428 | recipe = """<build> |
---|
429 | <step id="foo"> |
---|
430 | </step> |
---|
431 | </build>""" |
---|
432 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
433 | recipe=recipe).insert() |
---|
434 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
435 | started=42, status=Build.IN_PROGRESS) |
---|
436 | build.slave_info[Build.TOKEN] = '123'; |
---|
437 | build.insert() |
---|
438 | |
---|
439 | inbody = StringIO("""<result step="foo" status="success" |
---|
440 | time="2007-04-01T15:30:00.0000" |
---|
441 | duration="3.45"> |
---|
442 | <log generator="http://bitten.edgewall.org/tools/python#unittest"> |
---|
443 | <message level="info">Doing stuff</message> |
---|
444 | <message level="error">Ouch that hurt</message> |
---|
445 | </log> |
---|
446 | </result>""") |
---|
447 | outheaders = {} |
---|
448 | outbody = StringIO() |
---|
449 | req = Mock(method='POST', base_path='', |
---|
450 | path_info='/builds/%d/steps/' % build.id, |
---|
451 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
452 | remote_addr='127.0.0.1', args={}, |
---|
453 | perm=PermissionCache(self.env, 'hal'), |
---|
454 | read=inbody.read, |
---|
455 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
456 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
457 | write=outbody.write, |
---|
458 | incookie=Cookie('trac_auth=123')) |
---|
459 | module = BuildMaster(self.env) |
---|
460 | |
---|
461 | module._start_new_step(build, 'foo').insert() |
---|
462 | |
---|
463 | assert module.match_request(req) |
---|
464 | |
---|
465 | self.assertRaises(RequestDone, module.process_request, req) |
---|
466 | |
---|
467 | self.assertEqual(201, outheaders['Status']) |
---|
468 | self.assertEqual('20', outheaders['Content-Length']) |
---|
469 | self.assertEqual('text/plain', outheaders['Content-Type']) |
---|
470 | self.assertEqual('Build step processed', outbody.getvalue()) |
---|
471 | |
---|
472 | build = Build.fetch(self.env, build.id) |
---|
473 | self.assertEqual(Build.SUCCESS, build.status) |
---|
474 | assert build.stopped |
---|
475 | assert build.stopped > build.started |
---|
476 | |
---|
477 | steps = list(BuildStep.select(self.env, build.id)) |
---|
478 | self.assertEqual(1, len(steps)) |
---|
479 | self.assertEqual('foo', steps[0].name) |
---|
480 | self.assertEqual(BuildStep.SUCCESS, steps[0].status) |
---|
481 | |
---|
482 | logs = list(BuildLog.select(self.env, build=build.id, step='foo')) |
---|
483 | self.assertEqual(1, len(logs)) |
---|
484 | self.assertEqual('http://bitten.edgewall.org/tools/python#unittest', |
---|
485 | logs[0].generator) |
---|
486 | self.assertEqual(2, len(logs[0].messages)) |
---|
487 | self.assertEqual((u'info', u'Doing stuff'), logs[0].messages[0]) |
---|
488 | self.assertEqual((u'error', u'Ouch that hurt'), logs[0].messages[1]) |
---|
489 | |
---|
490 | def test_process_build_step_success_with_report(self): |
---|
491 | recipe = """<build> |
---|
492 | <step id="foo"> |
---|
493 | </step> |
---|
494 | </build>""" |
---|
495 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
496 | recipe=recipe).insert() |
---|
497 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
498 | started=42, status=Build.IN_PROGRESS) |
---|
499 | build.slave_info[Build.TOKEN] = '123'; |
---|
500 | build.insert() |
---|
501 | |
---|
502 | inbody = StringIO("""<result step="foo" status="success" |
---|
503 | time="2007-04-01T15:30:00.0000" |
---|
504 | duration="3.45"> |
---|
505 | <report category="test" |
---|
506 | generator="http://bitten.edgewall.org/tools/python#unittest"> |
---|
507 | <test fixture="my.Fixture" file="my/test/file.py"> |
---|
508 | <stdout>Doing my thing</stdout> |
---|
509 | </test> |
---|
510 | </report> |
---|
511 | </result>""") |
---|
512 | outheaders = {} |
---|
513 | outbody = StringIO() |
---|
514 | req = Mock(method='POST', base_path='', |
---|
515 | path_info='/builds/%d/steps/' % build.id, |
---|
516 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
517 | remote_addr='127.0.0.1', args={}, |
---|
518 | perm=PermissionCache(self.env, 'hal'), |
---|
519 | read=inbody.read, |
---|
520 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
521 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
522 | write=outbody.write, |
---|
523 | incookie=Cookie('trac_auth=123')) |
---|
524 | module = BuildMaster(self.env) |
---|
525 | |
---|
526 | module._start_new_step(build, 'foo').insert() |
---|
527 | |
---|
528 | assert module.match_request(req) |
---|
529 | |
---|
530 | self.assertRaises(RequestDone, module.process_request, req) |
---|
531 | |
---|
532 | self.assertEqual(201, outheaders['Status']) |
---|
533 | self.assertEqual('20', outheaders['Content-Length']) |
---|
534 | self.assertEqual('text/plain', outheaders['Content-Type']) |
---|
535 | self.assertEqual('Build step processed', outbody.getvalue()) |
---|
536 | |
---|
537 | build = Build.fetch(self.env, build.id) |
---|
538 | self.assertEqual(Build.SUCCESS, build.status) |
---|
539 | assert build.stopped |
---|
540 | assert build.stopped > build.started |
---|
541 | |
---|
542 | steps = list(BuildStep.select(self.env, build.id)) |
---|
543 | self.assertEqual(1, len(steps)) |
---|
544 | self.assertEqual('foo', steps[0].name) |
---|
545 | self.assertEqual(BuildStep.SUCCESS, steps[0].status) |
---|
546 | |
---|
547 | reports = list(Report.select(self.env, build=build.id, step='foo')) |
---|
548 | self.assertEqual(1, len(reports)) |
---|
549 | self.assertEqual('test', reports[0].category) |
---|
550 | self.assertEqual('http://bitten.edgewall.org/tools/python#unittest', |
---|
551 | reports[0].generator) |
---|
552 | self.assertEqual(1, len(reports[0].items)) |
---|
553 | self.assertEqual({ |
---|
554 | 'fixture': 'my.Fixture', |
---|
555 | 'file': 'my/test/file.py', |
---|
556 | 'stdout': 'Doing my thing', |
---|
557 | 'type': 'test', |
---|
558 | }, reports[0].items[0]) |
---|
559 | |
---|
560 | def test_process_build_step_wrong_slave(self): |
---|
561 | recipe = """<build> |
---|
562 | <step id="foo"> |
---|
563 | </step> |
---|
564 | </build>""" |
---|
565 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
566 | recipe=recipe).insert() |
---|
567 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
568 | started=42, status=Build.IN_PROGRESS) |
---|
569 | build.slave_info[Build.TOKEN] = '123'; |
---|
570 | build.insert() |
---|
571 | |
---|
572 | inbody = StringIO("""<result step="foo" status="success" |
---|
573 | time="2007-04-01T15:30:00.0000" |
---|
574 | duration="3.45"> |
---|
575 | <log generator="http://bitten.edgewall.org/tools/python#unittest"> |
---|
576 | <message level="info">Doing stuff</message> |
---|
577 | <message level="error">Ouch that hurt</message> |
---|
578 | </log> |
---|
579 | </result>""") |
---|
580 | outheaders = {} |
---|
581 | outbody = StringIO() |
---|
582 | req = Mock(method='POST', base_path='', |
---|
583 | path_info='/builds/%d/steps/' % build.id, |
---|
584 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
585 | remote_addr='127.0.0.1', args={}, |
---|
586 | perm=PermissionCache(self.env, 'hal'), |
---|
587 | read=inbody.read, |
---|
588 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
589 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
590 | write=outbody.write, |
---|
591 | incookie=Cookie('trac_auth=')) |
---|
592 | module = BuildMaster(self.env) |
---|
593 | |
---|
594 | module._start_new_step(build, 'foo').insert() |
---|
595 | |
---|
596 | assert module.match_request(req) |
---|
597 | |
---|
598 | self.assertRaises(RequestDone, module.process_request, req) |
---|
599 | |
---|
600 | self.assertEqual(409, outheaders['Status']) |
---|
601 | self.assertEqual('Token mismatch (wrong slave): slave=, build=123', |
---|
602 | outbody.getvalue()) |
---|
603 | |
---|
604 | build = Build.fetch(self.env, build.id) |
---|
605 | self.assertEqual(Build.IN_PROGRESS, build.status) |
---|
606 | assert not build.stopped |
---|
607 | |
---|
608 | steps = list(BuildStep.select(self.env, build.id)) |
---|
609 | self.assertEqual(1, len(steps)) |
---|
610 | |
---|
611 | def test_process_build_step_invalidated_build(self): |
---|
612 | recipe = """<build> |
---|
613 | <step id="foo"> |
---|
614 | </step> |
---|
615 | <step id="foo2"> |
---|
616 | </step> |
---|
617 | </build>""" |
---|
618 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
619 | recipe=recipe).insert() |
---|
620 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
621 | started=42, status=Build.IN_PROGRESS) |
---|
622 | build.slave_info[Build.TOKEN] = '123'; |
---|
623 | build.insert() |
---|
624 | |
---|
625 | inbody = StringIO("""<result step="foo" status="success" |
---|
626 | time="2007-04-01T15:30:00.0000" |
---|
627 | duration="3.45"> |
---|
628 | <log generator="http://bitten.edgewall.org/tools/python#unittest"> |
---|
629 | <message level="info">Doing stuff</message> |
---|
630 | <message level="error">Ouch that hurt</message> |
---|
631 | </log> |
---|
632 | </result>""") |
---|
633 | outheaders = {} |
---|
634 | outbody = StringIO() |
---|
635 | req = Mock(method='POST', base_path='', |
---|
636 | path_info='/builds/%d/steps/' % build.id, |
---|
637 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
638 | remote_addr='127.0.0.1', args={}, |
---|
639 | perm=PermissionCache(self.env, 'hal'), |
---|
640 | read=inbody.read, |
---|
641 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
642 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
643 | write=outbody.write, |
---|
644 | incookie=Cookie('trac_auth=123')) |
---|
645 | module = BuildMaster(self.env) |
---|
646 | |
---|
647 | module._start_new_step(build, 'foo').insert() |
---|
648 | |
---|
649 | assert module.match_request(req) |
---|
650 | |
---|
651 | self.assertRaises(RequestDone, module.process_request, req) |
---|
652 | |
---|
653 | build = Build.fetch(self.env, build.id) |
---|
654 | self.assertEqual(Build.IN_PROGRESS, build.status) |
---|
655 | assert not build.stopped |
---|
656 | |
---|
657 | steps = list(BuildStep.select(self.env, build.id)) |
---|
658 | self.assertEqual(2, len(steps)) |
---|
659 | |
---|
660 | # invalidate the build. |
---|
661 | |
---|
662 | build = Build.fetch(self.env, build.id) |
---|
663 | build.slave = None |
---|
664 | build.status = Build.PENDING |
---|
665 | build.slave_info = {} |
---|
666 | for step in list(BuildStep.select(self.env, build=build.id)): |
---|
667 | step.delete() |
---|
668 | build.update() |
---|
669 | |
---|
670 | # have this slave submit more data. |
---|
671 | inbody = StringIO("""<result step="foo2" status="success" |
---|
672 | time="2007-04-01T15:45:00.0000" |
---|
673 | duration="4"> |
---|
674 | <log generator="http://bitten.edgewall.org/tools/python#unittest"> |
---|
675 | <message level="info">This is a step after invalidation</message> |
---|
676 | </log> |
---|
677 | </result>""") |
---|
678 | outheaders = {} |
---|
679 | outbody = StringIO() |
---|
680 | req = Mock(method='POST', base_path='', |
---|
681 | path_info='/builds/%d/steps/' % build.id, |
---|
682 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
683 | remote_addr='127.0.0.1', args={}, |
---|
684 | perm=PermissionCache(self.env, 'hal'), |
---|
685 | read=inbody.read, |
---|
686 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
687 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
688 | write=outbody.write, |
---|
689 | incookie=Cookie('trac_auth=123')) |
---|
690 | module = BuildMaster(self.env) |
---|
691 | |
---|
692 | module._start_new_step(build, 'foo').insert() |
---|
693 | |
---|
694 | assert module.match_request(req) |
---|
695 | |
---|
696 | self.assertRaises(RequestDone, module.process_request, req) |
---|
697 | |
---|
698 | self.assertEquals(409, outheaders['Status']) |
---|
699 | self.assertEquals('Token mismatch (wrong slave): slave=123, build=', |
---|
700 | outbody.getvalue()) |
---|
701 | |
---|
702 | build = Build.fetch(self.env, build.id) |
---|
703 | self.assertEqual(Build.PENDING, build.status) |
---|
704 | |
---|
705 | def test_process_build_step_failure(self): |
---|
706 | recipe = """<build> |
---|
707 | <step id="foo"> |
---|
708 | </step> |
---|
709 | </build>""" |
---|
710 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
711 | recipe=recipe).insert() |
---|
712 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
713 | started=42, status=Build.IN_PROGRESS) |
---|
714 | build.slave_info[Build.TOKEN] = '123'; |
---|
715 | build.insert() |
---|
716 | |
---|
717 | inbody = StringIO("""<result step="foo" status="failure" |
---|
718 | time="2007-04-01T15:30:00.0000" |
---|
719 | duration="3.45"> |
---|
720 | </result>""") |
---|
721 | outheaders = {} |
---|
722 | outbody = StringIO() |
---|
723 | req = Mock(method='POST', base_path='', |
---|
724 | path_info='/builds/%d/steps/' % build.id, |
---|
725 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
726 | remote_addr='127.0.0.1', args={}, |
---|
727 | perm=PermissionCache(self.env, 'hal'), |
---|
728 | read=inbody.read, |
---|
729 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
730 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
731 | write=outbody.write, |
---|
732 | incookie=Cookie('trac_auth=123')) |
---|
733 | module = BuildMaster(self.env) |
---|
734 | |
---|
735 | module._start_new_step(build, 'foo').insert() |
---|
736 | |
---|
737 | assert module.match_request(req) |
---|
738 | |
---|
739 | self.assertRaises(RequestDone, module.process_request, req) |
---|
740 | |
---|
741 | self.assertEqual(201, outheaders['Status']) |
---|
742 | self.assertEqual('20', outheaders['Content-Length']) |
---|
743 | self.assertEqual('text/plain', outheaders['Content-Type']) |
---|
744 | self.assertEqual('Build step processed', outbody.getvalue()) |
---|
745 | |
---|
746 | build = Build.fetch(self.env, build.id) |
---|
747 | self.assertEqual(Build.FAILURE, build.status) |
---|
748 | assert build.stopped |
---|
749 | assert build.stopped > build.started |
---|
750 | |
---|
751 | steps = list(BuildStep.select(self.env, build.id)) |
---|
752 | self.assertEqual(1, len(steps)) |
---|
753 | self.assertEqual('foo', steps[0].name) |
---|
754 | self.assertEqual(BuildStep.FAILURE, steps[0].status) |
---|
755 | |
---|
756 | def test_process_build_step_failure_ignored(self): |
---|
757 | recipe = """<build> |
---|
758 | <step id="foo" onerror="ignore"> |
---|
759 | </step> |
---|
760 | </build>""" |
---|
761 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
762 | recipe=recipe).insert() |
---|
763 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
764 | started=42, status=Build.IN_PROGRESS) |
---|
765 | build.slave_info[Build.TOKEN] = '123'; |
---|
766 | |
---|
767 | build.insert() |
---|
768 | |
---|
769 | inbody = StringIO("""<result step="foo" status="failure" |
---|
770 | time="2007-04-01T15:30:00.0000" |
---|
771 | duration="3.45"> |
---|
772 | </result>""") |
---|
773 | outheaders = {} |
---|
774 | outbody = StringIO() |
---|
775 | req = Mock(method='POST', base_path='', |
---|
776 | path_info='/builds/%d/steps/' % build.id, |
---|
777 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
778 | remote_addr='127.0.0.1', args={}, |
---|
779 | perm=PermissionCache(self.env, 'hal'), |
---|
780 | read=inbody.read, |
---|
781 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
782 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
783 | write=outbody.write, |
---|
784 | incookie=Cookie('trac_auth=123')) |
---|
785 | module = BuildMaster(self.env) |
---|
786 | |
---|
787 | module._start_new_step(build, 'foo').insert() |
---|
788 | |
---|
789 | assert module.match_request(req) |
---|
790 | |
---|
791 | self.assertRaises(RequestDone, module.process_request, req) |
---|
792 | |
---|
793 | self.assertEqual(201, outheaders['Status']) |
---|
794 | self.assertEqual('20', outheaders['Content-Length']) |
---|
795 | self.assertEqual('text/plain', outheaders['Content-Type']) |
---|
796 | self.assertEqual('Build step processed', outbody.getvalue()) |
---|
797 | |
---|
798 | build = Build.fetch(self.env, build.id) |
---|
799 | self.assertEqual(Build.SUCCESS, build.status) |
---|
800 | assert build.stopped |
---|
801 | assert build.stopped > build.started |
---|
802 | |
---|
803 | steps = list(BuildStep.select(self.env, build.id)) |
---|
804 | self.assertEqual(1, len(steps)) |
---|
805 | self.assertEqual('foo', steps[0].name) |
---|
806 | self.assertEqual(BuildStep.FAILURE, steps[0].status) |
---|
807 | |
---|
808 | |
---|
809 | def test_process_build_step_failure_continue(self): |
---|
810 | recipe = """<build> |
---|
811 | <step id="foo" onerror="continue"> |
---|
812 | </step> |
---|
813 | </build>""" |
---|
814 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
815 | recipe=recipe).insert() |
---|
816 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
817 | started=42, status=Build.IN_PROGRESS) |
---|
818 | build.slave_info[Build.TOKEN] = '123'; |
---|
819 | |
---|
820 | build.insert() |
---|
821 | |
---|
822 | inbody = StringIO("""<result step="foo" status="failure" |
---|
823 | time="2007-04-01T15:30:00.0000" |
---|
824 | duration="3.45"> |
---|
825 | </result>""") |
---|
826 | outheaders = {} |
---|
827 | outbody = StringIO() |
---|
828 | req = Mock(method='POST', base_path='', |
---|
829 | path_info='/builds/%d/steps/' % build.id, |
---|
830 | href=Href('/trac'), abs_href=Href('http://example.org/trac'), |
---|
831 | remote_addr='127.0.0.1', args={}, |
---|
832 | perm=PermissionCache(self.env, 'hal'), |
---|
833 | read=inbody.read, |
---|
834 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
835 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
836 | write=outbody.write, |
---|
837 | incookie=Cookie('trac_auth=123')) |
---|
838 | module = BuildMaster(self.env) |
---|
839 | |
---|
840 | module._start_new_step(build, 'foo').insert() |
---|
841 | |
---|
842 | assert module.match_request(req) |
---|
843 | |
---|
844 | self.assertRaises(RequestDone, module.process_request, req) |
---|
845 | |
---|
846 | self.assertEqual(201, outheaders['Status']) |
---|
847 | self.assertEqual('20', outheaders['Content-Length']) |
---|
848 | self.assertEqual('text/plain', outheaders['Content-Type']) |
---|
849 | self.assertEqual('Build step processed', outbody.getvalue()) |
---|
850 | |
---|
851 | build = Build.fetch(self.env, build.id) |
---|
852 | self.assertEqual(Build.FAILURE, build.status) |
---|
853 | assert build.stopped |
---|
854 | assert build.stopped > build.started |
---|
855 | |
---|
856 | steps = list(BuildStep.select(self.env, build.id)) |
---|
857 | self.assertEqual(1, len(steps)) |
---|
858 | self.assertEqual('foo', steps[0].name) |
---|
859 | self.assertEqual(BuildStep.FAILURE, steps[0].status) |
---|
860 | |
---|
861 | def test_process_build_step_invalid_xml(self): |
---|
862 | recipe = """<build> |
---|
863 | <step id="foo"> |
---|
864 | </step> |
---|
865 | </build>""" |
---|
866 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
867 | recipe=recipe).insert() |
---|
868 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
869 | started=42) |
---|
870 | build.insert() |
---|
871 | |
---|
872 | inbody = StringIO("""<result></rsleut>""") |
---|
873 | outheaders = {} |
---|
874 | outbody = StringIO() |
---|
875 | req = Mock(method='POST', base_path='', |
---|
876 | path_info='/builds/%d/steps/' % build.id, |
---|
877 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
878 | perm=PermissionCache(self.env, 'hal'), |
---|
879 | read=inbody.read, |
---|
880 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
881 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
882 | write=outbody.write, |
---|
883 | incookie=Cookie('trac_auth=')) |
---|
884 | |
---|
885 | module = BuildMaster(self.env) |
---|
886 | |
---|
887 | module._start_new_step(build, 'foo').insert() |
---|
888 | |
---|
889 | assert module.match_request(req) |
---|
890 | |
---|
891 | self.assertRaises(RequestDone, module.process_request, req) |
---|
892 | |
---|
893 | self.assertEquals(400, outheaders['Status']) |
---|
894 | self.assertEquals('XML parser error', outbody.getvalue()) |
---|
895 | |
---|
896 | |
---|
897 | def test_process_build_step_no_post(self): |
---|
898 | BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
899 | recipe='<build></build>').insert() |
---|
900 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
901 | started=42) |
---|
902 | build.insert() |
---|
903 | |
---|
904 | outheaders = {} |
---|
905 | outbody = StringIO() |
---|
906 | req = Mock(method='GET', base_path='', |
---|
907 | path_info='/builds/%d/steps/' % build.id, |
---|
908 | href=Href('/trac'), remote_addr='127.0.0.1', args={}, |
---|
909 | perm=PermissionCache(self.env, 'hal'), |
---|
910 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
911 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
912 | write=outbody.write, |
---|
913 | incookie=Cookie('trac_auth=')) |
---|
914 | |
---|
915 | module = BuildMaster(self.env) |
---|
916 | |
---|
917 | module._start_new_step(build, 'foo').insert() |
---|
918 | |
---|
919 | assert module.match_request(req) |
---|
920 | |
---|
921 | self.assertRaises(RequestDone, module.process_request, req) |
---|
922 | |
---|
923 | self.assertEqual(405, outheaders['Status']) |
---|
924 | self.assertEqual('Method GET not allowed', outbody.getvalue()) |
---|
925 | |
---|
926 | def test_process_attach_collection_default_member(self): |
---|
927 | req = Mock(args={}, path_info='/builds/12/attach/config') |
---|
928 | module = BuildMaster(self.env) |
---|
929 | self.assertEquals(True, module.match_request(req)) |
---|
930 | self.assertTrue(req.args['collection'], 'attach') |
---|
931 | self.assertTrue(req.args['member'], '') |
---|
932 | |
---|
933 | def test_process_attach_collection_config(self): |
---|
934 | req = Mock(args={}, path_info='/builds/12/attach/config') |
---|
935 | module = BuildMaster(self.env) |
---|
936 | self.assertEquals(True, module.match_request(req)) |
---|
937 | self.assertTrue(req.args['collection'], 'attach') |
---|
938 | self.assertTrue(req.args['member'], 'config') |
---|
939 | |
---|
940 | def test_process_attach_collection_config(self): |
---|
941 | req = Mock(args={}, path_info='/builds/12/attach/build') |
---|
942 | module = BuildMaster(self.env) |
---|
943 | self.assertEquals(True, module.match_request(req)) |
---|
944 | self.assertTrue(req.args['collection'], 'attach') |
---|
945 | self.assertTrue(req.args['member'], 'build') |
---|
946 | |
---|
947 | def test_process_attach_config(self): |
---|
948 | body, content_type = encode_multipart_formdata({ |
---|
949 | 'description': 'baz baz', |
---|
950 | 'file': ('baz.txt', 'hello baz'), |
---|
951 | '__FORM_TOKEN': '123456'}) |
---|
952 | args = {} |
---|
953 | for k, v in dict(cgi.FieldStorage(fp=StringIO(body), environ={ |
---|
954 | 'REQUEST_METHOD': 'POST', |
---|
955 | 'CONTENT_TYPE': content_type}) |
---|
956 | ).items(): |
---|
957 | if v.filename: |
---|
958 | args[k] = v |
---|
959 | else: |
---|
960 | args[k] = v.value |
---|
961 | args.update({'collection': 'attach', 'member': 'config'}) |
---|
962 | self.assertTrue('file' in args) |
---|
963 | |
---|
964 | outheaders = {} |
---|
965 | outbody = StringIO() |
---|
966 | |
---|
967 | req = Mock(args=args, form_token='123456', authname='hal', |
---|
968 | remote_addr='127.0.0.1', |
---|
969 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
970 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
971 | write=outbody.write) |
---|
972 | |
---|
973 | config = BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
974 | recipe='') |
---|
975 | config.insert() |
---|
976 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
977 | started=42, status=Build.IN_PROGRESS) |
---|
978 | build.insert() |
---|
979 | |
---|
980 | module = BuildMaster(self.env) |
---|
981 | |
---|
982 | self.assertRaises(RequestDone, module._process_attachment, |
---|
983 | req, config, build) |
---|
984 | self.assertEqual(201, outheaders['Status']) |
---|
985 | self.assertEqual('18', outheaders['Content-Length']) |
---|
986 | self.assertEqual('text/plain', outheaders['Content-Type']) |
---|
987 | self.assertEqual('Attachment created', outbody.getvalue()) |
---|
988 | |
---|
989 | config_atts = list(Attachment.select(self.env, 'build', 'test')) |
---|
990 | self.assertEquals(1, len(config_atts)) |
---|
991 | self.assertEquals('hal', config_atts[0].author) |
---|
992 | self.assertEquals('baz baz', config_atts[0].description) |
---|
993 | self.assertEquals('baz.txt', config_atts[0].filename) |
---|
994 | self.assertEquals('hello baz', |
---|
995 | config_atts[0].open().read()) |
---|
996 | |
---|
997 | |
---|
998 | def test_process_attach_build(self): |
---|
999 | body, content_type = encode_multipart_formdata({ |
---|
1000 | 'description': 'baz baz', |
---|
1001 | 'file': ('baz.txt', 'hello baz'), |
---|
1002 | '__FORM_TOKEN': '123456'}) |
---|
1003 | args = {} |
---|
1004 | for k, v in dict(cgi.FieldStorage(fp=StringIO(body), environ={ |
---|
1005 | 'REQUEST_METHOD': 'POST', |
---|
1006 | 'CONTENT_TYPE': content_type}) |
---|
1007 | ).items(): |
---|
1008 | if v.filename: |
---|
1009 | args[k] = v |
---|
1010 | else: |
---|
1011 | args[k] = v.value |
---|
1012 | args.update({'collection': 'attach', 'member': 'build'}) |
---|
1013 | self.assertTrue('file' in args) |
---|
1014 | |
---|
1015 | outheaders = {} |
---|
1016 | outbody = StringIO() |
---|
1017 | |
---|
1018 | req = Mock(args=args, form_token='123456', authname='hal', |
---|
1019 | remote_addr='127.0.0.1', |
---|
1020 | send_response=lambda x: outheaders.setdefault('Status', x), |
---|
1021 | send_header=lambda x, y: outheaders.setdefault(x, y), |
---|
1022 | write=outbody.write) |
---|
1023 | |
---|
1024 | config = BuildConfig(self.env, 'test', path='somepath', active=True, |
---|
1025 | recipe='') |
---|
1026 | config.insert() |
---|
1027 | build = Build(self.env, 'test', '123', 1, slave='hal', rev_time=42, |
---|
1028 | started=42, status=Build.IN_PROGRESS) |
---|
1029 | build.insert() |
---|
1030 | |
---|
1031 | module = BuildMaster(self.env) |
---|
1032 | |
---|
1033 | self.assertRaises(RequestDone, module._process_attachment, |
---|
1034 | req, config, build) |
---|
1035 | self.assertEqual(201, outheaders['Status']) |
---|
1036 | self.assertEqual('18', outheaders['Content-Length']) |
---|
1037 | self.assertEqual('text/plain', outheaders['Content-Type']) |
---|
1038 | self.assertEqual('Attachment created', outbody.getvalue()) |
---|
1039 | |
---|
1040 | build_atts = list(Attachment.select(self.env, 'build', 'test/1')) |
---|
1041 | self.assertEquals(1, len(build_atts)) |
---|
1042 | self.assertEquals('hal', build_atts[0].author) |
---|
1043 | self.assertEquals('baz baz', build_atts[0].description) |
---|
1044 | self.assertEquals('baz.txt', build_atts[0].filename) |
---|
1045 | self.assertEquals('hello baz', |
---|
1046 | build_atts[0].open().read()) |
---|
1047 | |
---|
1048 | |
---|
1049 | |
---|
1050 | def suite(): |
---|
1051 | suite = unittest.TestSuite() |
---|
1052 | suite.addTest(unittest.makeSuite(BuildMasterTestCase, 'test')) |
---|
1053 | return suite |
---|
1054 | |
---|
1055 | if __name__ == '__main__': |
---|
1056 | unittest.main(defaultTest='suite') |
---|