blob: 1abf02a55e3a9eb3ff426f3fc8149740a1920646 [file]
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import gc
import io
import os
import time
import types
import unittest
import uuid
import weakref
from unittest import mock
import pyarrow.fs as pafs
from pyarrow.fs import PyFileSystem
from pypaimon.catalog.rest.rest_token import RESTToken
from pypaimon.catalog.rest.rest_token_file_io import RESTTokenFileIO
from pypaimon.common.identifier import Identifier
from pypaimon.common.options import Options
from pypaimon.common.options.config import OssOptions
from pypaimon.filesystem import jindo_file_system_handler as jindo_module
from pypaimon.filesystem import oss_user_agent
from pypaimon.filesystem import pyarrow_file_io as pyarrow_file_io_module
from pypaimon.filesystem.jindo_file_system_handler import (
JindoFileSystemHandler,
JindoInputFile,
JindoOutputFile,
JINDO_AVAILABLE,
)
class _RecordingConfig:
def __init__(self, values=None):
self.values = dict(values or {})
def set(self, key, value):
self.values[key] = value
class _RecordingInputStream:
def __init__(self, position=0):
self.position = position
self.seeks = []
self.reads = []
self.closed = False
def seek(self, position, whence=0):
self.seeks.append((position, whence))
self.position = position
return position
def tell(self):
return self.position
def read(self, size=None):
self.reads.append(size)
self.position += size or 0
return b"x" * (size or 0)
def close(self):
self.closed = True
class JindoInputFileTest(unittest.TestCase):
def test_known_size_avoids_backend_seek_from_end(self):
stream = _RecordingInputStream()
input_file = JindoInputFile(stream, file_size=100, owner=object())
self.assertEqual(95, input_file.seek(-5, os.SEEK_END))
self.assertEqual([(95, os.SEEK_SET)], stream.seeks)
def test_known_size_bounds_unlimited_read(self):
stream = _RecordingInputStream(position=40)
input_file = JindoInputFile(stream, file_size=100, owner=object())
self.assertEqual(b"x" * 60, input_file.read())
self.assertEqual([60], stream.reads)
class JindoConfigTest(unittest.TestCase):
def test_input_file_seek_returns_position(self):
class JindoStream:
def __init__(self):
self._stream = io.BytesIO(b"video")
def seek(self, offset, whence=io.SEEK_SET):
self._stream.seek(offset, whence)
def tell(self):
return self._stream.tell()
stream = JindoInputFile(JindoStream(), owner=object())
self.assertEqual(5, stream.seek(0, io.SEEK_END))
def test_forwards_native_options_to_connect(self):
created_config = _RecordingConfig()
config_factory = mock.Mock(return_value=created_config)
read_config = mock.Mock()
fake_jutil = types.SimpleNamespace(
Config=config_factory,
read_config=read_config,
)
options = Options({
"fs.oss.accesskeyid": "ak",
"fs.oss.accesskeysecret": "sk",
"fs.oss.securitytoken": "token",
OssOptions.OSS_ENDPOINT.key(): "https://cache-seed:80",
OssOptions.OSS_IMPL.key(): "jindo",
"fs.oss.dlf-cache.consistent-hash.enabled": True,
"fs.oss.dlf-cache.server.address": "http://cache-server:18101",
"fs.oss.https.enable": False,
"fs.oss.second.level.domain.enable": "true",
"fs.jindocache.client.metrics.enable": True,
"logger.dir": "/tmp/jindo-log",
"logger.verbose": 3,
"logger.console.log.enable": False,
"fs.oss.unset.option": None,
"metastore": "rest",
})
connect = mock.Mock(return_value=mock.sentinel.jindo_fs)
fake_jfs = types.SimpleNamespace(connect=connect)
with mock.patch.object(jindo_module, "JINDO_AVAILABLE", True), \
mock.patch.object(jindo_module, "jfs", fake_jfs), \
mock.patch.object(jindo_module, "jutil", fake_jutil):
handler = JindoFileSystemHandler("oss://bucket/", options)
config_factory.assert_called_once_with()
read_config.assert_not_called()
connect.assert_called_once_with("oss://bucket/", "root", mock.ANY)
self.assertIs(handler._jindo_fs, mock.sentinel.jindo_fs)
config = connect.call_args[0][2]
self.assertIs(config, created_config)
self.assertEqual(config.values["fs.oss.accessKeyId"], "ak")
self.assertEqual(config.values["fs.oss.accessKeySecret"], "sk")
self.assertEqual(config.values["fs.oss.securityToken"], "token")
self.assertEqual(config.values["fs.oss.endpoint"], "cache-seed:80")
self.assertEqual(
config.values["fs.oss.dlf-cache.consistent-hash.enabled"], "true")
self.assertEqual(
config.values["fs.oss.dlf-cache.server.address"],
"http://cache-server:18101")
self.assertEqual(config.values["fs.oss.https.enable"], "false")
self.assertEqual(
config.values["fs.oss.second.level.domain.enable"], "true")
self.assertEqual(
config.values["fs.jindocache.client.metrics.enable"], "true")
self.assertEqual(config.values["logger.dir"], "/tmp/jindo-log")
self.assertEqual(config.values["logger.verbose"], "3")
self.assertEqual(config.values["logger.console.log.enable"], "false")
self.assertEqual(
config.values["fs.oss.user.agent.features"], oss_user_agent.identity())
self.assertNotIn("fs.oss.user.agent.module", config.values)
self.assertNotIn("fs.oss.user.agent.extended", config.values)
self.assertNotIn(OssOptions.OSS_IMPL.key(), config.values)
self.assertNotIn("fs.oss.unset.option", config.values)
self.assertNotIn("metastore", config.values)
def test_does_not_load_external_config(self):
created_config = _RecordingConfig()
config_factory = mock.Mock(return_value=created_config)
read_config = mock.Mock(
side_effect=AssertionError("external config must not be loaded"))
fake_jutil = types.SimpleNamespace(
Config=config_factory,
read_config=read_config,
)
options = Options({
OssOptions.OSS_ENDPOINT.key(): "http://127.0.0.1:80",
"logger.verbose": "3",
})
with mock.patch.object(jindo_module, "JINDO_AVAILABLE", True), \
mock.patch.object(jindo_module, "jutil", fake_jutil):
config = jindo_module.build_jindo_config(options)
config_factory.assert_called_once_with()
read_config.assert_not_called()
self.assertIs(config, created_config)
self.assertEqual(config.values["fs.oss.endpoint"], "127.0.0.1:80")
self.assertEqual(config.values["logger.verbose"], "3")
self.assertNotIn("fs.oss.provider.endpoint", config.values)
self.assertNotIn("fs.oss.provider.format", config.values)
def test_user_agent_keeps_user_values_and_appends_access_tracking(self):
fake_jutil = types.SimpleNamespace(Config=_RecordingConfig)
options = Options({
"fs.oss.user.agent.module": "MyModule/1.0",
"fs.oss.user.agent.features": "Flink",
"fs.oss.user.agent.extended": "user/ext",
"dlf.access-tracking.extended-info": "acs/xxx k/v",
})
with mock.patch.object(jindo_module, "JINDO_AVAILABLE", True), \
mock.patch.object(jindo_module, "jutil", fake_jutil), \
mock.patch.object(oss_user_agent, "identity", return_value="pypaimon/2.2.dev0"):
config = jindo_module.build_jindo_config(options)
self.assertEqual(config.values["fs.oss.user.agent.module"], "MyModule/1.0")
self.assertEqual(
config.values["fs.oss.user.agent.features"], "pypaimon/2.2.dev0 Flink")
self.assertEqual(
config.values["fs.oss.user.agent.extended"], "user/ext acs/xxx k/v")
self.assertNotIn("dlf.access-tracking.extended-info", config.values)
def test_user_agent_from_common_keys(self):
fake_jutil = types.SimpleNamespace(Config=_RecordingConfig)
options = Options({
"user-agent.module": "MyApp/1.0",
"user-agent.features": "Flink",
"user-agent.extended": "vvr",
"dlf.access-tracking.extended-info": "uid/123",
})
with mock.patch.object(jindo_module, "JINDO_AVAILABLE", True), \
mock.patch.object(jindo_module, "jutil", fake_jutil), \
mock.patch.object(oss_user_agent, "identity", return_value="pypaimon/2.2.dev0"):
config = jindo_module.build_jindo_config(options)
self.assertEqual(config.values["fs.oss.user.agent.module"], "MyApp/1.0")
self.assertEqual(config.values["fs.oss.user.agent.features"], "pypaimon/2.2.dev0 Flink")
self.assertEqual(config.values["fs.oss.user.agent.extended"], "vvr uid/123")
def test_user_agent_extended_from_access_tracking_only(self):
fake_jutil = types.SimpleNamespace(Config=_RecordingConfig)
options = Options({"dlf.access-tracking.extended-info": "acs/xxx"})
with mock.patch.object(jindo_module, "JINDO_AVAILABLE", True), \
mock.patch.object(jindo_module, "jutil", fake_jutil):
config = jindo_module.build_jindo_config(options)
self.assertEqual(config.values["fs.oss.user.agent.extended"], "acs/xxx")
def test_forwards_native_options_to_jindo_oss_filesystem(self):
created_config = _RecordingConfig()
config_factory = mock.Mock(return_value=created_config)
fake_jutil = types.SimpleNamespace(Config=config_factory)
jindo_oss_filesystem = mock.Mock(return_value=mock.sentinel.jindo_oss_fs)
fake_jossfs = types.SimpleNamespace(JindoOssFileSystem=jindo_oss_filesystem)
options = Options({
"fs.oss.dlf-cache.consistent-hash.enabled": "true",
"fs.oss.dlf-cache.server.address": "http://cache-server:18101",
"logger.verbose": 3,
})
with mock.patch.object(jindo_module, "JINDO_AVAILABLE", True), \
mock.patch.object(jindo_module, "JINDO_OSSFS_AVAILABLE", True), \
mock.patch.object(jindo_module, "jutil", fake_jutil), \
mock.patch.object(jindo_module, "jossfs", fake_jossfs):
filesystem = jindo_module.create_jindo_oss_filesystem(
"oss://bucket/", options)
self.assertIs(filesystem, mock.sentinel.jindo_oss_fs)
config_factory.assert_called_once_with()
jindo_oss_filesystem.assert_called_once_with(
uri="oss://bucket/",
config=created_config,
auto_mkdir=False,
skip_instance_cache=True,
)
self.assertEqual(
created_config.values["fs.oss.dlf-cache.consistent-hash.enabled"],
"true")
self.assertEqual(
created_config.values["fs.oss.dlf-cache.server.address"],
"http://cache-server:18101")
self.assertEqual(created_config.values["logger.verbose"], "3")
class _EvictionRecordingFileSystem:
"""Fake ``jfs.connect`` result: owns the native store, records teardown."""
def __init__(self, events):
self.events = events
def open(self, path, mode):
return _EvictionRecordingNativeStream(self, self.events)
def __del__(self):
self.events.append('fs-destroyed')
class _EvictionRecordingNativeStream:
"""Fake pyjindo stream: non-owning reference to the native store."""
def __init__(self, fake_fs, events):
# Weak on purpose: the native stream must not keep the store alive.
self._fs = weakref.ref(fake_fs)
self._events = events
self.closed = False
def read(self, size=None):
if self._fs() is None:
raise RuntimeError('use-after-free: read after store teardown')
return b'x' * (size or 1)
def pread(self, size, offset):
return self.read(size)
def write(self, data):
if self._fs() is None:
raise RuntimeError('use-after-free: write after store teardown')
return len(data)
def seek(self, position, whence=0):
return position
def tell(self):
return 0
def close(self):
if self.closed:
return
if self._fs() is None:
# Emulates the native use-after-free the keep-alive prevents.
self._events.append('stream-closed-after-store-destroyed')
return
self.closed = True
self._events.append('stream-closed')
def __del__(self):
# pyjindo closes its native streams when they are released.
if not self.closed:
self.close()
class JindoKeepAliveTest(unittest.TestCase):
"""Streams keep the native store alive across a FileIO cache eviction and close
before the store is released, without relying on attribute dealloc order."""
@staticmethod
def _handler_with_recording_fs(events):
options = Options({
"fs.oss.impl": "jindo",
"fs.oss.endpoint": "https://oss-cn-hangzhou.aliyuncs.com",
"fs.oss.accessKeyId": "test-ak",
"fs.oss.accessKeySecret": "test-sk",
})
fake_fs = _EvictionRecordingFileSystem(events)
fake_jfs = types.SimpleNamespace(connect=lambda *a, **kw: fake_fs)
with mock.patch.object(jindo_module, "JINDO_AVAILABLE", True), \
mock.patch.object(jindo_module, "jfs", fake_jfs), \
mock.patch.object(jindo_module, "jutil",
types.SimpleNamespace(Config=_RecordingConfig)):
handler = JindoFileSystemHandler("oss://bucket/", options)
return handler, weakref.ref(fake_fs), weakref.ref(handler)
def test_evicted_filesystem_outlives_open_stream(self):
events = []
handler, fs_ref, handler_ref = self._handler_with_recording_fs(events)
input_file = handler.open_input_file('object')
# A FileIO cache eviction drops the handler chain: the handler (and its
# credentials) must die, the native store must not.
del handler
gc.collect()
self.assertIsNone(
handler_ref(), 'eviction must not pin the handler')
self.assertIsNotNone(
fs_ref(), 'eviction freed the native store while the stream was open')
# The stream keeps reading after eviction.
self.assertEqual(b'x', input_file.read(1))
# Closing releases the keep-alive: the still-referenced closed file
# no longer pins the store, and the stream closes before teardown.
input_file.close()
self.assertEqual(['stream-closed', 'fs-destroyed'], events)
self.assertIsNone(fs_ref())
del input_file
gc.collect()
# No double close on finalization.
self.assertEqual(['stream-closed', 'fs-destroyed'], events)
def test_evicted_filesystem_outlives_open_output_stream(self):
events = []
handler, fs_ref, handler_ref = self._handler_with_recording_fs(events)
output_file = handler.open_output_stream('object', None)
del handler
gc.collect()
self.assertIsNone(handler_ref())
self.assertIsNotNone(fs_ref())
self.assertEqual(4, output_file.write(b'data'))
output_file.close()
self.assertEqual(['stream-closed', 'fs-destroyed'], events)
self.assertIsNone(fs_ref())
def test_finalizer_closes_stream_before_store_teardown(self):
# Attribute release order is a CPython implementation detail: if a
# future refactor assigns _owner before _stream in __init__, the
# owner would be released first on dealloc. The finalizer must still
# close the stream while the owner pins the store.
events = []
fs = _EvictionRecordingFileSystem(events)
fs_ref = weakref.ref(fs)
stream = _EvictionRecordingNativeStream(fs, events)
class _ReversedInitInputFile(JindoInputFile):
def __init__(self, jindo_stream, owner):
self._owner = owner
self._stream = jindo_stream
self._file_size = None
self._closed = False
input_file = _ReversedInitInputFile(stream, owner=fs)
del fs, stream
gc.collect()
self.assertIsNotNone(fs_ref())
# Drop without closing: the finalizer must close the stream first...
del input_file
gc.collect()
self.assertIsNone(fs_ref())
# ...and the store must be destroyed only afterwards.
self.assertEqual(
['stream-closed', 'fs-destroyed'], events,
'native stream must be closed before the store is destroyed')
def test_owner_argument_is_required(self):
with self.assertRaises(TypeError):
JindoInputFile(_RecordingInputStream())
with self.assertRaises(TypeError):
JindoOutputFile(_RecordingInputStream())
class JindoTokenRefreshTest(unittest.TestCase):
"""RESTTokenFileIO over the Jindo backend across a data token refresh."""
def setUp(self):
RESTTokenFileIO._FILE_IO_CACHE.clear()
self.addCleanup(RESTTokenFileIO._FILE_IO_CACHE.clear)
@staticmethod
def _token(security_token, expire_at_millis):
return RESTToken({
OssOptions.OSS_ENDPOINT.key(): "oss-cn-hangzhou.aliyuncs.com",
OssOptions.OSS_ACCESS_KEY_ID.key(): "STS.test-ak",
OssOptions.OSS_ACCESS_KEY_SECRET.key(): "test-sk",
OssOptions.OSS_SECURITY_TOKEN.key(): security_token,
}, expire_at_millis)
def test_token_refresh_keeps_previous_jindo_handler(self):
events = []
connected = []
def connect(*args, **kwargs):
fs = _EvictionRecordingFileSystem(events)
connected.append(weakref.ref(fs))
return fs
table_path = "oss://bucket/db.db/t"
expire_at_millis = int(time.time() * 1000) + 7_200_000
with mock.patch.object(jindo_module, "JINDO_AVAILABLE", True), \
mock.patch.object(pyarrow_file_io_module, "JINDO_AVAILABLE", True), \
mock.patch.object(jindo_module, "jfs", types.SimpleNamespace(connect=connect)), \
mock.patch.object(jindo_module, "jutil",
types.SimpleNamespace(Config=_RecordingConfig)), \
mock.patch.object(RESTTokenFileIO, "try_to_refresh_token"):
readers = [RESTTokenFileIO(Identifier.create("db", "t"), table_path,
Options({OssOptions.OSS_IMPL.key(): "jindo"}))
for _ in range(2)]
for reader in readers:
reader.token = self._token("sts-1", expire_at_millis)
stream = readers[0].new_input_stream(table_path + "/bucket-0/data.blob")
handler_ref = weakref.ref(readers[0].filesystem.handler)
self.assertIs(readers[0].file_io(), readers[1].file_io())
self.assertEqual(1, len(connected))
for reader in readers:
reader.token = self._token("sts-2", expire_at_millis + 1)
self.assertIs(readers[0].file_io(), readers[1].file_io())
gc.collect()
self.assertEqual(2, len(connected))
self.assertIsNotNone(handler_ref(), "token refresh released the previous handler")
self.assertEqual(b"x", stream.read(1))
stream.close()
self.assertEqual(["stream-closed"], events)
class JindoFileSystemTest(unittest.TestCase):
"""Test cases for JindoFileSystem."""
def setUp(self):
"""Set up test fixtures."""
if not JINDO_AVAILABLE:
self.skipTest("pyjindo is not available")
# Get OSS credentials from environment variables or use test values
bucket = os.environ.get("OSS_TEST_BUCKET")
access_key_id = os.environ.get("OSS_ACCESS_KEY_ID")
access_key_secret = os.environ.get("OSS_ACCESS_KEY_SECRET")
endpoint = os.environ.get("OSS_ENDPOINT")
if not bucket:
self.skipTest("test bucket is not configured")
return
if not access_key_id:
self.skipTest("test access key id is not configured")
return
if not access_key_secret:
self.skipTest("test access key secret is not configured")
return
if not endpoint:
self.skipTest("test endpoint is not configured")
return
self.root_path = f"oss://{bucket}/"
self.catalog_options = Options({
OssOptions.OSS_ACCESS_KEY_ID.key(): access_key_id,
OssOptions.OSS_ACCESS_KEY_SECRET.key(): access_key_secret,
OssOptions.OSS_ENDPOINT.key(): endpoint,
})
# Create JindoFileSystemHandler instance
fs_handler = JindoFileSystemHandler(self.root_path, self.catalog_options)
self.fs = PyFileSystem(fs_handler)
# Create unique test prefix to avoid conflicts
self.test_prefix = f"test-{uuid.uuid4().hex[:8]}/"
def tearDown(self):
"""Clean up test files and directories."""
# Delete the entire test prefix directory
test_dir = f"{self.root_path}{self.test_prefix}"
try:
file_info = self.fs.get_file_info(test_dir)
if file_info.type == pafs.FileType.Directory:
try:
self.fs.delete_dir(test_dir)
except Exception:
pass
except Exception:
pass # Ignore cleanup errors
def _get_test_path(self, name: str) -> str:
"""Get a test path under test_prefix."""
return f"{self.root_path}{self.test_prefix}{name}"
def test_get_root_file_info(self):
"""Test get_file_info for root path."""
# Verify directory exists using get_file_info
file_info = self.fs.get_file_info(self.root_path)
# Verify the returned file info
self.assertIsInstance(file_info, pafs.FileInfo)
self.assertEqual(file_info.type, pafs.FileType.Directory)
def test_create_dir_recursive(self):
"""Test create_dir with recursive=True."""
test_dir = self._get_test_path("nested/deep/dir/")
# Create nested directory
self.fs.create_dir(test_dir, recursive=True)
# Verify directory exists
file_info = self.fs.get_file_info(test_dir)
self.assertEqual(file_info.type, pafs.FileType.Directory)
def test_write_and_read_small_file(self):
"""Test writing and reading a small file (10 bytes) with random data."""
test_file = self._get_test_path("small-file.txt")
test_data = os.urandom(10)
# Write file
out_stream = self.fs.open_output_stream(test_file)
out_stream.write(test_data)
out_stream.close()
# Read file
with self.fs.open_input_file(test_file) as in_file:
read_data = in_file.read()
# Verify data correctness
self.assertEqual(test_data, read_data)
self.assertEqual(len(read_data), 10)
# Verify file info
file_info = self.fs.get_file_info(test_file)
self.assertEqual(file_info.type, pafs.FileType.File)
self.assertEqual(file_info.size, 10)
def test_write_and_read_small_file_with_context(self):
"""Test writing and reading a small file (10 bytes) with random data using context manager."""
test_file = self._get_test_path("small-file-with-context.txt")
test_data = os.urandom(10)
# Write file using context manager
with self.fs.open_output_stream(test_file) as out_stream:
out_stream.write(test_data)
# Read file
with self.fs.open_input_file(test_file) as in_file:
read_data = in_file.read()
# Verify data correctness
self.assertEqual(test_data, read_data)
self.assertEqual(len(read_data), 10)
# Verify file info
file_info = self.fs.get_file_info(test_file)
self.assertEqual(file_info.type, pafs.FileType.File)
self.assertEqual(file_info.size, 10)
def test_write_and_read_large_file(self):
"""Test writing and reading a large file (20MB) with random data."""
test_file = self._get_test_path("large-file.bin")
file_size = 20 * 1024 * 1024 # 20MB
test_data = os.urandom(file_size)
# Write file in chunks
chunk_size = 1024 * 1024 # 1MB chunks
out_stream = self.fs.open_output_stream(test_file)
for i in range(0, len(test_data), chunk_size):
chunk = test_data[i:i + chunk_size]
out_stream.write(chunk)
out_stream.close()
# Verify file info
file_info = self.fs.get_file_info(test_file)
self.assertEqual(file_info.type, pafs.FileType.File)
self.assertEqual(file_info.size, file_size)
# Test JindoInputFile all methods
with self.fs.open_input_file(test_file) as in_file:
# Test tell() - should be at position 0 initially
self.assertEqual(in_file.tell(), 0)
# Test read() with specific size
chunk1 = in_file.read(1024)
self.assertEqual(len(chunk1), 1024)
self.assertEqual(in_file.tell(), 1024)
self.assertEqual(chunk1, test_data[0:1024])
# Test seek() - seek to middle of file
middle_pos = file_size // 2
in_file.seek(middle_pos)
self.assertEqual(in_file.tell(), middle_pos)
# Test read_at()
read_at_data = in_file.read_at(1024, 0)
self.assertEqual(len(read_at_data), 1024)
self.assertEqual(read_at_data, test_data[0:1024])
# Test read_at() at different offset
offset = file_size - 2048
read_at_data2 = in_file.read_at(1024, offset)
self.assertEqual(len(read_at_data2), 1024)
self.assertEqual(read_at_data2, test_data[offset:offset + 1024])
# Test seek() to end
in_file.seek(file_size)
self.assertEqual(in_file.tell(), file_size)
# Test read() at end - should return empty bytes
empty_data = in_file.read(100)
self.assertEqual(len(empty_data), 0)
# Test read() without size - read remaining
in_file.seek(0)
all_data = in_file.read()
self.assertEqual(len(all_data), file_size)
self.assertEqual(all_data, test_data)
# Verify complete file read
with self.fs.open_input_file(test_file) as in_file:
complete_data = in_file.read()
self.assertEqual(complete_data, test_data)
self.assertEqual(len(complete_data), file_size)
def test_get_file_info_single_path(self):
"""Test get_file_info with single path."""
test_file = self._get_test_path("info-test.txt")
test_data = b"test content"
# Write file
out_stream = self.fs.open_output_stream(test_file)
out_stream.write(test_data)
out_stream.close()
# Get file info
file_info = self.fs.get_file_info(test_file)
self.assertIsInstance(file_info, pafs.FileInfo)
self.assertEqual(file_info.type, pafs.FileType.File)
self.assertEqual(file_info.size, len(test_data))
def test_get_file_info_list(self):
"""Test get_file_info with list of paths."""
test_file = self._get_test_path("info-test1.txt")
test_dir = self._get_test_path("dir1")
# Write files
out_stream = self.fs.open_output_stream(test_file)
out_stream.write(b"content1")
out_stream.close()
self.fs.create_dir(test_dir)
# Get file info for list
file_infos = self.fs.get_file_info([test_file, test_dir])
self.assertIsInstance(file_infos, list)
self.assertEqual(len(file_infos), 2)
self.assertEqual(file_infos[0].type, pafs.FileType.File)
self.assertTrue(test_file in file_infos[0].path)
self.assertEqual(file_infos[1].type, pafs.FileType.Directory)
self.assertTrue(test_dir in file_infos[1].path)
def test_get_file_info_with_selector(self):
"""Test get_file_info with FileSelector."""
test_dir = self._get_test_path("selector-test/")
test_file1 = self._get_test_path("selector-test/file1.txt")
test_file2 = self._get_test_path("selector-test/file2.txt")
# Create files
self.fs.create_dir(test_dir)
out_stream1 = self.fs.open_output_stream(test_file1)
out_stream1.write(b"content1")
out_stream1.close()
out_stream2 = self.fs.open_output_stream(test_file2)
out_stream2.write(b"content2")
out_stream2.close()
# Test non-recursive listing
selector = pafs.FileSelector(test_dir, recursive=False, allow_not_found=False)
file_infos = self.fs.get_file_info(selector)
self.assertIsInstance(file_infos, list)
self.assertEqual(len(file_infos), 2)
# Verify we got the files
file_names = [info.path for info in file_infos]
self.assertTrue(any("file1.txt" in name for name in file_names))
self.assertTrue(any("file2.txt" in name for name in file_names))
def test_get_file_info_with_selector_recursive(self):
"""Test get_file_info with FileSelector recursive=True."""
test_dir = self._get_test_path("selector-recursive/")
test_subdir = self._get_test_path("selector-recursive/subdir/")
test_file1 = self._get_test_path("selector-recursive/file1.txt")
test_file2 = self._get_test_path("selector-recursive/subdir/file2.txt")
# Create directory structure
self.fs.create_dir(test_dir)
self.fs.create_dir(test_subdir)
out_stream1 = self.fs.open_output_stream(test_file1)
out_stream1.write(b"content1")
out_stream1.close()
out_stream2 = self.fs.open_output_stream(test_file2)
out_stream2.write(b"content2")
out_stream2.close()
# Test recursive listing
selector = pafs.FileSelector(test_dir, recursive=True, allow_not_found=False)
file_infos = self.fs.get_file_info(selector)
self.assertIsInstance(file_infos, list)
self.assertEqual(len(file_infos), 3)
def test_get_file_info_not_found(self):
"""Test get_file_info for non-existent file."""
non_existent = self._get_test_path("non-existent-file.txt")
file_info = self.fs.get_file_info(non_existent)
self.assertEqual(file_info.type, pafs.FileType.NotFound)
# Try to open non-existent file should raise FileNotFoundError
with self.assertRaises(FileNotFoundError):
with self.fs.open_input_file(non_existent) as in_file:
in_file.read()
def test_get_file_info_selector_allow_not_found(self):
"""Test get_file_info with FileSelector allow_not_found=True."""
non_existent_dir = self._get_test_path("non-existent-dir/")
# Test with allow_not_found=True
selector = pafs.FileSelector(non_existent_dir, recursive=False, allow_not_found=True)
file_infos = self.fs.get_file_info(selector)
self.assertIsInstance(file_infos, list)
self.assertEqual(len(file_infos), 0)
def test_delete_file(self):
"""Test delete_file method."""
test_file = self._get_test_path("delete-test.txt")
# Create file
out_stream = self.fs.open_output_stream(test_file)
out_stream.write(b"test content")
out_stream.close()
# Verify file exists
file_info = self.fs.get_file_info(test_file)
self.assertEqual(file_info.type, pafs.FileType.File)
# Delete file
self.fs.delete_file(test_file)
# Verify file is deleted - should raise FileNotFoundError when accessing
with self.assertRaises(FileNotFoundError):
with self.fs.open_input_file(test_file) as in_file:
in_file.read()
def test_delete_dir(self):
"""Test delete_dir method."""
test_dir = self._get_test_path("delete-dir/")
# Create directory
self.fs.create_dir(test_dir)
# Verify directory exists
file_info = self.fs.get_file_info(test_dir)
self.assertEqual(file_info.type, pafs.FileType.Directory)
# Delete directory
self.fs.delete_dir(test_dir)
# Verify directory is deleted - should raise FileNotFoundError when accessing
with self.assertRaises(FileNotFoundError):
# Try to list directory contents
selector = pafs.FileSelector(test_dir, recursive=False, allow_not_found=False)
self.fs.get_file_info(selector)
def test_delete_dir_contents(self):
"""Test delete_dir_contents method."""
test_dir = self._get_test_path("delete-contents/")
test_file1 = self._get_test_path("delete-contents/file1.txt")
test_file2 = self._get_test_path("delete-contents/file2.txt")
test_subdir = self._get_test_path("delete-contents/subdir/")
test_file3 = self._get_test_path("delete-contents/subdir/file3.txt")
# Create directory structure
self.fs.create_dir(test_dir)
out_stream1 = self.fs.open_output_stream(test_file1)
out_stream1.write(b"content1")
out_stream1.close()
out_stream2 = self.fs.open_output_stream(test_file2)
out_stream2.write(b"content2")
out_stream2.close()
self.fs.create_dir(test_subdir)
out_stream3 = self.fs.open_output_stream(test_file3)
out_stream3.write(b"content3")
out_stream3.close()
# Delete directory contents
self.fs.delete_dir_contents(test_dir)
# Verify directory still exists but is empty
file_info = self.fs.get_file_info(test_dir)
self.assertEqual(file_info.type, pafs.FileType.Directory)
selector = pafs.FileSelector(test_dir, recursive=False, allow_not_found=False)
file_infos = self.fs.get_file_info(selector)
self.assertEqual(len(file_infos), 0)
# Verify files are deleted - should raise FileNotFoundError when accessing
with self.assertRaises(FileNotFoundError):
with self.fs.open_input_file(test_file1) as in_file:
in_file.read()
with self.assertRaises(FileNotFoundError):
with self.fs.open_input_file(test_file2) as in_file:
in_file.read()
with self.assertRaises(FileNotFoundError):
with self.fs.open_input_file(test_file3) as in_file:
in_file.read()
def test_move_file(self):
"""Test move method for file."""
src_file = self._get_test_path("move-src.txt")
dst_file = self._get_test_path("move-dst.txt")
test_data = b"move test content"
# Create source file
out_stream = self.fs.open_output_stream(src_file)
out_stream.write(test_data)
out_stream.close()
# Move file
self.fs.move(src_file, dst_file)
# Verify source is gone - should raise FileNotFoundError when accessing
with self.assertRaises(FileNotFoundError):
with self.fs.open_input_file(src_file) as in_file:
in_file.read()
# Verify destination exists with correct content
dst_info = self.fs.get_file_info(dst_file)
self.assertEqual(dst_info.type, pafs.FileType.File)
with self.fs.open_input_file(dst_file) as in_file:
read_data = in_file.read()
self.assertEqual(read_data, test_data)
def test_move_directory(self):
"""Test move method for directory."""
src_dir = self._get_test_path("move-src-dir/")
dst_dir = self._get_test_path("move-dst-dir/")
src_file = self._get_test_path("move-src-dir/file.txt")
# Create source directory and file
self.fs.create_dir(src_dir)
out_stream = self.fs.open_output_stream(src_file)
out_stream.write(b"test content")
out_stream.close()
# Move directory
self.fs.move(src_dir, dst_dir)
# Verify source is gone - should raise FileNotFoundError when accessing
with self.assertRaises(FileNotFoundError):
# Try to list directory contents
selector = pafs.FileSelector(src_dir, recursive=False, allow_not_found=False)
self.fs.get_file_info(selector)
# Verify destination exists
dst_info = self.fs.get_file_info(dst_dir)
self.assertEqual(dst_info.type, pafs.FileType.Directory)
def test_copy_file(self):
"""Test copy_file method."""
src_file = self._get_test_path("copy-src.txt")
dst_file = self._get_test_path("copy-dst.txt")
test_data = os.urandom(1024 * 1024) # 1MB random data
# Create source file
out_stream = self.fs.open_output_stream(src_file)
out_stream.write(test_data)
out_stream.close()
# Copy file
self.fs.copy_file(src_file, dst_file)
# Verify both files exist
src_info = self.fs.get_file_info(src_file)
self.assertEqual(src_info.type, pafs.FileType.File)
dst_info = self.fs.get_file_info(dst_file)
self.assertEqual(dst_info.type, pafs.FileType.File)
# Verify destination content matches source
with self.fs.open_input_file(dst_file) as in_file:
read_data = in_file.read()
self.assertEqual(read_data, test_data)
self.assertEqual(len(read_data), len(test_data))
def test_delete_nonexistent_file(self):
"""Test deleting non-existent file."""
non_existent = self._get_test_path("non-existent-delete.txt")
# Try to delete non-existent file
with self.assertRaises(IOError):
self.fs.delete_file(non_existent)
def test_multiple_sequential_reads(self):
"""Test multiple sequential reads from same file."""
test_file = self._get_test_path("sequential-read.txt")
test_data = os.urandom(10000) # 10KB
# Write file
out_stream = self.fs.open_output_stream(test_file)
out_stream.write(test_data)
out_stream.close()
# Read in chunks sequentially
with self.fs.open_input_file(test_file) as in_file:
chunk1 = in_file.read(1000)
chunk2 = in_file.read(2000)
chunk3 = in_file.read(3000)
chunk4 = in_file.read() # Read rest
# Verify all chunks
self.assertEqual(chunk1, test_data[0:1000])
self.assertEqual(chunk2, test_data[1000:3000])
self.assertEqual(chunk3, test_data[3000:6000])
self.assertEqual(chunk4, test_data[6000:])
# Verify total length
total_read = len(chunk1) + len(chunk2) + len(chunk3) + len(chunk4)
self.assertEqual(total_read, len(test_data))
if __name__ == '__main__':
unittest.main()