Debian Codegraph @

python/tests/test_test_protocol2.py

raw
1 #
2 # subunit: extensions to Python unittest to get test results from subprocesses.
3 # Copyright (C) 2013 Robert Collins <[email protected]>
4 #
5 # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
6 # license at the users choice. A copy of both licenses are available in the
7 # project source as Apache-2.0 and BSD. You may not use this file except in
8 # compliance with one of these two licences.
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # license you chose for the specific language governing permissions and
14 # limitations under that license.
15 #
16
17 import datetime
18 from io import BytesIO
19 from typing import Callable, Optional
20 from types import ModuleType
21
22 try:
23 from hypothesis import given
24
25 # To debug hypothesis
26 # from hypothesis import Settings, Verbosity
27 # Settings.default.verbosity = Verbosity.verbose
28 import hypothesis.strategies as st
29 except ImportError:
30 given: Optional[Callable[..., Callable[..., None]]] = None # type: ignore[assignment,no-redef]
31 st: Optional[ModuleType] = None # type: ignore[assignment,no-redef]
32 from testtools import TestCase
33 from testtools.matchers import Contains, HasLength
34 from testtools.testresult.doubles import StreamResult
35
36 try:
37 from testtools.tests.test_testresult import TestStreamResultContract # type: ignore[import-not-found]
38 except ImportError:
39 # testtools >= 2.8 no longer includes the tests submodule
40 TestStreamResultContract: Optional[type] = None # type: ignore[assignment,misc,no-redef]
41
42 import subunit
43 import iso8601
44
45 CONSTANT_ENUM = b"\xb3)\x01\x0c\x03foo\x08U_\x1b"
46 CONSTANT_INPROGRESS = b"\xb3)\x02\x0c\x03foo\x8e\xc1-\xb5"
47 CONSTANT_SUCCESS = b"\xb3)\x03\x0c\x03fooE\x9d\xfe\x10"
48 CONSTANT_UXSUCCESS = b"\xb3)\x04\x0c\x03fooX\x98\xce\xa8"
49 CONSTANT_SKIP = b"\xb3)\x05\x0c\x03foo\x93\xc4\x1d\r"
50 CONSTANT_FAIL = b"\xb3)\x06\x0c\x03foo\x15Po\xa3"
51 CONSTANT_XFAIL = b"\xb3)\x07\x0c\x03foo\xde\x0c\xbc\x06"
52 CONSTANT_EOF = b"\xb3!\x10\x08S\x15\x88\xdc"
53 CONSTANT_FILE_CONTENT = b"\xb3!@\x13\x06barney\x03wooA5\xe3\x8c"
54 CONSTANT_MIME = b"\xb3! #\x1aapplication/foo; charset=1x3Q\x15"
55 CONSTANT_TIMESTAMP = b"\xb3+\x03\x13<\x17T\xcf\x80\xaf\xc8\x03barI\x96>-"
56 CONSTANT_ROUTE_CODE = b"\xb3-\x03\x13\x03bar\x06source\x9cY9\x19"
57 CONSTANT_RUNNABLE = b"\xb3(\x03\x0c\x03foo\xe3\xea\xf5\xa4"
58 CONSTANT_TAGS = [
59 b"\xb3)\x80\x15\x03bar\x02\x03foo\x03barTHn\xb4",
60 b"\xb3)\x80\x15\x03bar\x02\x03bar\x03foo\xf8\xf1\x91o",
61 ]
62
63
64 if TestStreamResultContract is not None:
65
66 class TestStreamResultToBytesContract(TestCase, TestStreamResultContract):
67 """Check that StreamResult behaves as testtools expects."""
68
69 def _make_result(self):
70 return subunit.StreamResultToBytes(BytesIO())
71
72
73 class TestStreamResultToBytes(TestCase):
74 def _make_result(self):
75 output = BytesIO()
76 return subunit.StreamResultToBytes(output), output
77
78 def test_numbers(self):
79 result = subunit.StreamResultToBytes(BytesIO())
80 packet = []
81 self.assertRaises(Exception, result._write_number, -1, packet)
82 self.assertEqual([], packet)
83 result._write_number(0, packet)
84 self.assertEqual([b"\x00"], packet)
85 del packet[:]
86 result._write_number(63, packet)
87 self.assertEqual([b"\x3f"], packet)
88 del packet[:]
89 result._write_number(64, packet)
90 self.assertEqual([b"\x40\x40"], packet)
91 del packet[:]
92 result._write_number(16383, packet)
93 self.assertEqual([b"\x7f\xff"], packet)
94 del packet[:]
95 result._write_number(16384, packet)
96 self.assertEqual([b"\x80\x40", b"\x00"], packet)
97 del packet[:]
98 result._write_number(4194303, packet)
99 self.assertEqual([b"\xbf\xff", b"\xff"], packet)
100 del packet[:]
101 result._write_number(4194304, packet)
102 self.assertEqual([b"\xc0\x40\x00\x00"], packet)
103 del packet[:]
104 result._write_number(1073741823, packet)
105 self.assertEqual([b"\xff\xff\xff\xff"], packet)
106 del packet[:]
107 self.assertRaises(Exception, result._write_number, 1073741824, packet)
108 self.assertEqual([], packet)
109
110 def test_volatile_length(self):
111 # if the length of the packet data before the length itself is
112 # considered is right on the boundary for length's variable length
113 # encoding, it is easy to get the length wrong by not accounting for
114 # length itself.
115 # that is, the encoder has to ensure that length == sum (length_of_rest
116 # + length_of_length)
117 result, output = self._make_result()
118 # 1 byte short:
119 result.status(file_name="", file_bytes=b"\xff" * 0)
120 self.assertThat(output.getvalue(), HasLength(10))
121 self.assertEqual(b"\x0a", output.getvalue()[3:4])
122 output.seek(0)
123 output.truncate()
124 # 1 byte long:
125 result.status(file_name="", file_bytes=b"\xff" * 53)
126 self.assertThat(output.getvalue(), HasLength(63))
127 self.assertEqual(b"\x3f", output.getvalue()[3:4])
128 output.seek(0)
129 output.truncate()
130 # 2 bytes short
131 result.status(file_name="", file_bytes=b"\xff" * 54)
132 self.assertThat(output.getvalue(), HasLength(65))
133 self.assertEqual(b"\x40\x41", output.getvalue()[3:5])
134 output.seek(0)
135 output.truncate()
136 # 2 bytes long
137 result.status(file_name="", file_bytes=b"\xff" * 16371)
138 self.assertThat(output.getvalue(), HasLength(16383))
139 self.assertEqual(b"\x7f\xff", output.getvalue()[3:5])
140 output.seek(0)
141 output.truncate()
142 # 3 bytes short
143 result.status(file_name="", file_bytes=b"\xff" * 16372)
144 self.assertThat(output.getvalue(), HasLength(16385))
145 self.assertEqual(b"\x80\x40\x01", output.getvalue()[3:6])
146 output.seek(0)
147 output.truncate()
148 # 3 bytes long
149 result.status(file_name="", file_bytes=b"\xff" * 4194289)
150 self.assertThat(output.getvalue(), HasLength(4194303))
151 self.assertEqual(b"\xbf\xff\xff", output.getvalue()[3:6])
152 output.seek(0)
153 output.truncate()
154 self.assertRaises(Exception, result.status, file_name="", file_bytes=b"\xff" * 4194290)
155
156 def test_trivial_enumeration(self):
157 result, output = self._make_result()
158 result.status("foo", "exists")
159 self.assertEqual(CONSTANT_ENUM, output.getvalue())
160
161 def test_inprogress(self):
162 result, output = self._make_result()
163 result.status("foo", "inprogress")
164 self.assertEqual(CONSTANT_INPROGRESS, output.getvalue())
165
166 def test_success(self):
167 result, output = self._make_result()
168 result.status("foo", "success")
169 self.assertEqual(CONSTANT_SUCCESS, output.getvalue())
170
171 def test_uxsuccess(self):
172 result, output = self._make_result()
173 result.status("foo", "uxsuccess")
174 self.assertEqual(CONSTANT_UXSUCCESS, output.getvalue())
175
176 def test_skip(self):
177 result, output = self._make_result()
178 result.status("foo", "skip")
179 self.assertEqual(CONSTANT_SKIP, output.getvalue())
180
181 def test_fail(self):
182 result, output = self._make_result()
183 result.status("foo", "fail")
184 self.assertEqual(CONSTANT_FAIL, output.getvalue())
185
186 def test_xfail(self):
187 result, output = self._make_result()
188 result.status("foo", "xfail")
189 self.assertEqual(CONSTANT_XFAIL, output.getvalue())
190
191 def test_unknown_status(self):
192 result, output = self._make_result()
193 self.assertRaises(Exception, result.status, "foo", "boo")
194 self.assertEqual(b"", output.getvalue())
195
196 def test_eof(self):
197 result, output = self._make_result()
198 result.status(eof=True)
199 self.assertEqual(CONSTANT_EOF, output.getvalue())
200
201 def test_file_content(self):
202 result, output = self._make_result()
203 result.status(file_name="barney", file_bytes=b"woo")
204 self.assertEqual(CONSTANT_FILE_CONTENT, output.getvalue())
205
206 def test_mime(self):
207 result, output = self._make_result()
208 result.status(mime_type="application/foo; charset=1")
209 self.assertEqual(CONSTANT_MIME, output.getvalue())
210
211 def test_route_code(self):
212 result, output = self._make_result()
213 result.status(test_id="bar", test_status="success", route_code="source")
214 self.assertEqual(CONSTANT_ROUTE_CODE, output.getvalue())
215
216 def test_runnable(self):
217 result, output = self._make_result()
218 result.status("foo", "success", runnable=False)
219 self.assertEqual(CONSTANT_RUNNABLE, output.getvalue())
220
221 def test_tags(self):
222 result, output = self._make_result()
223 result.status(test_id="bar", test_tags={"foo", "bar"})
224 self.assertThat(CONSTANT_TAGS, Contains(output.getvalue()))
225
226 def test_timestamp(self):
227 timestamp = datetime.datetime(2001, 12, 12, 12, 59, 59, 45, iso8601.UTC)
228 result, output = self._make_result()
229 result.status(test_id="bar", test_status="success", timestamp=timestamp)
230 self.assertEqual(CONSTANT_TIMESTAMP, output.getvalue())
231
232
233 class TestByteStreamToStreamResult(TestCase):
234 def test_non_subunit_encapsulated(self):
235 source = BytesIO(b"foo\nbar\n")
236 result = StreamResult()
237 subunit.ByteStreamToStreamResult(source, non_subunit_name="stdout").run(result)
238 self.assertEqual(
239 [
240 ("status", None, None, None, True, "stdout", b"f", False, None, None, None),
241 ("status", None, None, None, True, "stdout", b"o", False, None, None, None),
242 ("status", None, None, None, True, "stdout", b"o", False, None, None, None),
243 ("status", None, None, None, True, "stdout", b"\n", False, None, None, None),
244 ("status", None, None, None, True, "stdout", b"b", False, None, None, None),
245 ("status", None, None, None, True, "stdout", b"a", False, None, None, None),
246 ("status", None, None, None, True, "stdout", b"r", False, None, None, None),
247 ("status", None, None, None, True, "stdout", b"\n", False, None, None, None),
248 ],
249 result._events,
250 )
251 self.assertEqual(b"", source.read())
252
253 def test_signature_middle_utf8_char(self):
254 utf8_bytes = b"\xe3\xb3\x8a"
255 source = BytesIO(utf8_bytes)
256 # Should be treated as one character (it is u'\u3cca') and wrapped
257 result = StreamResult()
258 subunit.ByteStreamToStreamResult(source, non_subunit_name="stdout").run(result)
259 self.assertEqual(
260 [
261 ("status", None, None, None, True, "stdout", b"\xe3", False, None, None, None),
262 ("status", None, None, None, True, "stdout", b"\xb3", False, None, None, None),
263 ("status", None, None, None, True, "stdout", b"\x8a", False, None, None, None),
264 ],
265 result._events,
266 )
267
268 def test_non_subunit_disabled_raises(self):
269 source = BytesIO(b"foo\nbar\n")
270 result = StreamResult()
271 case = subunit.ByteStreamToStreamResult(source)
272 e = self.assertRaises(Exception, case.run, result)
273 self.assertEqual(b"f", e.args[1])
274 self.assertEqual(b"oo\nbar\n", source.read())
275 self.assertEqual([], result._events)
276
277 def test_trivial_enumeration(self):
278 source = BytesIO(CONSTANT_ENUM)
279 result = StreamResult()
280 subunit.ByteStreamToStreamResult(source, non_subunit_name="stdout").run(result)
281 self.assertEqual(b"", source.read())
282 self.assertEqual(
283 [
284 ("status", "foo", "exists", None, True, None, None, False, None, None, None),
285 ],
286 result._events,
287 )
288
289 def test_multiple_events(self):
290 source = BytesIO(CONSTANT_ENUM + CONSTANT_ENUM)
291 result = StreamResult()
292 subunit.ByteStreamToStreamResult(source, non_subunit_name="stdout").run(result)
293 self.assertEqual(b"", source.read())
294 self.assertEqual(
295 [
296 ("status", "foo", "exists", None, True, None, None, False, None, None, None),
297 ("status", "foo", "exists", None, True, None, None, False, None, None, None),
298 ],
299 result._events,
300 )
301
302 def test_inprogress(self):
303 self.check_event(CONSTANT_INPROGRESS, "inprogress")
304
305 def test_success(self):
306 self.check_event(CONSTANT_SUCCESS, "success")
307
308 def test_uxsuccess(self):
309 self.check_event(CONSTANT_UXSUCCESS, "uxsuccess")
310
311 def test_skip(self):
312 self.check_event(CONSTANT_SKIP, "skip")
313
314 def test_fail(self):
315 self.check_event(CONSTANT_FAIL, "fail")
316
317 def test_xfail(self):
318 self.check_event(CONSTANT_XFAIL, "xfail")
319
320 def check_events(self, source_bytes, events):
321 source = BytesIO(source_bytes)
322 result = StreamResult()
323 subunit.ByteStreamToStreamResult(source, non_subunit_name="stdout").run(result)
324 self.assertEqual(b"", source.read())
325 self.assertEqual(events, result._events)
326 # - any file attachments should be bytes equivalent [as users assume that].
327 for event in result._events:
328 if event[5] is not None:
329 bytes(event[6])
330
331 def check_event(
332 self,
333 source_bytes,
334 test_status=None,
335 test_id="foo",
336 route_code=None,
337 timestamp=None,
338 tags=None,
339 mime_type=None,
340 file_name=None,
341 file_bytes=None,
342 eof=False,
343 runnable=True,
344 ):
345 event = self._event(
346 test_id=test_id,
347 test_status=test_status,
348 tags=tags,
349 runnable=runnable,
350 file_name=file_name,
351 file_bytes=file_bytes,
352 eof=eof,
353 mime_type=mime_type,
354 route_code=route_code,
355 timestamp=timestamp,
356 )
357 self.check_events(source_bytes, [event])
358
359 def _event(
360 self,
361 test_status=None,
362 test_id=None,
363 route_code=None,
364 timestamp=None,
365 tags=None,
366 mime_type=None,
367 file_name=None,
368 file_bytes=None,
369 eof=False,
370 runnable=True,
371 ):
372 return (
373 "status",
374 test_id,
375 test_status,
376 tags,
377 runnable,
378 file_name,
379 file_bytes,
380 eof,
381 mime_type,
382 route_code,
383 timestamp,
384 )
385
386 def test_eof(self):
387 self.check_event(CONSTANT_EOF, test_id=None, eof=True)
388
389 def test_file_content(self):
390 self.check_event(CONSTANT_FILE_CONTENT, test_id=None, file_name="barney", file_bytes=b"woo")
391
392 def test_file_content_length_into_checksum(self):
393 # A bad file content length which creeps into the checksum.
394 bad_file_length_content = b"\xb3!@\x13\x06barney\x04woo\xdc\xe2\xdb\x35"
395 self.check_events(
396 bad_file_length_content,
397 [
398 self._event(
399 test_id="subunit.parser",
400 eof=True,
401 file_name="Packet data",
402 file_bytes=bad_file_length_content,
403 mime_type="application/octet-stream",
404 ),
405 self._event(
406 test_id="subunit.parser",
407 test_status="fail",
408 eof=True,
409 file_name="Parser Error",
410 file_bytes=b"File content extends past end of packet: claimed 4 bytes, 3 available",
411 mime_type="text/plain;charset=utf8",
412 ),
413 ],
414 )
415
416 def test_packet_length_4_word_varint(self):
417 packet_data = b"\xb3!@\xc0\x00\x11"
418 self.check_events(
419 packet_data,
420 [
421 self._event(
422 test_id="subunit.parser",
423 eof=True,
424 file_name="Packet data",
425 file_bytes=packet_data,
426 mime_type="application/octet-stream",
427 ),
428 self._event(
429 test_id="subunit.parser",
430 test_status="fail",
431 eof=True,
432 file_name="Parser Error",
433 file_bytes=b"3 byte maximum given but 4 byte value found.",
434 mime_type="text/plain;charset=utf8",
435 ),
436 ],
437 )
438
439 def test_mime(self):
440 self.check_event(CONSTANT_MIME, test_id=None, mime_type="application/foo; charset=1")
441
442 def test_route_code(self):
443 self.check_event(CONSTANT_ROUTE_CODE, "success", route_code="source", test_id="bar")
444
445 def test_runnable(self):
446 self.check_event(CONSTANT_RUNNABLE, test_status="success", runnable=False)
447
448 def test_tags(self):
449 self.check_event(CONSTANT_TAGS[0], None, tags={"foo", "bar"}, test_id="bar")
450
451 def test_timestamp(self):
452 timestamp = datetime.datetime(2001, 12, 12, 12, 59, 59, 45, iso8601.UTC)
453 self.check_event(CONSTANT_TIMESTAMP, "success", test_id="bar", timestamp=timestamp)
454
455 def test_bad_crc_errors_via_status(self):
456 file_bytes = CONSTANT_MIME[:-1] + b"\x00"
457 self.check_events(
458 file_bytes,
459 [
460 self._event(
461 test_id="subunit.parser",
462 eof=True,
463 file_name="Packet data",
464 file_bytes=file_bytes,
465 mime_type="application/octet-stream",
466 ),
467 self._event(
468 test_id="subunit.parser",
469 test_status="fail",
470 eof=True,
471 file_name="Parser Error",
472 file_bytes=b"Bad checksum - calculated (0x78335115), stored (0x78335100)",
473 mime_type="text/plain;charset=utf8",
474 ),
475 ],
476 )
477
478 def test_not_utf8_in_string(self):
479 file_bytes = CONSTANT_ROUTE_CODE[:5] + b"\xb4" + CONSTANT_ROUTE_CODE[6:-4] + b"\xce\x56\xc6\x17"
480 self.check_events(
481 file_bytes,
482 [
483 self._event(
484 test_id="subunit.parser",
485 eof=True,
486 file_name="Packet data",
487 file_bytes=file_bytes,
488 mime_type="application/octet-stream",
489 ),
490 self._event(
491 test_id="subunit.parser",
492 test_status="fail",
493 eof=True,
494 file_name="Parser Error",
495 file_bytes=b"UTF8 string at offset 2 is not UTF8",
496 mime_type="text/plain;charset=utf8",
497 ),
498 ],
499 )
500
501 def test_NULL_in_string(self):
502 file_bytes = CONSTANT_ROUTE_CODE[:6] + b"\x00" + CONSTANT_ROUTE_CODE[7:-4] + b"\xd7\x41\xac\xfe"
503 self.check_events(
504 file_bytes,
505 [
506 self._event(
507 test_id="subunit.parser",
508 eof=True,
509 file_name="Packet data",
510 file_bytes=file_bytes,
511 mime_type="application/octet-stream",
512 ),
513 self._event(
514 test_id="subunit.parser",
515 test_status="fail",
516 eof=True,
517 file_name="Parser Error",
518 file_bytes=b"UTF8 string at offset 2 contains NUL byte",
519 mime_type="text/plain;charset=utf8",
520 ),
521 ],
522 )
523
524 def test_bad_utf8_stringlength(self):
525 file_bytes = CONSTANT_ROUTE_CODE[:4] + b"\x3f" + CONSTANT_ROUTE_CODE[5:-4] + b"\xbe\x29\xe0\xc2"
526 self.check_events(
527 file_bytes,
528 [
529 self._event(
530 test_id="subunit.parser",
531 eof=True,
532 file_name="Packet data",
533 file_bytes=file_bytes,
534 mime_type="application/octet-stream",
535 ),
536 self._event(
537 test_id="subunit.parser",
538 test_status="fail",
539 eof=True,
540 file_name="Parser Error",
541 file_bytes=b"UTF8 string at offset 2 extends past end of packet: claimed 63 bytes, 10 available",
542 mime_type="text/plain;charset=utf8",
543 ),
544 ],
545 )
546
547 def test_route_code_and_file_content(self):
548 content = BytesIO()
549 subunit.StreamResultToBytes(content).status(
550 route_code="0", mime_type="text/plain", file_name="bar", file_bytes=b"foo"
551 )
552 self.check_event(
553 content.getvalue(), test_id=None, file_name="bar", route_code="0", mime_type="text/plain", file_bytes=b"foo"
554 )
555
556 if st is not None:
557
558 @given(st.binary())
559 def test_hypothesis_decoding(self, code_bytes):
560 source = BytesIO(code_bytes)
561 result = StreamResult()
562 stream = subunit.ByteStreamToStreamResult(source, non_subunit_name="stdout")
563 stream.run(result)
564 self.assertEqual(b"", source.read())