5
5
# --------------------------------------------------------------------------
6
6
import base64
7
7
import os
8
+ import tempfile
8
9
import uuid
9
10
from datetime import datetime , timedelta
10
11
40
41
TEST_BLOB_PREFIX = 'blob'
41
42
TEST_DIRECTORY_PREFIX = 'dir'
42
43
TEST_FILE_PREFIX = 'file'
43
- INPUT_FILE_PATH = 'file_input.temp.{}.dat' .format (str (uuid .uuid4 ()))
44
- OUTPUT_FILE_PATH = 'file_output.temp.{}.dat' .format (str (uuid .uuid4 ()))
45
44
LARGE_FILE_SIZE = 64 * 1024 + 5
46
45
TEST_FILE_PERMISSIONS = 'O:S-1-5-21-2127521184-1604012920-1887927527-21560751G:S-1-5-21-2127521184-' \
47
46
'1604012920-1887927527-513D:AI(A;;FA;;;SY)(A;;FA;;;BA)(A;;0x1200a9;;;' \
@@ -78,12 +77,6 @@ def _setup(self, storage_account_name, storage_account_key, rmt_account=None, rm
78
77
self .fsc2 = ShareServiceClient (remote_url , credential = remote_credential )
79
78
self .remote_share_name = None
80
79
81
- def _teardown (self , FILE_PATH ):
82
- if os .path .isfile (FILE_PATH ):
83
- try :
84
- os .remove (FILE_PATH )
85
- except :
86
- pass
87
80
# --Helpers-----------------------------------------------------------------
88
81
89
82
def _get_file_reference (self , prefix = TEST_FILE_PREFIX ):
@@ -222,6 +215,7 @@ def test_make_file_url_with_sas(self, **kwargs):
222
215
storage_account_key = kwargs .pop ("storage_account_key" )
223
216
224
217
self ._setup (storage_account_name , storage_account_key )
218
+ # cspell:disable-next-line
225
219
sas = '?sv=2015-04-05&st=2015-04-29T22%3A18%3A26Z&se=2015-04-30T02%3A23%3A26Z&sr=b&sp=rw&sip=168.1.5.60-168.1.5.70&spr=https&sig=Z%2FRHIX5Xcg0Mq2rqI3OlWTjEg2tYkboXr1P9ZUXDtkk%3D'
226
220
file_client = ShareFileClient (
227
221
self .account_url (storage_account_name , "file" ),
@@ -1933,8 +1927,6 @@ def test_create_file_from_path(self, **kwargs):
1933
1927
self ._setup (storage_account_name , storage_account_key )
1934
1928
file_name = self ._get_file_reference ()
1935
1929
data = self .get_random_bytes (LARGE_FILE_SIZE )
1936
- with open (INPUT_FILE_PATH , 'wb' ) as stream :
1937
- stream .write (data )
1938
1930
file_client = ShareFileClient (
1939
1931
self .account_url (storage_account_name , "file" ),
1940
1932
share_name = self .share_name ,
@@ -1943,15 +1935,16 @@ def test_create_file_from_path(self, **kwargs):
1943
1935
max_range_size = 4 * 1024 )
1944
1936
1945
1937
# Act
1946
- with open (INPUT_FILE_PATH , 'rb' ) as stream :
1947
- response = file_client .upload_file (stream , max_concurrency = 2 )
1938
+ with tempfile .TemporaryFile () as temp_file :
1939
+ temp_file .write (data )
1940
+ temp_file .seek (0 )
1941
+ response = file_client .upload_file (temp_file , max_concurrency = 2 )
1948
1942
assert isinstance (response , dict )
1949
1943
assert 'last_modified' in response
1950
1944
assert 'etag' in response
1951
1945
1952
1946
# Assert
1953
1947
self .assertFileEqual (file_client , data )
1954
- self ._teardown (INPUT_FILE_PATH )
1955
1948
1956
1949
@pytest .mark .live_test_only
1957
1950
@FileSharePreparer ()
@@ -1962,8 +1955,6 @@ def test_create_file_from_path_with_progress(self, **kwargs):
1962
1955
self ._setup (storage_account_name , storage_account_key )
1963
1956
file_name = self ._get_file_reference ()
1964
1957
data = self .get_random_bytes (LARGE_FILE_SIZE )
1965
- with open (INPUT_FILE_PATH , 'wb' ) as stream :
1966
- stream .write (data )
1967
1958
file_client = ShareFileClient (
1968
1959
self .account_url (storage_account_name , "file" ),
1969
1960
share_name = self .share_name ,
@@ -1979,19 +1970,17 @@ def callback(response):
1979
1970
if current is not None :
1980
1971
progress .append ((current , total ))
1981
1972
1982
- with open (INPUT_FILE_PATH , 'rb' ) as stream :
1983
- response = file_client .upload_file (stream , max_concurrency = 2 , raw_response_hook = callback )
1973
+ with tempfile .TemporaryFile () as temp_file :
1974
+ temp_file .write (data )
1975
+ temp_file .seek (0 )
1976
+ response = file_client .upload_file (temp_file , max_concurrency = 2 , raw_response_hook = callback )
1984
1977
assert isinstance (response , dict )
1985
1978
assert 'last_modified' in response
1986
1979
assert 'etag' in response
1987
1980
1988
1981
# Assert
1989
1982
self .assertFileEqual (file_client , data )
1990
- self .assert_upload_progress (
1991
- len (data ),
1992
- self .fsc ._config .max_range_size ,
1993
- progress , unknown_size = False )
1994
- self ._teardown (INPUT_FILE_PATH )
1983
+ self .assert_upload_progress (len (data ), self .fsc ._config .max_range_size , progress , unknown_size = False )
1995
1984
1996
1985
@pytest .mark .live_test_only
1997
1986
@FileSharePreparer ()
@@ -2002,8 +1991,6 @@ def test_create_file_from_stream(self, **kwargs):
2002
1991
self ._setup (storage_account_name , storage_account_key )
2003
1992
file_name = self ._get_file_reference ()
2004
1993
data = self .get_random_bytes (LARGE_FILE_SIZE )
2005
- with open (INPUT_FILE_PATH , 'wb' ) as stream :
2006
- stream .write (data )
2007
1994
file_client = ShareFileClient (
2008
1995
self .account_url (storage_account_name , "file" ),
2009
1996
share_name = self .share_name ,
@@ -2013,15 +2000,16 @@ def test_create_file_from_stream(self, **kwargs):
2013
2000
2014
2001
# Act
2015
2002
file_size = len (data )
2016
- with open (INPUT_FILE_PATH , 'rb' ) as stream :
2017
- response = file_client .upload_file (stream , max_concurrency = 2 )
2003
+ with tempfile .TemporaryFile () as temp_file :
2004
+ temp_file .write (data )
2005
+ temp_file .seek (0 )
2006
+ response = file_client .upload_file (temp_file , max_concurrency = 2 )
2018
2007
assert isinstance (response , dict )
2019
2008
assert 'last_modified' in response
2020
2009
assert 'etag' in response
2021
2010
2022
2011
# Assert
2023
2012
self .assertFileEqual (file_client , data [:file_size ])
2024
- self ._teardown (INPUT_FILE_PATH )
2025
2013
2026
2014
@pytest .mark .live_test_only
2027
2015
@FileSharePreparer ()
@@ -2032,8 +2020,6 @@ def test_create_file_from_stream_non_seekable(self, **kwargs):
2032
2020
self ._setup (storage_account_name , storage_account_key )
2033
2021
file_name = self ._get_file_reference ()
2034
2022
data = self .get_random_bytes (LARGE_FILE_SIZE )
2035
- with open (INPUT_FILE_PATH , 'wb' ) as stream :
2036
- stream .write (data )
2037
2023
file_client = ShareFileClient (
2038
2024
self .account_url (storage_account_name , "file" ),
2039
2025
share_name = self .share_name ,
@@ -2043,13 +2029,14 @@ def test_create_file_from_stream_non_seekable(self, **kwargs):
2043
2029
2044
2030
# Act
2045
2031
file_size = len (data )
2046
- with open (INPUT_FILE_PATH , 'rb' ) as stream :
2047
- non_seekable_file = TestStorageFile .NonSeekableFile (stream )
2032
+ with tempfile .TemporaryFile () as temp_file :
2033
+ temp_file .write (data )
2034
+ temp_file .seek (0 )
2035
+ non_seekable_file = TestStorageFile .NonSeekableFile (temp_file )
2048
2036
file_client .upload_file (non_seekable_file , length = file_size , max_concurrency = 1 )
2049
2037
2050
2038
# Assert
2051
2039
self .assertFileEqual (file_client , data [:file_size ])
2052
- self ._teardown (INPUT_FILE_PATH )
2053
2040
2054
2041
@pytest .mark .live_test_only
2055
2042
@FileSharePreparer ()
@@ -2060,8 +2047,6 @@ def test_create_file_from_stream_with_progress(self, **kwargs):
2060
2047
self ._setup (storage_account_name , storage_account_key )
2061
2048
file_name = self ._get_file_reference ()
2062
2049
data = self .get_random_bytes (LARGE_FILE_SIZE )
2063
- with open (INPUT_FILE_PATH , 'wb' ) as stream :
2064
- stream .write (data )
2065
2050
file_client = ShareFileClient (
2066
2051
self .account_url (storage_account_name , "file" ),
2067
2052
share_name = self .share_name ,
@@ -2078,16 +2063,14 @@ def callback(response):
2078
2063
progress .append ((current , total ))
2079
2064
2080
2065
file_size = len (data )
2081
- with open (INPUT_FILE_PATH , 'rb' ) as stream :
2082
- file_client .upload_file (stream , max_concurrency = 2 , raw_response_hook = callback )
2066
+ with tempfile .TemporaryFile () as temp_file :
2067
+ temp_file .write (data )
2068
+ temp_file .seek (0 )
2069
+ file_client .upload_file (temp_file , max_concurrency = 2 , raw_response_hook = callback )
2083
2070
2084
2071
# Assert
2085
2072
self .assertFileEqual (file_client , data [:file_size ])
2086
- self .assert_upload_progress (
2087
- len (data ),
2088
- self .fsc ._config .max_range_size ,
2089
- progress , unknown_size = False )
2090
- self ._teardown (INPUT_FILE_PATH )
2073
+ self .assert_upload_progress (len (data ), self .fsc ._config .max_range_size , progress , unknown_size = False )
2091
2074
2092
2075
@pytest .mark .live_test_only
2093
2076
@FileSharePreparer ()
@@ -2098,8 +2081,6 @@ def test_create_file_from_stream_truncated(self, **kwargs):
2098
2081
self ._setup (storage_account_name , storage_account_key )
2099
2082
file_name = self ._get_file_reference ()
2100
2083
data = self .get_random_bytes (LARGE_FILE_SIZE )
2101
- with open (INPUT_FILE_PATH , 'wb' ) as stream :
2102
- stream .write (data )
2103
2084
file_client = ShareFileClient (
2104
2085
self .account_url (storage_account_name , "file" ),
2105
2086
share_name = self .share_name ,
@@ -2109,12 +2090,13 @@ def test_create_file_from_stream_truncated(self, **kwargs):
2109
2090
2110
2091
# Act
2111
2092
file_size = len (data ) - 512
2112
- with open (INPUT_FILE_PATH , 'rb' ) as stream :
2113
- file_client .upload_file (stream , length = file_size , max_concurrency = 2 )
2093
+ with tempfile .TemporaryFile () as temp_file :
2094
+ temp_file .write (data )
2095
+ temp_file .seek (0 )
2096
+ file_client .upload_file (temp_file , length = file_size , max_concurrency = 2 )
2114
2097
2115
2098
# Assert
2116
2099
self .assertFileEqual (file_client , data [:file_size ])
2117
- self ._teardown (INPUT_FILE_PATH )
2118
2100
2119
2101
@pytest .mark .live_test_only
2120
2102
@FileSharePreparer ()
@@ -2125,8 +2107,6 @@ def test_create_file_from_stream_with_progress_truncated(self, **kwargs):
2125
2107
self ._setup (storage_account_name , storage_account_key )
2126
2108
file_name = self ._get_file_reference ()
2127
2109
data = self .get_random_bytes (LARGE_FILE_SIZE )
2128
- with open (INPUT_FILE_PATH , 'wb' ) as stream :
2129
- stream .write (data )
2130
2110
file_client = ShareFileClient (
2131
2111
self .account_url (storage_account_name , "file" ),
2132
2112
share_name = self .share_name ,
@@ -2143,17 +2123,14 @@ def callback(response):
2143
2123
progress .append ((current , total ))
2144
2124
2145
2125
file_size = len (data ) - 5
2146
- with open (INPUT_FILE_PATH , 'rb' ) as stream :
2147
- file_client .upload_file (stream , length = file_size , max_concurrency = 2 , raw_response_hook = callback )
2148
-
2126
+ with tempfile .TemporaryFile () as temp_file :
2127
+ temp_file .write (data )
2128
+ temp_file .seek (0 )
2129
+ file_client .upload_file (temp_file , length = file_size , max_concurrency = 2 , raw_response_hook = callback )
2149
2130
2150
2131
# Assert
2151
2132
self .assertFileEqual (file_client , data [:file_size ])
2152
- self .assert_upload_progress (
2153
- file_size ,
2154
- self .fsc ._config .max_range_size ,
2155
- progress , unknown_size = False )
2156
- self ._teardown (INPUT_FILE_PATH )
2133
+ self .assert_upload_progress (file_size , self .fsc ._config .max_range_size , progress , unknown_size = False )
2157
2134
2158
2135
@FileSharePreparer ()
2159
2136
@recorded_by_proxy
0 commit comments