Esempio n. 1
0
async def test_clear_directory(alice_workspace):
    local_item1 = mock.Mock()
    local_item1.name = "item1"
    local_item2 = mock.Mock()
    local_item2.name = "item2"

    path = trio.Path("/test_directory")
    path.iterdir = AsyncMock(spec=mock.Mock,
                             side_effect=lambda: [local_item1, local_item2])

    folder_manifest = mock.Mock()
    folder_manifest.children = {"item1": "id1", "item2": "id2", "item3": "id3"}

    clear_path_mock = AsyncMock(spec=mock.Mock())
    with mock.patch("parsec.core.cli.rsync._clear_path", clear_path_mock):
        await rsync._clear_directory(FsPath("/path_in_workspace"), path,
                                     alice_workspace, folder_manifest)
        clear_path_mock.assert_called_once_with(
            alice_workspace, FsPath("/path_in_workspace/item3"))

    clear_path_mock.reset_mock()
    folder_manifest.children["item4"] = "id4"
    with mock.patch("parsec.core.cli.rsync._clear_path", clear_path_mock):
        await rsync._clear_directory(FsPath("/path_in_workspace"), path,
                                     alice_workspace, folder_manifest)
        clear_path_mock.assert_has_calls([
            mock.call(alice_workspace, FsPath("/path_in_workspace/item3")),
            mock.call(alice_workspace, FsPath("/path_in_workspace/item4")),
        ])

    clear_path_mock.reset_mock()
    del folder_manifest.children["item3"]
    del folder_manifest.children["item4"]

    with mock.patch("parsec.core.cli.rsync._clear_path", clear_path_mock):
        await rsync._clear_directory(FsPath("/path_in_workspace"), path,
                                     alice_workspace, folder_manifest)
        clear_path_mock.assert_not_called()
Esempio n. 2
0
async def test_update_file(alice_workspace):
    block_mock1 = mock.Mock()
    block_mock1.digest = b"block1"
    block_mock2 = mock.Mock()
    block_mock2.digest = b"block2"

    manifest_mock = mock.Mock()
    manifest_mock.blocks = [block_mock1, block_mock2]

    load_manifest_mock = AsyncMock(spec=mock.Mock,
                                   side_effect=lambda x: manifest_mock)
    alice_workspace.remote_loader.load_manifest = load_manifest_mock

    write_bytes_mock = AsyncMock(spec=mock.Mock)
    alice_workspace.write_bytes = write_bytes_mock

    sync_by_id_mock = AsyncMock(spec=mock.Mock)
    alice_workspace.sync_by_id = sync_by_id_mock
    HashDigest.from_data = mock.Mock(side_effect=lambda x: x)

    with mock.patch(
            "parsec.core.cli.rsync._chunks_from_path",
            AsyncMock(spec=mock.Mock, side_effect=[[b"block1", b"block2"]]),
    ):
        entry_id = EntryID()
        await rsync._update_file(alice_workspace, entry_id,
                                 FsPath("/src_file"),
                                 FsPath("/path_in_workspace"))
        rsync._chunks_from_path.assert_called_once_with(FsPath("/src_file"))
        load_manifest_mock.assert_called_once_with(entry_id)
        write_bytes_mock.assert_not_called()
        sync_by_id_mock.assert_called_once_with(entry_id,
                                                remote_changed=False,
                                                recursive=False)

    load_manifest_mock.reset_mock()
    sync_by_id_mock.reset_mock()

    with mock.patch(
            "parsec.core.cli.rsync._chunks_from_path",
            AsyncMock(spec=mock.Mock, side_effect=[[b"block1", b"block3"]]),
    ):

        await rsync._update_file(alice_workspace, entry_id,
                                 FsPath("/src_file"),
                                 FsPath("/path_in_workspace"))
        rsync._chunks_from_path.assert_called_once_with(FsPath("/src_file"))
        load_manifest_mock.assert_called_once_with(entry_id)
        write_bytes_mock.assert_called_once_with(FsPath("/path_in_workspace"),
                                                 b"block3", len("block1"))
        sync_by_id_mock.assert_called_once_with(entry_id,
                                                remote_changed=False,
                                                recursive=False)

    load_manifest_mock.reset_mock()
    sync_by_id_mock.reset_mock()
    write_bytes_mock.reset_mock()

    with mock.patch(
            "parsec.core.cli.rsync._chunks_from_path",
            AsyncMock(spec=mock.Mock, side_effect=[[b"block3", b"block4"]]),
    ):
        await rsync._update_file(alice_workspace, entry_id,
                                 FsPath("/src_file"),
                                 FsPath("/path_in_workspace"))
        rsync._chunks_from_path.assert_called_once_with(FsPath("/src_file"))
        alice_workspace.remote_loader.load_manifest.assert_called_once_with(
            entry_id)
        write_bytes_mock.assert_has_calls([
            mock.call(FsPath("/path_in_workspace"), b"block3", 0),
            mock.call(FsPath("/path_in_workspace"), b"block4", len("block3")),
        ])
        sync_by_id_mock.assert_called_once_with(entry_id,
                                                remote_changed=False,
                                                recursive=False)
