|
| 1 | +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | +# use this file except in compliance with the License. You may obtain a copy of |
| 5 | +# the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations under |
| 13 | +# the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for file system plugins""" |
| 16 | + |
| 17 | +import os |
| 18 | +import time |
| 19 | + |
| 20 | +import pytest |
| 21 | +import tensorflow as tf |
| 22 | +import tensorflow_io as tfio # pylint: disable=unused-import |
| 23 | + |
| 24 | +S3_URI = "s3e" |
| 25 | +AZ_URI = "az" |
| 26 | +HDFS_URI = "hdfse" |
| 27 | +VIEWFS_URI = "viewfse" |
| 28 | +HAR_URI = "hare" |
| 29 | +GCS_URI = "gse" |
| 30 | + |
| 31 | + |
| 32 | +def setup_env(uri, envs): |
| 33 | + # when `envs is None`, it is the default case without |
| 34 | + # additional envs and should run with every plugins. |
| 35 | + if envs is None: |
| 36 | + pytest.skip() |
| 37 | + |
| 38 | + uri_env, additional_env = envs |
| 39 | + if uri != uri_env: |
| 40 | + pytest.skip() |
| 41 | + |
| 42 | + # TODO(vnvo2409): Use monkeypatch |
| 43 | + # ------------------------------------ s3 ------------------------------------ # |
| 44 | + if uri == S3_URI: |
| 45 | + os.environ["AWS_REGION"] = "us-east-1" |
| 46 | + os.environ["AWS_ACCESS_KEY_ID"] = "ACCESS_KEY" |
| 47 | + os.environ["AWS_SECRET_ACCESS_KEY"] = "SECRET_KEY" |
| 48 | + os.environ["S3_ENDPOINT"] = "localhost:4566" |
| 49 | + os.environ["S3_USE_HTTPS"] = "0" |
| 50 | + os.environ["S3_VERIFY_SSL"] = "0" |
| 51 | + |
| 52 | + for key, value in additional_env.items(): |
| 53 | + os.environ[key] = value |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture( |
| 57 | + scope="session", |
| 58 | + autouse=True, |
| 59 | + params=[ |
| 60 | + S3_URI, |
| 61 | + pytest.param(AZ_URI, marks=pytest.mark.skip(reason="TODO")), |
| 62 | + pytest.param(HDFS_URI, marks=pytest.mark.skip(reason="TODO")), |
| 63 | + pytest.param(VIEWFS_URI, marks=pytest.mark.skip(reason="TODO")), |
| 64 | + pytest.param(HAR_URI, marks=pytest.mark.skip(reason="TODO")), |
| 65 | + pytest.param(GCS_URI, marks=pytest.mark.skip(reason="TODO")), |
| 66 | + ], |
| 67 | +) |
| 68 | +def uri_builder(request): |
| 69 | + uri = request.param |
| 70 | + bucket_name = "{}{}e/tf-io-test".format(uri, time.time()) |
| 71 | + |
| 72 | + def get_uri(object_name): |
| 73 | + return "{}://{}/{}".format(uri, bucket_name, object_name) |
| 74 | + |
| 75 | + tf.io.gfile.makedirs("{}://{}/".format(uri, bucket_name)) |
| 76 | + return uri, bucket_name, get_uri |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize("envs", [None]) |
| 80 | +def test_init(uri_builder, envs): |
| 81 | + uri, _, get_uri = uri_builder |
| 82 | + setup_env(uri, envs) |
| 83 | + assert tf.io.gfile.exists(get_uri("")) is True |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "envs", [None, (S3_URI, {"S3_DISABLE_MULTI_PART_DOWNLOAD": "1"})] |
| 88 | +) |
| 89 | +def test_write_read_file(uri_builder, envs): |
| 90 | + """Test write/read file.""" |
| 91 | + uri, _, get_uri = uri_builder |
| 92 | + setup_env(uri, envs) |
| 93 | + |
| 94 | + # Setup and check preconditions. |
| 95 | + file_name = get_uri("writereadfile") |
| 96 | + if tf.io.gfile.exists(file_name): |
| 97 | + tf.io.gfile.remove(file_name) |
| 98 | + |
| 99 | + # Write data. |
| 100 | + with tf.io.gfile.GFile(file_name, "w") as w: |
| 101 | + w.write("Hello\n, world!") |
| 102 | + |
| 103 | + # Read data. |
| 104 | + with tf.io.gfile.GFile(file_name, "r") as r: |
| 105 | + file_read = r.read() |
| 106 | + assert file_read == "Hello\n, world!" |
0 commit comments