Exemplo n.º 1
0
async def _run_mountpoint(config, device, timestamp: DateTime = None):
    config = config.evolve(mountpoint_enabled=True)
    async with logged_client_factory(config, device):
        display_device = click.style(device.device_id, fg="yellow")
        mountpoint_display = click.style(str(
            config.mountpoint_base_dir.absolute()),
                                         fg="yellow")
        click.echo(f"{display_device}'s drive mounted at {mountpoint_display}")

        await trio.sleep_forever()
Exemplo n.º 2
0
async def _do_run_client(config, device, qt_on_ready):
    # Quick fix to avoid MultiError<Cancelled, ...> exception bubbling up
    # TODO: replace this by a proper generic MultiError handling
    with trio.MultiError.catch(lambda exc: None
                               if isinstance(exc, trio.Cancelled) else exc):
        async with logged_client_factory(config=config,
                                         device=device,
                                         event_bus=None) as client:
            # Create our own job scheduler allows us to cancel all pending
            # jobs depending on us when we logout
            client_jobs_ctx = QtToTrioJobScheduler()
            async with trio.open_service_nursery() as nursery:
                await nursery.start(client_jobs_ctx._start)
                qt_on_ready.emit(client, client_jobs_ctx)
Exemplo n.º 3
0
async def test_work_within_logged_client(base_mountpoint, client_config, alice,
                                         tmpdir):
    client_config = client_config.evolve(mountpoint_base_dir=base_mountpoint)

    async with logged_client_factory(client_config, alice) as alice_client:
        manager = alice_client.mountpoint_manager
        wid = await alice_client.user_fs.workspace_create("w")
        workspace = alice_client.user_fs.get_workspace(wid)
        await workspace.touch("/bar.txt")

        with pytest.raises(MountpointNotMounted):
            get_path_in_mountpoint(manager, wid, "/bar.txt")
        await manager.mount_workspace(wid)
        bar_txt = get_path_in_mountpoint(manager, wid, "/bar.txt")
        assert await bar_txt.exists()

    assert not await bar_txt.exists()
Exemplo n.º 4
0
async def test_init_offline_backend_late_reply(server_factory, client_config,
                                               alice, event_bus):
    can_serve_client = trio.Event()

    async def _handle_client(stream):
        await can_serve_client.wait()
        await stream.aclose()

    async with server_factory(_handle_client, alice.organization_addr):
        with trio.fail_after(2):
            async with logged_client_factory(config=client_config,
                                             device=alice,
                                             event_bus=event_bus) as client:
                with client.event_bus.listen() as spy:
                    can_serve_client.set()
                    await spy.wait(
                        ClientEvent.BACKEND_CONNECTION_CHANGED,
                        kwargs={
                            "status": BackendConnStatus.LOST,
                            "status_exc": ANY
                        },
                    )
Exemplo n.º 5
0
async def test_init_online_backend_late_reply(server_factory, client_config,
                                              alice, event_bus, backend):
    can_serve_client = trio.Event()

    async def _handle_client(stream):
        await can_serve_client.wait()
        return await backend.handle_client(stream)

    async with server_factory(_handle_client, alice.organization_addr):
        with trio.fail_after(2):
            async with logged_client_factory(config=client_config,
                                             device=alice,
                                             event_bus=event_bus) as client:
                # We don't want for backend to reply finish client init
                with client.event_bus.listen() as spy:
                    can_serve_client.set()
                    # Now backend reply, monitor should send events accordingly
                    await spy.wait(
                        ClientEvent.BACKEND_CONNECTION_CHANGED,
                        kwargs={
                            "status": BackendConnStatus.READY,
                            "status_exc": None
                        },
                    )
