OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 """ |
| 4 Copyright 2014 Google Inc. |
| 5 |
| 6 Use of this source code is governed by a BSD-style license that can be |
| 7 found in the LICENSE file. |
| 8 |
| 9 Test download.py |
| 10 |
| 11 TODO(epoger): Create a command to update the expected results (in |
| 12 OUTPUT_DIR_EXPECTED) when appropriate. For now, you should: |
| 13 1. examine the results in OUTPUT_DIR_ACTUAL and make sure they are ok |
| 14 2. rm -rf OUTPUT_DIR_EXPECTED |
| 15 3. mv OUTPUT_DIR_ACTUAL OUTPUT_DIR_EXPECTED |
| 16 Although, if you're using an SVN checkout, this will blow away .svn directories |
| 17 within OUTPUT_DIR_EXPECTED, which wouldn't be good... |
| 18 |
| 19 """ |
| 20 |
| 21 # System-level imports |
| 22 import filecmp |
| 23 import os |
| 24 import shutil |
| 25 import tempfile |
| 26 import unittest |
| 27 import urllib |
| 28 |
| 29 # Imports from within Skia |
| 30 import download_actuals |
| 31 |
| 32 PARENT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 33 INPUT_DIR = os.path.join(PARENT_DIR, 'tests', 'inputs') |
| 34 OUTPUT_DIR_ACTUAL = os.path.join(PARENT_DIR, 'tests', 'outputs', 'actual') |
| 35 OUTPUT_DIR_EXPECTED = os.path.join(PARENT_DIR, 'tests', 'outputs', 'expected') |
| 36 |
| 37 |
| 38 class DownloadTest(unittest.TestCase): |
| 39 |
| 40 def setUp(self): |
| 41 self._temp_dir = tempfile.mkdtemp() |
| 42 self._output_dir_actual = os.path.join(OUTPUT_DIR_ACTUAL, self.id()) |
| 43 self._output_dir_expected = os.path.join(OUTPUT_DIR_EXPECTED, self.id()) |
| 44 create_empty_dir(self._output_dir_actual) |
| 45 |
| 46 def tearDown(self): |
| 47 shutil.rmtree(self._temp_dir) |
| 48 if os.path.exists(self._output_dir_expected): |
| 49 different_files = find_different_files(self._output_dir_actual, |
| 50 self._output_dir_expected) |
| 51 # Maybe we should move this assert elsewhere? It's unusual to see an |
| 52 # assert within tearDown(), but my thinking was: |
| 53 # 1. Every test case will have some collection of output files that need |
| 54 # to be validated. |
| 55 # 2. So put that validation within tearDown(), which will be called after |
| 56 # every test case! |
| 57 # |
| 58 # I have confirmed that the test really does fail if this assert is |
| 59 # triggered. |
| 60 # |
| 61 # Ravi notes: if somebody later comes along and adds cleanup code below |
| 62 # this assert, then if tests fail, the artifacts will not be cleaned up. |
| 63 assert (not different_files), \ |
| 64 ('found differing files between actual dir %s and expected dir %s: %s' % |
| 65 (self._output_dir_actual, self._output_dir_expected, different_files)) |
| 66 |
| 67 def shortDescription(self): |
| 68 """Tell unittest framework to not print docstrings for test cases.""" |
| 69 return None |
| 70 |
| 71 def test_fetch(self): |
| 72 """Tests fetch() of GM results from actual-results.json .""" |
| 73 downloader = download_actuals.Download( |
| 74 actuals_base_url=download_actuals.create_filepath_url( |
| 75 os.path.join(INPUT_DIR, 'gm-actuals')), |
| 76 gm_actuals_root_url=download_actuals.create_filepath_url( |
| 77 os.path.join(INPUT_DIR, 'fake-gm-imagefiles'))) |
| 78 downloader.fetch( |
| 79 builder_name='Test-Android-GalaxyNexus-SGX540-Arm7-Release', |
| 80 dest_dir=self._output_dir_actual) |
| 81 |
| 82 def test_create_filepath_url(self): |
| 83 """Tests create_filepath_url(). """ |
| 84 with self.assertRaises(Exception): |
| 85 url_or_path.create_filepath_url('http://1.2.3.4/path') |
| 86 # Pass absolute filepath. |
| 87 self.assertEquals( |
| 88 download_actuals.create_filepath_url( |
| 89 '%sdir%sfile' % (os.path.sep, os.path.sep)), |
| 90 'file:///dir/file') |
| 91 # Pass relative filepath. |
| 92 self.assertEquals( |
| 93 download_actuals.create_filepath_url(os.path.join('dir', 'file')), |
| 94 'file://%s/dir/file' % urllib.pathname2url(os.getcwd())) |
| 95 |
| 96 def test_copy_contents(self): |
| 97 """Tests copy_contents(). """ |
| 98 contents = 'these are the contents' |
| 99 tempdir_path = tempfile.mkdtemp() |
| 100 try: |
| 101 source_path = os.path.join(tempdir_path, 'source') |
| 102 source_url = download_actuals.create_filepath_url(source_path) |
| 103 with open(source_path, 'w') as source_handle: |
| 104 source_handle.write(contents) |
| 105 dest_path = os.path.join(tempdir_path, 'new_subdir', 'dest') |
| 106 # Destination subdir does not exist, so copy_contents() should fail |
| 107 # if create_subdirs_if_needed is False. |
| 108 with self.assertRaises(Exception): |
| 109 download_actuals.copy_contents(source_url=source_url, |
| 110 dest_path=dest_path, |
| 111 create_subdirs_if_needed=False) |
| 112 # If create_subdirs_if_needed is True, it should work. |
| 113 download_actuals.copy_contents(source_url=source_url, |
| 114 dest_path=dest_path, |
| 115 create_subdirs_if_needed=True) |
| 116 self.assertEquals(open(dest_path).read(), contents) |
| 117 finally: |
| 118 shutil.rmtree(tempdir_path) |
| 119 |
| 120 |
| 121 # TODO(epoger): create_empty_dir(), find_different_files(), etc. should be |
| 122 # extracted from this file to some common location, where they can be shared |
| 123 # with results_test.py and other users. |
| 124 |
| 125 def create_empty_dir(path): |
| 126 """Create an empty directory at the given path.""" |
| 127 if os.path.isdir(path): |
| 128 shutil.rmtree(path) |
| 129 elif os.path.lexists(path): |
| 130 os.remove(path) |
| 131 os.makedirs(path) |
| 132 |
| 133 |
| 134 def find_different_files(dir1, dir2, ignore_subtree_names=None): |
| 135 """Returns a list of any files that differ between the directory trees rooted |
| 136 at dir1 and dir2. |
| 137 |
| 138 Args: |
| 139 dir1: root of a directory tree; if nonexistent, will raise OSError |
| 140 dir2: root of another directory tree; if nonexistent, will raise OSError |
| 141 ignore_subtree_names: list of subtree directory names to ignore; |
| 142 defaults to ['.svn'], so all SVN files are ignores |
| 143 |
| 144 TODO(epoger): include the dirname within each filename (not just the |
| 145 basename), to make it easier to locate any differences |
| 146 """ |
| 147 differing_files = [] |
| 148 if ignore_subtree_names is None: |
| 149 ignore_subtree_names = ['.svn'] |
| 150 dircmp = filecmp.dircmp(dir1, dir2, ignore=ignore_subtree_names) |
| 151 differing_files.extend(dircmp.left_only) |
| 152 differing_files.extend(dircmp.right_only) |
| 153 differing_files.extend(dircmp.common_funny) |
| 154 differing_files.extend(dircmp.diff_files) |
| 155 differing_files.extend(dircmp.funny_files) |
| 156 for common_dir in dircmp.common_dirs: |
| 157 differing_files.extend(find_different_files( |
| 158 os.path.join(dir1, common_dir), os.path.join(dir2, common_dir))) |
| 159 return differing_files |
| 160 |
| 161 |
| 162 def main(): |
| 163 suite = unittest.TestLoader().loadTestsFromTestCase(DownloadTest) |
| 164 unittest.TextTestRunner(verbosity=2).run(suite) |
| 165 |
| 166 |
| 167 if __name__ == '__main__': |
| 168 main() |
OLD | NEW |