Ejemplo n.º 1
0
class AzureStorageAccount:
    def __init__(self, name, key):
        self.name = name
        self.key = key
        self.url = 'https://' + self.name + '.file.core.windows.net'
        self._client = ShareServiceClient(account_url=self.url,
                                          credential=self.key)

    def get_share_names(self):
        return [share.name for share in self._client.list_shares()]

    def get_share(self, name):
        share = self._client.get_share_client(name)
        return AzureStorageShare(account=self, name=name, client=share)

    def create_share(self, name):
        self._client.create_share(share_name=name)

    def delete_share(self, name):
        self._client.delete_share(share_name=name)
class StorageDirectoryTest(StorageTestCase):
    def _setup(self, storage_account_name, storage_account_key):
        url = self.account_url(storage_account_name, "file")
        credential = storage_account_key
        self.fsc = ShareServiceClient(url, credential=credential)
        self.share_name = self.get_resource_name('utshare')

        if not self.is_playback():
            try:
                self.fsc.create_share(self.share_name)
            except:
                pass

    def _teardown(self, FILE_PATH):
        if os.path.isfile(FILE_PATH):
            try:
                os.remove(FILE_PATH)
            except:
                pass

    # --Helpers-----------------------------------------------------------------

    # --Test cases for directories ----------------------------------------------
    @FileSharePreparer()
    def test_create_directories(self, storage_account_name,
                                storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        # Act
        created = share_client.create_directory('dir1')

        # Assert
        self.assertTrue(created)

    @FileSharePreparer()
    def test_create_directories_with_metadata(self, storage_account_name,
                                              storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        metadata = {'hello': 'world', 'number': '42'}

        # Act
        directory = share_client.create_directory('dir1', metadata=metadata)

        # Assert
        md = directory.get_directory_properties().metadata
        self.assertDictEqual(md, metadata)

    @FileSharePreparer()
    def test_create_directories_fail_on_exist(self, storage_account_name,
                                              storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        # Act
        created = share_client.create_directory('dir1')
        with self.assertRaises(ResourceExistsError):
            share_client.create_directory('dir1')

        # Assert
        self.assertTrue(created)

    @FileSharePreparer()
    def test_create_subdirectories(self, storage_account_name,
                                   storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        created = directory.create_subdirectory('dir2')

        # Assert
        self.assertTrue(created)
        self.assertEqual(created.directory_path, 'dir1/dir2')

    @FileSharePreparer()
    def test_create_subdirectories_with_metadata(self, storage_account_name,
                                                 storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        metadata = {'hello': 'world', 'number': '42'}

        # Act
        created = directory.create_subdirectory('dir2', metadata=metadata)

        # Assert
        self.assertTrue(created)
        self.assertEqual(created.directory_path, 'dir1/dir2')
        sub_metadata = created.get_directory_properties().metadata
        self.assertEqual(sub_metadata, metadata)

    @FileSharePreparer()
    def test_create_file_in_directory(self, storage_account_name,
                                      storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        file_data = b'12345678' * 1024
        file_name = self.get_resource_name('file')
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        new_file = directory.upload_file(file_name, file_data)

        # Assert
        file_content = new_file.download_file().readall()
        self.assertEqual(file_content, file_data)

    @FileSharePreparer()
    def test_delete_file_in_directory(self, storage_account_name,
                                      storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        file_name = self.get_resource_name('file')
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        new_file = directory.upload_file(file_name, "hello world")

        # Act
        deleted = directory.delete_file(file_name)

        # Assert
        self.assertIsNone(deleted)
        with self.assertRaises(ResourceNotFoundError):
            new_file.get_file_properties()

    @FileSharePreparer()
    def test_share_directory_exists(self, storage_account_name,
                                    storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        directory2 = share_client.get_directory_client("dir2")

        self.assertTrue(directory.exists())
        self.assertFalse(directory2.exists())

    @FileSharePreparer()
    def test_delete_subdirectories(self, storage_account_name,
                                   storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory('dir2')

        # Act
        deleted = directory.delete_subdirectory('dir2')

        # Assert
        self.assertIsNone(deleted)
        subdir = directory.get_subdirectory_client('dir2')
        with self.assertRaises(ResourceNotFoundError):
            subdir.get_directory_properties()

    @FileSharePreparer()
    def test_get_directory_properties(self, storage_account_name,
                                      storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        props = directory.get_directory_properties()

        # Assert
        self.assertIsNotNone(props)
        self.assertIsNotNone(props.etag)
        self.assertIsNotNone(props.last_modified)

    @FileSharePreparer()
    def test_get_directory_properties_with_snapshot(self, storage_account_name,
                                                    storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        metadata = {"test1": "foo", "test2": "bar"}
        directory = share_client.create_directory('dir1', metadata=metadata)
        snapshot1 = share_client.create_snapshot()
        metadata2 = {"test100": "foo100", "test200": "bar200"}
        directory.set_directory_metadata(metadata2)

        # Act
        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot1)
        snap_dir = share_client.get_directory_client('dir1')
        props = snap_dir.get_directory_properties()

        # Assert
        self.assertIsNotNone(props)
        self.assertIsNotNone(props.etag)
        self.assertIsNotNone(props.last_modified)
        self.assertDictEqual(metadata, props.metadata)

    @FileSharePreparer()
    def test_get_directory_metadata_with_snapshot(self, storage_account_name,
                                                  storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        metadata = {"test1": "foo", "test2": "bar"}
        directory = share_client.create_directory('dir1', metadata=metadata)
        snapshot1 = share_client.create_snapshot()
        metadata2 = {"test100": "foo100", "test200": "bar200"}
        directory.set_directory_metadata(metadata2)

        # Act
        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot1)
        snap_dir = share_client.get_directory_client('dir1')
        snapshot_metadata = snap_dir.get_directory_properties().metadata

        # Assert
        self.assertIsNotNone(snapshot_metadata)
        self.assertDictEqual(metadata, snapshot_metadata)

    @FileSharePreparer()
    def test_get_directory_properties_with_non_existing_directory(
            self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.get_directory_client('dir1')

        # Act
        with self.assertRaises(ResourceNotFoundError):
            directory.get_directory_properties()

            # Assert

    @FileSharePreparer()
    def test_directory_exists(self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        exists = directory.get_directory_properties()

        # Assert
        self.assertTrue(exists)

    @FileSharePreparer()
    def test_directory_not_exists(self, storage_account_name,
                                  storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.get_directory_client('dir1')

        # Act
        with self.assertRaises(ResourceNotFoundError):
            directory.get_directory_properties()

        # Assert

    @FileSharePreparer()
    def test_directory_parent_not_exists(self, storage_account_name,
                                         storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.get_directory_client('missing1/missing2')

        # Act
        with self.assertRaises(ResourceNotFoundError) as e:
            directory.get_directory_properties()

        # Assert
        self.assertEqual(e.exception.error_code,
                         StorageErrorCode.parent_not_found)

    @FileSharePreparer()
    def test_directory_exists_with_snapshot(self, storage_account_name,
                                            storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        snapshot = share_client.create_snapshot()
        directory.delete_directory()

        # Act
        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot)
        snap_dir = share_client.get_directory_client('dir1')
        exists = snap_dir.get_directory_properties()

        # Assert
        self.assertTrue(exists)

    @FileSharePreparer()
    def test_directory_not_exists_with_snapshot(self, storage_account_name,
                                                storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        snapshot = share_client.create_snapshot()
        directory = share_client.create_directory('dir1')

        # Act
        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot)
        snap_dir = share_client.get_directory_client('dir1')

        with self.assertRaises(ResourceNotFoundError):
            snap_dir.get_directory_properties()

        # Assert

    @FileSharePreparer()
    def test_get_set_directory_metadata(self, storage_account_name,
                                        storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        metadata = {'hello': 'world', 'number': '43'}

        # Act
        directory.set_directory_metadata(metadata)
        md = directory.get_directory_properties().metadata

        # Assert
        self.assertDictEqual(md, metadata)

    @FileSharePreparer()
    def test_set_directory_properties_with_empty_smb_properties(
            self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory_client = share_client.create_directory('dir1')
        directory_properties_on_creation = directory_client.get_directory_properties(
        )

        # Act
        directory_client.set_http_headers()
        directory_properties = directory_client.get_directory_properties()

        # Assert
        # Make sure set empty smb_properties doesn't change smb_properties
        self.assertEqual(directory_properties_on_creation.creation_time,
                         directory_properties.creation_time)
        self.assertEqual(directory_properties_on_creation.last_write_time,
                         directory_properties.last_write_time)
        self.assertEqual(directory_properties_on_creation.permission_key,
                         directory_properties.permission_key)

    @FileSharePreparer()
    def test_set_directory_properties_with_file_permission_key(
            self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory_client = share_client.create_directory('dir1')

        directory_properties_on_creation = directory_client.get_directory_properties(
        )
        permission_key = directory_properties_on_creation.permission_key
        last_write_time = directory_properties_on_creation.last_write_time
        creation_time = directory_properties_on_creation.creation_time

        new_last_write_time = last_write_time + timedelta(hours=1)
        new_creation_time = creation_time + timedelta(hours=1)

        # Act
        directory_client.set_http_headers(
            file_attributes='None',
            file_creation_time=new_creation_time,
            file_last_write_time=new_last_write_time,
            permission_key=permission_key)
        directory_properties = directory_client.get_directory_properties()

        # Assert
        self.assertIsNotNone(directory_properties)
        self.assertEqual(directory_properties.creation_time, new_creation_time)
        self.assertEqual(directory_properties.last_write_time,
                         new_last_write_time)

    @FileSharePreparer()
    def test_list_subdirectories_and_files(self, storage_account_name,
                                           storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory("subdir1")
        directory.create_subdirectory("subdir2")
        directory.create_subdirectory("subdir3")
        directory.upload_file("file1", "data1")
        directory.upload_file("file2", "data2")
        directory.upload_file("file3", "data3")

        # Act
        list_dir = list(directory.list_directories_and_files())

        # Assert
        self.assertEqual(len(list_dir), 6)
        self.assertEqual(list_dir[0]['name'], 'subdir1')
        self.assertEqual(list_dir[0]['is_directory'], True)
        self.assertEqual(list_dir[1]['name'], 'subdir2')
        self.assertEqual(list_dir[1]['is_directory'], True)
        self.assertEqual(list_dir[2]['name'], 'subdir3')
        self.assertEqual(list_dir[2]['is_directory'], True)
        self.assertEqual(list_dir[3]['name'], 'file1')
        self.assertEqual(list_dir[3]['is_directory'], False)
        self.assertEqual(list_dir[4]['name'], 'file2')
        self.assertEqual(list_dir[4]['is_directory'], False)
        self.assertEqual(list_dir[5]['name'], 'file3')
        self.assertEqual(list_dir[5]['is_directory'], False)

    @FileSharePreparer()
    def test_list_subdirectories_and_files_include_other_data(
            self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory("subdir1")
        directory.create_subdirectory("subdir2")
        directory.create_subdirectory("subdir3")
        directory.upload_file("file1", "data1")
        directory.upload_file("file2", "data2")
        directory.upload_file("file3", "data3")

        # Act
        list_dir = list(
            directory.list_directories_and_files(
                include=["timestamps", "Etag", "Attributes", "PermissionKey"]))

        self.assertEqual(len(list_dir), 6)
        self.assertIsNotNone(list_dir[0].etag)
        self.assertIsNotNone(list_dir[1].file_attributes)
        self.assertIsNotNone(list_dir[1].last_access_time)
        self.assertIsNotNone(list_dir[1].last_write_time)
        self.assertIsNotNone(list_dir[2].change_time)
        self.assertIsNotNone(list_dir[2].creation_time)
        self.assertIsNotNone(list_dir[2].file_id)
        try:
            share_client.delete_share()
        except:
            pass

    @FileSharePreparer()
    def test_list_subdirectories_and_files_include_extended_info(
            self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory("subdir1")

        list_dir = list(
            directory.list_directories_and_files(include_extended_info=True))
        self.assertEqual(len(list_dir), 1)
        self.assertIsNotNone(list_dir[0].file_id)
        self.assertIsNone(list_dir[0].file_attributes)
        self.assertIsNone(list_dir[0].last_access_time)

    @FileSharePreparer()
    def test_list_subdirectories_and_files_with_prefix(self,
                                                       storage_account_name,
                                                       storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory("subdir1")
        directory.create_subdirectory("subdir2")
        directory.create_subdirectory("subdir3")
        directory.upload_file("file1", "data1")
        directory.upload_file("file2", "data2")
        directory.upload_file("file3", "data3")

        # Act
        list_dir = list(
            directory.list_directories_and_files(name_starts_with="sub"))

        # Assert
        self.assertEqual(len(list_dir), 3)
        self.assertEqual(list_dir[0]['name'], 'subdir1')
        self.assertEqual(list_dir[0]['is_directory'], True)
        self.assertEqual(list_dir[1]['name'], 'subdir2')
        self.assertEqual(list_dir[1]['is_directory'], True)
        self.assertEqual(list_dir[2]['name'], 'subdir3')
        self.assertEqual(list_dir[2]['is_directory'], True)

    @FileSharePreparer()
    def test_list_subdirectories_and_files_with_snapshot(
            self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory("subdir1")
        directory.create_subdirectory("subdir2")
        directory.upload_file("file1", "data1")

        snapshot = share_client.create_snapshot()
        directory.create_subdirectory("subdir3")
        directory.upload_file("file2", "data2")
        directory.upload_file("file3", "data3")

        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot)
        snapshot_dir = share_client.get_directory_client('dir1')

        # Act
        list_dir = list(snapshot_dir.list_directories_and_files())

        # Assert
        self.assertEqual(len(list_dir), 3)
        self.assertEqual(list_dir[0]['name'], 'subdir1')
        self.assertEqual(list_dir[0]['is_directory'], True)
        self.assertEqual(list_dir[1]['name'], 'subdir2')
        self.assertEqual(list_dir[1]['is_directory'], True)
        self.assertEqual(list_dir[2]['name'], 'file1')
        self.assertEqual(list_dir[2]['is_directory'], False)
        self.assertEqual(list_dir[2]['size'], 5)

    @FileSharePreparer()
    def test_list_nested_subdirectories_and_files(self, storage_account_name,
                                                  storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        subdir = directory.create_subdirectory("subdir1")
        subdir.create_subdirectory("subdir2")
        subdir.create_subdirectory("subdir3")
        directory.upload_file("file1", "data1")
        subdir.upload_file("file2", "data2")
        subdir.upload_file("file3", "data3")

        # Act
        list_dir = list(directory.list_directories_and_files())

        # Assert
        self.assertEqual(len(list_dir), 2)
        self.assertEqual(list_dir[0]['name'], 'subdir1')
        self.assertEqual(list_dir[0]['is_directory'], True)
        self.assertEqual(list_dir[1]['name'], 'file1')
        self.assertEqual(list_dir[1]['is_directory'], False)
        self.assertEqual(list_dir[1]['size'], 5)

    @FileSharePreparer()
    def test_delete_directory_with_existing_share(self, storage_account_name,
                                                  storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        deleted = directory.delete_directory()

        # Assert
        self.assertIsNone(deleted)
        with self.assertRaises(ResourceNotFoundError):
            directory.get_directory_properties()

    @FileSharePreparer()
    def test_delete_directory_with_non_existing_directory(
            self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.get_directory_client('dir1')

        # Act
        with self.assertRaises(ResourceNotFoundError):
            directory.delete_directory()

        # Assert

    @FileSharePreparer()
    def test_get_directory_properties_server_encryption(
            self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        props = directory.get_directory_properties()

        # Assert
        self.assertIsNotNone(props)
        self.assertIsNotNone(props.etag)
        self.assertIsNotNone(props.last_modified)
        self.assertTrue(props.server_encrypted)

    @FileSharePreparer()
    def test_rename_directory(self, storage_account_name, storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        source_directory = share_client.create_directory('dir1')

        # Act
        new_directory = source_directory.rename_directory('dir2')

        # Assert
        props = new_directory.get_directory_properties()
        self.assertIsNotNone(props)

    @FileSharePreparer()
    def test_rename_directory_different_directory(self, storage_account_name,
                                                  storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        parent_source_directory = share_client.create_directory('dir1')
        source_directory = parent_source_directory.create_subdirectory('sub1')

        dest_parent_directory = share_client.create_directory('dir2')

        # Act
        new_directory_path = dest_parent_directory.directory_path + '/sub2'
        new_directory = source_directory.rename_directory(new_directory_path)

        # Assert
        props = new_directory.get_directory_properties()
        self.assertIsNotNone(props)

    @FileSharePreparer()
    def test_rename_directory_ignore_readonly(self, storage_account_name,
                                              storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        source_directory = share_client.create_directory('dir1')
        dest_directory = share_client.create_directory('dir2')
        dest_file = dest_directory.get_file_client('test')

        file_attributes = NTFSAttributes(read_only=True)
        dest_file.create_file(1024, file_attributes=file_attributes)

        # Act
        new_directory = source_directory.rename_directory(
            dest_directory.directory_path + '/' + dest_file.file_name,
            overwrite=True,
            ignore_read_only=True)

        # Assert
        props = new_directory.get_directory_properties()
        self.assertIsNotNone(props)
        self.assertTrue(props.is_directory)

    @FileSharePreparer()
    def test_rename_directory_file_permission(self, storage_account_name,
                                              storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        file_permission_key = share_client.create_permission_for_share(
            TEST_FILE_PERMISSIONS)

        source_directory = share_client.create_directory('dir1')

        # Act
        new_directory = source_directory.rename_directory(
            'dir2', file_permission=TEST_FILE_PERMISSIONS)

        # Assert
        props = new_directory.get_directory_properties()
        self.assertIsNotNone(props)
        self.assertEqual(file_permission_key, props.permission_key)

    @FileSharePreparer()
    def test_rename_directory_preserve_permission(self, storage_account_name,
                                                  storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        source_directory = share_client.create_directory(
            'dir1', file_permission=TEST_FILE_PERMISSIONS)
        source_props = source_directory.get_directory_properties()
        source_permission_key = source_props.permission_key

        # Act
        new_directory = source_directory.rename_directory(
            'dir2', file_permission='preserve')

        # Assert
        props = new_directory.get_directory_properties()
        self.assertIsNotNone(props)
        self.assertEqual(source_permission_key, props.permission_key)

    @FileSharePreparer()
    def test_rename_directory_smb_properties(self, storage_account_name,
                                             storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        source_directory = share_client.create_directory('dir1')

        file_attributes = NTFSAttributes(read_only=True, directory=True)
        file_creation_time = datetime(2022, 1, 26, 10, 9, 30, 500000)
        file_last_write_time = datetime(2022, 1, 26, 10, 14, 30, 500000)

        # Act
        new_directory = source_directory.rename_directory(
            'dir2',
            file_attributes=file_attributes,
            file_creation_time=file_creation_time,
            file_last_write_time=file_last_write_time)

        # Assert
        props = new_directory.get_directory_properties()
        self.assertIsNotNone(props)
        self.assertTrue(props.is_directory)
        self.assertEqual(str(file_attributes),
                         props.file_attributes.replace(' ', ''))
        self.assertEqual(file_creation_time, props.creation_time)
        self.assertEqual(file_last_write_time, props.last_write_time)

    @FileSharePreparer()
    def test_rename_directory_dest_lease(self, storage_account_name,
                                         storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        source_directory = share_client.create_directory('dir1')
        dest_directory = share_client.create_directory('dir2')
        dest_file = dest_directory.upload_file('test', b'Hello World')
        lease = dest_file.acquire_lease()

        # Act
        new_directory = source_directory.rename_directory(
            dest_directory.directory_path + '/' + dest_file.file_name,
            overwrite=True,
            lease=lease)

        # Assert
        props = new_directory.get_directory_properties()
        self.assertIsNotNone(props)
        self.assertTrue(props.is_directory)

    @pytest.mark.live_test_only
    @FileSharePreparer()
    def test_rename_directory_share_sas(self, storage_account_name,
                                        storage_account_key):
        self._setup(storage_account_name, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        token = generate_share_sas(share_client.account_name,
                                   share_client.share_name,
                                   share_client.credential.account_key,
                                   expiry=datetime.utcnow() +
                                   timedelta(hours=1),
                                   permission=ShareSasPermissions(read=True,
                                                                  write=True))

        source_directory = ShareDirectoryClient(self.account_url(
            storage_account_name, 'file'),
                                                share_client.share_name,
                                                'dir1',
                                                credential=token)
        source_directory.create_directory()

        # Act
        new_directory = source_directory.rename_directory('dir2' + '?' + token)

        # Assert
        props = new_directory.get_directory_properties()
        self.assertIsNotNone(props)
Ejemplo n.º 3
0
class StorageGetFileTest(FileTestCase):
    def setUp(self):
        super(StorageGetFileTest, self).setUp()

        # test chunking functionality by reducing the threshold
        # for chunking and the size of each chunk, otherwise
        # the tests would take too long to execute
        self.MAX_SINGLE_GET_SIZE = 32 * 1024
        self.MAX_CHUNK_GET_SIZE = 4 * 1024

        url = self.get_file_url()
        credential = self.get_shared_key_credential()

        self.fsc = ShareServiceClient(
            url,
            credential=credential,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        self.share_name = self.get_resource_name('utshare')
        self.directory_name = self.get_resource_name('utdir')

        if not self.is_playback():
            share = self.fsc.create_share(self.share_name)
            share.create_directory(self.directory_name)

        self.byte_file = self.get_resource_name('bytefile')
        self.byte_data = self.get_random_bytes(64 * 1024 + 5)

        if not self.is_playback():
            byte_file = self.directory_name + '/' + self.byte_file
            file_client = ShareFileClient(self.get_file_url(),
                                          share_name=self.share_name,
                                          file_path=byte_file,
                                          credential=credential)
            file_client.upload_file(self.byte_data)

    def tearDown(self):
        if not self.is_playback():
            try:
                self.fsc.delete_share(self.share_name,
                                      delete_snapshots='include')
            except:
                pass

        if os.path.isfile(FILE_PATH):
            try:
                os.remove(FILE_PATH)
            except:
                pass

        return super(StorageGetFileTest, self).tearDown()

    # --Helpers-----------------------------------------------------------------

    def _get_file_reference(self):
        return self.get_resource_name(TEST_FILE_PREFIX)

    class NonSeekableFile(object):
        def __init__(self, wrapped_file):
            self.wrapped_file = wrapped_file

        def write(self, data):
            self.wrapped_file.write(data)

        def read(self, count):
            return self.wrapped_file.read(count)

        def seekable(self):
            return False

    # -- Get test cases for files ----------------------------------------------

    @record
    def test_unicode_get_file_unicode_data(self):
        # Arrange
        file_data = u'hello world啊齄丂狛狜'.encode('utf-8')
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        # Act
        file_content = file_client.download_file().readall()

        # Assert
        self.assertEqual(file_content, file_data)

    @record
    def test_unicode_get_file_binary_data(self):
        # Arrange
        base64_data = 'AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/wABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v8AAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+f4CBgoOEhYaHiImKi4yNjo+QkZKTlJWWl5iZmpucnZ6foKGio6SlpqeoqaqrrK2ur7CxsrO0tba3uLm6u7y9vr/AwcLDxMXGx8jJysvMzc7P0NHS09TV1tfY2drb3N3e3+Dh4uPk5ebn6Onq6+zt7u/w8fLz9PX29/j5+vv8/f7/AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w=='
        binary_data = base64.b64decode(base64_data)

        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(binary_data)

        # Act
        file_content = file_client.download_file().readall()

        # Assert
        self.assertEqual(file_content, binary_data)

    @record
    def test_get_file_no_content(self):
        # Arrange
        file_data = b''
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        # Act
        file_output = file_client.download_file()

        # Assert
        self.assertEqual(file_data, file_output.readall())
        self.assertEqual(0, file_output.properties.size)

    def test_get_file_to_bytes(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        file_content = file_client.download_file(max_concurrency=2).readall()

        # Assert
        self.assertEqual(self.byte_data, file_content)

    def test_get_file_to_bytes_with_progress(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(raw_response_hook=callback,
                                                 max_concurrency=2).readall()

        # Assert
        self.assertEqual(self.byte_data, file_content)
        self.assert_download_progress(len(self.byte_data),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_to_bytes_non_parallel(self):
        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(
            raw_response_hook=callback).readall()

        # Assert
        self.assertEqual(self.byte_data, file_content)
        self.assert_download_progress(len(self.byte_data),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_to_bytes_small(self):
        # Arrange
        file_data = self.get_random_bytes(1024)
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(
            raw_response_hook=callback).readall()

        # Assert
        self.assertEqual(file_data, file_content)
        self.assert_download_progress(len(file_data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    def test_get_file_with_iter(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            for data in file_client.download_file().chunks():
                stream.write(data)
        # Assert
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)

    def test_get_file_to_stream(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                max_concurrency=2).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)

    def test_get_file_to_stream_with_progress(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                raw_response_hook=callback, max_concurrency=2).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)
        self.assert_download_progress(len(self.byte_data),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_to_stream_non_parallel(self):
        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                raw_response_hook=callback, max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)
        self.assert_download_progress(len(self.byte_data),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_to_stream_small(self):
        # Arrange
        file_data = self.get_random_bytes(1024)
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                raw_response_hook=callback, max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(file_data, actual)
        self.assert_download_progress(len(file_data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    def test_get_file_to_stream_from_snapshot(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = snapshot_client.download_file(
                max_concurrency=2).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)

    def test_get_file_to_stream_with_progress_from_snapshot(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = snapshot_client.download_file(
                raw_response_hook=callback, max_concurrency=2).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)
        self.assert_download_progress(len(self.byte_data),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_to_stream_non_parallel_from_snapshot(self):
        # Arrange
        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = snapshot_client.download_file(
                raw_response_hook=callback, max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)
        self.assert_download_progress(len(self.byte_data),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_to_stream_small_from_snapshot(self):
        # Arrange
        file_data = self.get_random_bytes(1024)
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.upload_file(file_data)

        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = snapshot_client.download_file(
                raw_response_hook=callback, max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(file_data, actual)
        self.assert_download_progress(len(file_data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    def test_ranged_get_file_to_path(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        end_range = self.MAX_SINGLE_GET_SIZE + 1024
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                offset=1, length=end_range, max_concurrency=2).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data[1:end_range + 1], actual)

    def test_ranged_get_file_to_path_with_single_byte(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        end_range = self.MAX_SINGLE_GET_SIZE + 1024
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(offset=0,
                                                   length=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(1, len(actual))
            self.assertEqual(self.byte_data[0], actual[0])

    @record
    def test_ranged_get_file_to_bytes_with_zero_byte(self):
        # Arrange
        file_data = b''
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        # Act
        # the get request should fail in this case since the blob is empty and yet there is a range specified
        with self.assertRaises(HttpResponseError):
            file_client.download_file(offset=0, length=5).readall()

        with self.assertRaises(HttpResponseError):
            file_client.download_file(offset=3, length=5).readall()

    def test_ranged_get_file_to_path_with_progress(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        start_range = 3
        end_range = self.MAX_SINGLE_GET_SIZE + 1024
        with open(FILE_PATH, 'wb') as stream:
            length = end_range - start_range + 1
            bytes_read = file_client.download_file(
                offset=start_range,
                length=length,
                raw_response_hook=callback,
                max_concurrency=2).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data[start_range:end_range + 1], actual)
        self.assert_download_progress(end_range - start_range + 1,
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_ranged_get_file_to_path_small(self):
        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                offset=1, length=4, max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data[1:5], actual)

    @record
    def test_ranged_get_file_to_path_non_parallel(self):
        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            bytes_read = file_client.download_file(
                offset=1, length=3, max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data[1:4], actual)

    @record
    def test_ranged_get_file_to_path_invalid_range_parallel(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_size = self.MAX_SINGLE_GET_SIZE + 1
        file_data = self.get_random_bytes(file_size)
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        # Act
        start = 3
        end_range = 2 * self.MAX_SINGLE_GET_SIZE
        with open(FILE_PATH, 'wb') as stream:
            length = end_range - start + 1
            bytes_read = file_client.download_file(
                offset=start, length=length,
                max_concurrency=2).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(file_data[start:file_size], actual)

    @record
    def test_ranged_get_file_to_path_invalid_range_non_parallel(self):

        # Arrange
        file_size = 1024
        file_data = self.get_random_bytes(file_size)
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        # Act
        start = 3
        end_range = 2 * self.MAX_SINGLE_GET_SIZE
        with open(FILE_PATH, 'wb') as stream:
            length = end_range - start + 1
            bytes_read = file_client.download_file(
                offset=start, length=length,
                max_concurrency=1).readinto(stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(file_data[start:file_size], actual)

    def test_get_file_to_text(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        text_file = self.get_resource_name('textfile')
        text_data = self.get_random_text_data(self.MAX_SINGLE_GET_SIZE + 1)
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + text_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(text_data)

        # Act
        file_content = file_client.download_file(max_concurrency=2,
                                                 encoding='utf-8').readall()

        # Assert
        self.assertEqual(text_data, file_content)

    def test_get_file_to_text_with_progress(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        text_file = self.get_resource_name('textfile')
        text_data = self.get_random_text_data(self.MAX_SINGLE_GET_SIZE + 1)
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + text_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(text_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(raw_response_hook=callback,
                                                 max_concurrency=2,
                                                 encoding='utf-8').readall()

        # Assert
        self.assertEqual(text_data, file_content)
        self.assert_download_progress(len(text_data.encode('utf-8')),
                                      self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_to_text_non_parallel(self):
        # Arrange
        text_file = self._get_file_reference()
        text_data = self.get_random_text_data(self.MAX_SINGLE_GET_SIZE + 1)
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + text_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(text_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(raw_response_hook=callback,
                                                 max_concurrency=1,
                                                 encoding='utf-8').readall()

        # Assert
        self.assertEqual(text_data, file_content)
        self.assert_download_progress(len(text_data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_to_text_small(self):
        # Arrange
        file_data = self.get_random_text_data(1024)
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(file_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(raw_response_hook=callback,
                                                 encoding='utf-8').readall()

        # Assert
        self.assertEqual(file_data, file_content)
        self.assert_download_progress(len(file_data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_to_text_with_encoding(self):
        # Arrange
        text = u'hello 啊齄丂狛狜 world'
        data = text.encode('utf-16')
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(data)

        # Act
        file_content = file_client.download_file(encoding='UTF-16').readall()

        # Assert
        self.assertEqual(text, file_content)

    @record
    def test_get_file_to_text_with_encoding_and_progress(self):
        # Arrange
        text = u'hello 啊齄丂狛狜 world'
        data = text.encode('utf-16')
        file_name = self._get_file_reference()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(data)

        # Act
        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        file_content = file_client.download_file(raw_response_hook=callback,
                                                 encoding='UTF-16').readall()

        # Assert
        self.assertEqual(text, file_content)
        self.assert_download_progress(len(data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    @record
    def test_get_file_non_seekable(self):
        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream)
            bytes_read = file_client.download_file(
                max_concurrency=1).readinto(non_seekable_stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)

    def test_get_file_non_seekable_parallel(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream)

            with self.assertRaises(ValueError):
                file_client.download_file(
                    max_concurrency=2).readinto(non_seekable_stream)

                # Assert

    @record
    def test_get_file_non_seekable_from_snapshot(self):
        # Arrange
        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream)
            bytes_read = snapshot_client.download_file(
                max_concurrency=1).readinto(non_seekable_stream)

        # Assert
        self.assertIsInstance(bytes_read, int)
        with open(FILE_PATH, 'rb') as stream:
            actual = stream.read()
            self.assertEqual(self.byte_data, actual)

    def test_get_file_non_seekable_parallel_from_snapshot(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        # Create a snapshot of the share and delete the file
        share_client = self.fsc.get_share_client(self.share_name)
        share_snapshot = share_client.create_snapshot()
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY)
        file_client.delete_file()

        snapshot_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            snapshot=share_snapshot,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        with open(FILE_PATH, 'wb') as stream:
            non_seekable_stream = StorageGetFileTest.NonSeekableFile(stream)

            with self.assertRaises(ValueError):
                snapshot_client.download_file(
                    max_concurrency=2).readinto(non_seekable_stream)

    @record
    def test_get_file_exact_get_size(self):
        # Arrange
        file_name = self._get_file_reference()
        byte_data = self.get_random_bytes(self.MAX_SINGLE_GET_SIZE)
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(byte_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(raw_response_hook=callback)

        # Assert
        self.assertEqual(byte_data, file_content.readall())
        self.assert_download_progress(len(byte_data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    def test_get_file_exact_chunk_size(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_name = self._get_file_reference()
        byte_data = self.get_random_bytes(self.MAX_SINGLE_GET_SIZE +
                                          self.MAX_CHUNK_GET_SIZE)
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + file_name,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)
        file_client.upload_file(byte_data)

        progress = []

        def callback(response):
            current = response.context['download_stream_current']
            total = response.context['data_stream_total']
            if current is not None:
                progress.append((current, total))

        # Act
        file_content = file_client.download_file(raw_response_hook=callback,
                                                 max_concurrency=2)

        # Assert
        self.assertEqual(byte_data, file_content.readall())
        self.assert_download_progress(len(byte_data), self.MAX_CHUNK_GET_SIZE,
                                      self.MAX_SINGLE_GET_SIZE, progress)

    def test_get_file_with_md5(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        file_content = file_client.download_file(validate_content=True)

        # Assert
        self.assertEqual(self.byte_data, file_content.readall())

    def test_get_file_range_with_md5(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return

        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        file_content = file_client.download_file(offset=0,
                                                 length=1024,
                                                 validate_content=True)

        # Assert
        self.assertIsNone(file_content.properties.content_settings.content_md5)

        # Arrange
        props = file_client.get_file_properties()
        props.content_settings.content_md5 = b'MDAwMDAwMDA='
        file_client.set_http_headers(props.content_settings)

        # Act
        file_content = file_client.download_file(offset=0,
                                                 length=1024,
                                                 validate_content=True)

        # Assert
        self.assertEqual(b'MDAwMDAwMDA=',
                         file_content.properties.content_settings.content_md5)

    @record
    def test_get_file_server_encryption(self):

        #Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        file_content = file_client.download_file(offset=0,
                                                 length=1024,
                                                 validate_content=True)

        # Assert
        if self.is_file_encryption_enabled():
            self.assertTrue(file_content.properties.server_encrypted)
        else:
            self.assertFalse(file_content.properties.server_encrypted)

    @record
    def test_get_file_properties_server_encryption(self):

        # Arrange
        file_client = ShareFileClient(
            self.get_file_url(),
            share_name=self.share_name,
            file_path=self.directory_name + '/' + self.byte_file,
            credential=self.settings.STORAGE_ACCOUNT_KEY,
            max_single_get_size=self.MAX_SINGLE_GET_SIZE,
            max_chunk_get_size=self.MAX_CHUNK_GET_SIZE)

        # Act
        props = file_client.get_file_properties()

        # Assert
        if self.is_file_encryption_enabled():
            self.assertTrue(props.server_encrypted)
        else:
            self.assertFalse(props.server_encrypted)
Ejemplo n.º 4
0
class StorageDirectoryTest(StorageTestCase):
    def _setup(self, storage_account, storage_account_key):
        url = self.account_url(storage_account, "file")
        credential = storage_account_key
        self.fsc = ShareServiceClient(url, credential=credential)
        self.share_name = self.get_resource_name('utshare')

        if not self.is_playback():
            self.fsc.create_share(self.share_name)

    def _teardown(self, FILE_PATH):
        if os.path.isfile(FILE_PATH):
            try:
                os.remove(FILE_PATH)
            except:
                pass

    # --Helpers-----------------------------------------------------------------

    # --Test cases for directories ----------------------------------------------
    @GlobalStorageAccountPreparer()
    def test_create_directories(self, resource_group, location,
                                storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        # Act
        created = share_client.create_directory('dir1')

        # Assert
        self.assertTrue(created)

    @GlobalStorageAccountPreparer()
    def test_create_directories_with_metadata(self, resource_group, location,
                                              storage_account,
                                              storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        metadata = {'hello': 'world', 'number': '42'}

        # Act
        directory = share_client.create_directory('dir1', metadata=metadata)

        # Assert
        md = directory.get_directory_properties().metadata
        self.assertDictEqual(md, metadata)

    @GlobalStorageAccountPreparer()
    def test_create_directories_fail_on_exist(self, resource_group, location,
                                              storage_account,
                                              storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)

        # Act
        created = share_client.create_directory('dir1')
        with self.assertRaises(ResourceExistsError):
            share_client.create_directory('dir1')

        # Assert
        self.assertTrue(created)

    @GlobalStorageAccountPreparer()
    def test_create_subdirectories(self, resource_group, location,
                                   storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        created = directory.create_subdirectory('dir2')

        # Assert
        self.assertTrue(created)
        self.assertEqual(created.directory_path, 'dir1/dir2')

    @GlobalStorageAccountPreparer()
    def test_create_subdirectories_with_metadata(self, resource_group,
                                                 location, storage_account,
                                                 storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        metadata = {'hello': 'world', 'number': '42'}

        # Act
        created = directory.create_subdirectory('dir2', metadata=metadata)

        # Assert
        self.assertTrue(created)
        self.assertEqual(created.directory_path, 'dir1/dir2')
        sub_metadata = created.get_directory_properties().metadata
        self.assertEqual(sub_metadata, metadata)

    @GlobalStorageAccountPreparer()
    def test_create_file_in_directory(self, resource_group, location,
                                      storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        file_data = b'12345678' * 1024
        file_name = self.get_resource_name('file')
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        new_file = directory.upload_file(file_name, file_data)

        # Assert
        file_content = new_file.download_file().readall()
        self.assertEqual(file_content, file_data)

    @GlobalStorageAccountPreparer()
    def test_delete_file_in_directory(self, resource_group, location,
                                      storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        file_name = self.get_resource_name('file')
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        new_file = directory.upload_file(file_name, "hello world")

        # Act
        deleted = directory.delete_file(file_name)

        # Assert
        self.assertIsNone(deleted)
        with self.assertRaises(ResourceNotFoundError):
            new_file.get_file_properties()

    @GlobalStorageAccountPreparer()
    def test_delete_subdirectories(self, resource_group, location,
                                   storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory('dir2')

        # Act
        deleted = directory.delete_subdirectory('dir2')

        # Assert
        self.assertIsNone(deleted)
        subdir = directory.get_subdirectory_client('dir2')
        with self.assertRaises(ResourceNotFoundError):
            subdir.get_directory_properties()

    @GlobalStorageAccountPreparer()
    def test_get_directory_properties(self, resource_group, location,
                                      storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        props = directory.get_directory_properties()

        # Assert
        self.assertIsNotNone(props)
        self.assertIsNotNone(props.etag)
        self.assertIsNotNone(props.last_modified)

    @GlobalStorageAccountPreparer()
    def test_get_directory_properties_with_snapshot(self, resource_group,
                                                    location, storage_account,
                                                    storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        metadata = {"test1": "foo", "test2": "bar"}
        directory = share_client.create_directory('dir1', metadata=metadata)
        snapshot1 = share_client.create_snapshot()
        metadata2 = {"test100": "foo100", "test200": "bar200"}
        directory.set_directory_metadata(metadata2)

        # Act
        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot1)
        snap_dir = share_client.get_directory_client('dir1')
        props = snap_dir.get_directory_properties()

        # Assert
        self.assertIsNotNone(props)
        self.assertIsNotNone(props.etag)
        self.assertIsNotNone(props.last_modified)
        self.assertDictEqual(metadata, props.metadata)

    @GlobalStorageAccountPreparer()
    def test_get_directory_metadata_with_snapshot(self, resource_group,
                                                  location, storage_account,
                                                  storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        metadata = {"test1": "foo", "test2": "bar"}
        directory = share_client.create_directory('dir1', metadata=metadata)
        snapshot1 = share_client.create_snapshot()
        metadata2 = {"test100": "foo100", "test200": "bar200"}
        directory.set_directory_metadata(metadata2)

        # Act
        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot1)
        snap_dir = share_client.get_directory_client('dir1')
        snapshot_metadata = snap_dir.get_directory_properties().metadata

        # Assert
        self.assertIsNotNone(snapshot_metadata)
        self.assertDictEqual(metadata, snapshot_metadata)

    @GlobalStorageAccountPreparer()
    def test_get_directory_properties_with_non_existing_directory(
            self, resource_group, location, storage_account,
            storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.get_directory_client('dir1')

        # Act
        with self.assertRaises(ResourceNotFoundError):
            directory.get_directory_properties()

            # Assert

    @GlobalStorageAccountPreparer()
    def test_directory_exists(self, resource_group, location, storage_account,
                              storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        exists = directory.get_directory_properties()

        # Assert
        self.assertTrue(exists)

    @GlobalStorageAccountPreparer()
    def test_directory_not_exists(self, resource_group, location,
                                  storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.get_directory_client('dir1')

        # Act
        with self.assertRaises(ResourceNotFoundError):
            directory.get_directory_properties()

        # Assert

    @GlobalStorageAccountPreparer()
    def test_directory_parent_not_exists(self, resource_group, location,
                                         storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.get_directory_client('missing1/missing2')

        # Act
        with self.assertRaises(ResourceNotFoundError) as e:
            directory.get_directory_properties()

        # Assert
        self.assertEqual(e.exception.error_code,
                         StorageErrorCode.parent_not_found)

    @GlobalStorageAccountPreparer()
    def test_directory_exists_with_snapshot(self, resource_group, location,
                                            storage_account,
                                            storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        snapshot = share_client.create_snapshot()
        directory.delete_directory()

        # Act
        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot)
        snap_dir = share_client.get_directory_client('dir1')
        exists = snap_dir.get_directory_properties()

        # Assert
        self.assertTrue(exists)

    @GlobalStorageAccountPreparer()
    def test_directory_not_exists_with_snapshot(self, resource_group, location,
                                                storage_account,
                                                storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        snapshot = share_client.create_snapshot()
        directory = share_client.create_directory('dir1')

        # Act
        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot)
        snap_dir = share_client.get_directory_client('dir1')

        with self.assertRaises(ResourceNotFoundError):
            snap_dir.get_directory_properties()

        # Assert

    @GlobalStorageAccountPreparer()
    def test_get_set_directory_metadata(self, resource_group, location,
                                        storage_account, storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        metadata = {'hello': 'world', 'number': '43'}

        # Act
        directory.set_directory_metadata(metadata)
        md = directory.get_directory_properties().metadata

        # Assert
        self.assertDictEqual(md, metadata)

    @GlobalStorageAccountPreparer()
    def test_set_directory_properties_with_empty_smb_properties(
            self, resource_group, location, storage_account,
            storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory_client = share_client.create_directory('dir1')
        directory_properties_on_creation = directory_client.get_directory_properties(
        )

        # Act
        directory_client.set_http_headers()
        directory_properties = directory_client.get_directory_properties()

        # Assert
        # Make sure set empty smb_properties doesn't change smb_properties
        self.assertEqual(directory_properties_on_creation.creation_time,
                         directory_properties.creation_time)
        self.assertEqual(directory_properties_on_creation.last_write_time,
                         directory_properties.last_write_time)
        self.assertEqual(directory_properties_on_creation.permission_key,
                         directory_properties.permission_key)

    @GlobalStorageAccountPreparer()
    def test_set_directory_properties_with_file_permission_key(
            self, resource_group, location, storage_account,
            storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory_client = share_client.create_directory('dir1')

        directory_properties_on_creation = directory_client.get_directory_properties(
        )
        permission_key = directory_properties_on_creation.permission_key
        last_write_time = directory_properties_on_creation.last_write_time
        creation_time = directory_properties_on_creation.creation_time

        new_last_write_time = last_write_time + timedelta(hours=1)
        new_creation_time = creation_time + timedelta(hours=1)

        # Act
        directory_client.set_http_headers(
            file_attributes='None',
            file_creation_time=new_creation_time,
            file_last_write_time=new_last_write_time,
            permission_key=permission_key)
        directory_properties = directory_client.get_directory_properties()

        # Assert
        self.assertIsNotNone(directory_properties)
        self.assertEqual(directory_properties.creation_time, new_creation_time)
        self.assertEqual(directory_properties.last_write_time,
                         new_last_write_time)

    @GlobalStorageAccountPreparer()
    def test_list_subdirectories_and_files(self, resource_group, location,
                                           storage_account,
                                           storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory("subdir1")
        directory.create_subdirectory("subdir2")
        directory.create_subdirectory("subdir3")
        directory.upload_file("file1", "data1")
        directory.upload_file("file2", "data2")
        directory.upload_file("file3", "data3")

        # Act
        list_dir = list(directory.list_directories_and_files())

        # Assert
        expected = [
            {
                'name': 'subdir1',
                'is_directory': True
            },
            {
                'name': 'subdir2',
                'is_directory': True
            },
            {
                'name': 'subdir3',
                'is_directory': True
            },
            {
                'name': 'file1',
                'is_directory': False,
                'size': 5
            },
            {
                'name': 'file2',
                'is_directory': False,
                'size': 5
            },
            {
                'name': 'file3',
                'is_directory': False,
                'size': 5
            },
        ]
        self.assertEqual(len(list_dir), 6)
        self.assertEqual(list_dir, expected)

    @GlobalStorageAccountPreparer()
    def test_list_subdirectories_and_files_with_prefix(self, resource_group,
                                                       location,
                                                       storage_account,
                                                       storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory("subdir1")
        directory.create_subdirectory("subdir2")
        directory.create_subdirectory("subdir3")
        directory.upload_file("file1", "data1")
        directory.upload_file("file2", "data2")
        directory.upload_file("file3", "data3")

        # Act
        list_dir = list(
            directory.list_directories_and_files(name_starts_with="sub"))

        # Assert
        expected = [
            {
                'name': 'subdir1',
                'is_directory': True
            },
            {
                'name': 'subdir2',
                'is_directory': True
            },
            {
                'name': 'subdir3',
                'is_directory': True
            },
        ]
        self.assertEqual(len(list_dir), 3)
        self.assertEqual(list_dir, expected)

    @GlobalStorageAccountPreparer()
    def test_list_subdirectories_and_files_with_snapshot(
            self, resource_group, location, storage_account,
            storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        directory.create_subdirectory("subdir1")
        directory.create_subdirectory("subdir2")
        directory.upload_file("file1", "data1")

        snapshot = share_client.create_snapshot()
        directory.create_subdirectory("subdir3")
        directory.upload_file("file2", "data2")
        directory.upload_file("file3", "data3")

        share_client = self.fsc.get_share_client(self.share_name,
                                                 snapshot=snapshot)
        snapshot_dir = share_client.get_directory_client('dir1')

        # Act
        list_dir = list(snapshot_dir.list_directories_and_files())

        # Assert
        expected = [
            {
                'name': 'subdir1',
                'is_directory': True
            },
            {
                'name': 'subdir2',
                'is_directory': True
            },
            {
                'name': 'file1',
                'is_directory': False,
                'size': 5
            },
        ]
        self.assertEqual(len(list_dir), 3)
        self.assertEqual(list_dir, expected)

    @GlobalStorageAccountPreparer()
    def test_list_nested_subdirectories_and_files(self, resource_group,
                                                  location, storage_account,
                                                  storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')
        subdir = directory.create_subdirectory("subdir1")
        subdir.create_subdirectory("subdir2")
        subdir.create_subdirectory("subdir3")
        directory.upload_file("file1", "data1")
        subdir.upload_file("file2", "data2")
        subdir.upload_file("file3", "data3")

        # Act
        list_dir = list(directory.list_directories_and_files())

        # Assert
        expected = [
            {
                'name': 'subdir1',
                'is_directory': True
            },
            {
                'name': 'file1',
                'is_directory': False,
                'size': 5
            },
        ]
        self.assertEqual(len(list_dir), 2)
        self.assertEqual(list_dir, expected)

    @GlobalStorageAccountPreparer()
    def test_delete_directory_with_existing_share(self, resource_group,
                                                  location, storage_account,
                                                  storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        deleted = directory.delete_directory()

        # Assert
        self.assertIsNone(deleted)
        with self.assertRaises(ResourceNotFoundError):
            directory.get_directory_properties()

    @GlobalStorageAccountPreparer()
    def test_delete_directory_with_non_existing_directory(
            self, resource_group, location, storage_account,
            storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.get_directory_client('dir1')

        # Act
        with self.assertRaises(ResourceNotFoundError):
            directory.delete_directory()

        # Assert

    @GlobalStorageAccountPreparer()
    def test_get_directory_properties_server_encryption(
            self, resource_group, location, storage_account,
            storage_account_key):
        self._setup(storage_account, storage_account_key)
        share_client = self.fsc.get_share_client(self.share_name)
        directory = share_client.create_directory('dir1')

        # Act
        props = directory.get_directory_properties()

        # Assert
        self.assertIsNotNone(props)
        self.assertIsNotNone(props.etag)
        self.assertIsNotNone(props.last_modified)
        self.assertTrue(props.server_encrypted)