PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/telemetry/telemetry/testing/system_stub_unittest.py

https://gitlab.com/jonnialva90/iridium-browser
Python | 251 lines | 215 code | 18 blank | 18 comment | 0 complexity | 6d666dbce09a934e696bc19b03a6ff67 MD5 | raw file
  1. # Copyright 2013 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import os
  5. import unittest
  6. PERF_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  7. from telemetry.testing import system_stub
  8. from telemetry.internal.testing import system_stub_test_module
  9. class CloudStorageTest(unittest.TestCase):
  10. SUCCESS_FILE_HASH = 'success'.zfill(40)
  11. PUBLIC_FILE_HASH = 'public'.zfill(40)
  12. PARTNER_FILE_HASH = 'partner'.zfill(40)
  13. INTERNAL_FILE_HASH = 'internal'.zfill(40)
  14. UPDATED_HASH = 'updated'.zfill(40)
  15. def setUp(self):
  16. self.cloud_storage = system_stub.CloudStorageModuleStub()
  17. # Files in Cloud Storage.
  18. self.remote_files = ['preset_public_file.wpr',
  19. 'preset_partner_file.wpr',
  20. 'preset_internal_file.wpr']
  21. self.remote_paths = {
  22. self.cloud_storage.PUBLIC_BUCKET:
  23. {'preset_public_file.wpr':CloudStorageTest.PUBLIC_FILE_HASH},
  24. self.cloud_storage.PARTNER_BUCKET:
  25. {'preset_partner_file.wpr':CloudStorageTest.PARTNER_FILE_HASH},
  26. self.cloud_storage.INTERNAL_BUCKET:
  27. {'preset_internal_file.wpr':CloudStorageTest.INTERNAL_FILE_HASH}}
  28. # Local data files and hashes.
  29. self.data_files = [
  30. os.path.join(os.path.sep, 'path', 'to', 'success.wpr'),
  31. os.path.join(os.path.sep, 'path', 'to', 'wrong_hash.wpr'),
  32. os.path.join(os.path.sep, 'path', 'to', 'preset_public_file.wpr'),
  33. os.path.join(os.path.sep, 'path', 'to', 'preset_partner_file.wpr'),
  34. os.path.join(os.path.sep, 'path', 'to', 'preset_internal_file.wpr')]
  35. self.local_file_hashes = {
  36. os.path.join(os.path.sep, 'path', 'to', 'success.wpr'):
  37. CloudStorageTest.SUCCESS_FILE_HASH,
  38. os.path.join(os.path.sep, 'path', 'to', 'wrong_hash.wpr'):
  39. CloudStorageTest.SUCCESS_FILE_HASH,
  40. os.path.join(os.path.sep, 'path', 'to', 'preset_public_file.wpr'):
  41. CloudStorageTest.PUBLIC_FILE_HASH,
  42. os.path.join(os.path.sep, 'path', 'to', 'preset_partner_file.wpr'):
  43. CloudStorageTest.PARTNER_FILE_HASH,
  44. os.path.join(os.path.sep, 'path', 'to', 'preset_internal_file.wpr'):
  45. CloudStorageTest.INTERNAL_FILE_HASH,
  46. }
  47. self.cloud_storage.SetCalculatedHashesForTesting(self.local_file_hashes)
  48. # Local hash files and their contents.
  49. local_hash_files = {
  50. os.path.join(os.path.sep, 'path', 'to', 'success.wpr.sha1'):
  51. CloudStorageTest.SUCCESS_FILE_HASH,
  52. os.path.join(os.path.sep, 'path', 'to', 'wrong_hash.wpr.sha1'):
  53. 'wronghash'.zfill(40),
  54. os.path.join(os.path.sep, 'path', 'to', 'preset_public_file.wpr.sha1'):
  55. CloudStorageTest.PUBLIC_FILE_HASH,
  56. os.path.join(os.path.sep, 'path', 'to', 'preset_partner_file.wpr.sha1'):
  57. CloudStorageTest.PARTNER_FILE_HASH,
  58. os.path.join(os.path.sep, 'path', 'to',
  59. 'preset_internal_file.wpr.sha1'):
  60. CloudStorageTest.INTERNAL_FILE_HASH,
  61. }
  62. self.cloud_storage.SetHashFileContentsForTesting(local_hash_files)
  63. def testSetup(self):
  64. self.assertEqual(self.local_file_hashes,
  65. self.cloud_storage.local_file_hashes)
  66. self.assertEqual(set(self.data_files),
  67. set(self.cloud_storage.GetLocalDataFiles()))
  68. self.assertEqual(self.cloud_storage.default_remote_paths,
  69. self.cloud_storage.GetRemotePathsForTesting())
  70. self.cloud_storage.SetRemotePathsForTesting(self.remote_paths)
  71. self.assertEqual(self.remote_paths,
  72. self.cloud_storage.GetRemotePathsForTesting())
  73. def testExistsEmptyCloudStorage(self):
  74. # Test empty remote files dictionary.
  75. self.assertFalse(self.cloud_storage.Exists(self.cloud_storage.PUBLIC_BUCKET,
  76. 'preset_public_file.wpr'))
  77. self.assertFalse(self.cloud_storage.Exists(
  78. self.cloud_storage.PARTNER_BUCKET, 'preset_partner_file.wpr'))
  79. self.assertFalse(self.cloud_storage.Exists(
  80. self.cloud_storage.INTERNAL_BUCKET, 'preset_internal_file.wpr'))
  81. def testExistsNonEmptyCloudStorage(self):
  82. # Test non-empty remote files dictionary.
  83. self.cloud_storage.SetRemotePathsForTesting(self.remote_paths)
  84. self.assertTrue(self.cloud_storage.Exists(
  85. self.cloud_storage.PUBLIC_BUCKET, 'preset_public_file.wpr'))
  86. self.assertTrue(self.cloud_storage.Exists(
  87. self.cloud_storage.PARTNER_BUCKET, 'preset_partner_file.wpr'))
  88. self.assertTrue(self.cloud_storage.Exists(
  89. self.cloud_storage.INTERNAL_BUCKET, 'preset_internal_file.wpr'))
  90. self.assertFalse(self.cloud_storage.Exists(
  91. self.cloud_storage.PUBLIC_BUCKET, 'fake_file'))
  92. self.assertFalse(self.cloud_storage.Exists(
  93. self.cloud_storage.PARTNER_BUCKET, 'fake_file'))
  94. self.assertFalse(self.cloud_storage.Exists(
  95. self.cloud_storage.INTERNAL_BUCKET, 'fake_file'))
  96. # Reset state.
  97. self.cloud_storage.SetRemotePathsForTesting()
  98. def testNonEmptyInsertAndExistsPublic(self):
  99. # Test non-empty remote files dictionary.
  100. self.cloud_storage.SetRemotePathsForTesting(self.remote_paths)
  101. self.assertFalse(self.cloud_storage.Exists(self.cloud_storage.PUBLIC_BUCKET,
  102. 'success.wpr'))
  103. self.cloud_storage.Insert(
  104. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr',
  105. os.path.join(os.path.sep, 'path', 'to', 'success.wpr'))
  106. self.assertTrue(self.cloud_storage.Exists(
  107. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr'))
  108. # Reset state.
  109. self.cloud_storage.SetRemotePathsForTesting()
  110. def testEmptyInsertAndExistsPublic(self):
  111. # Test empty remote files dictionary.
  112. self.assertFalse(self.cloud_storage.Exists(
  113. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr'))
  114. self.cloud_storage.Insert(
  115. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr',
  116. os.path.join(os.path.sep, 'path', 'to', 'success.wpr'))
  117. self.assertTrue(self.cloud_storage.Exists(
  118. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr'))
  119. def testEmptyInsertAndGet(self):
  120. self.assertRaises(self.cloud_storage.NotFoundError, self.cloud_storage.Get,
  121. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr',
  122. os.path.join(os.path.sep, 'path', 'to', 'success.wpr'))
  123. self.cloud_storage.Insert(self.cloud_storage.PUBLIC_BUCKET, 'success.wpr',
  124. os.path.join(os.path.sep, 'path', 'to',
  125. 'success.wpr'))
  126. self.assertTrue(self.cloud_storage.Exists(
  127. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr'))
  128. self.assertEqual(CloudStorageTest.SUCCESS_FILE_HASH, self.cloud_storage.Get(
  129. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr',
  130. os.path.join(os.path.sep, 'path', 'to', 'success.wpr')))
  131. def testNonEmptyInsertAndGet(self):
  132. self.cloud_storage.SetRemotePathsForTesting(self.remote_paths)
  133. self.assertRaises(self.cloud_storage.NotFoundError, self.cloud_storage.Get,
  134. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr',
  135. os.path.join(os.path.sep, 'path', 'to', 'success.wpr'))
  136. self.cloud_storage.Insert(self.cloud_storage.PUBLIC_BUCKET, 'success.wpr',
  137. os.path.join(os.path.sep, 'path', 'to',
  138. 'success.wpr'))
  139. self.assertTrue(self.cloud_storage.Exists(self.cloud_storage.PUBLIC_BUCKET,
  140. 'success.wpr'))
  141. self.assertEqual(
  142. CloudStorageTest.SUCCESS_FILE_HASH, self.cloud_storage.Get(
  143. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr',
  144. os.path.join(os.path.sep, 'path', 'to', 'success.wpr')))
  145. # Reset state.
  146. self.cloud_storage.SetRemotePathsForTesting()
  147. def testGetIfChanged(self):
  148. self.cloud_storage.SetRemotePathsForTesting(self.remote_paths)
  149. self.assertRaises(
  150. self.cloud_storage.NotFoundError, self.cloud_storage.Get,
  151. self.cloud_storage.PUBLIC_BUCKET, 'success.wpr',
  152. os.path.join(os.path.sep, 'path', 'to', 'success.wpr'))
  153. self.assertFalse(self.cloud_storage.GetIfChanged(
  154. os.path.join(os.path.sep, 'path', 'to', 'preset_public_file.wpr'),
  155. self.cloud_storage.PUBLIC_BUCKET))
  156. self.cloud_storage.ChangeRemoteHashForTesting(
  157. self.cloud_storage.PUBLIC_BUCKET, 'preset_public_file.wpr',
  158. CloudStorageTest.UPDATED_HASH)
  159. self.assertTrue(self.cloud_storage.GetIfChanged(
  160. os.path.join(os.path.sep, 'path', 'to', 'preset_public_file.wpr'),
  161. self.cloud_storage.PUBLIC_BUCKET))
  162. self.assertFalse(self.cloud_storage.GetIfChanged(
  163. os.path.join(os.path.sep, 'path', 'to', 'preset_public_file.wpr'),
  164. self.cloud_storage.PUBLIC_BUCKET))
  165. # Reset state.
  166. self.cloud_storage.SetRemotePathsForTesting()
  167. def testList(self):
  168. self.assertEqual([],
  169. self.cloud_storage.List(self.cloud_storage.PUBLIC_BUCKET))
  170. self.cloud_storage.SetRemotePathsForTesting(self.remote_paths)
  171. self.assertEqual(['preset_public_file.wpr'],
  172. self.cloud_storage.List(self.cloud_storage.PUBLIC_BUCKET))
  173. # Reset state.
  174. self.cloud_storage.SetRemotePathsForTesting()
  175. def testPermissionError(self):
  176. self.cloud_storage.SetRemotePathsForTesting(self.remote_paths)
  177. self.cloud_storage.SetPermissionLevelForTesting(
  178. self.cloud_storage.PUBLIC_PERMISSION)
  179. self.assertRaises(
  180. self.cloud_storage.PermissionError, self.cloud_storage.Get,
  181. self.cloud_storage.INTERNAL_BUCKET, 'preset_internal_file.wpr',
  182. os.path.join(os.path.sep, 'path', 'to', 'preset_internal_file.wpr'))
  183. self.assertRaises(
  184. self.cloud_storage.PermissionError, self.cloud_storage.GetIfChanged,
  185. os.path.join(os.path.sep, 'path', 'to', 'preset_internal_file.wpr'),
  186. self.cloud_storage.INTERNAL_BUCKET)
  187. self.assertRaises(
  188. self.cloud_storage.PermissionError, self.cloud_storage.List,
  189. self.cloud_storage.INTERNAL_BUCKET)
  190. self.assertRaises(
  191. self.cloud_storage.PermissionError, self.cloud_storage.Exists,
  192. self.cloud_storage.INTERNAL_BUCKET, 'preset_internal_file.wpr')
  193. self.assertRaises(
  194. self.cloud_storage.PermissionError, self.cloud_storage.Insert,
  195. self.cloud_storage.INTERNAL_BUCKET, 'success.wpr',
  196. os.path.join(os.path.sep, 'path', 'to', 'success.wpr'))
  197. # Reset state.
  198. self.cloud_storage.SetRemotePathsForTesting()
  199. def testCredentialsError(self):
  200. self.cloud_storage.SetRemotePathsForTesting(self.remote_paths)
  201. self.cloud_storage.SetPermissionLevelForTesting(
  202. self.cloud_storage.CREDENTIALS_ERROR_PERMISSION)
  203. self.assertRaises(
  204. self.cloud_storage.CredentialsError, self.cloud_storage.Get,
  205. self.cloud_storage.INTERNAL_BUCKET, 'preset_internal_file.wpr',
  206. os.path.join(os.path.sep, 'path', 'to', 'preset_internal_file.wpr'))
  207. self.assertRaises(
  208. self.cloud_storage.CredentialsError, self.cloud_storage.GetIfChanged,
  209. self.cloud_storage.INTERNAL_BUCKET,
  210. os.path.join(os.path.sep, 'path', 'to', 'preset_internal_file.wpr'))
  211. self.assertRaises(
  212. self.cloud_storage.CredentialsError, self.cloud_storage.List,
  213. self.cloud_storage.INTERNAL_BUCKET)
  214. self.assertRaises(
  215. self.cloud_storage.CredentialsError, self.cloud_storage.Exists,
  216. self.cloud_storage.INTERNAL_BUCKET, 'preset_internal_file.wpr')
  217. self.assertRaises(
  218. self.cloud_storage.CredentialsError, self.cloud_storage.Insert,
  219. self.cloud_storage.INTERNAL_BUCKET, 'success.wpr',
  220. os.path.join(os.path.sep, 'path', 'to', 'success.wpr'))
  221. # Reset state.
  222. self.cloud_storage.SetRemotePathsForTesting()
  223. def testOpenRestoresCorrectly(self):
  224. file_path = os.path.realpath(__file__)
  225. stubs = system_stub.Override(system_stub_test_module, ['open'])
  226. stubs.open.files = {file_path:'contents'}
  227. f = system_stub_test_module.SystemStubTest.TestOpen(file_path)
  228. self.assertEqual(type(f), system_stub.OpenFunctionStub.FileStub)
  229. stubs.open.files = {}
  230. stubs.Restore()
  231. # This will throw an error if the open stub wasn't restored correctly.
  232. f = system_stub_test_module.SystemStubTest.TestOpen(file_path)
  233. self.assertEqual(type(f), file)