2023-02-03 17:04:35 +00:00
|
|
|
import pytest
|
2023-02-03 19:09:24 +00:00
|
|
|
import os.path as path
|
2023-02-20 16:09:01 +00:00
|
|
|
from os import makedirs
|
2023-02-22 15:58:36 +00:00
|
|
|
from os import remove
|
|
|
|
from os import listdir
|
2023-02-03 17:04:35 +00:00
|
|
|
|
|
|
|
from selfprivacy_api.services.test_service import DummyService
|
|
|
|
|
2023-02-22 13:35:55 +00:00
|
|
|
from selfprivacy_api.models.backup.snapshot import Snapshot
|
|
|
|
|
2023-02-08 15:14:08 +00:00
|
|
|
from selfprivacy_api.backup import Backups
|
2023-02-01 11:58:55 +00:00
|
|
|
import selfprivacy_api.backup.providers as providers
|
|
|
|
from selfprivacy_api.backup.providers import AbstractBackupProvider
|
|
|
|
|
|
|
|
from selfprivacy_api.backup.providers.backblaze import Backblaze
|
2023-03-29 11:45:52 +00:00
|
|
|
|
2023-02-01 11:58:55 +00:00
|
|
|
from selfprivacy_api.graphql.queries.providers import BackupProvider
|
|
|
|
|
2023-03-29 11:45:52 +00:00
|
|
|
from selfprivacy_api.backup.tasks import start_backup
|
2023-02-01 11:58:55 +00:00
|
|
|
|
2023-02-03 19:09:24 +00:00
|
|
|
TESTFILE_BODY = "testytest!"
|
2023-02-08 14:05:25 +00:00
|
|
|
REPO_NAME = "test_backup"
|
2023-02-03 19:09:24 +00:00
|
|
|
|
|
|
|
|
2023-02-20 16:09:01 +00:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def backups(tmpdir):
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.reset()
|
|
|
|
|
2023-02-20 16:09:01 +00:00
|
|
|
test_repo_path = path.join(tmpdir, "totallyunrelated")
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.set_localfile_repo(test_repo_path)
|
2023-03-13 19:03:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def backups_backblaze(generic_userdata):
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.reset()
|
2023-02-20 16:09:01 +00:00
|
|
|
|
|
|
|
|
2023-02-03 17:04:35 +00:00
|
|
|
@pytest.fixture()
|
2023-02-20 16:09:01 +00:00
|
|
|
def raw_dummy_service(tmpdir, backups):
|
|
|
|
service_dir = path.join(tmpdir, "test_service")
|
|
|
|
makedirs(service_dir)
|
|
|
|
|
|
|
|
testfile_path = path.join(service_dir, "testfile.txt")
|
|
|
|
with open(testfile_path, "w") as file:
|
2023-02-03 19:09:24 +00:00
|
|
|
file.write(TESTFILE_BODY)
|
2023-02-08 14:05:25 +00:00
|
|
|
|
|
|
|
# we need this to not change get_location() much
|
2023-02-20 16:09:01 +00:00
|
|
|
class TestDummyService(DummyService, location=service_dir):
|
2023-02-08 14:05:25 +00:00
|
|
|
pass
|
|
|
|
|
2023-02-20 10:35:51 +00:00
|
|
|
service = TestDummyService()
|
2023-02-20 16:09:01 +00:00
|
|
|
return service
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def dummy_service(tmpdir, backups, raw_dummy_service):
|
|
|
|
service = raw_dummy_service
|
|
|
|
repo_path = path.join(tmpdir, "test_repo")
|
|
|
|
assert not path.exists(repo_path)
|
|
|
|
# assert not repo_path
|
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.init_repo(service)
|
2023-02-20 10:35:51 +00:00
|
|
|
return service
|
2023-02-03 17:04:35 +00:00
|
|
|
|
|
|
|
|
2023-02-03 18:49:24 +00:00
|
|
|
@pytest.fixture()
|
2023-02-08 14:05:25 +00:00
|
|
|
def memory_backup() -> AbstractBackupProvider:
|
2023-02-03 18:49:24 +00:00
|
|
|
ProviderClass = providers.get_provider(BackupProvider.MEMORY)
|
|
|
|
assert ProviderClass is not None
|
|
|
|
memory_provider = ProviderClass(login="", key="")
|
|
|
|
assert memory_provider is not None
|
|
|
|
return memory_provider
|
|
|
|
|
|
|
|
|
2023-02-20 11:50:52 +00:00
|
|
|
@pytest.fixture()
|
|
|
|
def file_backup(tmpdir) -> AbstractBackupProvider:
|
|
|
|
test_repo_path = path.join(tmpdir, "test_repo")
|
|
|
|
ProviderClass = providers.get_provider(BackupProvider.FILE)
|
|
|
|
assert ProviderClass is not None
|
|
|
|
provider = ProviderClass(test_repo_path)
|
|
|
|
assert provider is not None
|
|
|
|
return provider
|
|
|
|
|
|
|
|
|
2023-03-10 14:14:41 +00:00
|
|
|
def test_config_load(generic_userdata):
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.reset()
|
|
|
|
provider = Backups.provider()
|
2023-03-10 14:14:41 +00:00
|
|
|
|
|
|
|
assert provider is not None
|
|
|
|
assert isinstance(provider, Backblaze)
|
|
|
|
assert provider.login == "ID"
|
|
|
|
assert provider.key == "KEY"
|
|
|
|
|
|
|
|
|
2023-02-01 11:58:55 +00:00
|
|
|
def test_select_backend():
|
|
|
|
provider = providers.get_provider(BackupProvider.BACKBLAZE)
|
|
|
|
assert provider is not None
|
|
|
|
assert provider == Backblaze
|
2023-02-03 17:04:35 +00:00
|
|
|
|
|
|
|
|
2023-02-20 11:50:52 +00:00
|
|
|
def test_file_backend_init(file_backup):
|
|
|
|
file_backup.backuper.init("somerepo")
|
|
|
|
|
|
|
|
|
2023-02-20 16:09:01 +00:00
|
|
|
def test_backup_simple_file(raw_dummy_service, file_backup):
|
2023-02-03 17:04:35 +00:00
|
|
|
# temporarily incomplete
|
2023-02-20 16:09:01 +00:00
|
|
|
service = raw_dummy_service
|
|
|
|
assert service is not None
|
|
|
|
assert file_backup is not None
|
|
|
|
|
|
|
|
name = service.get_id()
|
|
|
|
file_backup.backuper.init(name)
|
2023-02-08 15:14:08 +00:00
|
|
|
|
|
|
|
|
2023-02-20 16:09:01 +00:00
|
|
|
def test_backup_service(dummy_service, backups):
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.back_up(dummy_service)
|
2023-02-13 11:16:35 +00:00
|
|
|
|
|
|
|
|
2023-02-17 15:55:19 +00:00
|
|
|
def test_no_repo(memory_backup):
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
assert memory_backup.backuper.get_snapshots("") == []
|
|
|
|
|
|
|
|
|
2023-02-22 10:25:51 +00:00
|
|
|
def test_one_snapshot(backups, dummy_service):
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.back_up(dummy_service)
|
2023-02-22 13:35:55 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
snaps = Backups.get_snapshots(dummy_service)
|
2023-02-22 13:35:55 +00:00
|
|
|
assert len(snaps) == 1
|
|
|
|
snap = snaps[0]
|
|
|
|
assert snap.service_name == dummy_service.get_id()
|
2023-02-22 15:58:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_restore(backups, dummy_service):
|
|
|
|
service_folder = dummy_service.get_location()
|
|
|
|
file_to_nuke = listdir(service_folder)[0]
|
|
|
|
assert file_to_nuke is not None
|
|
|
|
path_to_nuke = path.join(service_folder, file_to_nuke)
|
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.back_up(dummy_service)
|
|
|
|
snap = Backups.get_snapshots(dummy_service)[0]
|
2023-02-22 15:58:36 +00:00
|
|
|
assert snap is not None
|
|
|
|
|
|
|
|
assert path.exists(path_to_nuke)
|
|
|
|
remove(path_to_nuke)
|
|
|
|
assert not path.exists(path_to_nuke)
|
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.restore_service_from_snapshot(dummy_service, snap.id)
|
2023-02-22 15:58:36 +00:00
|
|
|
assert path.exists(path_to_nuke)
|
2023-02-22 18:48:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_sizing(backups, dummy_service):
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.back_up(dummy_service)
|
|
|
|
snap = Backups.get_snapshots(dummy_service)[0]
|
|
|
|
size = Backups.service_snapshot_size(dummy_service, snap.id)
|
2023-02-22 18:48:08 +00:00
|
|
|
assert size is not None
|
|
|
|
assert size > 0
|
2023-03-13 19:03:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_redis_storage(backups_backblaze):
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.reset()
|
|
|
|
provider = Backups.provider()
|
2023-03-13 19:03:41 +00:00
|
|
|
|
|
|
|
assert provider is not None
|
|
|
|
|
|
|
|
assert isinstance(provider, Backblaze)
|
|
|
|
assert provider.login == "ID"
|
|
|
|
assert provider.key == "KEY"
|
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.store_provider_redis(provider)
|
|
|
|
restored_provider = Backups.load_provider_redis()
|
2023-03-13 19:03:41 +00:00
|
|
|
assert isinstance(restored_provider, Backblaze)
|
|
|
|
assert restored_provider.login == "ID"
|
|
|
|
assert restored_provider.key == "KEY"
|
2023-03-14 00:39:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
# lowlevel
|
|
|
|
def test_init_tracking_caching(backups, raw_dummy_service):
|
2023-03-29 11:15:38 +00:00
|
|
|
assert Backups._has_redis_init_mark(raw_dummy_service) is False
|
2023-03-14 00:39:15 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups._redis_mark_as_init(raw_dummy_service)
|
2023-03-14 00:39:15 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
assert Backups._has_redis_init_mark(raw_dummy_service) is True
|
|
|
|
assert Backups.is_initted(raw_dummy_service) is True
|
2023-03-14 00:39:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
# lowlevel
|
|
|
|
def test_init_tracking_caching2(backups, raw_dummy_service):
|
2023-03-29 11:15:38 +00:00
|
|
|
assert Backups._has_redis_init_mark(raw_dummy_service) is False
|
2023-03-14 00:39:15 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.init_repo(raw_dummy_service)
|
2023-03-14 00:39:15 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
assert Backups._has_redis_init_mark(raw_dummy_service) is True
|
2023-03-14 00:39:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
# only public API
|
|
|
|
def test_init_tracking(backups, raw_dummy_service):
|
2023-03-29 11:15:38 +00:00
|
|
|
assert Backups.is_initted(raw_dummy_service) is False
|
2023-03-14 00:39:15 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
Backups.init_repo(raw_dummy_service)
|
2023-03-14 00:39:15 +00:00
|
|
|
|
2023-03-29 11:15:38 +00:00
|
|
|
assert Backups.is_initted(raw_dummy_service) is True
|
2023-03-29 11:45:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_backup_service_task(backups, dummy_service):
|
|
|
|
handle = start_backup(dummy_service)
|
|
|
|
handle(blocking=True)
|
|
|
|
|
|
|
|
snaps = Backups.get_snapshots(dummy_service)
|
|
|
|
assert len(snaps) == 1
|