| 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=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 |
| 11 import json | 11 import json |
| 12 import logging | 12 import logging |
| 13 import io | 13 import io |
| 14 import os | 14 import os |
| 15 import StringIO | 15 import StringIO |
| 16 import sys | 16 import sys |
| 17 import tarfile |
| 17 import tempfile | 18 import tempfile |
| 18 import unittest | 19 import unittest |
| 19 import urllib | 20 import urllib |
| 20 import zlib | 21 import zlib |
| 21 | 22 |
| 22 # net_utils adjusts sys.path. | 23 # net_utils adjusts sys.path. |
| 23 import net_utils | 24 import net_utils |
| 24 | 25 |
| 25 import auth | 26 import auth |
| 26 import isolated_format | 27 import isolated_format |
| (...skipping 996 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1023 self.expected_requests(requests) | 1024 self.expected_requests(requests) |
| 1024 self.assertEqual(0, isolateserver.main(cmd)) | 1025 self.assertEqual(0, isolateserver.main(cmd)) |
| 1025 expected = dict( | 1026 expected = dict( |
| 1026 (os.path.join(self.tempdir, k), v) for k, v in files.iteritems()) | 1027 (os.path.join(self.tempdir, k), v) for k, v in files.iteritems()) |
| 1027 self.assertEqual(expected, actual) | 1028 self.assertEqual(expected, actual) |
| 1028 expected_stdout = ( | 1029 expected_stdout = ( |
| 1029 'To run this test please run from the directory %s:\n Absurb command\n' | 1030 'To run this test please run from the directory %s:\n Absurb command\n' |
| 1030 % os.path.join(self.tempdir, 'a')) | 1031 % os.path.join(self.tempdir, 'a')) |
| 1031 self.checkOutput(expected_stdout, '') | 1032 self.checkOutput(expected_stdout, '') |
| 1032 | 1033 |
| 1033 def test_download_isolated_archive(self): | 1034 def test_download_isolated_ar_archive(self): |
| 1034 # Test downloading an isolated tree. | 1035 # Test downloading an isolated tree. |
| 1035 actual = {} | 1036 actual = {} |
| 1036 def putfile_mock( | 1037 def putfile_mock( |
| 1037 srcfileobj, dstpath, file_mode=None, size=-1, use_symlink=False): | 1038 srcfileobj, dstpath, file_mode=None, size=-1, use_symlink=False): |
| 1038 actual[dstpath] = srcfileobj.read(size) | 1039 actual[dstpath] = srcfileobj.read(size) |
| 1039 self.mock(isolateserver, 'putfile', putfile_mock) | 1040 self.mock(isolateserver, 'putfile', putfile_mock) |
| 1040 self.mock(os, 'makedirs', lambda _: None) | 1041 self.mock(os, 'makedirs', lambda _: None) |
| 1041 server = 'http://example.com' | 1042 server = 'http://example.com' |
| 1042 | 1043 |
| 1043 files = { | 1044 files = { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1125 self.expected_requests(requests) | 1126 self.expected_requests(requests) |
| 1126 self.assertEqual(0, isolateserver.main(cmd)) | 1127 self.assertEqual(0, isolateserver.main(cmd)) |
| 1127 expected = dict( | 1128 expected = dict( |
| 1128 (os.path.join(self.tempdir, k), v) for k, v in files.iteritems()) | 1129 (os.path.join(self.tempdir, k), v) for k, v in files.iteritems()) |
| 1129 self.assertEqual(expected, actual) | 1130 self.assertEqual(expected, actual) |
| 1130 expected_stdout = ( | 1131 expected_stdout = ( |
| 1131 'To run this test please run from the directory %s:\n Absurb command\n' | 1132 'To run this test please run from the directory %s:\n Absurb command\n' |
| 1132 % os.path.join(self.tempdir, 'a')) | 1133 % os.path.join(self.tempdir, 'a')) |
| 1133 self.checkOutput(expected_stdout, '') | 1134 self.checkOutput(expected_stdout, '') |
| 1134 | 1135 |
| 1136 def test_download_isolated_tar_archive(self): |
| 1137 # Test downloading an isolated tree. |
| 1138 actual = {} |
| 1139 def putfile_mock( |
| 1140 srcfileobj, dstpath, file_mode=None, size=-1, use_symlink=False): |
| 1141 actual[dstpath] = srcfileobj.read(size) |
| 1142 self.mock(isolateserver, 'putfile', putfile_mock) |
| 1143 self.mock(os, 'makedirs', lambda _: None) |
| 1144 server = 'http://example.com' |
| 1145 |
| 1146 files = { |
| 1147 os.path.join('a', 'foo'): 'Content', |
| 1148 'b': 'More content', |
| 1149 'c': 'Even more content!', |
| 1150 } |
| 1151 |
| 1152 # Generate a tar archive |
| 1153 tf = io.BytesIO() |
| 1154 with tarfile.TarFile(mode='w', fileobj=tf) as tar: |
| 1155 f1 = tarfile.TarInfo() |
| 1156 f1.type = tarfile.REGTYPE |
| 1157 f1.name = 'a/foo' |
| 1158 f1.size = 7 |
| 1159 tar.addfile(f1, io.BytesIO('Content')) |
| 1160 |
| 1161 f2 = tarfile.TarInfo() |
| 1162 f2.type = tarfile.REGTYPE |
| 1163 f2.name = 'b' |
| 1164 f2.size = 12 |
| 1165 tar.addfile(f2, io.BytesIO('More content')) |
| 1166 archive = tf.getvalue() |
| 1167 |
| 1168 isolated = { |
| 1169 'command': ['Absurb', 'command'], |
| 1170 'relative_cwd': 'a', |
| 1171 'files': { |
| 1172 'archive1': { |
| 1173 'h': isolateserver_mock.hash_content(archive), |
| 1174 's': len(archive), |
| 1175 't': 'tar', |
| 1176 }, |
| 1177 'c': { |
| 1178 'h': isolateserver_mock.hash_content(files['c']), |
| 1179 's': len(files['c']), |
| 1180 }, |
| 1181 }, |
| 1182 'version': isolated_format.ISOLATED_FILE_VERSION, |
| 1183 } |
| 1184 isolated_data = json.dumps(isolated, sort_keys=True, separators=(',',':')) |
| 1185 isolated_hash = isolateserver_mock.hash_content(isolated_data) |
| 1186 requests = [ |
| 1187 (isolated['files']['archive1']['h'], archive), |
| 1188 (isolated['files']['c']['h'], files['c']), |
| 1189 ] |
| 1190 requests.append((isolated_hash, isolated_data)) |
| 1191 requests = [ |
| 1192 ( |
| 1193 server + '/api/isolateservice/v1/retrieve', |
| 1194 { |
| 1195 'data': { |
| 1196 'digest': h.encode('utf-8'), |
| 1197 'namespace': { |
| 1198 'namespace': 'default-gzip', |
| 1199 'digest_hash': 'sha-1', |
| 1200 'compression': 'flate', |
| 1201 }, |
| 1202 'offset': 0, |
| 1203 }, |
| 1204 'read_timeout': 60, |
| 1205 }, |
| 1206 {'content': base64.b64encode(zlib.compress(v))}, |
| 1207 ) for h, v in requests |
| 1208 ] |
| 1209 cmd = [ |
| 1210 'download', |
| 1211 '--isolate-server', server, |
| 1212 '--target', self.tempdir, |
| 1213 '--isolated', isolated_hash, |
| 1214 ] |
| 1215 self.expected_requests(requests) |
| 1216 self.assertEqual(0, isolateserver.main(cmd)) |
| 1217 expected = dict( |
| 1218 (os.path.join(self.tempdir, k), v) for k, v in files.iteritems()) |
| 1219 self.assertEqual(expected, actual) |
| 1220 expected_stdout = ( |
| 1221 'To run this test please run from the directory %s:\n Absurb command\n' |
| 1222 % os.path.join(self.tempdir, 'a')) |
| 1223 self.checkOutput(expected_stdout, '') |
| 1224 |
| 1135 | 1225 |
| 1136 | |
| 1137 def get_storage(_isolate_server, namespace): | 1226 def get_storage(_isolate_server, namespace): |
| 1138 class StorageFake(object): | 1227 class StorageFake(object): |
| 1139 def __enter__(self, *_): | 1228 def __enter__(self, *_): |
| 1140 return self | 1229 return self |
| 1141 | 1230 |
| 1142 def __exit__(self, *_): | 1231 def __exit__(self, *_): |
| 1143 pass | 1232 pass |
| 1144 | 1233 |
| 1145 @property | 1234 @property |
| 1146 def hash_algo(self): # pylint: disable=R0201 | 1235 def hash_algo(self): # pylint: disable=R0201 |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1363 | 1452 |
| 1364 | 1453 |
| 1365 if __name__ == '__main__': | 1454 if __name__ == '__main__': |
| 1366 fix_encoding.fix_encoding() | 1455 fix_encoding.fix_encoding() |
| 1367 if '-v' in sys.argv: | 1456 if '-v' in sys.argv: |
| 1368 unittest.TestCase.maxDiff = None | 1457 unittest.TestCase.maxDiff = None |
| 1369 logging.basicConfig( | 1458 logging.basicConfig( |
| 1370 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL)) | 1459 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL)) |
| 1371 clear_env_vars() | 1460 clear_env_vars() |
| 1372 unittest.main() | 1461 unittest.main() |
| OLD | NEW |