Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add partial backup reload options #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions aiohasupervisor/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
NewBackup,
PartialBackupOptions,
PartialRestoreOptions,
ReloadOptions,
)


Expand All @@ -35,9 +36,11 @@ async def set_options(self, options: BackupsOptions) -> None:
"""Set options for backups."""
await self._client.post("backups/options", json=options.to_dict())

async def reload(self) -> None:
async def reload(self, options: ReloadOptions | None = None) -> None:
"""Reload backups cache."""
await self._client.post("backups/reload")
await self._client.post(
"backups/reload", json=options.to_dict() if options else None
)

async def freeze(self, options: FreezeOptions | None = None) -> None:
"""Start a freeze for external snapshot process."""
Expand Down
2 changes: 2 additions & 0 deletions aiohasupervisor/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
NewBackup,
PartialBackupOptions,
PartialRestoreOptions,
ReloadOptions,
)
from aiohasupervisor.models.discovery import (
Discovery,
Expand Down Expand Up @@ -203,6 +204,7 @@
"NewBackup",
"PartialBackupOptions",
"PartialRestoreOptions",
"ReloadOptions",
"Discovery",
"DiscoveryConfig",
"AccessPoint",
Expand Down
9 changes: 9 additions & 0 deletions aiohasupervisor/models/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class BackupBaseFields(ABC):
type: BackupType
size: float
location: str | None
locations: list[str | None]
protected: bool
compressed: bool

Expand Down Expand Up @@ -167,3 +168,11 @@ class FullRestoreOptions(Request):
@dataclass(frozen=True, slots=True)
class PartialRestoreOptions(FullRestoreOptions, PartialBackupRestoreOptions):
"""PartialRestoreOptions model."""


@dataclass(frozen=True, slots=True)
class ReloadOptions(Request):
"""ReloadOptions model."""

location: str | None
filename: str
17 changes: 17 additions & 0 deletions tests/test_backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
FullBackupOptions,
PartialBackupOptions,
PartialRestoreOptions,
ReloadOptions,
)

from . import load_fixture
Expand All @@ -28,7 +29,7 @@
responses.get(
f"{SUPERVISOR_URL}/backups", status=200, body=load_fixture("backups_list.json")
)
backups = await supervisor_client.backups.list()

Check failure on line 32 in tests/test_backups.py

View workflow job for this annotation

GitHub Actions / Run tests Python 3.12.7

test_backups_list mashumaro.exceptions.InvalidFieldValue: Field "backups" of type list[Backup] in BackupList has invalid value [{'slug': '58bc7491', 'name': 'Full Backup 2024-04-06 00:05:39', 'date': '2024-04-06T07:05:40.000000+00:00', 'type': 'full', 'size': 828.81, 'location': None, 'protected': False, 'compressed': True, 'content': {'homeassistant': True, 'addons': ['core_matter_server', 'core_samba', 'core_ssh', 'a0d7b954_vscode', 'core_configurator', 'core_mosquitto', 'd5369777_music_assistant_beta', 'cebe7a76_hassio_google_drive_backup'], 'folders': ['share', 'addons/local', 'ssl', 'media']}}, {'slug': '69558789', 'name': 'addon_core_mosquitto_6.4.0', 'date': '2024-05-31T20:48:03.838030+00:00', 'type': 'partial', 'size': 0.01, 'location': None, 'protected': False, 'compressed': True, 'content': {'homeassistant': False, 'addons': ['core_mosquitto'], 'folders': []}}]
assert backups[0].slug == "58bc7491"
assert backups[0].type == "full"
assert backups[0].date == datetime(2024, 4, 6, 7, 5, 40, 0, UTC)
Expand All @@ -48,7 +49,7 @@
status=200,
body=load_fixture("backups_info.json"),
)
info = await supervisor_client.backups.info()

Check failure on line 52 in tests/test_backups.py

View workflow job for this annotation

GitHub Actions / Run tests Python 3.12.7

test_backups_info mashumaro.exceptions.InvalidFieldValue: Field "backups" of type list[Backup] in BackupsInfo has invalid value [{'slug': '58bc7491', 'name': 'Full Backup 2024-04-06 00:05:39', 'date': '2024-04-06T07:05:40.000000+00:00', 'type': 'full', 'size': 828.81, 'location': None, 'protected': False, 'compressed': True, 'content': {'homeassistant': True, 'addons': ['core_matter_server', 'core_samba', 'core_ssh', 'a0d7b954_vscode', 'core_configurator', 'core_mosquitto', 'd5369777_music_assistant_beta', 'cebe7a76_hassio_google_drive_backup'], 'folders': ['share', 'addons/local', 'ssl', 'media']}}, {'slug': '69558789', 'name': 'addon_core_mosquitto_6.4.0', 'date': '2024-05-31T20:48:03.838030+00:00', 'type': 'partial', 'size': 0.01, 'location': None, 'protected': False, 'compressed': True, 'content': {'homeassistant': False, 'addons': ['core_mosquitto'], 'folders': []}}]
assert info.backups[0].slug == "58bc7491"
assert info.backups[1].slug == "69558789"
assert info.days_until_stale == 30
Expand Down Expand Up @@ -79,6 +80,22 @@
}


async def test_backups_partial_reload(
responses: aioresponses, supervisor_client: SupervisorClient
) -> None:
"""Test backups partial reload API."""
responses.post(f"{SUPERVISOR_URL}/backups/reload", status=200)
assert (
await supervisor_client.backups.reload(
ReloadOptions(location=None, filename="test.tar")
)
is None
)
assert responses.requests.keys() == {
("POST", URL(f"{SUPERVISOR_URL}/backups/reload"))
}


@pytest.mark.parametrize("options", [None, FreezeOptions(timeout=1000)])
async def test_backups_freeze(
responses: aioresponses,
Expand Down Expand Up @@ -192,7 +209,7 @@
status=200,
body=load_fixture("backup_info.json"),
)
result = await supervisor_client.backups.backup_info("69558789")

Check failure on line 212 in tests/test_backups.py

View workflow job for this annotation

GitHub Actions / Run tests Python 3.12.7

test_backup_info mashumaro.exceptions.MissingField: Field "locations" of type list[Optional[str]] is missing in BackupComplete instance
assert result.slug == "69558789"
assert result.type == "partial"
assert result.date == datetime(2024, 5, 31, 0, 0, 0, 0, UTC)
Expand Down Expand Up @@ -222,7 +239,7 @@
status=200,
body=load_fixture("backup_info_no_homeassistant.json"),
)
result = await supervisor_client.backups.backup_info("d13dedd0")

Check failure on line 242 in tests/test_backups.py

View workflow job for this annotation

GitHub Actions / Run tests Python 3.12.7

test_backup_info_no_homeassistant mashumaro.exceptions.MissingField: Field "locations" of type list[Optional[str]] is missing in BackupComplete instance
assert result.slug == "d13dedd0"
assert result.type == "partial"
assert result.homeassistant is None
Expand Down
Loading