Esempio n. 3
0
async def test_sync_directory_content(alice_workspace):

    path_dir1 = trio.Path("/test_dir1")
    path_dir1.is_dir = AsyncMock(spec=mock.Mock(), side_effect=lambda: True)
    path_dir2 = trio.Path("/test_dir2")
    path_dir2.is_dir = AsyncMock(spec=mock.Mock(), side_effect=lambda: True)

    workspace_path = FsPath("/path_in_workspace")
    source = trio.Path("/test")
    source.iterdir = AsyncMock(spec=mock.Mock(),
                               side_effect=lambda: [path_dir1, path_dir2])

    mock_manifest = mock.Mock()
    mock_manifest.children = {"test_dir1": "id1"}

    _sync_directory_mock = AsyncMock(spec=mock.Mock())
    _upsert_file_mock = AsyncMock(spec=mock.Mock())

    with mock.patch("parsec.core.cli.rsync._sync_directory",
                    _sync_directory_mock):
        with mock.patch("parsec.core.cli.rsync._upsert_file",
                        _upsert_file_mock):
            await rsync._sync_directory_content(workspace_path, source,
                                                alice_workspace, mock_manifest)
            _sync_directory_mock.assert_has_calls([
                mock.call(
                    "id1",
                    alice_workspace,
                    trio.Path("/test_dir1"),
                    FsPath("/path_in_workspace/test_dir1"),
                ),
                mock.call(
                    None,
                    alice_workspace,
                    trio.Path("/test_dir2"),
                    FsPath("/path_in_workspace/test_dir2"),
                ),
            ])
            _upsert_file_mock.assert_not_called()

    _sync_directory_mock.reset_mock()
    path_dir1.is_dir.side_effect = lambda: False
    path_dir2.is_dir.side_effect = lambda: False

    with mock.patch("parsec.core.cli.rsync._sync_directory",
                    _sync_directory_mock):
        with mock.patch("parsec.core.cli.rsync._upsert_file",
                        _upsert_file_mock):
            await rsync._sync_directory_content(workspace_path, source,
                                                alice_workspace, mock_manifest)
            _upsert_file_mock.assert_has_calls([
                mock.call(
                    "id1",
                    alice_workspace,
                    trio.Path("/test_dir1"),
                    FsPath("/path_in_workspace/test_dir1"),
                ),
                mock.call(
                    None,
                    alice_workspace,
                    trio.Path("/test_dir2"),
                    FsPath("/path_in_workspace/test_dir2"),
                ),
            ])
            _sync_directory_mock.assert_not_called()

    _upsert_file_mock.reset_mock()
    path_dir1.is_dir.side_effect = lambda: True
    path_dir2.is_dir.side_effect = lambda: False

    with mock.patch("parsec.core.cli.rsync._sync_directory",
                    _sync_directory_mock):
        with mock.patch("parsec.core.cli.rsync._upsert_file",
                        _upsert_file_mock):
            await rsync._sync_directory_content(workspace_path, source,
                                                alice_workspace, mock_manifest)
            _sync_directory_mock.assert_called_once_with(
                "id1",
                alice_workspace,
                trio.Path("/test_dir1"),
                FsPath("/path_in_workspace/test_dir1"),
            )
            _upsert_file_mock.assert_called_once_with(
                None,
                alice_workspace,
                trio.Path("/test_dir2"),
                FsPath("/path_in_workspace/test_dir2"),
            )