fix(backup): early abort and better error reporting for restore_all

This commit is contained in:
Houkime 2024-09-23 11:35:46 +00:00
parent 2ad518499e
commit 5850c05bd1
3 changed files with 41 additions and 8 deletions

View file

@ -220,6 +220,7 @@ def do_full_restore(job: Job) -> None:
Backups.restore_snapshot(snap) Backups.restore_snapshot(snap)
except Exception as error: except Exception as error:
report_job_error(error, job) report_job_error(error, job)
return
progress = progress + progress_per_service progress = progress + progress_per_service
Jobs.update( Jobs.update(
job, job,

View file

@ -186,11 +186,11 @@ def get_testfile_bodies(service: DummyService):
return bodies return bodies
def is_original_files(service: DummyService): def assert_original_files(service: DummyService):
# For use in restoration tests mostly # For use in restoration tests mostly
paths = testfile_paths(service.get_folders()) paths = testfile_paths(service.get_folders())
return get_testfile_bodies(service) == { assert get_testfile_bodies(service) == {
paths[0]: TESTFILE_BODY, paths[0]: TESTFILE_BODY,
paths[1]: TESTFILE2_BODY, paths[1]: TESTFILE2_BODY,
} }
@ -219,7 +219,7 @@ def raw_dummy_service(tmpdir) -> DummyService:
service = TestDummyService() service = TestDummyService()
write_testfile_bodies(service, bodies) write_testfile_bodies(service, bodies)
assert is_original_files(service) assert_original_files(service)
return service return service
@ -306,7 +306,7 @@ def catch_nixos_rebuild_calls(fp):
return fp return fp
def assert_rebuild_was_made(fp, unit_name): def assert_rebuild_was_made(fp):
# You call it after you have done the operation that # You call it after you have done the operation that
# calls a rebuild # calls a rebuild
assert_rebuild_or_upgrade_was_made(fp, API_REBUILD_SYSTEM_UNIT) assert_rebuild_or_upgrade_was_made(fp, API_REBUILD_SYSTEM_UNIT)

View file

@ -1,5 +1,7 @@
import pytest import pytest
from typing import List
import os import os
import os.path as path import os.path as path
from os import remove from os import remove
@ -11,7 +13,6 @@ import tempfile
from selfprivacy_api.utils.huey import huey from selfprivacy_api.utils.huey import huey
from selfprivacy_api.services.service import ServiceStatus from selfprivacy_api.services.service import ServiceStatus
from selfprivacy_api.services import ServiceManager from selfprivacy_api.services import ServiceManager
@ -40,6 +41,7 @@ from selfprivacy_api.backup.tasks import (
reload_snapshot_cache, reload_snapshot_cache,
total_backup, total_backup,
do_full_restore, do_full_restore,
which_snapshots_to_full_restore,
) )
from selfprivacy_api.backup.storage import Storage from selfprivacy_api.backup.storage import Storage
from selfprivacy_api.backup.local_secret import LocalBackupSecret from selfprivacy_api.backup.local_secret import LocalBackupSecret
@ -50,6 +52,12 @@ from selfprivacy_api.backup.jobs import (
) )
from tests.common import assert_job_errored from tests.common import assert_job_errored
from tests.conftest import (
write_testfile_bodies,
get_testfile_bodies,
assert_original_files,
assert_rebuild_was_made,
)
from tests.test_dkim import dkim_file from tests.test_dkim import dkim_file
from tests.test_graphql.test_services import ( from tests.test_graphql.test_services import (
@ -119,6 +127,18 @@ def file_backup(tmpdir) -> AbstractBackupProvider:
return provider return provider
def ids(snapshots: List[Snapshot]) -> List[str]:
return [snapshot.id for snapshot in snapshots]
def assert_job_ok(job: Job):
try:
assert job.status == JobStatus.FINISHED
# For easier debug
except AssertionError:
raise ValueError("Job errored out when it was not supposed to:", job.error)
def test_reset_sets_to_none1(): def test_reset_sets_to_none1():
Backups.reset() Backups.reset()
provider = Backups.provider() provider = Backups.provider()
@ -842,15 +862,27 @@ def test_backup_all_restore_all(
fp = catch_nixos_rebuild_calls fp = catch_nixos_rebuild_calls
fp.pass_command(["restic", fp.any()]) fp.pass_command(["restic", fp.any()])
fp.keep_last_process(True) fp.keep_last_process(True)
fp.pass_command(["rclone", fp.any()])
fp.keep_last_process(True)
fp.pass_command(["lsblk", fp.any()])
fp.keep_last_process(True)
assert len(Backups.get_all_snapshots()) == 0
backup_job = add_total_backup_job() backup_job = add_total_backup_job()
total_backup(backup_job) total_backup(backup_job)
assert len(Backups.get_all_snapshots()) == 2 assert len(Backups.get_all_snapshots()) == 2
assert set(ids(which_snapshots_to_full_restore())) == set(
ids(Backups.get_all_snapshots())
)
write_testfile_bodies(dummy_service, ["bogus", "bleeegh corruption ><"])
restore_job = add_total_restore_job() restore_job = add_total_restore_job()
do_full_restore(restore_job) do_full_restore(restore_job)
assert_job_ok(restore_job)
# TODO: maybe test integrity. assert_rebuild_was_made(fp)
# since it is standard restore operations they would have failed if something went wrong assert_original_files(dummy_service)
# however, one still needs to check that all of the required restores have even started