progress_stream_test.py 3.7 KB

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