progress_stream_test.py 3.7 KB

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