PageRenderTime 77ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/telemetry/third_party/gsutilz/third_party/apitools/samples/storage_sample/downloads_test.py

https://gitlab.com/jonnialva90/iridium-browser
Python | 179 lines | 139 code | 29 blank | 11 comment | 5 complexity | 0b8c51286377479abd599ec3cb50e011 MD5 | raw file
  1. """Integration tests for uploading and downloading to GCS.
  2. These tests exercise most of the corner cases for upload/download of
  3. files in apitools, via GCS. There are no performance tests here yet.
  4. """
  5. import json
  6. import os
  7. import unittest
  8. import six
  9. import apitools.base.py as apitools_base
  10. import storage
  11. _CLIENT = None
  12. def _GetClient():
  13. global _CLIENT # pylint: disable=global-statement
  14. if _CLIENT is None:
  15. _CLIENT = storage.StorageV1()
  16. return _CLIENT
  17. class DownloadsTest(unittest.TestCase):
  18. _DEFAULT_BUCKET = 'apitools'
  19. _TESTDATA_PREFIX = 'testdata'
  20. def setUp(self):
  21. self.__client = _GetClient()
  22. self.__ResetDownload()
  23. def __ResetDownload(self, auto_transfer=False):
  24. self.__buffer = six.StringIO()
  25. self.__download = storage.Download.FromStream(
  26. self.__buffer, auto_transfer=auto_transfer)
  27. def __GetTestdataFileContents(self, filename):
  28. file_contents = open('testdata/%s' % filename).read()
  29. self.assertIsNotNone(
  30. file_contents, msg=('Could not read file %s' % filename))
  31. return file_contents
  32. @classmethod
  33. def __GetRequest(cls, filename):
  34. object_name = os.path.join(cls._TESTDATA_PREFIX, filename)
  35. return storage.StorageObjectsGetRequest(
  36. bucket=cls._DEFAULT_BUCKET, object=object_name)
  37. def __GetFile(self, request):
  38. response = self.__client.objects.Get(request, download=self.__download)
  39. self.assertIsNone(response, msg=(
  40. 'Unexpected nonempty response for file download: %s' % response))
  41. def __GetAndStream(self, request):
  42. self.__GetFile(request)
  43. self.__download.StreamInChunks()
  44. def testZeroBytes(self):
  45. request = self.__GetRequest('zero_byte_file')
  46. self.__GetAndStream(request)
  47. self.assertEqual(0, self.__buffer.tell())
  48. def testObjectDoesNotExist(self):
  49. self.__ResetDownload(auto_transfer=True)
  50. with self.assertRaises(apitools_base.HttpError):
  51. self.__GetFile(self.__GetRequest('nonexistent_file'))
  52. def testAutoTransfer(self):
  53. self.__ResetDownload(auto_transfer=True)
  54. self.__GetFile(self.__GetRequest('fifteen_byte_file'))
  55. file_contents = self.__GetTestdataFileContents('fifteen_byte_file')
  56. self.assertEqual(15, self.__buffer.tell())
  57. self.__buffer.seek(0)
  58. self.assertEqual(file_contents, self.__buffer.read())
  59. def testFilenameWithSpaces(self):
  60. self.__ResetDownload(auto_transfer=True)
  61. self.__GetFile(self.__GetRequest('filename with spaces'))
  62. # NOTE(craigcitro): We add _ here to make this play nice with blaze.
  63. file_contents = self.__GetTestdataFileContents('filename_with_spaces')
  64. self.assertEqual(15, self.__buffer.tell())
  65. self.__buffer.seek(0)
  66. self.assertEqual(file_contents, self.__buffer.read())
  67. def testGetRange(self):
  68. # TODO(craigcitro): Test about a thousand more corner cases.
  69. file_contents = self.__GetTestdataFileContents('fifteen_byte_file')
  70. self.__GetFile(self.__GetRequest('fifteen_byte_file'))
  71. self.__download.GetRange(5, 10)
  72. self.assertEqual(6, self.__buffer.tell())
  73. self.__buffer.seek(0)
  74. self.assertEqual(file_contents[5:11], self.__buffer.read())
  75. def testGetRangeWithNegativeStart(self):
  76. file_contents = self.__GetTestdataFileContents('fifteen_byte_file')
  77. self.__GetFile(self.__GetRequest('fifteen_byte_file'))
  78. self.__download.GetRange(-3)
  79. self.assertEqual(3, self.__buffer.tell())
  80. self.__buffer.seek(0)
  81. self.assertEqual(file_contents[-3:], self.__buffer.read())
  82. def testGetRangeWithPositiveStart(self):
  83. file_contents = self.__GetTestdataFileContents('fifteen_byte_file')
  84. self.__GetFile(self.__GetRequest('fifteen_byte_file'))
  85. self.__download.GetRange(2)
  86. self.assertEqual(13, self.__buffer.tell())
  87. self.__buffer.seek(0)
  88. self.assertEqual(file_contents[2:15], self.__buffer.read())
  89. def testSmallChunksizes(self):
  90. file_contents = self.__GetTestdataFileContents('fifteen_byte_file')
  91. request = self.__GetRequest('fifteen_byte_file')
  92. for chunksize in (2, 3, 15, 100):
  93. self.__ResetDownload()
  94. self.__download.chunksize = chunksize
  95. self.__GetAndStream(request)
  96. self.assertEqual(15, self.__buffer.tell())
  97. self.__buffer.seek(0)
  98. self.assertEqual(file_contents, self.__buffer.read(15))
  99. def testLargeFileChunksizes(self):
  100. request = self.__GetRequest('thirty_meg_file')
  101. for chunksize in (1048576, 40 * 1048576):
  102. self.__ResetDownload()
  103. self.__download.chunksize = chunksize
  104. self.__GetAndStream(request)
  105. self.__buffer.seek(0)
  106. def testAutoGzipObject(self):
  107. # TODO(craigcitro): Move this to a new object once we have a more
  108. # permanent one, see: http://b/12250275
  109. request = storage.StorageObjectsGetRequest(
  110. bucket='ottenl-gzip', object='50K.txt')
  111. # First, try without auto-transfer.
  112. self.__GetFile(request)
  113. self.assertEqual(0, self.__buffer.tell())
  114. self.__download.StreamInChunks()
  115. self.assertEqual(50000, self.__buffer.tell())
  116. # Next, try with auto-transfer.
  117. self.__ResetDownload(auto_transfer=True)
  118. self.__GetFile(request)
  119. self.assertEqual(50000, self.__buffer.tell())
  120. def testSmallGzipObject(self):
  121. request = self.__GetRequest('zero-gzipd.html')
  122. self.__GetFile(request)
  123. self.assertEqual(0, self.__buffer.tell())
  124. additional_headers = {'accept-encoding': 'gzip, deflate'}
  125. self.__download.StreamInChunks(additional_headers=additional_headers)
  126. self.assertEqual(0, self.__buffer.tell())
  127. def testSerializedDownload(self):
  128. def _ProgressCallback(unused_response, download_object):
  129. print 'Progress %s' % download_object.progress
  130. file_contents = self.__GetTestdataFileContents('fifteen_byte_file')
  131. object_name = os.path.join(self._TESTDATA_PREFIX, 'fifteen_byte_file')
  132. request = storage.StorageObjectsGetRequest(
  133. bucket=self._DEFAULT_BUCKET, object=object_name)
  134. response = self.__client.objects.Get(request)
  135. self.__buffer = six.StringIO()
  136. download_data = json.dumps({
  137. 'auto_transfer': False,
  138. 'progress': 0,
  139. 'total_size': response.size,
  140. 'url': response.mediaLink,
  141. })
  142. self.__download = storage.Download.FromData(
  143. self.__buffer, download_data, http=self.__client.http)
  144. self.__download.StreamInChunks(callback=_ProgressCallback)
  145. self.assertEqual(15, self.__buffer.tell())
  146. self.__buffer.seek(0)
  147. self.assertEqual(file_contents, self.__buffer.read(15))
  148. if __name__ == '__main__':
  149. unittest.main()