binary_sizes_test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env vpython3
  2. # Copyright 2020 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import json
  6. import os
  7. import shutil
  8. import subprocess
  9. import tempfile
  10. import unittest
  11. import binary_sizes
  12. from common import DIR_SOURCE_ROOT
  13. _EXAMPLE_BLOBS = """
  14. {
  15. "web_engine": [
  16. {
  17. "merkle": "77e876447dd2daaaab7048d646e87fe8b6d9fecef6cbfcc4af30b8fbfa50b881",
  18. "path": "locales/ta.pak",
  19. "bytes": 17916,
  20. "is_counted": true,
  21. "size": 16384
  22. },
  23. {
  24. "merkle": "5f1932b8c9fe954f3c3fdb34ab2089d2af34e5a0cef90cad41a1cd37d92234bf",
  25. "path": "lib/libEGL.so",
  26. "bytes": 226960,
  27. "is_counted": true,
  28. "size": 90112
  29. },
  30. {
  31. "merkle": "9822fc0dd95cdd1cc46b5c6632a928a6ad19b76ed0157397d82a2f908946fc34",
  32. "path": "meta.far",
  33. "bytes": 24576,
  34. "is_counted": true,
  35. "size": 16384
  36. },
  37. {
  38. "merkle": "090aed4593c4f7d04a3ad80e9971c0532dd5b1d2bdf4754202cde510a88fd220",
  39. "path": "locales/ru.pak",
  40. "bytes": 11903,
  41. "is_counted": true,
  42. "size": 16384
  43. }
  44. ]
  45. }
  46. """
  47. class TestBinarySizes(unittest.TestCase):
  48. tmpdir = None
  49. @classmethod
  50. def setUpClass(cls):
  51. cls.tmpdir = tempfile.mkdtemp()
  52. @classmethod
  53. def tearDownClass(cls):
  54. shutil.rmtree(cls.tmpdir)
  55. def testReadAndWritePackageBlobs(self):
  56. # TODO(1309977): Disabled on Windows because Windows doesn't allow opening a
  57. # NamedTemporaryFile by name.
  58. if os.name == 'nt':
  59. return
  60. with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
  61. tmp_file.write(_EXAMPLE_BLOBS)
  62. tmp_file.flush()
  63. package_blobs = binary_sizes.ReadPackageBlobsJson(tmp_file.name)
  64. tmp_package_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
  65. tmp_package_file.close()
  66. try:
  67. binary_sizes.WritePackageBlobsJson(tmp_package_file.name, package_blobs)
  68. self.assertEqual(binary_sizes.ReadPackageBlobsJson(tmp_package_file.name),
  69. package_blobs)
  70. finally:
  71. os.remove(tmp_package_file.name)
  72. def testReadAndWritePackageSizes(self):
  73. # TODO(1309977): Disabled on Windows because Windows doesn't allow opening a
  74. # NamedTemporaryFile by name.
  75. if os.name == 'nt':
  76. return
  77. with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
  78. tmp_file.write(_EXAMPLE_BLOBS)
  79. tmp_file.flush()
  80. blobs = binary_sizes.ReadPackageBlobsJson(tmp_file.name)
  81. sizes = binary_sizes.GetPackageSizes(blobs)
  82. new_sizes = {}
  83. with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
  84. binary_sizes.WritePackageSizesJson(tmp_file.name, sizes)
  85. new_sizes = binary_sizes.ReadPackageSizesJson(tmp_file.name)
  86. self.assertEqual(new_sizes, sizes)
  87. self.assertIn('web_engine', new_sizes)
  88. def testGetPackageSizesUsesBlobMerklesForCount(self):
  89. # TODO(1309977): Disabled on Windows because Windows doesn't allow opening a
  90. # NamedTemporaryFile by name.
  91. if os.name == 'nt':
  92. return
  93. blobs = json.loads(_EXAMPLE_BLOBS)
  94. # Make a duplicate of the last blob.
  95. last_blob = dict(blobs['web_engine'][-1])
  96. blobs['cast_runner'] = []
  97. last_blob['path'] = 'foo' # Give a non-sense name, but keep merkle.
  98. # If the merkle is the same, the blob_count increases by 1.
  99. # This effectively reduces the size of the blobs size by half.
  100. # In both packages, despite it appearing in both and under different
  101. # names.
  102. blobs['cast_runner'].append(last_blob)
  103. with tempfile.NamedTemporaryFile(mode='w') as tmp_file:
  104. tmp_file.write(json.dumps(blobs))
  105. tmp_file.flush()
  106. blobs = binary_sizes.ReadPackageBlobsJson(tmp_file.name)
  107. sizes = binary_sizes.GetPackageSizes(blobs)
  108. self.assertEqual(sizes['cast_runner'].compressed, last_blob['size'] / 2)
  109. if __name__ == '__main__':
  110. unittest.main()