| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The LUCI Authors. All rights reserved. | 2 # Copyright 2013 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 # pylint: disable=R0201 | 6 # pylint: disable=R0201 |
| 7 | 7 |
| 8 import StringIO | 8 import StringIO |
| 9 import base64 | 9 import base64 |
| 10 import contextlib | 10 import contextlib |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 with open(p, 'rb') as f: | 64 with open(p, 'rb') as f: |
| 65 out[os.path.relpath(p, path)] = f.read() | 65 out[os.path.relpath(p, path)] = f.read() |
| 66 return out | 66 return out |
| 67 | 67 |
| 68 | 68 |
| 69 @contextlib.contextmanager | 69 @contextlib.contextmanager |
| 70 def init_named_caches_stub(_run_dir): | 70 def init_named_caches_stub(_run_dir): |
| 71 yield | 71 yield |
| 72 | 72 |
| 73 | 73 |
| 74 def put_to_named_cache(manager, cache_name, file_name, contents): | |
| 75 """Puts files into named cache.""" | |
| 76 tdir = tempfile.mkdtemp(prefix=u'run_isolated_test') | |
| 77 try: | |
| 78 cache_dir = os.path.join(tdir, 'cache') | |
| 79 manager.install(cache_dir, cache_name) | |
| 80 with open(os.path.join(cache_dir, file_name), 'wb') as f: | |
| 81 f.write(contents) | |
| 82 manager.uninstall(cache_dir, cache_name) | |
| 83 finally: | |
| 84 file_path.rmtree(tdir) | |
| 85 | |
| 86 | |
| 87 class StorageFake(object): | 74 class StorageFake(object): |
| 88 def __init__(self, files): | 75 def __init__(self, files): |
| 89 self._files = files.copy() | 76 self._files = files.copy() |
| 90 self.namespace = 'default-gzip' | 77 self.namespace = 'default-gzip' |
| 91 self.location = 'http://localhost:1' | 78 self.location = 'http://localhost:1' |
| 92 | 79 |
| 93 def __enter__(self, *_): | 80 def __enter__(self, *_): |
| 94 return self | 81 return self |
| 95 | 82 |
| 96 def __exit__(self, *_): | 83 def __exit__(self, *_): |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 '--named-cache-root', os.path.join(self.tempdir, 'c'), | 526 '--named-cache-root', os.path.join(self.tempdir, 'c'), |
| 540 '--named-cache', 'cache_foo', 'foo', | 527 '--named-cache', 'cache_foo', 'foo', |
| 541 '--named-cache', 'cache_bar', 'bar', | 528 '--named-cache', 'cache_bar', 'bar', |
| 542 'bin/echo${EXECUTABLE_SUFFIX}', | 529 'bin/echo${EXECUTABLE_SUFFIX}', |
| 543 'hello', | 530 'hello', |
| 544 'world', | 531 'world', |
| 545 ] | 532 ] |
| 546 ret = run_isolated.main(cmd) | 533 ret = run_isolated.main(cmd) |
| 547 self.assertEqual(0, ret) | 534 self.assertEqual(0, ret) |
| 548 | 535 |
| 549 for cache_name in ('cache_foo', 'cache_bar'): | 536 for path, cache_name in [('foo', 'cache_foo'), ('bar', 'cache_bar')]: |
| 550 named_path = os.path.join(self.tempdir, 'c', 'named', cache_name) | |
| 551 self.assertTrue(os.path.exists(named_path)) | |
| 552 self.assertEqual( | 537 self.assertEqual( |
| 553 os.path.join(self.tempdir, 'c'), | 538 os.path.abspath(os.readlink( |
| 554 os.path.dirname(os.readlink(named_path)), | 539 os.path.join(self.tempdir, 'ir', path))), |
| 540 os.path.abspath(os.readlink( |
| 541 os.path.join(self.tempdir, 'c', 'named', cache_name))), |
| 555 ) | 542 ) |
| 556 | 543 |
| 557 def test_modified_cwd(self): | 544 def test_modified_cwd(self): |
| 558 isolated = json_dumps({ | 545 isolated = json_dumps({ |
| 559 'command': ['../out/some.exe', 'arg'], | 546 'command': ['../out/some.exe', 'arg'], |
| 560 'relative_cwd': 'some', | 547 'relative_cwd': 'some', |
| 561 }) | 548 }) |
| 562 isolated_hash = isolateserver_mock.hash_content(isolated) | 549 isolated_hash = isolateserver_mock.hash_content(isolated) |
| 563 files = {isolated_hash:isolated} | 550 files = {isolated_hash:isolated} |
| 564 _ = self._run_tha_test(isolated_hash, files) | 551 _ = self._run_tha_test(isolated_hash, files) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 big = small * 1014 | 603 big = small * 1014 |
| 617 small_digest = unicode(ALGO(small).hexdigest()) | 604 small_digest = unicode(ALGO(small).hexdigest()) |
| 618 big_digest = unicode(ALGO(big).hexdigest()) | 605 big_digest = unicode(ALGO(big).hexdigest()) |
| 619 with isolate_cache: | 606 with isolate_cache: |
| 620 fake_time = 1 | 607 fake_time = 1 |
| 621 isolate_cache.write(big_digest, [big]) | 608 isolate_cache.write(big_digest, [big]) |
| 622 fake_time = 2 | 609 fake_time = 2 |
| 623 isolate_cache.write(small_digest, [small]) | 610 isolate_cache.write(small_digest, [small]) |
| 624 with named_cache_manager.open(time_fn=lambda: fake_time): | 611 with named_cache_manager.open(time_fn=lambda: fake_time): |
| 625 fake_time = 1 | 612 fake_time = 1 |
| 626 put_to_named_cache(named_cache_manager, u'first', u'big', big) | 613 p = named_cache_manager.request('first') |
| 614 with open(os.path.join(p, 'big'), 'wb') as f: |
| 615 f.write(big) |
| 627 fake_time = 3 | 616 fake_time = 3 |
| 628 put_to_named_cache(named_cache_manager, u'second', u'small', small) | 617 p = named_cache_manager.request('second') |
| 618 with open(os.path.join(p, 'small'), 'wb') as f: |
| 619 f.write(small) |
| 629 | 620 |
| 630 # Ensures the cache contain the expected data. | 621 # Ensures the cache contain the expected data. |
| 631 actual = genTree(np) | 622 actual = genTree(np) |
| 632 # Figure out the cache path names. | 623 # Figure out the cache path names. |
| 633 cache_small = [ | 624 cache_small = [ |
| 634 os.path.dirname(n) for n in actual if os.path.basename(n) == 'small'][0] | 625 os.path.dirname(n) for n in actual if os.path.basename(n) == 'small'][0] |
| 635 cache_big = [ | 626 cache_big = [ |
| 636 os.path.dirname(n) for n in actual if os.path.basename(n) == 'big'][0] | 627 os.path.dirname(n) for n in actual if os.path.basename(n) == 'big'][0] |
| 637 expected = { | 628 expected = { |
| 638 os.path.join(cache_small, u'small'): small, | 629 os.path.join(cache_small, u'small'): small, |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 995 self.assertEqual(expected, actual) | 986 self.assertEqual(expected, actual) |
| 996 | 987 |
| 997 | 988 |
| 998 if __name__ == '__main__': | 989 if __name__ == '__main__': |
| 999 fix_encoding.fix_encoding() | 990 fix_encoding.fix_encoding() |
| 1000 if '-v' in sys.argv: | 991 if '-v' in sys.argv: |
| 1001 unittest.TestCase.maxDiff = None | 992 unittest.TestCase.maxDiff = None |
| 1002 logging.basicConfig( | 993 logging.basicConfig( |
| 1003 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) | 994 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
| 1004 unittest.main() | 995 unittest.main() |
| OLD | NEW |