Skip to content

Commit 7632200

Browse files
[Storage] Use tempfile for file-share package (Azure#27771)
* Tempfile for datalake package * Revert "Tempfile for datalake package" This reverts commit 5de69d8. * Tempfile for datalake package * Revert "Tempfile for datalake package" This reverts commit 5de69d8. * Finished test_file sync * Test_file async done * Remove constants * Finished test_get_file sync * Finished test_get_file async * Add cspell disable * PR feedback (remove teardown) * Finished test_file sync * Test_file async done * Remove constants * Finished test_get_file sync * Finished test_get_file async * Finished test_file sync * Test_file async done * Remove constants * Finished test_get_file sync * Finished test_get_file async
1 parent 44ec83d commit 7632200

File tree

4 files changed

+313
-478
lines changed

4 files changed

+313
-478
lines changed

sdk/storage/azure-storage-file-share/tests/test_file.py

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# --------------------------------------------------------------------------
66
import base64
77
import os
8+
import tempfile
89
import uuid
910
from datetime import datetime, timedelta
1011

@@ -40,8 +41,6 @@
4041
TEST_BLOB_PREFIX = 'blob'
4142
TEST_DIRECTORY_PREFIX = 'dir'
4243
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()))
4544
LARGE_FILE_SIZE = 64 * 1024 + 5
4645
TEST_FILE_PERMISSIONS = 'O:S-1-5-21-2127521184-1604012920-1887927527-21560751G:S-1-5-21-2127521184-' \
4746
'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
7877
self.fsc2 = ShareServiceClient(remote_url, credential=remote_credential)
7978
self.remote_share_name = None
8079

81-
def _teardown(self, FILE_PATH):
82-
if os.path.isfile(FILE_PATH):
83-
try:
84-
os.remove(FILE_PATH)
85-
except:
86-
pass
8780
# --Helpers-----------------------------------------------------------------
8881

8982
def _get_file_reference(self, prefix=TEST_FILE_PREFIX):
@@ -222,6 +215,7 @@ def test_make_file_url_with_sas(self, **kwargs):
222215
storage_account_key = kwargs.pop("storage_account_key")
223216

224217
self._setup(storage_account_name, storage_account_key)
218+
# cspell:disable-next-line
225219
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'
226220
file_client = ShareFileClient(
227221
self.account_url(storage_account_name, "file"),
@@ -1933,8 +1927,6 @@ def test_create_file_from_path(self, **kwargs):
19331927
self._setup(storage_account_name, storage_account_key)
19341928
file_name = self._get_file_reference()
19351929
data = self.get_random_bytes(LARGE_FILE_SIZE)
1936-
with open(INPUT_FILE_PATH, 'wb') as stream:
1937-
stream.write(data)
19381930
file_client = ShareFileClient(
19391931
self.account_url(storage_account_name, "file"),
19401932
share_name=self.share_name,
@@ -1943,15 +1935,16 @@ def test_create_file_from_path(self, **kwargs):
19431935
max_range_size=4 * 1024)
19441936

19451937
# 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)
19481942
assert isinstance(response, dict)
19491943
assert 'last_modified' in response
19501944
assert 'etag' in response
19511945

19521946
# Assert
19531947
self.assertFileEqual(file_client, data)
1954-
self._teardown(INPUT_FILE_PATH)
19551948

19561949
@pytest.mark.live_test_only
19571950
@FileSharePreparer()
@@ -1962,8 +1955,6 @@ def test_create_file_from_path_with_progress(self, **kwargs):
19621955
self._setup(storage_account_name, storage_account_key)
19631956
file_name = self._get_file_reference()
19641957
data = self.get_random_bytes(LARGE_FILE_SIZE)
1965-
with open(INPUT_FILE_PATH, 'wb') as stream:
1966-
stream.write(data)
19671958
file_client = ShareFileClient(
19681959
self.account_url(storage_account_name, "file"),
19691960
share_name=self.share_name,
@@ -1979,19 +1970,17 @@ def callback(response):
19791970
if current is not None:
19801971
progress.append((current, total))
19811972

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)
19841977
assert isinstance(response, dict)
19851978
assert 'last_modified' in response
19861979
assert 'etag' in response
19871980

19881981
# Assert
19891982
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)
19951984

