progress_stream_test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # ~*~ encoding: utf-8 ~*~
  2. from __future__ import absolute_import
  3. from __future__ import unicode_literals
  4. import io
  5. import os
  6. import random
  7. import shutil
  8. import tempfile
  9. from six import StringIO
  10. from compose import progress_stream
  11. from tests import unittest
  12. class ProgressStreamTestCase(unittest.TestCase):
  13. def test_stream_output(self):
  14. output = [
  15. b'{"status": "Downloading", "progressDetail": {"current": '
  16. b'31019763, "start": 1413653874, "total": 62763875}, '
  17. b'"progress": "..."}',
  18. ]
  19. events = list(progress_stream.stream_output(output, StringIO()))
  20. assert len(events) == 1
  21. def test_stream_output_div_zero(self):
  22. output = [
  23. b'{"status": "Downloading", "progressDetail": {"current": '
  24. b'0, "start": 1413653874, "total": 0}, '
  25. b'"progress": "..."}',
  26. ]
  27. events = list(progress_stream.stream_output(output, StringIO()))
  28. assert len(events) == 1
  29. def test_stream_output_null_total(self):
  30. output = [
  31. b'{"status": "Downloading", "progressDetail": {"current": '
  32. b'0, "start": 1413653874, "total": null}, '
  33. b'"progress": "..."}',
  34. ]
  35. events = list(progress_stream.stream_output(output, StringIO()))
  36. assert len(events) == 1
  37. def test_stream_output_progress_event_tty(self):
  38. events = [
  39. b'{"status": "Already exists", "progressDetail": {}, "id": "8d05e3af52b0"}'
  40. ]
  41. class TTYStringIO(StringIO):
  42. def isatty(self):
  43. return True
  44. output = TTYStringIO()
  45. events = list(progress_stream.stream_output(events, output))
  46. assert len(output.getvalue()) > 0
  47. def test_stream_output_progress_event_no_tty(self):
  48. events = [
  49. b'{"status": "Already exists", "progressDetail": {}, "id": "8d05e3af52b0"}'
  50. ]
  51. output = StringIO()
  52. events = list(progress_stream.stream_output(events, output))
  53. assert len(output.getvalue()) == 0
  54. def test_stream_output_no_progress_event_no_tty(self):
  55. events = [
  56. b'{"status": "Pulling from library/xy", "id": "latest"}'
  57. ]
  58. output = StringIO()
  59. events = list(progress_stream.stream_output(events, output))
  60. assert len(output.getvalue()) > 0
  61. def test_mismatched_encoding_stream_write(self):
  62. tmpdir = tempfile.mkdtemp()
  63. self.addCleanup(shutil.rmtree, tmpdir, True)
  64. def mktempfile(encoding):
  65. fname = os.path.join(tmpdir, hex(random.getrandbits(128))[2:-1])
  66. return io.open(fname, mode='w+', encoding=encoding)
  67. text = '就吃饭'
  68. with mktempfile(encoding='utf-8') as tf:
  69. progress_stream.write_to_stream(text, tf)
  70. tf.seek(0)
  71. assert tf.read() == text
  72. with mktempfile(encoding='utf-32') as tf:
  73. progress_stream.write_to_stream(text, tf)
  74. tf.seek(0)
  75. assert tf.read() == text
  76. with mktempfile(encoding='ascii') as tf:
  77. progress_stream.write_to_stream(text, tf)
  78. tf.seek(0)
  79. assert tf.read() == '???'
  80. def test_get_digest_from_push(self):
  81. digest = "sha256:abcd"
  82. events = [
  83. {"status": "..."},
  84. {"status": "..."},
  85. {"progressDetail": {}, "aux": {"Digest": digest}},
  86. ]
  87. assert progress_stream.get_digest_from_push(events) == digest
  88. def test_get_digest_from_pull(self):
  89. events = list()
  90. assert progress_stream.get_digest_from_pull(events) is None
  91. digest = "sha256:abcd"
  92. events = [
  93. {"status": "..."},
  94. {"status": "..."},
  95. {"status": "Digest: %s" % digest},
  96. {"status": "..."},
  97. ]
  98. assert progress_stream.get_digest_from_pull(events) == digest