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_upload_wrong_slug(time, ha, server, uploader): # verify a snapshot with the wrong slug also throws bad_data = await uploader.upload( createSnapshotTar("wrongslug", "Test Name", time.now(), 1024 * 1024)) dummy = DummySnapshot("Test Name", time.now(), "src", "slug", "dummy") with pytest.raises(UploadFailed): await ha.save(dummy, bad_data)
async def test_corrupt_upload(time, ha, server, uploader): # verify a corrupt snapshot throws the right exception bad_data = await uploader.upload(getTestStream(100)) dummy = DummySnapshot("Test Name", time.now(), "src", "slug2", "dummy") with pytest.raises(UploadFailed): await ha.save(dummy, bad_data)
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)
async def createFile(self, size=1024 * 1024 * 2, slug="testslug", name="Test Name"): from_snapshot: DummySnapshot = DummySnapshot( name, self.time.toUtc(self.time.local(1985, 12, 6)), "fake source", slug) data = await self.uploader.upload( createSnapshotTar(slug, name, self.time.now(), size)) return from_snapshot, data
async def test_upload(time, ha, server, uploader): data = await uploader.upload( createSnapshotTar("slug", "Test Name", time.now(), 1024 * 1024)) dummy = DummySnapshot("Test Name", time.now(), "src", "slug", "dummy") snapshot: HASnapshot = await ha.save(dummy, data) assert snapshot.name() == "Test Name" assert snapshot.slug() == "slug" assert snapshot.size() == round(data.size() / 1024.0 / 1024.0, 2) * 1024 * 1024 assert snapshot.retained() # ensure its still retained on a refresh assert list((await ha.get()).values())[0].retained()
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
def makeSnapshot(slug, date, name=None) -> Snapshot: if not name: name = slug return DummySnapshot(name, date.astimezone(tzutc()), "src", slug)