19961985
@pytest.mark.live_test_only
19971986
@FileSharePreparer()
@@ -2002,8 +1991,6 @@ def test_create_file_from_stream(self, **kwargs):
20021991
self._setup(storage_account_name, storage_account_key)
20031992
file_name = self._get_file_reference()
20041993
data = self.get_random_bytes(LARGE_FILE_SIZE)
2005-
with open(INPUT_FILE_PATH, 'wb') as stream:
2006-
stream.write(data)
20071994
file_client = ShareFileClient(
20081995
self.account_url(storage_account_name, "file"),
20091996
share_name=self.share_name,
@@ -2013,15 +2000,16 @@ def test_create_file_from_stream(self, **kwargs):
20132000

20142001
# Act
20152002
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)
20182007
assert isinstance(response, dict)
20192008
assert 'last_modified' in response
20202009
assert 'etag' in response
20212010

20222011
# Assert
20232012
self.assertFileEqual(file_client, data[:file_size])
2024-
self._teardown(INPUT_FILE_PATH)
20252013

20262014
@pytest.mark.live_test_only
20272015
@FileSharePreparer()
@@ -2032,8 +2020,6 @@ def test_create_file_from_stream_non_seekable(self, **kwargs):
20322020
self._setup(storage_account_name, storage_account_key)
20332021
file_name = self._get_file_reference()
20342022
data = self.get_random_bytes(LARGE_FILE_SIZE)
2035-
with open(INPUT_FILE_PATH, 'wb') as stream:
2036-
stream.write(data)
20372023
file_client = ShareFileClient(
20382024
self.account_url(storage_account_name, "file"),
20392025
share_name=self.share_name,
@@ -2043,13 +2029,14 @@ def test_create_file_from_stream_non_seekable(self, **kwargs):
20432029

20442030
# Act
20452031
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)
20482036
file_client.upload_file(non_seekable_file, length=file_size, max_concurrency=1)
20492037

20502038
# Assert
20512039
self.assertFileEqual(file_client, data[:file_size])
2052-
self._teardown(INPUT_FILE_PATH)
20532040

20542041
@pytest.mark.live_test_only
20552042
@FileSharePreparer()
@@ -2060,8 +2047,6 @@ def test_create_file_from_stream_with_progress(self, **kwargs):
20602047
self._setup(storage_account_name, storage_account_key)
20612048
file_name = self._get_file_reference()
20622049
data = self.get_random_bytes(LARGE_FILE_SIZE)
2063-
with open(INPUT_FILE_PATH, 'wb') as stream:
2064-
stream.write(data)
20652050
file_client = ShareFileClient(
20662051
self.account_url(storage_account_name, "file"),
20672052
share_name=self.share_name,
@@ -2078,16 +2063,14 @@ def callback(response):
20782063
progress.append((current, total))
20792064

20802065
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)
20832070

20842071
# Assert
20852072
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)
20912074

20922075
@pytest.mark.live_test_only
20932076
@FileSharePreparer()
@@ -2098,8 +2081,6 @@ def test_create_file_from_stream_truncated(self, **kwargs):
20982081
self._setup(storage_account_name, storage_account_key)
20992082
file_name = self._get_file_reference()
21002083
data = self.get_random_bytes(LARGE_FILE_SIZE)
2101-
with open(INPUT_FILE_PATH, 'wb') as stream:
2102-
stream.write(data)
21032084
file_client = ShareFileClient(
21042085
self.account_url(storage_account_name, "file"),
21052086
share_name=self.share_name,
@@ -2109,12 +2090,13 @@ def test_create_file_from_stream_truncated(self, **kwargs):
21092090

21102091
# Act
21112092
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)
21142097

21152098
# Assert
21162099
self.assertFileEqual(file_client, data[:file_size])
2117-
self._teardown(INPUT_FILE_PATH)
21182100

21192101
@pytest.mark.live_test_only
21202102
@FileSharePreparer()
@@ -2125,8 +2107,6 @@ def test_create_file_from_stream_with_progress_truncated(self, **kwargs):
21252107
self._setup(storage_account_name, storage_account_key)
21262108
file_name = self._get_file_reference()
21272109
data = self.get_random_bytes(LARGE_FILE_SIZE)
2128-
with open(INPUT_FILE_PATH, 'wb') as stream:
2129-
stream.write(data)
21302110
file_client = ShareFileClient(
21312111
self.account_url(storage_account_name, "file"),
21322112
share_name=self.share_name,
@@ -2143,17 +2123,14 @@ def callback(response):
21432123
progress.append((current, total))
21442124

21452125
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)
21492130

21502131
# Assert
21512132
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)
21572134

21582135
@FileSharePreparer()
21592136
@recorded_by_proxy

0 commit comments

Comments
 (0)