Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: client/tests/isolateserver_test.py

Issue 2060983006: luci-py/isolateserver.py: Add archive support when downloading. (Closed) Base URL: https://github.com/luci/luci-py.git@master
Patch Set: Small fixes. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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=W0212,W0223,W0231,W0613 6 # pylint: disable=W0212,W0223,W0231,W0613
7 7
8 import base64 8 import base64
9 import collections 9 import collections
10 import hashlib 10 import hashlib
(...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 queue.add(item.digest) 717 queue.add(item.digest)
718 718
719 # Wait for fetch to complete. 719 # Wait for fetch to complete.
720 while pending: 720 while pending:
721 fetched = queue.wait(pending) 721 fetched = queue.wait(pending)
722 pending.discard(fetched) 722 pending.discard(fetched)
723 723
724 # Ensure fetched same data as was pushed. 724 # Ensure fetched same data as was pushed.
725 self.assertEqual( 725 self.assertEqual(
726 [i.buffer for i in items], 726 [i.buffer for i in items],
727 [cache.read(i.digest) for i in items]) 727 [cache.getfileobj(i.digest).read() for i in items])
M-A Ruel 2016/06/21 13:31:09 please close everywhere
mithro 2016/06/22 12:03:11 Done.
728 728
729 def test_push_and_fetch(self): 729 def test_push_and_fetch(self):
730 self.run_push_and_fetch_test('default') 730 self.run_push_and_fetch_test('default')
731 731
732 def test_push_and_fetch_gzip(self): 732 def test_push_and_fetch_gzip(self):
733 self.run_push_and_fetch_test('default-gzip') 733 self.run_push_and_fetch_test('default-gzip')
734 734
735 if sys.maxsize == (2**31) - 1: 735 if sys.maxsize == (2**31) - 1:
736 def test_archive_multiple_huge_file(self): 736 def test_archive_multiple_huge_file(self):
737 self.server.discard_content() 737 self.server.discard_content()
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 '--file', 'sha-1', 'path/to/a', 831 '--file', 'sha-1', 'path/to/a',
832 '--file', 'sha-2', 'path/to/b', 832 '--file', 'sha-2', 'path/to/b',
833 ] 833 ]
834 self.assertEqual(0, isolateserver.main(cmd)) 834 self.assertEqual(0, isolateserver.main(cmd))
835 expected = { 835 expected = {
836 os.path.join(net_utils.ROOT_DIR, 'path/to/a'): 'Coucou', 836 os.path.join(net_utils.ROOT_DIR, 'path/to/a'): 'Coucou',
837 os.path.join(net_utils.ROOT_DIR, 'path/to/b'): 'Bye Bye', 837 os.path.join(net_utils.ROOT_DIR, 'path/to/b'): 'Bye Bye',
838 } 838 }
839 self.assertEqual(expected, actual) 839 self.assertEqual(expected, actual)
840 840
841 def test_download_isolated(self): 841 def test_download_isolated_simple(self):
842 # Test downloading an isolated tree. 842 # Test downloading an isolated tree.
843 actual = {} 843 actual = {}
844 def file_write_mock(key, generator): 844 def putfile_mock(srcfileobj, dstpath, file_mode=None):
845 actual[key] = ''.join(generator) 845 actual[dstpath] = srcfileobj.read()
846 self.mock(isolateserver, 'file_write', file_write_mock) 846 self.mock(isolateserver, 'putfile', putfile_mock)
847 self.mock(os, 'makedirs', lambda _: None) 847 self.mock(os, 'makedirs', lambda _: None)
848 server = 'http://example.com' 848 server = 'http://example.com'
849 files = { 849 files = {
850 os.path.join('a', 'foo'): 'Content', 850 os.path.join('a', 'foo'): 'Content',
851 'b': 'More content', 851 'b': 'More content',
852 } 852 }
853 isolated = { 853 isolated = {
854 'command': ['Absurb', 'command'], 854 'command': ['Absurb', 'command'],
855 'relative_cwd': 'a', 855 'relative_cwd': 'a',
856 'files': dict( 856 'files': dict(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 self.expected_requests(requests) 889 self.expected_requests(requests)
890 self.assertEqual(0, isolateserver.main(cmd)) 890 self.assertEqual(0, isolateserver.main(cmd))
891 expected = dict( 891 expected = dict(
892 (os.path.join(self.tempdir, k), v) for k, v in files.iteritems()) 892 (os.path.join(self.tempdir, k), v) for k, v in files.iteritems())
893 self.assertEqual(expected, actual) 893 self.assertEqual(expected, actual)
894 expected_stdout = ( 894 expected_stdout = (
895 'To run this test please run from the directory %s:\n Absurb command\n' 895 'To run this test please run from the directory %s:\n Absurb command\n'
896 % os.path.join(self.tempdir, 'a')) 896 % os.path.join(self.tempdir, 'a'))
897 self.checkOutput(expected_stdout, '') 897 self.checkOutput(expected_stdout, '')
898 898
899 def test_download_isolated_archive(self):
900 # Test downloading an isolated tree.
901 actual = {}
902 def putfile_mock(srcfileobj, dstpath, file_mode=None, limit=-1):
903 actual[dstpath] = srcfileobj.read(limit)
904 self.mock(isolateserver, 'putfile', putfile_mock)
905 self.mock(os, 'makedirs', lambda _: None)
906 server = 'http://example.com'
907 files = {
908 os.path.join('a', 'foo'): 'Content',
909 'b': 'More content',
910 }
911
912 archive = (
913 # ar file header
914 '!<arch>\n'
915 # File 1 -------------------------
916 # (16 bytes) filename len
917 '#1/5 '
918 # file metadata
919 '1447140471 1000 1000 100640 '
920 # (10 bytes) Data size
921 '12 '
922 # (2 bytes) File magic
923 '\x60\n'
924 # (5 bytes) File name
925 'a/foo'
926 # (7 bytes) File data
927 'Content'
928 # File 2 -------------------------
929 # (16 bytes) filename
930 'b '
931 # file metadata
932 '1447140471 1000 1000 100640 '
933 # (12 bytes) Data size
934 '12 '
935 # (2 bytes) File magic
936 '\x60\n'
937 # (12 bytes) File data
938 'More content'
939 '')
940
941 isolated = {
942 'command': ['Absurb', 'command'],
943 'relative_cwd': 'a',
944 'files': {
945 'archive1': {
946 'h': isolateserver_mock.hash_content(archive),
947 's': len(archive),
948 'a': 'ar',
949 },
950 },
951 'version': isolated_format.ISOLATED_FILE_VERSION,
952 }
953 isolated_data = json.dumps(isolated, sort_keys=True, separators=(',',':'))
954 isolated_hash = isolateserver_mock.hash_content(isolated_data)
955 requests = [(isolated['files']['archive1']['h'], archive)]
956 requests.append((isolated_hash, isolated_data))
957 requests = [
958 (
959 server + '/_ah/api/isolateservice/v1/retrieve',
960 {
961 'data': {
962 'digest': h.encode('utf-8'),
963 'namespace': {
964 'namespace': 'default-gzip',
965 'digest_hash': 'sha-1',
966 'compression': 'flate',
967 },
968 'offset': 0,
969 },
970 'read_timeout': 60,
971 },
972 {'content': base64.b64encode(zlib.compress(v))},
973 ) for h, v in requests
974 ]
975 cmd = [
976 'download',
977 '--isolate-server', server,
978 '--target', self.tempdir,
979 '--isolated', isolated_hash,
980 ]
981 self.expected_requests(requests)
982 self.assertEqual(0, isolateserver.main(cmd))
983 expected = dict(
984 (os.path.join(self.tempdir, k), v) for k, v in files.iteritems())
985 self.assertEqual(expected, actual)
986 expected_stdout = (
987 'To run this test please run from the directory %s:\n Absurb command\n'
988 % os.path.join(self.tempdir, 'a'))
989 self.checkOutput(expected_stdout, '')
990
899 991
992
900 def get_storage(_isolate_server, namespace): 993 def get_storage(_isolate_server, namespace):
901 class StorageFake(object): 994 class StorageFake(object):
902 def __enter__(self, *_): 995 def __enter__(self, *_):
903 return self 996 return self
904 997
905 def __exit__(self, *_): 998 def __exit__(self, *_):
906 pass 999 pass
907 1000
908 @property 1001 @property
909 def hash_algo(self): # pylint: disable=R0201 1002 def hash_algo(self): # pylint: disable=R0201
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1081 1174
1082 1175
1083 if __name__ == '__main__': 1176 if __name__ == '__main__':
1084 fix_encoding.fix_encoding() 1177 fix_encoding.fix_encoding()
1085 if '-v' in sys.argv: 1178 if '-v' in sys.argv:
1086 unittest.TestCase.maxDiff = None 1179 unittest.TestCase.maxDiff = None
1087 logging.basicConfig( 1180 logging.basicConfig(
1088 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL)) 1181 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL))
1089 clear_env_vars() 1182 clear_env_vars()
1090 unittest.main() 1183 unittest.main()
OLDNEW
« client/isolateserver.py ('K') | « client/run_isolated.py ('k') | client/utils/fs.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698