1
0

progress_stream_test.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from __future__ import absolute_import
  2. from __future__ import unicode_literals
  3. from six import StringIO
  4. from compose import progress_stream
  5. from tests import unittest
  6. class ProgressStreamTestCase(unittest.TestCase):
  7. def test_stream_output(self):
  8. output = [
  9. b'{"status": "Downloading", "progressDetail": {"current": '
  10. b'31019763, "start": 1413653874, "total": 62763875}, '
  11. b'"progress": "..."}',
  12. ]
  13. events = progress_stream.stream_output(output, StringIO())
  14. self.assertEqual(len(events), 1)
  15. def test_stream_output_div_zero(self):
  16. output = [
  17. b'{"status": "Downloading", "progressDetail": {"current": '
  18. b'0, "start": 1413653874, "total": 0}, '
  19. b'"progress": "..."}',
  20. ]
  21. events = progress_stream.stream_output(output, StringIO())
  22. self.assertEqual(len(events), 1)
  23. def test_stream_output_null_total(self):
  24. output = [
  25. b'{"status": "Downloading", "progressDetail": {"current": '
  26. b'0, "start": 1413653874, "total": null}, '
  27. b'"progress": "..."}',
  28. ]
  29. events = progress_stream.stream_output(output, StringIO())
  30. self.assertEqual(len(events), 1)
  31. def test_stream_output_progress_event_tty(self):
  32. events = [
  33. b'{"status": "Already exists", "progressDetail": {}, "id": "8d05e3af52b0"}'
  34. ]
  35. class TTYStringIO(StringIO):
  36. def isatty(self):
  37. return True
  38. output = TTYStringIO()
  39. events = progress_stream.stream_output(events, output)
  40. self.assertTrue(len(output.getvalue()) > 0)
  41. def test_stream_output_progress_event_no_tty(self):
  42. events = [
  43. b'{"status": "Already exists", "progressDetail": {}, "id": "8d05e3af52b0"}'
  44. ]
  45. output = StringIO()
  46. events = progress_stream.stream_output(events, output)
  47. self.assertEqual(len(output.getvalue()), 0)
  48. def test_stream_output_no_progress_event_no_tty(self):
  49. events = [
  50. b'{"status": "Pulling from library/xy", "id": "latest"}'
  51. ]
  52. output = StringIO()
  53. events = progress_stream.stream_output(events, output)
  54. self.assertTrue(len(output.getvalue()) > 0)