Skip to content

Commit

Permalink
tests: switch to lighter-weight custom scheme tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kujenga committed Sep 3, 2024
1 parent 1b3f153 commit 3cf711a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 68 deletions.
86 changes: 19 additions & 67 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,46 @@
import os
import shutil
from pathlib import Path, PurePosixPath
import shutil
from tempfile import TemporaryDirectory
from typing import Dict, Optional

import boto3
import botocore
from azure.storage.blob import BlobServiceClient
from azure.storage.filedatalake import (
DataLakeServiceClient,
)
import boto3
import botocore
from dotenv import find_dotenv, load_dotenv
from google.cloud import storage as google_storage
from pytest_cases import fixture, fixture_union
from shortuuid import uuid
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed

import cloudpathlib.azure.azblobclient
import cloudpathlib.s3.s3client
from cloudpathlib import (
AzureBlobClient,
AzureBlobPath,
GSClient,
GSPath,
S3Client,
S3Path,
)
from cloudpathlib.azure.azblobclient import _hns_rmtree
from cloudpathlib.client import register_client_class
from cloudpathlib.cloudpath import implementation_registry, register_path_class
from cloudpathlib import AzureBlobClient, AzureBlobPath, GSClient, GSPath, S3Client, S3Path
from cloudpathlib.cloudpath import implementation_registry
from cloudpathlib.local import (
local_azure_blob_implementation,
LocalAzureBlobClient,
LocalAzureBlobPath,
local_gs_implementation,
LocalGSClient,
LocalGSPath,
local_s3_implementation,
LocalS3Client,
LocalS3Path,
local_azure_blob_implementation,
local_gs_implementation,
local_s3_implementation,
)

import cloudpathlib.azure.azblobclient
from cloudpathlib.azure.azblobclient import _hns_rmtree
import cloudpathlib.s3.s3client
from .mock_clients.mock_azureblob import MockBlobServiceClient, DEFAULT_CONTAINER_NAME
from .mock_clients.mock_adls_gen2 import MockedDataLakeServiceClient
from .mock_clients.mock_azureblob import DEFAULT_CONTAINER_NAME, MockBlobServiceClient
from .mock_clients.mock_gs import (
mocked_client_class_factory as mocked_gsclient_class_factory,
DEFAULT_GS_BUCKET_NAME,
MockTransferManager,
)
from .mock_clients.mock_gs import (
mocked_client_class_factory as mocked_gsclient_class_factory,
)
from .mock_clients.mock_s3 import DEFAULT_S3_BUCKET_NAME, mocked_session_class_factory
from .mock_clients.mock_s3 import mocked_session_class_factory, DEFAULT_S3_BUCKET_NAME


if os.getenv("USE_LIVE_CLOUD") == "1":
load_dotenv(find_dotenv())
Expand Down Expand Up @@ -317,7 +307,8 @@ def s3_rig(request, monkeypatch, assets_dir):
bucket.objects.filter(Prefix=test_dir).delete()


def _custom_s3_rig_helper(request, monkeypatch, assets_dir, path_class, client_class):
@fixture()
def custom_s3_rig(request, monkeypatch, assets_dir):
"""
Custom S3 rig used to test the integrations with non-AWS S3-compatible object storages like
- MinIO (https://min.io/)
Expand Down Expand Up @@ -379,8 +370,8 @@ def _spin_up_bucket():
)

rig = CloudProviderTestRig(
path_class=path_class,
client_class=client_class,
path_class=S3Path,
client_class=S3Client,
drive=drive,
test_dir=test_dir,
live_server=live_server,
Expand All @@ -402,43 +393,6 @@ def _spin_up_bucket():
bucket.objects.filter(Prefix=test_dir).delete()


@fixture()
def custom_s3_rig(request, monkeypatch, assets_dir):
"""
Custom S3 rig used to test the integrations with non-AWS S3-compatible object storages like
- MinIO (https://min.io/)
- CEPH (https://ceph.io/ceph-storage/object-storage/)
- others
"""
yield from _custom_s3_rig_helper(request, monkeypatch, assets_dir, S3Path, S3Client)


@register_path_class("mys3")
class MyS3Path(S3Path):
cloud_prefix: str = "mys3://"


@register_client_class("mys3")
class MyS3Client(S3Client):
pass


# Mirrors the definition of the S3Client class
MyS3Client.MyS3Path = MyS3Client.CloudPath # type: ignore


@fixture()
def custom_scheme_s3_rig(request, monkeypatch, assets_dir):
"""
Custom S3 rig used to test the integrations with non-AWS S3-compatible object storages like
- MinIO (https://min.io/)
- CEPH (https://ceph.io/ceph-storage/object-storage/)
- others
with the addition of a custom scheme being used.
"""
yield from _custom_s3_rig_helper(request, monkeypatch, assets_dir, MyS3Path, MyS3Client)


@fixture()
def local_azure_rig(request, monkeypatch, assets_dir):
drive = os.getenv("LIVE_AZURE_CONTAINER", DEFAULT_CONTAINER_NAME)
Expand Down Expand Up @@ -532,7 +486,6 @@ def local_s3_rig(request, monkeypatch, assets_dir):
gs_rig,
s3_rig,
custom_s3_rig,
custom_scheme_s3_rig,
local_azure_rig,
local_s3_rig,
local_gs_rig,
Expand All @@ -545,6 +498,5 @@ def local_s3_rig(request, monkeypatch, assets_dir):
[
s3_rig,
custom_s3_rig,
custom_scheme_s3_rig,
],
)
36 changes: 35 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import mimetypes
import os
from pathlib import Path
import random
import string
from pathlib import Path

from cloudpathlib import CloudPath
from cloudpathlib.client import register_client_class
from cloudpathlib.cloudpath import register_path_class
from cloudpathlib.s3.s3client import S3Client
from cloudpathlib.s3.s3path import S3Path


def test_default_client_instantiation(rig):
Expand Down Expand Up @@ -116,3 +119,34 @@ def my_content_type(path):

for suffix, content_type in mimes:
_test_write_content_type(suffix, content_type, rig)


@register_path_class("mys3")
class MyS3Path(S3Path):
cloud_prefix: str = "mys3://"


@register_client_class("mys3")
class MyS3Client(S3Client):
pass


def test_custom_mys3path_instantiation():
path = MyS3Path("mys3://bucket/dir/file.txt")
assert isinstance(path, MyS3Path)
assert path.cloud_prefix == "mys3://"
assert path.bucket == "bucket"
assert path.key == "dir/file.txt"


def test_custom_mys3client_instantiation():
client = MyS3Client()
assert isinstance(client, MyS3Client)
assert client.CloudPath("mys3://bucket/dir/file.txt").cloud_prefix == "mys3://"


def test_custom_mys3client_default_client():
MyS3Client().set_as_default_client()
path = CloudPath("mys3://bucket/dir/file.txt")
assert isinstance(path.client, MyS3Client)
assert path.cloud_prefix == "mys3://"

0 comments on commit 3cf711a

Please sign in to comment.