Skip to content

Commit 342d9db

Browse files
committed
add test for azure
1 parent a95d1e9 commit 342d9db

File tree

1 file changed

+67
-14
lines changed

1 file changed

+67
-14
lines changed

tests/test_fs_plugins.py

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,52 @@ def exists(key_name):
125125
return get_uri, read_file, write_file, exists
126126

127127

128-
MAP_URI_HELPER = {S3_URI: s3_init, GCS_URI: gcs_init}
128+
def az_init(monkeypatch):
129+
from azure.storage.blob import ContainerClient
130+
from azure.core.exceptions import ResourceNotFoundError
131+
132+
container_name = os.environ.get("AZ_TEST_CONTAINER")
133+
account = ""
134+
prefix = f"tf-io-dir-{int(time.time())}/"
135+
client = None
136+
if container_name is None:
137+
# This means we are running against emulator.
138+
monkeypatch.setenv("TF_AZURE_USE_DEV_STORAGE", "1")
139+
container_name = "tf-io-{}-{}".format(AZ_URI, int(time.time()))
140+
account = "devstoreaccount1"
141+
conn_str = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
142+
client = ContainerClient.from_connection_string(
143+
conn_str=conn_str, container_name=container_name
144+
)
145+
client.create_container()
146+
else:
147+
# TODO(vnvo2409): Implement this for testing against production
148+
pass
149+
150+
client.upload_blob(prefix, b"")
151+
152+
def get_uri(key_name):
153+
return "{}://{}/{}/{}".format(
154+
AZ_URI, account, container_name, prefix + key_name
155+
)
156+
157+
def read_file(key_name):
158+
return client.download_blob(prefix + key_name).content_as_bytes()
159+
160+
def write_file(key_name, body):
161+
client.upload_blob(prefix + key_name, body)
162+
163+
def exists(key_name):
164+
try:
165+
client.download_blob(prefix + key_name).content_as_bytes()
166+
except ResourceNotFoundError:
167+
return False
168+
return True
169+
170+
return get_uri, read_file, write_file, exists
171+
172+
173+
MAP_URI_HELPER = {S3_URI: s3_init, GCS_URI: gcs_init, AZ_URI: az_init}
129174

130175

131176
# ------------------------ URI CONDITION FOR EACH TEST ----------------------- #
@@ -188,7 +233,7 @@ def check_test_condition_and_setup_env(uri, envs, monkeypatch):
188233
autouse=True,
189234
params=[
190235
S3_URI,
191-
pytest.param(AZ_URI, marks=pytest.mark.skip(reason="TODO")),
236+
AZ_URI,
192237
pytest.param(HDFS_URI, marks=pytest.mark.skip(reason="TODO")),
193238
pytest.param(VIEWFS_URI, marks=pytest.mark.skip(reason="TODO")),
194239
pytest.param(HAR_URI, marks=pytest.mark.skip(reason="TODO")),
@@ -262,11 +307,12 @@ def test_gfile_GFile_readable(uri_init, envs, monkeypatch):
262307
assert file_read == body
263308

264309
# Notfound
265-
with pytest.raises(tf.errors.NotFoundError) as excinfo:
266-
fname_not_found = fname + "_not_found"
267-
with tf.io.gfile.GFile(fname_not_found, "rb") as f:
268-
_ = f.read()
269-
assert fname_not_found in str(excinfo.value)
310+
# TODO(vnvo2409): `az` returns an empty string when file does not exist.
311+
if uri != AZ_URI:
312+
with pytest.raises(tf.errors.NotFoundError):
313+
fname_not_found = fname + "_not_found"
314+
with tf.io.gfile.GFile(fname_not_found, "rb") as f:
315+
_ = f.read()
270316

271317
# Read length
272318
with tf.io.gfile.GFile(fname, "rb") as f:
@@ -325,10 +371,12 @@ def test_gfile_GFile_writable(uri_init, envs, monkeypatch):
325371
assert read_file(base_file_name) == body
326372

327373
# Append
328-
with tf.io.gfile.GFile(fname, "ab") as f:
329-
f.write(base_body)
330-
f.flush()
331-
assert read_file(base_file_name) == body + base_body
374+
# `NewAppendableFile` in `az` is actually `NewWritableFile`
375+
if uri != AZ_URI:
376+
with tf.io.gfile.GFile(fname, "ab") as f:
377+
f.write(base_body)
378+
f.flush()
379+
assert read_file(base_file_name) == body + base_body
332380

333381

334382
@pytest.mark.parametrize("envs", [("+all", None)])
@@ -341,7 +389,9 @@ def test_gfile_isdir(uri_init, envs, monkeypatch):
341389
base_file_name = base_dir_name + "fname"
342390
file_name = get_uri(base_file_name)
343391

344-
write_file(base_dir_name, "")
392+
# `az` considers non-file path as a directory
393+
if uri != AZ_URI:
394+
write_file(base_dir_name, "")
345395
write_file(base_file_name, "")
346396

347397
assert tf.io.gfile.isdir(dir_name) is True
@@ -355,7 +405,9 @@ def test_gfile_listdir(uri_init, envs, monkeypatch):
355405

356406
base_dir_name = "test_gfile_listdir/"
357407
dir_name = get_uri(base_dir_name)
358-
write_file(base_dir_name, "")
408+
# `az` considers non-file path as a directory
409+
if uri != AZ_URI:
410+
write_file(base_dir_name, "")
359411

360412
num_childs = 5
361413
childrens = [None] * num_childs
@@ -416,7 +468,8 @@ def test_gfile_rmtree(uri_init, envs, monkeypatch):
416468
assert [exists(get_uri(entry)) for entry in base_names] == [False] * num_entries
417469

418470

419-
@pytest.mark.parametrize("envs", [("+all", None)])
471+
# TODO(vnvo2409): `az` copy operations causes an infinite loop.
472+
@pytest.mark.parametrize("envs", [("+all:-az", None)])
420473
def test_gfile_copy(uri_init, envs, monkeypatch):
421474
uri, get_uri, read_file, write_file, _ = uri_init
422475
check_test_condition_and_setup_env(uri, envs, monkeypatch)

0 commit comments

Comments
 (0)