| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import os | 5 import os |
| 6 import sys | |
| 7 import unittest | 6 import unittest |
| 8 import StringIO | 7 import StringIO |
| 9 | 8 |
| 10 import mock # pylint: disable=import-error | 9 import mock # pylint: disable=import-error |
| 11 | 10 |
| 12 from core import path_util | 11 from core import path_util |
| 13 import fetch_benchmark_deps | 12 import fetch_benchmark_deps |
| 14 | 13 |
| 15 | 14 |
| 16 def NormPaths(paths): | 15 def NormPaths(paths): |
| 17 return sorted([os.path.normcase(p) for p in paths.splitlines()]) | 16 return sorted([os.path.normcase(p) for p in paths.splitlines()]) |
| 18 | 17 |
| 19 | 18 |
| 20 class FetchBenchmarkDepsUnittest(unittest.TestCase): | 19 class FetchBenchmarkDepsUnittest(unittest.TestCase): |
| 21 """The test guards fetch_benchmark_deps. | 20 """The test guards fetch_benchmark_deps. |
| 22 | 21 |
| 23 It assumes the following telemetry APIs always success: | 22 It assumes the following telemetry APIs always success: |
| 24 telemetry.wpr.archive_info.WprArchiveInfo.DownloadArchivesIfNeeded | 23 telemetry.wpr.archive_info.WprArchiveInfo.DownloadArchivesIfNeeded |
| 25 py_utils.cloud_storage.GetFilesInDirectoryIfChanged | 24 py_utils.cloud_storage.GetFilesInDirectoryIfChanged |
| 26 """ | 25 """ |
| 27 | 26 |
| 28 def setUp(self): | |
| 29 """Override sys.argv as if it is called from command line.""" | |
| 30 self._argv = sys.argv | |
| 31 sys.argv = ['./fetch_benchmark_deps', ''] | |
| 32 | |
| 33 def _RunFetchBenchmarkDepsTest(self, benchmark_name, | 27 def _RunFetchBenchmarkDepsTest(self, benchmark_name, |
| 34 expected_fetched_file_paths=None): | 28 expected_fetched_file_paths=None): |
| 35 """Simulates './fetch_benchmark_deps [benchmark_name]' | 29 """Simulates './fetch_benchmark_deps [benchmark_name]' |
| 36 | 30 |
| 37 It checks if the paths returned are expected and have corresponding sha1 | 31 It checks if the paths returned are expected and have corresponding sha1 |
| 38 checksums. The expected result can be omitted if the dependencies of | 32 checksums. The expected result can be omitted if the dependencies of |
| 39 specified benchmarks are subject to changes. | 33 specified benchmarks are subject to changes. |
| 40 | 34 |
| 41 Args: | 35 Args: |
| 42 benchmark_name: benchmark name | 36 benchmark_name: benchmark name |
| 43 expected_fetched_file_paths: the expected result. | 37 expected_fetched_file_paths: the expected result. |
| 44 """ | 38 """ |
| 45 sys.argv[1] = benchmark_name | 39 args = [benchmark_name] |
| 46 output = StringIO.StringIO() | 40 output = StringIO.StringIO() |
| 47 with mock.patch('telemetry.wpr.archive_info.WprArchiveInfo' | 41 with mock.patch('telemetry.wpr.archive_info.WprArchiveInfo' |
| 48 '.DownloadArchivesIfNeeded') as mock_download: | 42 '.DownloadArchivesIfNeeded') as mock_download: |
| 49 with mock.patch('py_utils.cloud_storage' | 43 with mock.patch('py_utils.cloud_storage' |
| 50 '.GetFilesInDirectoryIfChanged') as mock_get: | 44 '.GetFilesInDirectoryIfChanged') as mock_get: |
| 51 mock_download.return_value = True | 45 mock_download.return_value = True |
| 52 mock_get.GetFilesInDirectoryIfChanged.return_value = True | 46 mock_get.GetFilesInDirectoryIfChanged.return_value = True |
| 53 fetch_benchmark_deps.main(output) | 47 fetch_benchmark_deps.main(args, output) |
| 54 for f in output.getvalue().splitlines(): | 48 for f in output.getvalue().splitlines(): |
| 55 fullpath = os.path.join(path_util.GetChromiumSrcDir(), f) | 49 fullpath = os.path.join(path_util.GetChromiumSrcDir(), f) |
| 56 sha1path = fullpath + '.sha1' | 50 sha1path = fullpath + '.sha1' |
| 57 self.assertTrue(os.path.isfile(sha1path)) | 51 self.assertTrue(os.path.isfile(sha1path)) |
| 58 if expected_fetched_file_paths: | 52 if expected_fetched_file_paths: |
| 59 self.assertEquals(expected_fetched_file_paths, | 53 self.assertEquals(expected_fetched_file_paths, |
| 60 NormPaths(output.getvalue())) | 54 NormPaths(output.getvalue())) |
| 61 | 55 |
| 62 def testFetchWPRs(self): | 56 def testFetchWPRs(self): |
| 63 self._RunFetchBenchmarkDepsTest('smoothness.top_25_smooth') | 57 self._RunFetchBenchmarkDepsTest('smoothness.top_25_smooth') |
| 64 | 58 |
| 65 def testFetchServingDirs(self): | 59 def testFetchServingDirs(self): |
| 66 self._RunFetchBenchmarkDepsTest('media.tough_video_cases') | 60 self._RunFetchBenchmarkDepsTest('media.tough_video_cases') |
| 67 | 61 |
| 68 def testFetchOctane(self): | 62 def testFetchOctane(self): |
| 69 octane_wpr_path = os.path.join( | 63 octane_wpr_path = os.path.join( |
| 70 os.path.dirname(__file__), 'page_sets', 'data', 'octane_002.wpr') | 64 os.path.dirname(__file__), 'page_sets', 'data', 'octane_002.wpr') |
| 71 expected = os.path.relpath(octane_wpr_path, | 65 expected = os.path.relpath(octane_wpr_path, |
| 72 path_util.GetChromiumSrcDir()) | 66 path_util.GetChromiumSrcDir()) |
| 73 self._RunFetchBenchmarkDepsTest('octane', NormPaths(expected)) | 67 self._RunFetchBenchmarkDepsTest('octane', NormPaths(expected)) |
| OLD | NEW |