Chromium Code Reviews| 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 maxDiff = None | |
|
M-A Ruel
2016/11/08 14:33:44
remove
mithro
2016/11/09 00:19:35
Sorry, left over from debugging.
| |
| 1137 | |
| 1138 def test_download_isolated_tar_archive(self): | |
| 1139 # Test downloading an isolated tree. | |
| 1140 actual = {} | |
| 1141 def putfile_mock( | |
| 1142 srcfileobj, dstpath, file_mode=None, size=-1, use_symlink=False): | |
| 1143 actual[dstpath] = srcfileobj.read(size) | |
| 1144 self.mock(isolateserver, 'putfile', putfile_mock) | |
| 1145 self.mock(os, 'makedirs', lambda _: None) | |
| 1146 server = 'http://example.com' | |
| 1147 | |
| 1148 files = { | |
| 1149 os.path.join('a', 'foo'): 'Content', | |
| 1150 'b': 'More content', | |
| 1151 'c': 'Even more content!', | |
| 1152 } | |
| 1153 | |
| 1154 # Generate a tar archive | |
| 1155 tf = io.BytesIO() | |
| 1156 with tarfile.TarFile(mode='w', fileobj=tf) as tar: | |
| 1157 f1 = tarfile.TarInfo() | |
|
M-A Ruel
2016/11/08 14:33:44
indent at +2
mithro
2016/11/09 00:19:35
Done.
| |
| 1158 f1.type = tarfile.REGTYPE | |
| 1159 f1.name = 'a/foo' | |
| 1160 f1.size = 7 | |
| 1161 tar.addfile(f1, io.BytesIO('Content')) | |
| 1162 | |
| 1163 f2 = tarfile.TarInfo() | |
| 1164 f2.type = tarfile.REGTYPE | |
| 1165 f2.name = 'b' | |
| 1166 f2.size = 12 | |
| 1167 tar.addfile(f2, io.BytesIO('More content')) | |
| 1168 archive = tf.getvalue() | |
| 1169 | |
| 1170 isolated = { | |
| 1171 'command': ['Absurb', 'command'], | |
| 1172 'relative_cwd': 'a', | |
| 1173 'files': { | |
| 1174 'archive1': { | |
| 1175 'h': isolateserver_mock.hash_content(archive), | |
| 1176 's': len(archive), | |
| 1177 't': 'tar', | |
| 1178 }, | |
| 1179 'c': { | |
| 1180 'h': isolateserver_mock.hash_content(files['c']), | |
| 1181 's': len(files['c']), | |
| 1182 }, | |
| 1183 }, | |
| 1184 'version': isolated_format.ISOLATED_FILE_VERSION, | |
| 1185 } | |
| 1186 isolated_data = json.dumps(isolated, sort_keys=True, separators=(',',':')) | |
| 1187 isolated_hash = isolateserver_mock.hash_content(isolated_data) | |
| 1188 requests = [ | |
| 1189 (isolated['files']['archive1']['h'], archive), | |
| 1190 (isolated['files']['c']['h'], files['c']), | |
| 1191 ] | |
| 1192 requests.append((isolated_hash, isolated_data)) | |
| 1193 requests = [ | |
| 1194 ( | |
| 1195 server + '/api/isolateservice/v1/retrieve', | |
| 1196 { | |
| 1197 'data': { | |
| 1198 'digest': h.encode('utf-8'), | |
| 1199 'namespace': { | |
| 1200 'namespace': 'default-gzip', | |
| 1201 'digest_hash': 'sha-1', | |
| 1202 'compression': 'flate', | |
| 1203 }, | |
| 1204 'offset': 0, | |
| 1205 }, | |
| 1206 'read_timeout': 60, | |
| 1207 }, | |
| 1208 {'content': base64.b64encode(zlib.compress(v))}, | |
| 1209 ) for h, v in requests | |
| 1210 ] | |
| 1211 cmd = [ | |
| 1212 'download', | |
| 1213 '--isolate-server', server, | |
| 1214 '--target', self.tempdir, | |
| 1215 '--isolated', isolated_hash, | |
| 1216 ] | |
| 1217 self.expected_requests(requests) | |
| 1218 self.assertEqual(0, isolateserver.main(cmd)) | |
| 1219 expected = dict( | |
| 1220 (os.path.join(self.tempdir, k), v) for k, v in files.iteritems()) | |
| 1221 self.assertEqual(expected, actual) | |
| 1222 expected_stdout = ( | |
| 1223 'To run this test please run from the directory %s:\n Absurb command\n' | |
| 1224 % os.path.join(self.tempdir, 'a')) | |
| 1225 self.checkOutput(expected_stdout, '') | |
| 1226 | |
|
M-A Ruel
2016/11/08 14:33:44
only 2 lines between file level symbols
mithro
2016/11/09 00:19:35
Done.
| |
| 1135 | 1227 |
| 1136 | 1228 |
| 1137 def get_storage(_isolate_server, namespace): | 1229 def get_storage(_isolate_server, namespace): |
| 1138 class StorageFake(object): | 1230 class StorageFake(object): |
| 1139 def __enter__(self, *_): | 1231 def __enter__(self, *_): |
| 1140 return self | 1232 return self |
| 1141 | 1233 |
| 1142 def __exit__(self, *_): | 1234 def __exit__(self, *_): |
| 1143 pass | 1235 pass |
| 1144 | 1236 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1363 | 1455 |
| 1364 | 1456 |
| 1365 if __name__ == '__main__': | 1457 if __name__ == '__main__': |
| 1366 fix_encoding.fix_encoding() | 1458 fix_encoding.fix_encoding() |
| 1367 if '-v' in sys.argv: | 1459 if '-v' in sys.argv: |
| 1368 unittest.TestCase.maxDiff = None | 1460 unittest.TestCase.maxDiff = None |
| 1369 logging.basicConfig( | 1461 logging.basicConfig( |
| 1370 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL)) | 1462 level=(logging.DEBUG if '-v' in sys.argv else logging.CRITICAL)) |
| 1371 clear_env_vars() | 1463 clear_env_vars() |
| 1372 unittest.main() | 1464 unittest.main() |
| OLD | NEW |