import pytest import time from ucc.azure import * @pytest.fixture(scope='module', autouse=True) def set_globals(path_client): global azure_client global container global test_root azure_client = path_client._client container = path_client._container test_root = path_client._chroot @pytest.fixture(scope='session', autouse=True) def clean_folder(request, path_client): c = path_client def fin(): c.rmdirs('/') request.addfinalizer(fin) def upload_file_to_restore_folder_to_pretend_already_rehydrate(obj): meta = azure_client.get_object_metadata(container, obj) copy_archived_obj = azure_client._make_copy_archived_obj_key(meta) azure_client.upload_object(container, copy_archived_obj, fp=io.BytesIO(b'a'), storage_class='Hot') def upload_object(obj=None, fp=None, storage_class=None): if obj is None: obj = f'upload_{time.time()}' obj = f'{test_root}/{obj}' if fp is None: fp = io.BytesIO(b'a') azure_client.upload_object(container, obj, fp, storage_class=storage_class) return obj def test_restore_object_with_restore_folder_not_exist(): obj = upload_object(storage_class='Archive') with pytest.raises(NotReadyError): azure_client.restore_object(container, obj) meta = azure_client.get_object_metadata(container, obj) copy_archived_obj = azure_client._make_copy_archived_obj_key(meta) meta = azure_client.get_object_metadata(container, copy_archived_obj) assert meta._raw['x-ms-archive-status'] == 'rehydrate-pending-to-hot', meta._raw def test_restore_object_with_restore_folder_existing(): obj = upload_object(storage_class='Archive') with pytest.raises(NotReadyError): azure_client.restore_object(container, obj) with pytest.raises(NotReadyError): azure_client.restore_object(container, obj) def test_restore_object_which_already_rehydrate(): obj = upload_object(storage_class='Archive') upload_file_to_restore_folder_to_pretend_already_rehydrate(obj) assert None is azure_client.restore_object(container, obj) def test_download_object_without_rehydrate(): obj = upload_object(storage_class='Archive') with pytest.raises(InvalidState): azure_client.download_object(container, obj) def test_download_object_during_rehydrating(): obj = upload_object(storage_class='Archive') with pytest.raises(NotReadyError): azure_client.restore_object(container, obj) with pytest.raises(InvalidState): azure_client.download_object(container, obj) def test_download_object_already_rehydrating(): obj = upload_object(storage_class='Archive') upload_file_to_restore_folder_to_pretend_already_rehydrate(obj) azure_client.download_object(container, obj)