Exemple #1
0
def test_Dispatcher_list_files(mocked_socket):
    conf = copy.deepcopy(VALID_DISPATCHER_CONF)
    del conf["providers"]["redis"]
    dispatcher = dsp.Dispatcher(conf)
    with mock.patch("pyproxy.metadata.Files.values", return_value=[]):
        result = dispatcher.list()
    assert isinstance(result, list)
    assert len(result) == 0
    path = "key"
    expected_metadata = metadata.MetaDocument(path)
    with mock.patch("pyproxy.metadata.Files.values",
                    return_value=[expected_metadata]):
        result = dispatcher.list()
    assert isinstance(result, list)
    assert len(result) == 1
    assert isinstance(result[0], metadata.MetaDocument)
    assert result[0].path == path
def rebuild_node(provider, provider_name, documents=None):
    """
    Rebuilds metadata by reading data from a given storage node.
    Args:
        provider(Provider): Storage provider
        provider_name(str): Name of the provider
        documents(str, metadata.MetaDocument): The current list of MetaDocuments
    Returns:
        dict(str, metadata.MetaDocument): The list of MetaDocuments built from the database
    """
    if not provider:
        raise ValueError(
            "provider argument must be a valid Provider with a list and a get method"
        )
    if not provider_name or not isinstance(provider_name, (str, unicode)):
        raise ValueError("provider_name argument must be a non empty string")
    documents = documents or {}
    for block_id in provider.list():
        document_name = d.extract_path_from_key(block_id)
        document = documents.get(document_name, None)
        if not document:
            document = metadata.MetaDocument(document_name)
        metablock = None
        for a_metablock in document.blocks:
            if a_metablock.key == block_id:
                metablock = a_metablock
                break
        if not metablock:
            data = provider.get(block_id)
            checksum = hashlib.sha256(data).digest()
            if not document.entangling_blocks:
                document.entangling_blocks = metadata.extract_entanglement_data(
                    data)
            if not document.original_size:
                document.original_size = metadata.extract_document_size(data)
            metablock = metadata.MetaBlock(block_id,
                                           checksum=checksum,
                                           size=len(data))
            document.blocks = sorted((document.blocks + [metablock]),
                                     key=lambda b: b.key)
        metablock.providers = list(set(metablock.providers + [provider_name]))
        documents[document_name] = document
    return documents