Пример #1
0
async def test_storage_file_tree(tmp_path, alice):
    manifest_sqlite_db = tmp_path / alice.slug / "user_data-v1.sqlite"

    async with UserStorage.run(tmp_path, alice) as aus:
        assert aus.manifest_storage.path == manifest_sqlite_db

    assert manifest_sqlite_db.is_file()
Пример #2
0
    async def _core_factory(device, event_bus=None, user_manifest_in_v0=False):
        await running_backend_ready.wait()
        event_bus = event_bus or event_bus_factory()

        if not user_manifest_in_v0:
            # Create a storage just for this operation (the underlying database
            # will be reused by the core's storage thanks to `persistent_mockup`)
            path = core_config.data_base_dir / device.slug
            async with UserStorage.run(device=device, path=path) as storage:
                await initialize_userfs_storage_v1(storage)

        with event_bus.listen() as spy:
            async with logged_core_factory(core_config, device,
                                           event_bus) as core:
                # On startup core is always considered offline.
                # Hence we risk concurrency issues if the connection to backend
                # switches online concurrently with the test.
                if "running_backend" in request.fixturenames:
                    await spy.wait_with_timeout(
                        CoreEvent.BACKEND_CONNECTION_CHANGED,
                        {
                            "status": BackendConnStatus.READY,
                            "status_exc": spy.ANY
                        },
                    )
                assert core.are_monitors_idle()

                yield core
Пример #3
0
    async def _initialize_local_user_manifest(
        data_base_dir, device, initial_user_manifest: str
    ) -> None:
        assert initial_user_manifest in ("non_speculative_v0", "speculative_v0", "v1")
        # Create a storage just for this operation (the underlying database
        # will be reused by the core's storage thanks to `persistent_mockup`)
        with freeze_time("2000-01-01", device=device) as timestamp:
            async with UserStorage.run(data_base_dir, device) as storage:
                assert storage.get_user_manifest().base_version == 0

                if initial_user_manifest == "v1":
                    user_manifest = initial_user_manifest_state.get_user_manifest_v1_for_device(
                        storage.device
                    )
                    await storage.set_user_manifest(user_manifest)
                    # Chcekpoint 1 *is* the upload of user manifest v1
                    await storage.update_realm_checkpoint(1, {})

                elif initial_user_manifest == "non_speculative_v0":
                    user_manifest = LocalUserManifest.new_placeholder(
                        author=storage.device.device_id,
                        id=storage.device.user_manifest_id,
                        timestamp=timestamp,
                        speculative=False,
                    )
                    await storage.set_user_manifest(user_manifest)

                else:
                    # Nothing to do given speculative placeholder is the default
                    assert initial_user_manifest == "speculative_v0"
Пример #4
0
    async def run(
        cls: Type[UserFSTypeVar],
        device: LocalDevice,
        path: Path,
        backend_cmds: BackendAuthenticatedCmds,
        remote_devices_manager: RemoteDevicesManager,
        event_bus: EventBus,
        prevent_sync_pattern: Pattern[str],
    ) -> AsyncIterator[UserFSTypeVar]:
        self = cls(device, path, backend_cmds, remote_devices_manager,
                   event_bus, prevent_sync_pattern)

        # Run user storage
        async with UserStorage.run(self.device, self.path) as self.storage:

            # Nursery for workspace storages
            async with open_service_nursery(
            ) as self._workspace_storage_nursery:

                # Make sure all the workspaces are loaded
                # In particular, we want to make sure that any workspace available through
                # `userfs.get_user_manifest().workspaces` is also available through
                # `userfs.get_workspace(workspace_id)`.
                for workspace_entry in self.get_user_manifest().workspaces:
                    await self._load_workspace(workspace_entry.id)

                yield self

                # Stop the workspace storages
                self._workspace_storage_nursery.cancel_scope.cancel()
Пример #5
0
async def test_storage_file_tree(tmpdir, alice):
    path = Path(tmpdir)
    manifest_sqlite_db = path / "user_data-v1.sqlite"

    async with UserStorage.run(alice, tmpdir) as aus:
        assert aus.manifest_storage.path == manifest_sqlite_db

    assert set(path.iterdir()) == {manifest_sqlite_db}
Пример #6
0
async def test_initialization(alice, data_base_dir, alice_user_storage, user_manifest):
    aus = alice_user_storage

    # Brand new user storage contains user manifest placeholder
    user_manifest_v0 = aus.get_user_manifest()
    assert user_manifest_v0.id == alice.user_manifest_id
    assert user_manifest_v0.need_sync
    assert user_manifest_v0.base.author == alice.device_id
    assert user_manifest_v0.base.id == alice.user_manifest_id
    assert user_manifest_v0.base.version == 0

    await aus.set_user_manifest(user_manifest)
    assert aus.get_user_manifest() == user_manifest
    async with UserStorage.run(data_base_dir, aus.device) as aus2:
        assert aus2.get_user_manifest() == user_manifest

    new_user_manifest = user_manifest.evolve(need_sync=False)
    await aus.set_user_manifest(new_user_manifest)
    assert aus.get_user_manifest() == new_user_manifest
    async with UserStorage.run(data_base_dir, aus.device) as aus2:
        assert aus2.get_user_manifest() == new_user_manifest
Пример #7
0
    async def run(
        cls: Type[UserFSTypeVar],
        data_base_dir: Path,
        device: LocalDevice,
        backend_cmds: BackendAuthenticatedCmds,
        remote_devices_manager: RemoteDevicesManager,
        event_bus: EventBus,
        prevent_sync_pattern: Pattern[str],
        preferred_language: str = "en",
        workspace_storage_cache_size:
        int = DEFAULT_WORKSPACE_STORAGE_CACHE_SIZE,
    ) -> AsyncIterator[UserFSTypeVar]:
        self = cls(
            data_base_dir,
            device,
            backend_cmds,
            remote_devices_manager,
            event_bus,
            prevent_sync_pattern,
            preferred_language,
            workspace_storage_cache_size,
        )

        # Run user storage
        async with UserStorage.run(self.data_base_dir,
                                   self.device) as self.storage:

            # Nursery for workspace storages
            async with open_service_nursery(
            ) as self._workspace_storage_nursery:

                # Make sure all the workspaces are loaded
                # In particular, we want to make sure that any workspace available through
                # `userfs.get_user_manifest().workspaces` is also available through
                # `userfs.get_workspace(workspace_id)`.
                for workspace_entry in self.get_user_manifest().workspaces:
                    await self._load_workspace(workspace_entry.id)

                yield self

                # Stop the workspace storages
                self._workspace_storage_nursery.cancel_scope.cancel()
Пример #8
0
    async def run(cls, *args, **kwargs):
        self = cls(*args, **kwargs)

        # Run user storage
        async with UserStorage.run(self.device, self.path) as self.storage:

            # Nursery for workspace storages
            async with trio.open_service_nursery() as self._workspace_storage_nursery:

                # Make sure all the workspaces are loaded
                # In particular, we want to make sure that any workspace available through
                # `userfs.get_user_manifest().workspaces` is also available through
                # `userfs.get_workspace(workspace_id)`.
                for workspace_entry in self.get_user_manifest().workspaces:
                    await self._load_workspace(workspace_entry.id)

                yield self

                # Stop the workspace storages
                self._workspace_storage_nursery.cancel_scope.cancel()
Пример #9
0
async def alice_user_storage(data_base_dir, alice):
    async with UserStorage.run(data_base_dir, alice) as aus:
        yield aus
Пример #10
0
async def alice_user_storage(tmpdir, alice):
    async with UserStorage.run(alice, tmpdir) as aus:
        yield aus