Example #1
0
async def test_CRUD(ha: HaSource, time, interceptor: RequestInterceptor,
                    data_cache: DataCache) -> None:
    snapshot: HASnapshot = await ha.create(
        CreateOptions(time.now(), "Test Name"))

    assert snapshot.name() == "Test Name"
    assert type(snapshot) is HASnapshot
    assert not snapshot.retained()
    assert snapshot.snapshotType() == "full"
    assert not snapshot.protected()
    assert snapshot.name() == "Test Name"
    assert snapshot.source() == SOURCE_HA
    assert not snapshot.ignore()
    assert snapshot.madeByTheAddon()
    assert "pending" not in data_cache.snapshots

    # read the item directly, its metadata should match
    from_ha = await ha.harequests.snapshot(snapshot.slug())
    assert from_ha.size() == snapshot.size()
    assert from_ha.slug() == snapshot.slug()
    assert from_ha.source() == SOURCE_HA

    snapshots = await ha.get()
    assert len(snapshots) == 1
    assert snapshot.slug() in snapshots

    full = DummySnapshot(from_ha.name(), from_ha.date(), from_ha.size(),
                         from_ha.slug(), "dummy")
    full.addSource(snapshot)

    # download the item, its bytes should match up
    download = await ha.read(full)
    await download.setup()
    direct_download = await ha.harequests.download(snapshot.slug())
    await direct_download.setup()
    while True:
        from_file = await direct_download.read(1024 * 1024)
        from_download = await download.read(1024 * 1024)
        if len(from_file.getbuffer()) == 0:
            assert len(from_download.getbuffer()) == 0
            break
        assert from_file.getbuffer() == from_download.getbuffer()

    # update retention
    assert not snapshot.retained()
    await ha.retain(full, True)
    assert (await ha.get())[full.slug()].retained()
    await ha.retain(full, False)
    assert not (await ha.get())[full.slug()].retained()

    # Delete the item, make sure its gone
    await ha.delete(full)
    assert full.getSource(ha.name()) is None
    snapshots = await ha.get()
    assert len(snapshots) == 0
async def test_delete_error(time, ha: HaSource,
                            interceptor: RequestInterceptor):
    snapshot = await ha.create(CreateOptions(time.now(), "Some Name"))
    full = DummySnapshot(snapshot.name(), snapshot.date(), snapshot.size(),
                         snapshot.slug(), "dummy")
    full.addSource(snapshot)
    interceptor.setError(URL_MATCH_SNAPSHOT_DELETE, 400)
    with pytest.raises(HomeAssistantDeleteError):
        await ha.delete(full)

    interceptor.clear()
    await ha.delete(full)
async def test_download_timeout(ha: HaSource, time,
                                interceptor: RequestInterceptor,
                                config: Config) -> None:
    config.override(Setting.NEW_SNAPSHOT_TIMEOUT_SECONDS, 100)
    snapshot: HASnapshot = await ha.create(
        CreateOptions(time.now(), "Test Name"))
    from_ha = await ha.harequests.snapshot(snapshot.slug())
    full = DummySnapshot(from_ha.name(), from_ha.date(), from_ha.size(),
                         from_ha.slug(), "dummy")
    full.addSource(snapshot)

    interceptor.setSleep(URL_MATCH_SNAPSHOT_DOWNLOAD, sleep=100)
    config.override(Setting.DOWNLOAD_TIMEOUT_SECONDS, 1)
    direct_download = await ha.harequests.download(snapshot.slug())

    with pytest.raises(SupervisorTimeoutError):
        await direct_download.setup()
        await direct_download.read(1)