maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # coding=utf-8 |
Marc-Antoine Ruel | 8add124 | 2013-11-05 17:28:27 -0500 | [diff] [blame] | 3 | # Copyright 2013 The Swarming Authors. All rights reserved. |
Marc-Antoine Ruel | e98b112 | 2013-11-05 20:27:57 -0500 | [diff] [blame] | 4 | # Use of this source code is governed under the Apache License, Version 2.0 that |
| 5 | # can be found in the LICENSE file. |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 6 | |
| 7 | import logging |
| 8 | import os |
| 9 | import tempfile |
| 10 | import unittest |
| 11 | import shutil |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 12 | import StringIO |
| 13 | import subprocess |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 14 | import sys |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 15 | import time |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 16 | |
| 17 | BASE_DIR = unicode(os.path.dirname(os.path.abspath(__file__))) |
| 18 | ROOT_DIR = os.path.dirname(BASE_DIR) |
| 19 | sys.path.insert(0, ROOT_DIR) |
| 20 | |
| 21 | FILE_PATH = unicode(os.path.abspath(__file__)) |
| 22 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 23 | import test_utils |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 24 | from utils import file_path |
| 25 | |
| 26 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 27 | def write_content(filepath, content): |
| 28 | with open(filepath, 'wb') as f: |
| 29 | f.write(content) |
| 30 | |
| 31 | |
| 32 | class FilePathTest(unittest.TestCase): |
| 33 | def setUp(self): |
| 34 | super(FilePathTest, self).setUp() |
| 35 | self._tempdir = None |
| 36 | |
| 37 | def tearDown(self): |
| 38 | if self._tempdir: |
| 39 | for dirpath, dirnames, filenames in os.walk(self._tempdir, topdown=True): |
| 40 | for filename in filenames: |
| 41 | file_path.set_read_only(os.path.join(dirpath, filename), False) |
| 42 | for dirname in dirnames: |
| 43 | file_path.set_read_only(os.path.join(dirpath, dirname), False) |
| 44 | shutil.rmtree(self._tempdir) |
| 45 | super(FilePathTest, self).tearDown() |
| 46 | |
| 47 | @property |
| 48 | def tempdir(self): |
| 49 | if not self._tempdir: |
| 50 | self._tempdir = tempfile.mkdtemp(prefix='run_isolated_test') |
| 51 | return self._tempdir |
| 52 | |
| 53 | def assertFileMode(self, filepath, mode, umask=None): |
| 54 | umask = test_utils.umask() if umask is None else umask |
| 55 | actual = os.stat(filepath).st_mode |
| 56 | expected = mode & ~umask |
| 57 | self.assertEqual( |
| 58 | expected, |
| 59 | actual, |
| 60 | (filepath, oct(expected), oct(actual), oct(umask))) |
| 61 | |
| 62 | def assertMaskedFileMode(self, filepath, mode): |
| 63 | """It's usually when the file was first marked read only.""" |
| 64 | self.assertFileMode(filepath, mode, 0 if sys.platform == 'win32' else 077) |
| 65 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 66 | def test_native_case_end_with_os_path_sep(self): |
| 67 | # Make sure the trailing os.path.sep is kept. |
| 68 | path = file_path.get_native_path_case(ROOT_DIR) + os.path.sep |
| 69 | self.assertEqual(file_path.get_native_path_case(path), path) |
| 70 | |
| 71 | def test_native_case_end_with_dot_os_path_sep(self): |
| 72 | path = file_path.get_native_path_case(ROOT_DIR + os.path.sep) |
| 73 | self.assertEqual( |
| 74 | file_path.get_native_path_case(path + '.' + os.path.sep), |
| 75 | path) |
| 76 | |
| 77 | def test_native_case_non_existing(self): |
| 78 | # Make sure it doesn't throw on non-existing files. |
| 79 | non_existing = 'trace_input_test_this_file_should_not_exist' |
| 80 | path = os.path.expanduser('~/' + non_existing) |
| 81 | self.assertFalse(os.path.exists(path)) |
| 82 | path = file_path.get_native_path_case(ROOT_DIR) + os.path.sep |
| 83 | self.assertEqual(file_path.get_native_path_case(path), path) |
| 84 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 85 | def test_delete_wd_rf(self): |
| 86 | # Confirms that a RO file in a RW directory can be deleted on non-Windows. |
| 87 | dir_foo = os.path.join(self.tempdir, 'foo') |
| 88 | file_bar = os.path.join(dir_foo, 'bar') |
| 89 | os.mkdir(dir_foo, 0777) |
| 90 | write_content(file_bar, 'bar') |
| 91 | file_path.set_read_only(dir_foo, False) |
| 92 | file_path.set_read_only(file_bar, True) |
| 93 | self.assertFileMode(dir_foo, 040777) |
| 94 | self.assertMaskedFileMode(file_bar, 0100444) |
| 95 | if sys.platform == 'win32': |
| 96 | # On Windows, a read-only file can't be deleted. |
| 97 | with self.assertRaises(OSError): |
| 98 | os.remove(file_bar) |
| 99 | else: |
| 100 | os.remove(file_bar) |
| 101 | |
| 102 | def test_delete_rd_wf(self): |
| 103 | # Confirms that a Rw file in a RO directory can be deleted on Windows only. |
| 104 | dir_foo = os.path.join(self.tempdir, 'foo') |
| 105 | file_bar = os.path.join(dir_foo, 'bar') |
| 106 | os.mkdir(dir_foo, 0777) |
| 107 | write_content(file_bar, 'bar') |
| 108 | file_path.set_read_only(dir_foo, True) |
| 109 | file_path.set_read_only(file_bar, False) |
| 110 | self.assertMaskedFileMode(dir_foo, 040555) |
| 111 | self.assertFileMode(file_bar, 0100666) |
| 112 | if sys.platform == 'win32': |
| 113 | # A read-only directory has a convoluted meaning on Windows, it means that |
| 114 | # the directory is "personalized". This is used as a signal by Windows |
| 115 | # Explorer to tell it to look into the directory for desktop.ini. |
| 116 | # See http://support.microsoft.com/kb/326549 for more details. |
| 117 | # As such, it is important to not try to set the read-only bit on |
| 118 | # directories on Windows since it has no effect other than trigger |
| 119 | # Windows Explorer to look for desktop.ini, which is unnecessary. |
| 120 | os.remove(file_bar) |
| 121 | else: |
| 122 | with self.assertRaises(OSError): |
| 123 | os.remove(file_bar) |
| 124 | |
| 125 | def test_delete_rd_rf(self): |
| 126 | # Confirms that a RO file in a RO directory can't be deleted. |
| 127 | dir_foo = os.path.join(self.tempdir, 'foo') |
| 128 | file_bar = os.path.join(dir_foo, 'bar') |
| 129 | os.mkdir(dir_foo, 0777) |
| 130 | write_content(file_bar, 'bar') |
| 131 | file_path.set_read_only(dir_foo, True) |
| 132 | file_path.set_read_only(file_bar, True) |
| 133 | self.assertMaskedFileMode(dir_foo, 040555) |
| 134 | self.assertMaskedFileMode(file_bar, 0100444) |
| 135 | with self.assertRaises(OSError): |
| 136 | # It fails for different reason depending on the OS. See the test cases |
| 137 | # above. |
| 138 | os.remove(file_bar) |
| 139 | |
| 140 | def test_hard_link_mode(self): |
| 141 | # Creates a hard link, see if the file mode changed on the node or the |
| 142 | # directory entry. |
| 143 | dir_foo = os.path.join(self.tempdir, 'foo') |
| 144 | file_bar = os.path.join(dir_foo, 'bar') |
| 145 | file_link = os.path.join(dir_foo, 'link') |
| 146 | os.mkdir(dir_foo, 0777) |
| 147 | write_content(file_bar, 'bar') |
| 148 | file_path.hardlink(file_bar, file_link) |
| 149 | self.assertFileMode(file_bar, 0100666) |
| 150 | self.assertFileMode(file_link, 0100666) |
| 151 | file_path.set_read_only(file_bar, True) |
| 152 | self.assertMaskedFileMode(file_bar, 0100444) |
| 153 | self.assertMaskedFileMode(file_link, 0100444) |
| 154 | # This is bad news for Windows; on Windows, the file must be writeable to be |
| 155 | # deleted, but the file node is modified. This means that every hard links |
| 156 | # must be reset to be read-only after deleting one of the hard link |
| 157 | # directory entry. |
| 158 | |
| 159 | if sys.platform == 'darwin': |
| 160 | def test_native_case_symlink_wrong_case(self): |
| 161 | base_dir = file_path.get_native_path_case(BASE_DIR) |
| 162 | trace_inputs_dir = os.path.join(base_dir, 'trace_inputs') |
| 163 | actual = file_path.get_native_path_case(trace_inputs_dir) |
| 164 | self.assertEqual(trace_inputs_dir, actual) |
| 165 | |
| 166 | # Make sure the symlink is not resolved. |
| 167 | data = os.path.join(trace_inputs_dir, 'Files2') |
| 168 | actual = file_path.get_native_path_case(data) |
| 169 | self.assertEqual( |
| 170 | os.path.join(trace_inputs_dir, 'files2'), actual) |
| 171 | |
| 172 | data = os.path.join(trace_inputs_dir, 'Files2', '') |
| 173 | actual = file_path.get_native_path_case(data) |
| 174 | self.assertEqual( |
| 175 | os.path.join(trace_inputs_dir, 'files2', ''), actual) |
| 176 | |
| 177 | data = os.path.join(trace_inputs_dir, 'Files2', 'Child1.py') |
| 178 | actual = file_path.get_native_path_case(data) |
| 179 | # TODO(maruel): Should be child1.py. |
| 180 | self.assertEqual( |
| 181 | os.path.join(trace_inputs_dir, 'files2', 'Child1.py'), actual) |
| 182 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 183 | if sys.platform in ('darwin', 'win32'): |
| 184 | def test_native_case_not_sensitive(self): |
| 185 | # The home directory is almost guaranteed to have mixed upper/lower case |
| 186 | # letters on both Windows and OSX. |
| 187 | # This test also ensures that the output is independent on the input |
| 188 | # string case. |
| 189 | path = os.path.expanduser(u'~') |
| 190 | self.assertTrue(os.path.isdir(path)) |
| 191 | path = path.replace('/', os.path.sep) |
| 192 | if sys.platform == 'win32': |
| 193 | # Make sure the drive letter is upper case for consistency. |
| 194 | path = path[0].upper() + path[1:] |
| 195 | # This test assumes the variable is in the native path case on disk, this |
| 196 | # should be the case. Verify this assumption: |
| 197 | self.assertEqual(path, file_path.get_native_path_case(path)) |
| 198 | self.assertEqual( |
| 199 | file_path.get_native_path_case(path.lower()), |
| 200 | file_path.get_native_path_case(path.upper())) |
| 201 | |
| 202 | def test_native_case_not_sensitive_non_existent(self): |
| 203 | # This test also ensures that the output is independent on the input |
| 204 | # string case. |
| 205 | non_existing = os.path.join( |
| 206 | 'trace_input_test_this_dir_should_not_exist', 'really not', '') |
| 207 | path = os.path.expanduser(os.path.join(u'~', non_existing)) |
| 208 | path = path.replace('/', os.path.sep) |
| 209 | self.assertFalse(os.path.exists(path)) |
| 210 | lower = file_path.get_native_path_case(path.lower()) |
| 211 | upper = file_path.get_native_path_case(path.upper()) |
| 212 | # Make sure non-existing element is not modified: |
| 213 | self.assertTrue(lower.endswith(non_existing.lower())) |
| 214 | self.assertTrue(upper.endswith(non_existing.upper())) |
| 215 | self.assertEqual(lower[:-len(non_existing)], upper[:-len(non_existing)]) |
| 216 | |
Marc-Antoine Ruel | e4ad07e | 2014-10-15 20:22:29 -0400 | [diff] [blame] | 217 | if sys.platform == 'win32': |
| 218 | def test_native_case_alternate_datastream(self): |
| 219 | # Create the file manually, since tempfile doesn't support ADS. |
| 220 | tempdir = unicode(tempfile.mkdtemp(prefix='trace_inputs')) |
| 221 | try: |
| 222 | tempdir = file_path.get_native_path_case(tempdir) |
| 223 | basename = 'foo.txt' |
| 224 | filename = basename + ':Zone.Identifier' |
| 225 | filepath = os.path.join(tempdir, filename) |
| 226 | open(filepath, 'w').close() |
| 227 | self.assertEqual(filepath, file_path.get_native_path_case(filepath)) |
| 228 | data_suffix = ':$DATA' |
| 229 | self.assertEqual( |
| 230 | filepath + data_suffix, |
| 231 | file_path.get_native_path_case(filepath + data_suffix)) |
| 232 | |
| 233 | open(filepath + '$DATA', 'w').close() |
| 234 | self.assertEqual( |
| 235 | filepath + data_suffix, |
| 236 | file_path.get_native_path_case(filepath + data_suffix)) |
| 237 | # Ensure the ADS weren't created as separate file. You love NTFS, don't |
| 238 | # you? |
| 239 | self.assertEqual([basename], os.listdir(tempdir)) |
| 240 | finally: |
| 241 | shutil.rmtree(tempdir) |
| 242 | |
| 243 | def test_rmtree_win(self): |
| 244 | # Mock our sleep for faster test case execution. |
| 245 | sleeps = [] |
| 246 | self.mock(time, 'sleep', sleeps.append) |
| 247 | self.mock(sys, 'stderr', StringIO.StringIO()) |
| 248 | |
| 249 | # Open a child process, so the file is locked. |
| 250 | subdir = os.path.join(self.tempdir, 'to_be_deleted') |
| 251 | os.mkdir(subdir) |
| 252 | script = 'import time; open(\'a\', \'w\'); time.sleep(60)' |
| 253 | proc = subprocess.Popen([sys.executable, '-c', script], cwd=subdir) |
| 254 | try: |
| 255 | # Wait until the file exist. |
| 256 | while not os.path.isfile(os.path.join(subdir, 'a')): |
| 257 | self.assertEqual(None, proc.poll()) |
| 258 | file_path.rmtree(subdir) |
| 259 | self.assertEqual([2, 4, 2], sleeps) |
| 260 | # sys.stderr.getvalue() would return a fair amount of output but it is |
| 261 | # not completely deterministic so we're not testing it here. |
| 262 | finally: |
| 263 | proc.wait() |
| 264 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 265 | if sys.platform != 'win32': |
| 266 | def test_symlink(self): |
| 267 | # This test will fail if the checkout is in a symlink. |
| 268 | actual = file_path.split_at_symlink(None, ROOT_DIR) |
| 269 | expected = (ROOT_DIR, None, None) |
| 270 | self.assertEqual(expected, actual) |
| 271 | |
| 272 | actual = file_path.split_at_symlink( |
| 273 | None, os.path.join(BASE_DIR, 'trace_inputs')) |
| 274 | expected = ( |
| 275 | os.path.join(BASE_DIR, 'trace_inputs'), None, None) |
| 276 | self.assertEqual(expected, actual) |
| 277 | |
| 278 | actual = file_path.split_at_symlink( |
| 279 | None, os.path.join(BASE_DIR, 'trace_inputs', 'files2')) |
| 280 | expected = ( |
| 281 | os.path.join(BASE_DIR, 'trace_inputs'), 'files2', '') |
| 282 | self.assertEqual(expected, actual) |
| 283 | |
| 284 | actual = file_path.split_at_symlink( |
| 285 | ROOT_DIR, os.path.join('tests', 'trace_inputs', 'files2')) |
| 286 | expected = ( |
| 287 | os.path.join('tests', 'trace_inputs'), 'files2', '') |
| 288 | self.assertEqual(expected, actual) |
| 289 | actual = file_path.split_at_symlink( |
| 290 | ROOT_DIR, os.path.join('tests', 'trace_inputs', 'files2', 'bar')) |
| 291 | expected = ( |
| 292 | os.path.join('tests', 'trace_inputs'), 'files2', '/bar') |
| 293 | self.assertEqual(expected, actual) |
| 294 | |
| 295 | def test_native_case_symlink_right_case(self): |
| 296 | actual = file_path.get_native_path_case( |
| 297 | os.path.join(BASE_DIR, 'trace_inputs')) |
| 298 | self.assertEqual('trace_inputs', os.path.basename(actual)) |
| 299 | |
| 300 | # Make sure the symlink is not resolved. |
| 301 | actual = file_path.get_native_path_case( |
| 302 | os.path.join(BASE_DIR, 'trace_inputs', 'files2')) |
| 303 | self.assertEqual('files2', os.path.basename(actual)) |
| 304 | |
maruel@chromium.org | 561d4b2 | 2013-09-26 21:08:08 +0000 | [diff] [blame] | 305 | |
| 306 | if __name__ == '__main__': |
| 307 | logging.basicConfig( |
| 308 | level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
| 309 | if '-v' in sys.argv: |
| 310 | unittest.TestCase.maxDiff = None |
| 311 | unittest.main() |