Exemplo n.º 6
0
async def initialize_test_organization(
    config_dir: Path,
    backend_address: BackendAddr,
    password: str,
    administration_token: str,
    force: bool,
    additional_users_number: int,
    additional_devices_number: int,
) -> Tuple[LocalDevice, LocalDevice, LocalDevice]:
    configure_logging("WARNING")
    organization_id = OrganizationID("Org")

    # Create organization
    async with apiv1_backend_administration_cmds_factory(
        backend_address, administration_token) as administration_cmds:

        rep = await administration_cmds.organization_create(organization_id)
        assert rep["status"] == "ok"
        bootstrap_token = rep["bootstrap_token"]

        organization_bootstrap_addr = BackendOrganizationBootstrapAddr.build(
            backend_address, organization_id, bootstrap_token)
    # Bootstrap organization and Alice user and create device "laptop" for Alice

    async with apiv1_backend_anonymous_cmds_factory(
            organization_bootstrap_addr) as anonymous_cmds:
        alice_device = await bootstrap_organization(
            cmds=anonymous_cmds,
            human_handle=HumanHandle(label="Alice", email="*****@*****.**"),
            device_label="laptop",
        )
        save_device_with_password(config_dir,
                                  alice_device,
                                  password,
                                  force=force)

    config = load_config(config_dir, debug="DEBUG" in os.environ)
    # Create context manager, alice_client will be needed for the rest of the script
    async with logged_client_factory(config, alice_device) as alice_client:
        async with backend_authenticated_cmds_factory(
                addr=alice_device.organization_addr,
                device_id=alice_device.device_id,
                signing_key=alice_device.signing_key,
        ) as alice_cmds:

            # Create new device "pc" for Alice
            other_alice_device = await _register_new_device(
                cmds=alice_cmds, author=alice_device, device_label="pc")
            save_device_with_password(config_dir,
                                      other_alice_device,
                                      password,
                                      force=force)
            # Add additional random device for alice
            if additional_devices_number > 0:
                print(
                    f"Adding {additional_devices_number} devices in the test group"
                )
                print(" ... please wait ...")
                await _add_random_device(
                    cmds=alice_cmds,
                    config_dir=config_dir,
                    force=force,
                    password=password,
                    device=alice_device,
                    additional_devices_number=additional_devices_number,
                )
                print(" Done ")
            # Invite Bob in organization
            bob_device = await _register_new_user(
                cmds=alice_cmds,
                author=alice_device,
                device_label="laptop",
                human_handle=HumanHandle(email="*****@*****.**", label="Bob"),
                profile=UserProfile.STANDARD,
            )
            save_device_with_password(config_dir,
                                      bob_device,
                                      password,
                                      force=force)
            # Create Alice workspace
            alice_ws_id = await alice_client.user_fs.workspace_create(
                "alice_workspace")
            # Create context manager
            async with logged_client_factory(config, bob_device) as bob_client:
                # Create Bob workspace
                bob_ws_id = await bob_client.user_fs.workspace_create(
                    "bob_workspace")
                # Bob share workspace with Alice
                await bob_client.user_fs.workspace_share(
                    bob_ws_id, alice_device.user_id, WorkspaceRole.MANAGER)
                # Alice share workspace with Bob
                await alice_client.user_fs.workspace_share(
                    alice_ws_id, bob_device.user_id, WorkspaceRole.MANAGER)
                # Add additional random users
                if additional_users_number > 0:
                    print(
                        f"Adding {additional_users_number} users in the test group"
                    )
                    print(" ... please wait ...")
                    await _add_random_users(
                        cmds=alice_cmds,
                        author=alice_device,
                        alice_client=alice_client,
                        bob_client=bob_client,
                        alice_ws_id=alice_ws_id,
                        bob_ws_id=bob_ws_id,
                        additional_users_number=additional_users_number,
                    )
                    print(" Done ")

    # Synchronize every device
    for device in (alice_device, other_alice_device, bob_device):
        async with logged_client_factory(config, device) as client:
            await client.user_fs.process_last_messages()
            await client.user_fs.sync()
    return (alice_device, other_alice_device, bob_device)
Exemplo n.º 7
0
async def _share_workspace(config, device, name, user_id):
    async with logged_client_factory(config, device) as client:
        await client.user_fs.workspace_share(f"/{name}", user_id)
Exemplo n.º 8
0
async def _create_workspace(config, device, name):
    async with logged_client_factory(config, device) as client:
        await client.user_fs.workspace_create(f"{name}")