-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_transfer.py
More file actions
1887 lines (1622 loc) · 74 KB
/
test_transfer.py
File metadata and controls
1887 lines (1622 loc) · 74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pylint: disable=C0302
import unittest2
class Test__Transfer(unittest2.TestCase):
URL = 'http://example.com/api'
def _getTargetClass(self):
from gcloud.streaming.transfer import _Transfer
return _Transfer
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_ctor_defaults(self):
from gcloud.streaming.transfer import _DEFAULT_CHUNKSIZE
stream = _Stream()
xfer = self._makeOne(stream)
self.assertTrue(xfer.stream is stream)
self.assertFalse(xfer.close_stream)
self.assertEqual(xfer.chunksize, _DEFAULT_CHUNKSIZE)
self.assertTrue(xfer.auto_transfer)
self.assertTrue(xfer.bytes_http is None)
self.assertTrue(xfer.http is None)
self.assertEqual(xfer.num_retries, 5)
self.assertTrue(xfer.url is None)
self.assertFalse(xfer.initialized)
def test_ctor_explicit(self):
stream = _Stream()
HTTP = object()
CHUNK_SIZE = 1 << 18
NUM_RETRIES = 8
xfer = self._makeOne(stream,
close_stream=True,
chunksize=CHUNK_SIZE,
auto_transfer=False,
http=HTTP,
num_retries=NUM_RETRIES)
self.assertTrue(xfer.stream is stream)
self.assertTrue(xfer.close_stream)
self.assertEqual(xfer.chunksize, CHUNK_SIZE)
self.assertFalse(xfer.auto_transfer)
self.assertTrue(xfer.bytes_http is HTTP)
self.assertTrue(xfer.http is HTTP)
self.assertEqual(xfer.num_retries, NUM_RETRIES)
def test_bytes_http_fallback_to_http(self):
stream = _Stream()
HTTP = object()
xfer = self._makeOne(stream, http=HTTP)
self.assertTrue(xfer.bytes_http is HTTP)
def test_bytes_http_setter(self):
stream = _Stream()
HTTP = object()
BYTES_HTTP = object()
xfer = self._makeOne(stream, http=HTTP)
xfer.bytes_http = BYTES_HTTP
self.assertTrue(xfer.bytes_http is BYTES_HTTP)
def test_num_retries_setter_invalid(self):
stream = _Stream()
xfer = self._makeOne(stream)
with self.assertRaises(ValueError):
xfer.num_retries = object()
def test_num_retries_setter_negative(self):
stream = _Stream()
xfer = self._makeOne(stream)
with self.assertRaises(ValueError):
xfer.num_retries = -1
def test__initialize_not_already_initialized_w_http(self):
HTTP = object()
stream = _Stream()
xfer = self._makeOne(stream)
xfer._initialize(HTTP, self.URL)
self.assertTrue(xfer.initialized)
self.assertTrue(xfer.http is HTTP)
self.assertTrue(xfer.url is self.URL)
def test__initialize_not_already_initialized_wo_http(self):
from httplib2 import Http
stream = _Stream()
xfer = self._makeOne(stream)
xfer._initialize(None, self.URL)
self.assertTrue(xfer.initialized)
self.assertTrue(isinstance(xfer.http, Http))
self.assertTrue(xfer.url is self.URL)
def test__initialize_w_existing_http(self):
HTTP_1, HTTP_2 = object(), object()
stream = _Stream()
xfer = self._makeOne(stream, http=HTTP_1)
xfer._initialize(HTTP_2, self.URL)
self.assertTrue(xfer.initialized)
self.assertTrue(xfer.http is HTTP_1)
self.assertTrue(xfer.url is self.URL)
def test__initialize_already_initialized(self):
from gcloud.streaming.exceptions import TransferInvalidError
URL_2 = 'http://example.com/other'
HTTP_1, HTTP_2 = object(), object()
stream = _Stream()
xfer = self._makeOne(stream)
xfer._initialize(HTTP_1, self.URL)
with self.assertRaises(TransferInvalidError):
xfer._initialize(HTTP_2, URL_2)
def test__ensure_initialized_hit(self):
HTTP = object()
stream = _Stream()
xfer = self._makeOne(stream)
xfer._initialize(HTTP, self.URL)
xfer._ensure_initialized() # no raise
def test__ensure_initialized_miss(self):
from gcloud.streaming.exceptions import TransferInvalidError
stream = _Stream()
xfer = self._makeOne(stream)
with self.assertRaises(TransferInvalidError):
xfer._ensure_initialized()
def test__ensure_uninitialized_hit(self):
stream = _Stream()
xfer = self._makeOne(stream)
xfer._ensure_uninitialized() # no raise
def test__ensure_uninitialized_miss(self):
from gcloud.streaming.exceptions import TransferInvalidError
stream = _Stream()
HTTP = object()
xfer = self._makeOne(stream)
xfer._initialize(HTTP, self.URL)
with self.assertRaises(TransferInvalidError):
xfer._ensure_uninitialized()
def test___del___closes_stream(self):
stream = _Stream()
xfer = self._makeOne(stream, close_stream=True)
self.assertFalse(stream._closed)
del xfer
self.assertTrue(stream._closed)
class Test_Download(unittest2.TestCase):
URL = "http://example.com/api"
def _getTargetClass(self):
from gcloud.streaming.transfer import Download
return Download
def _makeOne(self, *args, **kw):
return self._getTargetClass()(*args, **kw)
def test_ctor_defaults(self):
stream = _Stream()
download = self._makeOne(stream)
self.assertTrue(download.stream is stream)
self.assertTrue(download._initial_response is None)
self.assertEqual(download.progress, 0)
self.assertTrue(download.total_size is None)
self.assertTrue(download.encoding is None)
def test_ctor_w_kwds(self):
stream = _Stream()
CHUNK_SIZE = 123
download = self._makeOne(stream, chunksize=CHUNK_SIZE)
self.assertTrue(download.stream is stream)
self.assertEqual(download.chunksize, CHUNK_SIZE)
def test_ctor_w_total_size(self):
stream = _Stream()
SIZE = 123
download = self._makeOne(stream, total_size=SIZE)
self.assertTrue(download.stream is stream)
self.assertEqual(download.total_size, SIZE)
def test_from_file_w_existing_file_no_override(self):
import os
klass = self._getTargetClass()
with _tempdir() as tempdir:
filename = os.path.join(tempdir, 'file.out')
with open(filename, 'w') as fileobj:
fileobj.write('EXISTING FILE')
with self.assertRaises(ValueError):
klass.from_file(filename)
def test_from_file_w_existing_file_w_override_wo_auto_transfer(self):
import os
klass = self._getTargetClass()
with _tempdir() as tempdir:
filename = os.path.join(tempdir, 'file.out')
with open(filename, 'w') as fileobj:
fileobj.write('EXISTING FILE')
download = klass.from_file(filename, overwrite=True,
auto_transfer=False)
self.assertFalse(download.auto_transfer)
del download # closes stream
with open(filename, 'rb') as fileobj:
self.assertEqual(fileobj.read(), b'')
def test_from_stream_defaults(self):
stream = _Stream()
klass = self._getTargetClass()
download = klass.from_stream(stream)
self.assertTrue(download.stream is stream)
self.assertTrue(download.auto_transfer)
self.assertTrue(download.total_size is None)
def test_from_stream_explicit(self):
CHUNK_SIZE = 1 << 18
SIZE = 123
stream = _Stream()
klass = self._getTargetClass()
download = klass.from_stream(stream, auto_transfer=False,
total_size=SIZE, chunksize=CHUNK_SIZE)
self.assertTrue(download.stream is stream)
self.assertFalse(download.auto_transfer)
self.assertEqual(download.total_size, SIZE)
self.assertEqual(download.chunksize, CHUNK_SIZE)
def test_configure_request(self):
CHUNK_SIZE = 100
download = self._makeOne(_Stream(), chunksize=CHUNK_SIZE)
request = _Dummy(headers={})
url_builder = _Dummy(query_params={})
download.configure_request(request, url_builder)
self.assertEqual(request.headers, {'Range': 'bytes=0-99'})
self.assertEqual(url_builder.query_params, {'alt': 'media'})
def test__set_total_wo_content_range_wo_existing_total(self):
info = {}
download = self._makeOne(_Stream())
download._set_total(info)
self.assertEqual(download.total_size, 0)
def test__set_total_wo_content_range_w_existing_total(self):
SIZE = 123
info = {}
download = self._makeOne(_Stream(), total_size=SIZE)
download._set_total(info)
self.assertEqual(download.total_size, SIZE)
def test__set_total_w_content_range_w_existing_total(self):
SIZE = 123
info = {'content-range': 'bytes 123-234/4567'}
download = self._makeOne(_Stream(), total_size=SIZE)
download._set_total(info)
self.assertEqual(download.total_size, 4567)
def test__set_total_w_content_range_w_asterisk_total(self):
info = {'content-range': 'bytes 123-234/*'}
download = self._makeOne(_Stream())
download._set_total(info)
self.assertEqual(download.total_size, 0)
def test_initialize_download_already_initialized(self):
from gcloud.streaming.exceptions import TransferInvalidError
request = _Request()
download = self._makeOne(_Stream())
download._initialize(None, self.URL)
with self.assertRaises(TransferInvalidError):
download.initialize_download(request, http=object())
def test_initialize_download_wo_autotransfer(self):
request = _Request()
http = object()
download = self._makeOne(_Stream(), auto_transfer=False)
download.initialize_download(request, http)
self.assertTrue(download.http is http)
self.assertEqual(download.url, request.url)
def test_initialize_download_w_autotransfer_failing(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
from gcloud.streaming.exceptions import HttpError
request = _Request()
http = object()
download = self._makeOne(_Stream(), auto_transfer=True)
response = _makeResponse(http_client.BAD_REQUEST)
requester = _MakeRequest(response)
with _Monkey(MUT, make_api_request=requester):
with self.assertRaises(HttpError):
download.initialize_download(request, http)
self.assertTrue(len(requester._requested), 1)
self.assertTrue(requester._requested[0][0] is request)
def test_initialize_download_w_autotransfer_w_content_location(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
REDIRECT_URL = 'http://example.com/other'
request = _Request()
http = object()
info = {'content-location': REDIRECT_URL}
download = self._makeOne(_Stream(), auto_transfer=True)
response = _makeResponse(http_client.NO_CONTENT, info)
requester = _MakeRequest(response)
with _Monkey(MUT, make_api_request=requester):
download.initialize_download(request, http)
self.assertTrue(download._initial_response is None)
self.assertEqual(download.total_size, 0)
self.assertTrue(download.http is http)
self.assertEqual(download.url, REDIRECT_URL)
self.assertTrue(len(requester._requested), 1)
self.assertTrue(requester._requested[0][0] is request)
def test__normalize_start_end_w_end_w_start_lt_0(self):
from gcloud.streaming.exceptions import TransferInvalidError
download = self._makeOne(_Stream())
with self.assertRaises(TransferInvalidError):
download._normalize_start_end(-1, 0)
def test__normalize_start_end_w_end_w_start_gt_total(self):
from gcloud.streaming.exceptions import TransferInvalidError
download = self._makeOne(_Stream())
download._set_total({'content-range': 'bytes 0-1/2'})
with self.assertRaises(TransferInvalidError):
download._normalize_start_end(3, 0)
def test__normalize_start_end_w_end_lt_start(self):
from gcloud.streaming.exceptions import TransferInvalidError
download = self._makeOne(_Stream())
download._set_total({'content-range': 'bytes 0-1/2'})
with self.assertRaises(TransferInvalidError):
download._normalize_start_end(1, 0)
def test__normalize_start_end_w_end_gt_start(self):
download = self._makeOne(_Stream())
download._set_total({'content-range': 'bytes 0-1/2'})
self.assertEqual(download._normalize_start_end(1, 2), (1, 1))
def test__normalize_start_end_wo_end_w_start_lt_0(self):
download = self._makeOne(_Stream())
download._set_total({'content-range': 'bytes 0-1/2'})
self.assertEqual(download._normalize_start_end(-2), (0, 1))
self.assertEqual(download._normalize_start_end(-1), (1, 1))
def test__normalize_start_end_wo_end_w_start_ge_0(self):
download = self._makeOne(_Stream())
download._set_total({'content-range': 'bytes 0-1/100'})
self.assertEqual(download._normalize_start_end(0), (0, 99))
self.assertEqual(download._normalize_start_end(1), (1, 99))
def test__set_range_header_w_start_lt_0(self):
request = _Request()
download = self._makeOne(_Stream())
download._set_range_header(request, -1)
self.assertEqual(request.headers['range'], 'bytes=-1')
def test__set_range_header_w_start_ge_0_wo_end(self):
request = _Request()
download = self._makeOne(_Stream())
download._set_range_header(request, 0)
self.assertEqual(request.headers['range'], 'bytes=0-')
def test__set_range_header_w_start_ge_0_w_end(self):
request = _Request()
download = self._makeOne(_Stream())
download._set_range_header(request, 0, 1)
self.assertEqual(request.headers['range'], 'bytes=0-1')
def test__compute_end_byte_w_start_lt_0_w_end(self):
download = self._makeOne(_Stream())
self.assertEqual(download._compute_end_byte(-1, 1), 1)
def test__compute_end_byte_w_start_ge_0_wo_end_w_use_chunks(self):
CHUNK_SIZE = 5
download = self._makeOne(_Stream(), chunksize=CHUNK_SIZE)
self.assertEqual(download._compute_end_byte(0, use_chunks=True), 4)
def test__compute_end_byte_w_start_ge_0_w_end_w_use_chunks(self):
CHUNK_SIZE = 5
download = self._makeOne(_Stream(), chunksize=CHUNK_SIZE)
self.assertEqual(download._compute_end_byte(0, 3, use_chunks=True), 3)
self.assertEqual(download._compute_end_byte(0, 5, use_chunks=True), 4)
def test__compute_end_byte_w_start_ge_0_w_end_w_total_size(self):
CHUNK_SIZE = 50
download = self._makeOne(_Stream(), chunksize=CHUNK_SIZE)
download._set_total({'content-range': 'bytes 0-1/10'})
self.assertEqual(download._compute_end_byte(0, 100, use_chunks=False),
9)
self.assertEqual(download._compute_end_byte(0, 8, use_chunks=False), 8)
def test__compute_end_byte_w_start_ge_0_wo_end_w_total_size(self):
CHUNK_SIZE = 50
download = self._makeOne(_Stream(), chunksize=CHUNK_SIZE)
download._set_total({'content-range': 'bytes 0-1/10'})
self.assertEqual(download._compute_end_byte(0, use_chunks=False), 9)
def test__get_chunk_not_initialized(self):
from gcloud.streaming.exceptions import TransferInvalidError
download = self._makeOne(_Stream())
with self.assertRaises(TransferInvalidError):
download._get_chunk(0, 10)
def test__get_chunk(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
http = object()
download = self._makeOne(_Stream())
download._initialize(http, self.URL)
response = _makeResponse(http_client.OK)
requester = _MakeRequest(response)
with _Monkey(MUT,
Request=_Request,
make_api_request=requester):
found = download._get_chunk(0, 10)
self.assertTrue(found is response)
self.assertTrue(len(requester._requested), 1)
request = requester._requested[0][0]
self.assertEqual(request.headers['range'], 'bytes=0-10')
def test__process_response_w_FORBIDDEN(self):
from gcloud.streaming.exceptions import HttpError
from six.moves import http_client
download = self._makeOne(_Stream())
response = _makeResponse(http_client.FORBIDDEN)
with self.assertRaises(HttpError):
download._process_response(response)
def test__process_response_w_NOT_FOUND(self):
from gcloud.streaming.exceptions import HttpError
from six.moves import http_client
download = self._makeOne(_Stream())
response = _makeResponse(http_client.NOT_FOUND)
with self.assertRaises(HttpError):
download._process_response(response)
def test__process_response_w_other_error(self):
from gcloud.streaming.exceptions import TransferRetryError
from six.moves import http_client
download = self._makeOne(_Stream())
response = _makeResponse(http_client.BAD_REQUEST)
with self.assertRaises(TransferRetryError):
download._process_response(response)
def test__process_response_w_OK_wo_encoding(self):
from six.moves import http_client
stream = _Stream()
download = self._makeOne(stream)
response = _makeResponse(http_client.OK, content='OK')
found = download._process_response(response)
self.assertTrue(found is response)
self.assertEqual(stream._written, ['OK'])
self.assertEqual(download.progress, 2)
self.assertEqual(download.encoding, None)
def test__process_response_w_PARTIAL_CONTENT_w_encoding(self):
from six.moves import http_client
stream = _Stream()
download = self._makeOne(stream)
info = {'content-encoding': 'blah'}
response = _makeResponse(http_client.OK, info, 'PARTIAL')
found = download._process_response(response)
self.assertTrue(found is response)
self.assertEqual(stream._written, ['PARTIAL'])
self.assertEqual(download.progress, 7)
self.assertEqual(download.encoding, 'blah')
def test__process_response_w_REQUESTED_RANGE_NOT_SATISFIABLE(self):
from six.moves import http_client
stream = _Stream()
download = self._makeOne(stream)
response = _makeResponse(
http_client.REQUESTED_RANGE_NOT_SATISFIABLE)
found = download._process_response(response)
self.assertTrue(found is response)
self.assertEqual(stream._written, [])
self.assertEqual(download.progress, 0)
self.assertEqual(download.encoding, None)
def test__process_response_w_NO_CONTENT(self):
from six.moves import http_client
stream = _Stream()
download = self._makeOne(stream)
response = _makeResponse(status_code=http_client.NO_CONTENT)
found = download._process_response(response)
self.assertTrue(found is response)
self.assertEqual(stream._written, [''])
self.assertEqual(download.progress, 0)
self.assertEqual(download.encoding, None)
def test_get_range_not_initialized(self):
from gcloud.streaming.exceptions import TransferInvalidError
download = self._makeOne(_Stream())
with self.assertRaises(TransferInvalidError):
download.get_range(0, 10)
def test_get_range_wo_total_size_complete(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
CONTENT = b'ABCDEFGHIJ'
LEN = len(CONTENT)
REQ_RANGE = 'bytes=0-%d' % (LEN,)
RESP_RANGE = 'bytes 0-%d/%d' % (LEN - 1, LEN)
http = object()
stream = _Stream()
download = self._makeOne(stream)
download._initialize(http, self.URL)
info = {'content-range': RESP_RANGE}
response = _makeResponse(http_client.OK, info, CONTENT)
requester = _MakeRequest(response)
with _Monkey(MUT,
Request=_Request,
make_api_request=requester):
download.get_range(0, LEN)
self.assertTrue(len(requester._requested), 1)
request = requester._requested[0][0]
self.assertEqual(request.headers, {'range': REQ_RANGE})
self.assertEqual(stream._written, [CONTENT])
self.assertEqual(download.total_size, LEN)
def test_get_range_wo_total_size_wo_end(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
CONTENT = b'ABCDEFGHIJ'
LEN = len(CONTENT)
START = 5
CHUNK_SIZE = 123
REQ_RANGE = 'bytes=%d-%d' % (START, START + CHUNK_SIZE - 1,)
RESP_RANGE = 'bytes %d-%d/%d' % (START, LEN - 1, LEN)
http = object()
stream = _Stream()
download = self._makeOne(stream, chunksize=CHUNK_SIZE)
download._initialize(http, self.URL)
info = {'content-range': RESP_RANGE}
response = _makeResponse(http_client.OK, info, CONTENT[START:])
requester = _MakeRequest(response)
with _Monkey(MUT,
Request=_Request,
make_api_request=requester):
download.get_range(START)
self.assertTrue(len(requester._requested), 1)
request = requester._requested[0][0]
self.assertEqual(request.headers, {'range': REQ_RANGE})
self.assertEqual(stream._written, [CONTENT[START:]])
self.assertEqual(download.total_size, LEN)
def test_get_range_w_total_size_partial(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
CONTENT = b'ABCDEFGHIJ'
LEN = len(CONTENT)
PARTIAL_LEN = 5
REQ_RANGE = 'bytes=0-%d' % (PARTIAL_LEN,)
RESP_RANGE = 'bytes 0-%d/%d' % (PARTIAL_LEN, LEN,)
http = object()
stream = _Stream()
download = self._makeOne(stream, total_size=LEN)
download._initialize(http, self.URL)
info = {'content-range': RESP_RANGE}
response = _makeResponse(http_client.OK, info, CONTENT[:PARTIAL_LEN])
response.length = LEN
requester = _MakeRequest(response)
with _Monkey(MUT,
Request=_Request,
make_api_request=requester):
download.get_range(0, PARTIAL_LEN)
self.assertTrue(len(requester._requested), 1)
request = requester._requested[0][0]
self.assertEqual(request.headers, {'range': REQ_RANGE})
self.assertEqual(stream._written, [CONTENT[:PARTIAL_LEN]])
self.assertEqual(download.total_size, LEN)
def test_get_range_w_empty_chunk(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
from gcloud.streaming.exceptions import TransferRetryError
CONTENT = b'ABCDEFGHIJ'
LEN = len(CONTENT)
START = 5
CHUNK_SIZE = 123
REQ_RANGE = 'bytes=%d-%d' % (START, START + CHUNK_SIZE - 1,)
RESP_RANGE = 'bytes %d-%d/%d' % (START, LEN - 1, LEN)
http = object()
stream = _Stream()
download = self._makeOne(stream, chunksize=CHUNK_SIZE)
download._initialize(http, self.URL)
info = {'content-range': RESP_RANGE}
response = _makeResponse(http_client.OK, info)
requester = _MakeRequest(response)
with _Monkey(MUT,
Request=_Request,
make_api_request=requester):
with self.assertRaises(TransferRetryError):
download.get_range(START)
self.assertTrue(len(requester._requested), 1)
request = requester._requested[0][0]
self.assertEqual(request.headers, {'range': REQ_RANGE})
self.assertEqual(stream._written, [''])
self.assertEqual(download.total_size, LEN)
def test_get_range_w_total_size_wo_use_chunks(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
CONTENT = b'ABCDEFGHIJ'
LEN = len(CONTENT)
CHUNK_SIZE = 3
REQ_RANGE = 'bytes=0-%d' % (LEN - 1,)
RESP_RANGE = 'bytes 0-%d/%d' % (LEN - 1, LEN,)
http = object()
stream = _Stream()
download = self._makeOne(stream, total_size=LEN, chunksize=CHUNK_SIZE)
download._initialize(http, self.URL)
info = {'content-range': RESP_RANGE}
response = _makeResponse(http_client.OK, info, CONTENT)
requester = _MakeRequest(response)
with _Monkey(MUT,
Request=_Request,
make_api_request=requester):
download.get_range(0, use_chunks=False)
self.assertTrue(len(requester._requested), 1)
request = requester._requested[0][0]
self.assertEqual(request.headers, {'range': REQ_RANGE})
self.assertEqual(stream._written, [CONTENT])
self.assertEqual(download.total_size, LEN)
def test_get_range_w_multiple_chunks(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
CONTENT = b'ABCDE'
LEN = len(CONTENT)
CHUNK_SIZE = 3
REQ_RANGE_1 = 'bytes=0-%d' % (CHUNK_SIZE - 1,)
RESP_RANGE_1 = 'bytes 0-%d/%d' % (CHUNK_SIZE - 1, LEN)
REQ_RANGE_2 = 'bytes=%d-%d' % (CHUNK_SIZE, LEN - 1)
RESP_RANGE_2 = 'bytes %d-%d/%d' % (CHUNK_SIZE, LEN - 1, LEN)
http = object()
stream = _Stream()
download = self._makeOne(stream, chunksize=CHUNK_SIZE)
download._initialize(http, self.URL)
info_1 = {'content-range': RESP_RANGE_1}
response_1 = _makeResponse(http_client.PARTIAL_CONTENT, info_1,
CONTENT[:CHUNK_SIZE])
info_2 = {'content-range': RESP_RANGE_2}
response_2 = _makeResponse(http_client.OK, info_2,
CONTENT[CHUNK_SIZE:])
requester = _MakeRequest(response_1, response_2)
with _Monkey(MUT,
Request=_Request,
make_api_request=requester):
download.get_range(0)
self.assertTrue(len(requester._requested), 2)
request_1 = requester._requested[0][0]
self.assertEqual(request_1.headers, {'range': REQ_RANGE_1})
request_2 = requester._requested[1][0]
self.assertEqual(request_2.headers, {'range': REQ_RANGE_2})
self.assertEqual(stream._written, [b'ABC', b'DE'])
self.assertEqual(download.total_size, LEN)
def test_stream_file_not_initialized(self):
from gcloud.streaming.exceptions import TransferInvalidError
download = self._makeOne(_Stream())
with self.assertRaises(TransferInvalidError):
download.stream_file()
def test_stream_file_w_initial_response_complete(self):
from six.moves import http_client
CONTENT = b'ABCDEFGHIJ'
LEN = len(CONTENT)
RESP_RANGE = 'bytes 0-%d/%d' % (LEN - 1, LEN,)
stream = _Stream()
download = self._makeOne(stream, total_size=LEN)
info = {'content-range': RESP_RANGE}
download._initial_response = _makeResponse(
http_client.OK, info, CONTENT)
http = object()
download._initialize(http, _Request.URL)
download.stream_file()
self.assertEqual(stream._written, [CONTENT])
self.assertEqual(download.total_size, LEN)
def test_stream_file_w_initial_response_incomplete(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
CHUNK_SIZE = 3
CONTENT = b'ABCDEF'
LEN = len(CONTENT)
RESP_RANGE_1 = 'bytes 0-%d/%d' % (CHUNK_SIZE - 1, LEN,)
REQ_RANGE_2 = 'bytes=%d-%d' % (CHUNK_SIZE, LEN - 1)
RESP_RANGE_2 = 'bytes %d-%d/%d' % (CHUNK_SIZE, LEN - 1, LEN,)
stream = _Stream()
http = object()
download = self._makeOne(stream, chunksize=CHUNK_SIZE)
info_1 = {'content-range': RESP_RANGE_1}
download._initial_response = _makeResponse(
http_client.PARTIAL_CONTENT, info_1, CONTENT[:CHUNK_SIZE])
info_2 = {'content-range': RESP_RANGE_2}
response_2 = _makeResponse(
http_client.OK, info_2, CONTENT[CHUNK_SIZE:])
requester = _MakeRequest(response_2)
download._initialize(http, _Request.URL)
request = _Request()
with _Monkey(MUT,
Request=_Request,
make_api_request=requester):
download.stream_file()
self.assertTrue(len(requester._requested), 1)
request = requester._requested[0][0]
self.assertEqual(request.headers, {'range': REQ_RANGE_2})
self.assertEqual(stream._written,
[CONTENT[:CHUNK_SIZE], CONTENT[CHUNK_SIZE:]])
self.assertEqual(download.total_size, LEN)
def test_stream_file_wo_initial_response_wo_total_size(self):
from six.moves import http_client
from gcloud._testing import _Monkey
from gcloud.streaming import transfer as MUT
CONTENT = b'ABCDEFGHIJ'
LEN = len(CONTENT)
CHUNK_SIZE = 123
REQ_RANGE = 'bytes=0-%d' % (CHUNK_SIZE - 1)
RESP_RANGE = 'bytes 0-%d/%d' % (LEN - 1, LEN,)
stream = _Stream()
http = object()
download = self._makeOne(stream, chunksize=CHUNK_SIZE)
info = {'content-range': RESP_RANGE}
response = _makeResponse(http_client.OK, info, CONTENT)
requester = _MakeRequest(response)
download._initialize(http, _Request.URL)
request = _Request()
with _Monkey(MUT,
Request=_Request,
make_api_request=requester):
download.stream_file()
self.assertTrue(len(requester._requested), 1)
request = requester._requested[0][0]
self.assertEqual(request.headers, {'range': REQ_RANGE})
self.assertEqual(stream._written, [CONTENT])
self.assertEqual(download.total_size, LEN)
class Test_Upload(unittest2.TestCase):
URL = "http://example.com/api"
MIME_TYPE = 'application/octet-stream'
UPLOAD_URL = 'http://example.com/upload/id=foobar'
def _getTargetClass(self):
from gcloud.streaming.transfer import Upload
return Upload
def _makeOne(self, stream, mime_type=MIME_TYPE, *args, **kw):
return self._getTargetClass()(stream, mime_type, *args, **kw)
def test_ctor_defaults(self):
from gcloud.streaming.transfer import _DEFAULT_CHUNKSIZE
stream = _Stream()
upload = self._makeOne(stream)
self.assertTrue(upload.stream is stream)
self.assertTrue(upload._final_response is None)
self.assertTrue(upload._server_chunk_granularity is None)
self.assertFalse(upload.complete)
self.assertEqual(upload.mime_type, self.MIME_TYPE)
self.assertEqual(upload.progress, 0)
self.assertTrue(upload.strategy is None)
self.assertTrue(upload.total_size is None)
self.assertEqual(upload.chunksize, _DEFAULT_CHUNKSIZE)
def test_ctor_w_kwds(self):
stream = _Stream()
CHUNK_SIZE = 123
upload = self._makeOne(stream, chunksize=CHUNK_SIZE)
self.assertTrue(upload.stream is stream)
self.assertEqual(upload.mime_type, self.MIME_TYPE)
self.assertEqual(upload.chunksize, CHUNK_SIZE)
def test_from_file_w_nonesuch_file(self):
klass = self._getTargetClass()
filename = '~nosuchuser/file.txt'
with self.assertRaises(OSError):
klass.from_file(filename)
def test_from_file_wo_mimetype_w_unguessable_filename(self):
import os
klass = self._getTargetClass()
CONTENT = b'EXISTING FILE W/ UNGUESSABLE MIMETYPE'
with _tempdir() as tempdir:
filename = os.path.join(tempdir, 'file.unguessable')
with open(filename, 'wb') as fileobj:
fileobj.write(CONTENT)
with self.assertRaises(ValueError):
klass.from_file(filename)
def test_from_file_wo_mimetype_w_guessable_filename(self):
import os
klass = self._getTargetClass()
CONTENT = b'EXISTING FILE W/ GUESSABLE MIMETYPE'
with _tempdir() as tempdir:
filename = os.path.join(tempdir, 'file.txt')
with open(filename, 'wb') as fileobj:
fileobj.write(CONTENT)
upload = klass.from_file(filename)
self.assertEqual(upload.mime_type, 'text/plain')
self.assertTrue(upload.auto_transfer)
self.assertEqual(upload.total_size, len(CONTENT))
def test_from_file_w_mimetype_w_auto_transfer_w_kwds(self):
import os
klass = self._getTargetClass()
CONTENT = b'EXISTING FILE W/ GUESSABLE MIMETYPE'
CHUNK_SIZE = 3
with _tempdir() as tempdir:
filename = os.path.join(tempdir, 'file.unguessable')
with open(filename, 'wb') as fileobj:
fileobj.write(CONTENT)
upload = klass.from_file(
filename,
mime_type=self.MIME_TYPE,
auto_transfer=False,
chunksize=CHUNK_SIZE)
self.assertEqual(upload.mime_type, self.MIME_TYPE)
self.assertFalse(upload.auto_transfer)
self.assertEqual(upload.total_size, len(CONTENT))
self.assertEqual(upload.chunksize, CHUNK_SIZE)
def test_from_stream_wo_mimetype(self):
klass = self._getTargetClass()
stream = _Stream()
with self.assertRaises(ValueError):
klass.from_stream(stream, mime_type=None)
def test_from_stream_defaults(self):
klass = self._getTargetClass()
stream = _Stream()
upload = klass.from_stream(stream, mime_type=self.MIME_TYPE)
self.assertEqual(upload.mime_type, self.MIME_TYPE)
self.assertTrue(upload.auto_transfer)
self.assertEqual(upload.total_size, None)
def test_from_stream_explicit(self):
klass = self._getTargetClass()
stream = _Stream()
SIZE = 10
CHUNK_SIZE = 3
upload = klass.from_stream(
stream,
mime_type=self.MIME_TYPE,
auto_transfer=False,
total_size=SIZE,
chunksize=CHUNK_SIZE)
self.assertEqual(upload.mime_type, self.MIME_TYPE)
self.assertFalse(upload.auto_transfer)
self.assertEqual(upload.total_size, SIZE)
self.assertEqual(upload.chunksize, CHUNK_SIZE)
def test_strategy_setter_invalid(self):
upload = self._makeOne(_Stream())
with self.assertRaises(ValueError):
upload.strategy = object()
with self.assertRaises(ValueError):
upload.strategy = 'unknown'
def test_strategy_setter_SIMPLE_UPLOAD(self):
from gcloud.streaming.transfer import SIMPLE_UPLOAD
upload = self._makeOne(_Stream())
upload.strategy = SIMPLE_UPLOAD
self.assertEqual(upload.strategy, SIMPLE_UPLOAD)
def test_strategy_setter_RESUMABLE_UPLOAD(self):
from gcloud.streaming.transfer import RESUMABLE_UPLOAD
upload = self._makeOne(_Stream())
upload.strategy = RESUMABLE_UPLOAD
self.assertEqual(upload.strategy, RESUMABLE_UPLOAD)
def test_total_size_setter_initialized(self):
from gcloud.streaming.exceptions import TransferInvalidError
SIZE = 123
upload = self._makeOne(_Stream)
http = object()
upload._initialize(http, _Request.URL)
with self.assertRaises(TransferInvalidError):
upload.total_size = SIZE
def test_total_size_setter_not_initialized(self):
SIZE = 123
upload = self._makeOne(_Stream)
upload.total_size = SIZE
self.assertEqual(upload.total_size, SIZE)
def test__set_default_strategy_w_existing_strategy(self):
from gcloud.streaming.transfer import RESUMABLE_UPLOAD
config = _Dummy(
resumable_path='/resumable/endpoint',
simple_multipart=True,
simple_path='/upload/endpoint',
)
request = _Request()
upload = self._makeOne(_Stream)
upload.strategy = RESUMABLE_UPLOAD
upload._set_default_strategy(config, request)
self.assertEqual(upload.strategy, RESUMABLE_UPLOAD)
def test__set_default_strategy_wo_resumable_path(self):
from gcloud.streaming.transfer import SIMPLE_UPLOAD
config = _Dummy(
resumable_path=None,
simple_multipart=True,
simple_path='/upload/endpoint',
)
request = _Request()
upload = self._makeOne(_Stream())
upload._set_default_strategy(config, request)
self.assertEqual(upload.strategy, SIMPLE_UPLOAD)
def test__set_default_strategy_w_total_size_gt_threshhold(self):
from gcloud.streaming.transfer import RESUMABLE_UPLOAD_THRESHOLD
from gcloud.streaming.transfer import RESUMABLE_UPLOAD
config = _UploadConfig()
request = _Request()
upload = self._makeOne(
_Stream(), total_size=RESUMABLE_UPLOAD_THRESHOLD + 1)
upload._set_default_strategy(config, request)
self.assertEqual(upload.strategy, RESUMABLE_UPLOAD)
def test__set_default_strategy_w_body_wo_multipart(self):
from gcloud.streaming.transfer import RESUMABLE_UPLOAD
CONTENT = b'ABCDEFGHIJ'
config = _UploadConfig()
config.simple_multipart = False
request = _Request(body=CONTENT)
upload = self._makeOne(_Stream(), total_size=len(CONTENT))
upload._set_default_strategy(config, request)
self.assertEqual(upload.strategy, RESUMABLE_UPLOAD)
def test__set_default_strategy_w_body_w_multipart_wo_simple_path(self):
from gcloud.streaming.transfer import RESUMABLE_UPLOAD
CONTENT = b'ABCDEFGHIJ'
config = _UploadConfig()
config.simple_path = None
request = _Request(body=CONTENT)
upload = self._makeOne(_Stream(), total_size=len(CONTENT))
upload._set_default_strategy(config, request)
self.assertEqual(upload.strategy, RESUMABLE_UPLOAD)
def test__set_default_strategy_w_body_w_multipart_w_simple_path(self):
from gcloud.streaming.transfer import SIMPLE_UPLOAD
CONTENT = b'ABCDEFGHIJ'
config = _UploadConfig()
request = _Request(body=CONTENT)
upload = self._makeOne(_Stream(), total_size=len(CONTENT))
upload._set_default_strategy(config, request)
self.assertEqual(upload.strategy, SIMPLE_UPLOAD)
def test_configure_request_w_total_size_gt_max_size(self):
MAX_SIZE = 1000
config = _UploadConfig()
config.max_size = MAX_SIZE
request = _Request()
url_builder = _Dummy()
upload = self._makeOne(_Stream(), total_size=MAX_SIZE + 1)
with self.assertRaises(ValueError):