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 |
74 class StorageFake(object): | 87 class StorageFake(object): |
75 def __init__(self, files): | 88 def __init__(self, files): |
76 self._files = files.copy() | 89 self._files = files.copy() |
77 self.namespace = 'default-gzip' | 90 self.namespace = 'default-gzip' |
78 self.location = 'http://localhost:1' | 91 self.location = 'http://localhost:1' |
79 | 92 |
80 def __enter__(self, *_): | 93 def __enter__(self, *_): |
81 return self | 94 return self |
82 | 95 |
83 def __exit__(self, *_): | 96 def __exit__(self, *_): |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 '--named-cache-root', os.path.join(self.tempdir, 'c'), | 539 '--named-cache-root', os.path.join(self.tempdir, 'c'), |
527 '--named-cache', 'cache_foo', 'foo', | 540 '--named-cache', 'cache_foo', 'foo', |
528 '--named-cache', 'cache_bar', 'bar', | 541 '--named-cache', 'cache_bar', 'bar', |
529 'bin/echo${EXECUTABLE_SUFFIX}', | 542 'bin/echo${EXECUTABLE_SUFFIX}', |
530 'hello', | 543 'hello', |
531 'world', | 544 'world', |
532 ] | 545 ] |
533 ret = run_isolated.main(cmd) | 546 ret = run_isolated.main(cmd) |
534 self.assertEqual(0, ret) | 547 self.assertEqual(0, ret) |
535 | 548 |
536 for path, cache_name in [('foo', 'cache_foo'), ('bar', 'cache_bar')]: | 549 for cache_name in ('cache_foo', 'cache_bar'): |
| 550 named_path = os.path.join(self.tempdir, 'c', 'named', cache_name) |
| 551 self.assertTrue(os.path.exists(named_path)) |
537 self.assertEqual( | 552 self.assertEqual( |
538 os.path.abspath(os.readlink( | 553 os.path.join(self.tempdir, 'c'), |
539 os.path.join(self.tempdir, 'ir', path))), | 554 os.path.dirname(os.readlink(named_path)), |
540 os.path.abspath(os.readlink( | |
541 os.path.join(self.tempdir, 'c', 'named', cache_name))), | |
542 ) | 555 ) |
543 | 556 |
544 def test_modified_cwd(self): | 557 def test_modified_cwd(self): |
545 isolated = json_dumps({ | 558 isolated = json_dumps({ |
546 'command': ['../out/some.exe', 'arg'], | 559 'command': ['../out/some.exe', 'arg'], |
547 'relative_cwd': 'some', | 560 'relative_cwd': 'some', |
548 }) | 561 }) |
549 isolated_hash = isolateserver_mock.hash_content(isolated) | 562 isolated_hash = isolateserver_mock.hash_content(isolated) |
550 files = {isolated_hash:isolated} | 563 files = {isolated_hash:isolated} |
551 _ = self._run_tha_test(isolated_hash, files) | 564 _ = self._run_tha_test(isolated_hash, files) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 big = small * 1014 | 616 big = small * 1014 |
604 small_digest = unicode(ALGO(small).hexdigest()) | 617 small_digest = unicode(ALGO(small).hexdigest()) |
605 big_digest = unicode(ALGO(big).hexdigest()) | 618 big_digest = unicode(ALGO(big).hexdigest()) |
606 with isolate_cache: | 619 with isolate_cache: |
607 fake_time = 1 | 620 fake_time = 1 |
608 isolate_cache.write(big_digest, [big]) | 621 isolate_cache.write(big_digest, [big]) |
609 fake_time = 2 | 622 fake_time = 2 |
610 isolate_cache.write(small_digest, [small]) | 623 isolate_cache.write(small_digest, [small]) |
611 with named_cache_manager.open(time_fn=lambda: fake_time): | 624 with named_cache_manager.open(time_fn=lambda: fake_time): |
612 fake_time = 1 | 625 fake_time = 1 |
613 p = named_cache_manager.request('first') | 626 put_to_named_cache(named_cache_manager, u'first', u'big', big) |
614 with open(os.path.join(p, 'big'), 'wb') as f: | |
615 f.write(big) | |
616 fake_time = 3 | 627 fake_time = 3 |
617 p = named_cache_manager.request('second') | 628 put_to_named_cache(named_cache_manager, u'second', u'small', small) |
618 with open(os.path.join(p, 'small'), 'wb') as f: | |
619 f.write(small) | |
620 | 629 |
621 # Ensures the cache contain the expected data. | 630 # Ensures the cache contain the expected data. |
622 actual = genTree(np) | 631 actual = genTree(np) |
623 # Figure out the cache path names. | 632 # Figure out the cache path names. |
624 cache_small = [ | 633 cache_small = [ |
625 os.path.dirname(n) for n in actual if os.path.basename(n) == 'small'][0] | 634 os.path.dirname(n) for n in actual if os.path.basename(n) == 'small'][0] |
626 cache_big = [ | 635 cache_big = [ |
627 os.path.dirname(n) for n in actual if os.path.basename(n) == 'big'][0] | 636 os.path.dirname(n) for n in actual if os.path.basename(n) == 'big'][0] |
628 expected = { | 637 expected = { |
629 os.path.join(cache_small, u'small'): small, | 638 os.path.join(cache_small, u'small'): small, |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
986 self.assertEqual(expected, actual) | 995 self.assertEqual(expected, actual) |
987 | 996 |
988 | 997 |
989 if __name__ == '__main__': | 998 if __name__ == '__main__': |
990 fix_encoding.fix_encoding() | 999 fix_encoding.fix_encoding() |
991 if '-v' in sys.argv: | 1000 if '-v' in sys.argv: |
992 unittest.TestCase.maxDiff = None | 1001 unittest.TestCase.maxDiff = None |
993 logging.basicConfig( | 1002 logging.basicConfig( |
994 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) | 1003 level=logging.DEBUG if '-v' in sys.argv else logging.ERROR) |
995 unittest.main() | 1004 unittest.main() |
OLD | NEW |