progress_stream_test.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. assert 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. assert 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. assert 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. assert 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. assert 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. assert len(output.getvalue()) > 0
  55. def test_get_digest_from_push():
  56. digest = "sha256:abcd"
  57. events = [
  58. {"status": "..."},
  59. {"status": "..."},
  60. {"progressDetail": {}, "aux": {"Digest": digest}},
  61. ]
  62. assert progress_stream.get_digest_from_push(events) == digest
  63. def test_get_digest_from_pull():
  64. digest = "sha256:abcd"
  65. events = [
  66. {"status": "..."},
  67. {"status": "..."},
  68. {"status": "Digest: %s" % digest},
  69. ]
  70. assert progress_stream.get_digest_from_pull(events) == digest