mirror of
https://github.com/home-assistant/supervisor.git
synced 2026-10-01 12:23:36 +01:00
* dbus: support aux units in start_transient_unit
Extend `Systemd.start_transient_unit` to accept the `aux` parameter
(`a(sa(sv))`), which has been hardcoded to `[]` since the wrapper was
introduced. Aux entries take the form `(unit_name, properties)` and
let systemd create multiple transient units atomically — most usefully
a `.mount` and its `.automount` companion in one D-Bus call.
Add the unit-property constants we'll need to drive that:
- `DBUS_ATTR_LAZY_UNMOUNT` ("LazyUnmount"): set on the `.mount` so
systemd umounts with MNT_DETACH. Pairs with `softerr`/`soft` to make
stop/restart sequences reliable even when the server is gone.
- `DBUS_ATTR_TIMEOUT_IDLE_USEC` ("TimeoutIdleUSec"): set on the
`.automount` to control how long the mount stays around after the
last access before autofs expires it.
- `DBUS_ATTR_WHERE` ("Where"): mount-point property; required on the
`.automount` unit (and useful explicitly on `.mount` units too).
No call sites change in this commit — pure plumbing.
* mounts: pair each network .mount with a .automount companion
Network mounts now get a transient `.automount` unit created
atomically alongside their `.mount`, via the aux parameter on
`StartTransientUnit`. The systemd-managed autofs trigger handles
activation lazily: the path exists in the VFS even before the
underlying network mount runs, and the first access that crosses the
trigger fires `mount.cifs`/`mount.nfs` on demand.
Why this matters:
- PID 1's path-walks (chase, daemon-reload, generator scans) stop
crossing dead NFS/CIFS lookups — autofs returns from kernel memory
rather than entering the network filesystem. The class of failure
fixed at the systemd-timeout layer in #6834 stops being reachable
in the first place.
- Failed accesses fail fast, bounded by the `.mount`'s `TimeoutSec=`
rather than hanging forever.
- Background reconnect (NFSv4 state-manager kthread, CIFS
delayed_work) recovers transparently when the server returns; no
remount needed.
The `.automount` carries `TimeoutIdleUSec=5min` so kernels can expire
the mount after inactivity and re-trigger on the next access. The
companion `.mount` carries `LazyUnmount=true` (MNT_DETACH on stop),
so umount returns immediately even when the server is unreachable;
existing fds drain in their soft/softerr timeout regime instead of
pinning the umount syscall.
`Mount.unmount()` now stops the `.automount` first so the autofs
trigger can't re-fire the underlying `.mount` during cleanup. The
automount stop is best-effort — if the unit is gone or refuses to
stop, we log and proceed to the `.mount` stop, which remains
authoritative.
Bind mounts opt out via a `creates_automount` class flag — they have
no server to wait on and the lazy semantics would only add
indirection. Network mounts (CIFS/NFS) opt in.
This is wiring only; the periodic mount reload, the reload/restart
escalation, and the inner bind-mount layer for media/share usage are
still in place. The next commit removes them now that automount
makes them unnecessary.
* mounts: drop bind layer, periodic reload, and reload/restart machinery
With the kernel's autofs trigger handling lazy activation and
transparent reconnect, almost all of the supervisor-side mount
choreography becomes unnecessary. Strip it out.
What goes away:
* The inner bind-mount layer. Media/share mounts used to live at
`path_extern_mounts/{name}` and have a second `.mount` unit bind
them into `path_extern_media/{name}` (resp. `share`). The
`.automount` companion can sit directly at the container-facing
path, and the parent-dir RSLAVE bind into add-on containers
surfaces the autofs trigger the same way. `NetworkMount.where`
now switches on usage:
- MEDIA → `path_extern_media/{name}`
- SHARE → `path_extern_share/{name}`
- BACKUP → `path_extern_mounts/{name}` (unchanged)
* `BindMount`, `BoundMount`, `MountManager._bound_mounts`, the
`_bind_mount`/`_bind_media`/`_bind_share` helpers, and the entire
emergency-fallback dance (`path_emergency`). The empty-read-only
dir trick was a workaround for the harder PID 1 wedge problem,
which autofs solves at the root. Failed shares now surface as
ETIMEDOUT/EHOSTDOWN on access plus a resolution issue.
* `MountManager.reload()` and the 15-minute `RUN_RELOAD_MOUNTS`
periodic task. autofs re-activates on access; we don't need to
poll, and not polling means we don't randomly trip stale-FH /
softreval / dead-server edge cases on a timer.
* `Mount.reload()` and `Mount._restart()`. The reload→restart
escalation existed to make `is_mounted` honest in the face of
systemd's local-only state; now `is_mounted` IS honest (probe-
based), and any recovery the kernel knows how to do happens
inside autofs without our involvement.
* The `RELOADING` safety net introduced by #6834. That whole class
of PID 1 wedge stops being reachable when path lookups don't
cross dead network mounts.
What changes shape:
* `NetworkMount.is_mounted()` no longer gates on systemd's
`ActiveState` before probing. The `.mount` unit is dormant
whenever autofs hasn't recently triggered it, so systemd state
is meaningless as a health signal. The probe (now via
`statvfs("/path/.")` so the trailing dot forces `LOOKUP_DIRECTORY`
and triggers autofs) is the source of truth, and after it returns
we set `self._state` to ACTIVE/INACTIVE so the API reports
reachability rather than autofs idle state.
* `MountManager.reload_mount()` becomes probe-only — no systemd
reload/restart calls. The user-facing semantics: "tell me if this
mount is reachable right now, and refresh the resolution issue
accordingly." If the mount is dead, autofs will re-trigger it on
the next consumer access; the supervisor doesn't need to force it.
* `Mount.load()` keeps `_update_state_await` for the no-job-dispatched
case where a previous supervisor left a unit in `activating`, then
probes once via `is_mounted` so state reflects reachability rather
than the lazy-mount idle state.
* `BackupManager` no longer walks `bound_mounts`. Backups skip
network-mount subdirectories during folder archive (so we don't
recurse into the share), and unmount/re-mount them around folder
restores (so writes target the local mount-point dir, not the
remote share).
Behavior tradeoffs accepted:
* Dead media/share access now ~30s ETIMEDOUT instead of empty
read-only dir. The empty-dir was a workaround for the PID 1
wedge that autofs eliminates at the root; if specific add-ons
rely on the old behavior we can revisit with a 2-stage automount
whose fallback target is an emergency dir.
* Resolution-issue lag: failed mounts no longer surface within
15 min on their own. They appear when the user probes via the
API, when `BackupManager.reload()` walks the location, or when
load-time activation fails.
* tests: adapt bind-layer-era tests after rebase
Main gained tests for the bind layer after this branch was written:
the #7013 rebind regression test and the #7072 bind-step rollback test
cover machinery that no longer exists, and the healthy-reload test
asserted the unconditional rebind. Drop the first two and reduce the
third to asserting that a healthy probe performs no systemd operations
at all.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: rearchitect automount setup from systemd/kernel review
A code-level review of systemd's automount implementation (verified
against v254.13, the version HAOS ships) and the kernel autofs/VFS
plumbing surfaced three correctness-critical flaws in the automount
design, plus several hardening gaps. See automount-rearchitecture.md
for the full analysis.
- Make the .automount the primary transient unit with the .mount as
aux, mirroring `systemd-mount --automount=yes`. Aux units get no
start job: with the .mount as primary the trigger was never armed
and the design silently degraded to eager mounting.
- Set StartLimitIntervalUSec=0 on the .mount. With the default start
rate limit (5 starts/10s, counting successful starts) a fast-failing
mount plus any polling consumer trips the limit within seconds, and
systemd then detaches the autofs trigger entirely
(AUTOMOUNT_FAILURE_MOUNT_START_LIMIT_HIT) — the path silently
becomes a plain writable local directory.
- Drop the 5-minute TimeoutIdleUSec (default 0 = never expire).
Kernel idle expiry decides busyness via may_umount_tree(), which
only counts the init-namespace mount instance — open files held by
container processes are invisible, so expiry would unmount shares
under actively writing add-ons.
- Probe with a plain statvfs: the statfs syscall walks with
LOOKUP_AUTOMOUNT and triggers by itself; the trailing-dot trick is
unnecessary. Classify ELOOP as a mount-propagation
misconfiguration in the probe error handling.
- Re-arm the trigger from reload_mount(): if the .automount unit is
failed or gone (e.g. autofs unmounted out-of-band), reset failure
state and re-create the pair before probing.
- Tear down legacy eager-mount units during load(). The old design's
bind unit for media/share occupies the exact unit name the network
.mount uses now; on a warm upgrade adoption would mistake the old
bind mount for the network mount and leak the legacy data mount.
- Honor the systemd job result when stopping the .mount during
unmount and reset failure state on both units afterwards so dead
transient units get garbage-collected.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: adapt local data repair to the autofs design
Port of the mount-target-not-empty repair (#7089) on top of the
automount rearchitecture:
- relocate_local_data() handles a single target directory — the mount
sits directly at its container-facing path, the bind layer and with
it the multi-directory case are gone
- a repair_trigger() failure caused by blocking local data raises the
mount failed issue with the move_local_data suggestion instead of
the plain variant
- local data at load is detected via the mount unit itself; the
bind-layer detection test is rewritten accordingly
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* docker: use rslave propagation for execute_command share mount
The temporary container used for execute_command (e.g. core config
check) mounted /share without a propagation mode, unlike every other
share/media mount. With eager mounts this only meant missing mounts
made after container start; with lazy automount activation the shares
are routinely mounted after start, and accessing a not-yet-activated
automount from a private mount namespace fails with ELOOP.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: harden automount lifecycle from review findings
Address the findings of an adversarial review of the automount
rearchitecture:
- load() no longer adopts a dead automount trigger: a failed or
stopped .automount leaves the path a plain writable directory — the
silent local-write degradation this design must prevent. Tear down
and re-arm instead. An adopted pair whose probe fails now raises so
the manager surfaces the mount failed issue, same as a fresh mount.
- unmount() resolves the .mount unit only after stopping the
.automount (the lazy detach can garbage-collect the transient unit,
invalidating an earlier proxy), raises instead of warning when the
automount stop errors, and checks the stop job result so a failed
stop cannot leave an armed trigger behind a "successful" removal.
- repair_trigger() fully unmounts before re-arming: with the share
still attached, arming fails — or worse, mount() misreads the
mounted share's contents as blocking local data.
- Legacy unit teardown honors the stop job result for the same reason.
- reload_mount() escalates once to re-creating the unit pair when an
established mount is unreachable: a permanently dead session (e.g.
replaced server) keeps the path mounted, so the trigger can never
re-fire and kernel reconnection never succeeds. Teardown is safe now
(lazy unmount, no PID 1 path walks), unlike the removed reload →
restart escalation of the eager design.
- Reinstate the periodic mounts task as a probe-based reconcile: re-arm
dead triggers, refresh the reachability state reported by the API and
used by backup locations, and sync the mount failed issue in both
directions. No reload or restart of established mounts.
- Folder restore no longer fails after a successful restore when
re-mounting nested mounts cannot verify an unreachable server — the
trigger is armed, the share recovers on next access.
- Drop the now-unused Mount.update(), deduplicate unit name escaping,
hoist the backup exclusion set out of the per-file filter.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* tests: expect rslave propagation in core check container
The config check assertion missed the update for the rslave
propagation on the execute_command share mount.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* tests: cover reconcile trigger repair failure paths
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: arm automount before stopping the legacy data mount
Address review: the legacy teardown stopped the media/share bind unit
and the eager data mount in sequence, both strictly. When the bind
stop succeeded but the data mount stop failed — its unmount can time
out against an unreachable server, legacy units have no LazyUnmount —
load() raised with the container-facing path left a plain writable
directory and no trigger armed: the pollution mode this design is
meant to prevent. Reordering the stops would not help either, as
systemd stops the bind first anyway as a dependent of the data mount
(the implicit Requires= from RequiresMountsFor= on the bind's What=).
Instead, strictly stop only the path-conflicting unit (a failure
leaves the path covered, which is safe and retryable), arm the
automount right after, and stop the conflict-free legacy data mount
best-effort last — a failed stop logs a warning and leaves an orphaned
mount for the next Supervisor restart or a host reboot, with nothing
writable exposed.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: discard dead session instead of re-creating units on reload
Address review: re-creating the unit pair on reload of an unreachable
established mount exposed the target as a plain writable directory
between the lazy unmount and arming the replacement — a continuously
writing add-on could block the re-arm or slip writes under the new
mount in the check-to-arm window.
Stop only the .mount unit instead, keeping the .automount armed:
systemd re-installs the autofs trigger over the path — the same
mechanism idle expiry uses, with the automount's Triggers= reference
keeping the transient .mount definition alive — so the path is never
locally writable. The re-probe then mounts fresh through the trigger,
which establishes a new session and thereby covers the permanently
dead session case (e.g. a replaced server) the escalation exists for.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: surface arming failures, keep restore teardown re-armed
Address review findings on the arming/re-arming error paths:
- mount() checks the StartTransientUnit job result: a failed start job
means the trigger never armed and the path is a plain writable
directory — a hard MountError before the probe, so it cannot be
mistaken for the armed-but-unreachable MountActivationError.
- Folder restore includes the nested-mount teardown in the try block:
an unmount failing halfway (trigger disarmed, share still attached)
previously exited before any re-arm, leaving the path unprotected.
The finally re-arms via repair_trigger(), which no-ops on a still
armed trigger and handles partially torn-down pairs.
- The post-restore re-arm suppresses only MountActivationError (armed,
recovers on next access). Any other failure — arming failed, local
data blocking the target — left the path unprotected and surfaces.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: fold the two legacy unit teardowns into one helper
The eager-mount-era cleanup queried the .mount unit a second time
although load() had just fetched it, and the strict teardown of the
unit occupying the automount's path and the best-effort teardown of
the mount at the mounts data directory were near identical. Reuse the
unit from load() and give the shared helper a strict flag, which drops
a D-Bus round trip per mount on every load.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: re-arm the trigger when unmount cannot stop the mount
Stopping the automount detaches the whole stack at the path, so an
unmount that then fails to stop the .mount leaves a plain writable
directory behind. Arm a fresh pair before raising, best effort — the
local data repair covers what lands there if that fails too.
The automount stop itself needs no such handling: systemd's
automount_stop() enters dead synchronously and the detach it performs
(MNT_DETACH, no server contact) only logs its errors, so a failure
there means systemd could not be reached at all.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: raise translatable errors for mount operations
Errors from setting up, unmounting and reloading a mount reach users
through the API, so describe what failed in a translatable message and
leave the systemd job result or D-Bus error to the log.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: remove the emergency folder of the eager-mount design
The read-only fallback directory has no place in the automount design.
Remove what is left of it on existing installations, next to the legacy
addons directory cleanup, keeping it if it holds anything but the empty
mount points.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: trim comments to the behavior they describe
Drop the narration of what changed and why from comments and
docstrings, keeping the notes that record why a tempting alternative
does not work. Two comments still described the reload to restart
escalation this branch removed.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: correct stale claims in manager and reload test
The manager docstring credited the kernel with idle expiry, which is
deliberately disabled, and claimed no polling although the periodic
reconcile probes every 15 minutes. The reload test claimed systemd is
never contacted while the escalation stops the .mount unit; only reload
and restart of the unit are avoided.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: arm the trigger alone if re-creating the pair is rejected
Address review: the re-arm after a failed unmount submits the .mount as
an aux unit, but transient creation requires a pristine unit and the
definition is still loaded precisely when stopping it is what failed.
Fall back to creating the .automount on its own, which covers the path
and fires the surviving mount definition.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* mounts: never arm the trigger over local data after a failed unmount
Address review: the fallback caught the target validation errors too,
so data written while the path was uncovered would end up beneath a
fresh trigger. Reconciliation would then find a healthy mount and the
repair skips mount points, leaving the data hidden for good. Leave the
path untouched instead, so the next reconcile offers to move it away.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2410 lines
86 KiB
Python
2410 lines
86 KiB
Python
"""Test BackupManager class."""
|
|
|
|
import asyncio
|
|
import errno
|
|
from functools import partial
|
|
from pathlib import Path
|
|
from shutil import copy, rmtree
|
|
from types import SimpleNamespace
|
|
from unittest.mock import ANY, AsyncMock, MagicMock, Mock, PropertyMock, patch
|
|
|
|
from aiodocker.containers import DockerContainer
|
|
from awesomeversion import AwesomeVersion
|
|
from dbus_fast import DBusError
|
|
import pytest
|
|
|
|
from supervisor.apps.app import App
|
|
from supervisor.apps.const import AppBackupMode
|
|
from supervisor.apps.model import AppModel
|
|
from supervisor.backups.backup import Backup, BackupLocation
|
|
from supervisor.backups.const import LOCATION_TYPE, BackupJobStage, BackupType
|
|
from supervisor.backups.manager import BackupManager
|
|
from supervisor.const import FOLDER_HOMEASSISTANT, FOLDER_SHARE, AppState, CoreState
|
|
from supervisor.coresys import CoreSys
|
|
from supervisor.docker.app import DockerApp
|
|
from supervisor.docker.const import ContainerState
|
|
from supervisor.docker.homeassistant import DockerHomeAssistant
|
|
from supervisor.docker.monitor import DockerContainerStateEvent
|
|
from supervisor.exceptions import (
|
|
BackupError,
|
|
BackupFileNotFoundError,
|
|
BackupInvalidError,
|
|
BackupJobError,
|
|
BackupMountDownError,
|
|
DockerError,
|
|
MountError,
|
|
)
|
|
from supervisor.homeassistant.api import HomeAssistantAPI
|
|
from supervisor.homeassistant.const import WSType
|
|
from supervisor.homeassistant.core import HomeAssistantCore
|
|
from supervisor.homeassistant.module import HomeAssistant
|
|
from supervisor.jobs import JobSchedulerOptions
|
|
from supervisor.jobs.const import JobCondition
|
|
from supervisor.mounts.manager import MountManager
|
|
from supervisor.mounts.mount import Mount
|
|
from supervisor.resolution.const import UnhealthyReason
|
|
from supervisor.utils.json import read_json_file, write_json_file
|
|
|
|
from tests.common import force_app_state, get_fixture_path
|
|
from tests.const import TEST_ADDON_SLUG
|
|
from tests.dbus_service_mocks.base import DBusServiceMock
|
|
from tests.dbus_service_mocks.systemd import Systemd as SystemdService
|
|
from tests.dbus_service_mocks.systemd_unit import SystemdUnit as SystemdUnitService
|
|
|
|
|
|
async def test_do_backup_full(coresys: CoreSys, backup_mock, install_app_ssh):
|
|
"""Test creating Backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
# backup_mock fixture causes Backup() to be a MagicMock
|
|
backup_instance: MagicMock = await manager.do_backup_full()
|
|
|
|
# Check Backup has been created without password
|
|
assert backup_instance.new.call_args[0][2] == BackupType.FULL
|
|
assert backup_instance.new.call_args[0][3] is None
|
|
assert backup_instance.new.call_args[0][4] is True
|
|
|
|
backup_instance.store_homeassistant.assert_called_once()
|
|
backup_instance.store_repositories.assert_called_once()
|
|
|
|
backup_instance.store_apps.assert_called_once()
|
|
assert install_app_ssh in backup_instance.store_apps.call_args[0][0]
|
|
|
|
backup_instance.store_folders.assert_called_once()
|
|
assert len(backup_instance.store_folders.call_args[0][0]) == 4
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("filename", "filename_expected"),
|
|
[("../my file.tar", "/data/backup/my file.tar"), (None, "/data/backup/{}.tar")],
|
|
)
|
|
async def test_do_backup_full_with_filename(
|
|
coresys: CoreSys, filename: str, filename_expected: str, backup_mock
|
|
):
|
|
"""Test creating Backup with a specific file name."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
# backup_mock fixture causes Backup() to be a MagicMock
|
|
await manager.do_backup_full(filename=filename)
|
|
|
|
slug = backup_mock.call_args[0][2]
|
|
assert str(backup_mock.call_args[0][1]) == filename_expected.format(slug)
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.usefixtures("backup_mock")
|
|
async def test_do_backup_full_uncompressed(coresys: CoreSys, install_app_ssh: App):
|
|
"""Test creating Backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
# backup_mock fixture causes Backup() to be a MagicMock
|
|
backup_instance: MagicMock = await manager.do_backup_full(compressed=False)
|
|
|
|
# Check Backup has been created without password
|
|
assert backup_instance.new.call_args[0][2] == BackupType.FULL
|
|
assert backup_instance.new.call_args[0][3] is None
|
|
assert backup_instance.new.call_args[0][4] is False
|
|
|
|
backup_instance.store_homeassistant.assert_called_once()
|
|
backup_instance.store_repositories.assert_called_once()
|
|
|
|
backup_instance.store_apps.assert_called_once()
|
|
assert install_app_ssh in backup_instance.store_apps.call_args[0][0]
|
|
|
|
backup_instance.store_folders.assert_called_once()
|
|
assert len(backup_instance.store_folders.call_args[0][0]) == 4
|
|
backup_instance.store_homeassistant.assert_called_once()
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.usefixtures("backup_mock", "install_app_ssh")
|
|
async def test_do_backup_partial_minimal(coresys: CoreSys):
|
|
"""Test creating minimal partial Backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
# backup_mock fixture causes Backup() to be a MagicMock
|
|
backup_instance: MagicMock = await manager.do_backup_partial(homeassistant=False)
|
|
|
|
# Check Backup has been created without password
|
|
assert backup_instance.new.call_args[0][2] == BackupType.PARTIAL
|
|
assert backup_instance.new.call_args[0][3] is None
|
|
assert backup_instance.new.call_args[0][4] is True
|
|
|
|
backup_instance.store_homeassistant.assert_not_called()
|
|
backup_instance.store_repositories.assert_called_once()
|
|
|
|
backup_instance.store_apps.assert_not_called()
|
|
|
|
backup_instance.store_folders.assert_not_called()
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.usefixtures("backup_mock", "install_app_ssh")
|
|
async def test_do_backup_partial_minimal_uncompressed(coresys: CoreSys):
|
|
"""Test creating minimal partial Backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
# backup_mock fixture causes Backup() to be a MagicMock
|
|
backup_instance: MagicMock = await manager.do_backup_partial(
|
|
homeassistant=False, compressed=False
|
|
)
|
|
|
|
# Check Backup has been created without password
|
|
assert backup_instance.new.call_args[0][2] == BackupType.PARTIAL
|
|
assert backup_instance.new.call_args[0][3] is None
|
|
assert backup_instance.new.call_args[0][4] is False
|
|
|
|
backup_instance.store_homeassistant.assert_not_called()
|
|
backup_instance.store_repositories.assert_called_once()
|
|
|
|
backup_instance.store_apps.assert_not_called()
|
|
|
|
backup_instance.store_folders.assert_not_called()
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.usefixtures("backup_mock")
|
|
async def test_do_backup_partial_maximal(coresys: CoreSys, install_app_ssh: App):
|
|
"""Test creating maximal partial Backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
# backup_mock fixture causes Backup() to be a MagicMock
|
|
backup_instance: MagicMock = await manager.do_backup_partial(
|
|
apps=[TEST_ADDON_SLUG],
|
|
folders=[FOLDER_SHARE, FOLDER_HOMEASSISTANT],
|
|
homeassistant=True,
|
|
)
|
|
|
|
# Check Backup has been created without password
|
|
assert backup_instance.new.call_args[0][2] == BackupType.PARTIAL
|
|
assert backup_instance.new.call_args[0][3] is None
|
|
assert backup_instance.new.call_args[0][4] is True
|
|
|
|
backup_instance.store_homeassistant.assert_called_once()
|
|
backup_instance.store_repositories.assert_called_once()
|
|
|
|
backup_instance.store_apps.assert_called_once()
|
|
assert install_app_ssh in backup_instance.store_apps.call_args[0][0]
|
|
|
|
backup_instance.store_folders.assert_called_once()
|
|
assert len(backup_instance.store_folders.call_args[0][0]) == 1
|
|
backup_instance.store_homeassistant.assert_called_once()
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet")
|
|
async def test_do_restore_full(
|
|
coresys: CoreSys, full_backup_mock: Backup, install_app_ssh: App
|
|
):
|
|
"""Test restoring full Backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
coresys.homeassistant.core.start = AsyncMock(return_value=None)
|
|
coresys.homeassistant.core.stop = AsyncMock(return_value=None)
|
|
coresys.homeassistant.core.update = AsyncMock(return_value=None)
|
|
install_app_ssh.uninstall = AsyncMock(return_value=None)
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
backup_instance = full_backup_mock.return_value
|
|
backup_instance.sys_apps = coresys.apps
|
|
backup_instance.remove_delta_apps = partial(
|
|
Backup.remove_delta_apps, backup_instance
|
|
)
|
|
assert await manager.do_restore_full(backup_instance)
|
|
|
|
backup_instance.restore_homeassistant.assert_called_once()
|
|
backup_instance.restore_repositories.assert_called_once()
|
|
|
|
backup_instance.restore_apps.assert_called_once()
|
|
install_app_ssh.uninstall.assert_not_called()
|
|
|
|
backup_instance.restore_folders.assert_called_once()
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet")
|
|
async def test_do_restore_full_different_app(
|
|
coresys: CoreSys, full_backup_mock: Backup, install_app_ssh: App
|
|
):
|
|
"""Test restoring full Backup with different apps than installed."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
coresys.homeassistant.core.start = AsyncMock(return_value=None)
|
|
coresys.homeassistant.core.stop = AsyncMock(return_value=None)
|
|
coresys.homeassistant.core.update = AsyncMock(return_value=None)
|
|
install_app_ssh.uninstall = AsyncMock(return_value=None)
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
backup_instance = full_backup_mock.return_value
|
|
backup_instance.app_list = ["differentslug"]
|
|
backup_instance.sys_apps = coresys.apps
|
|
backup_instance.remove_delta_apps = partial(
|
|
Backup.remove_delta_apps, backup_instance
|
|
)
|
|
assert await manager.do_restore_full(backup_instance)
|
|
|
|
backup_instance.restore_homeassistant.assert_called_once()
|
|
backup_instance.restore_repositories.assert_called_once()
|
|
|
|
backup_instance.restore_apps.assert_called_once()
|
|
install_app_ssh.uninstall.assert_called_once()
|
|
|
|
backup_instance.restore_folders.assert_called_once()
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet", "install_app_ssh")
|
|
async def test_do_restore_partial_minimal(
|
|
coresys: CoreSys, partial_backup_mock: Backup
|
|
):
|
|
"""Test restoring partial Backup minimal."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
coresys.homeassistant.core.start = AsyncMock(return_value=None)
|
|
coresys.homeassistant.core.stop = AsyncMock(return_value=None)
|
|
coresys.homeassistant.core.update = AsyncMock(return_value=None)
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
backup_instance = partial_backup_mock.return_value
|
|
assert await manager.do_restore_partial(backup_instance, homeassistant=False)
|
|
|
|
backup_instance.restore_homeassistant.assert_not_called()
|
|
backup_instance.restore_repositories.assert_not_called()
|
|
|
|
backup_instance.restore_apps.assert_not_called()
|
|
|
|
backup_instance.restore_folders.assert_not_called()
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet")
|
|
async def test_do_restore_partial_maximal(
|
|
coresys: CoreSys, partial_backup_mock: Backup
|
|
):
|
|
"""Test restoring partial Backup minimal."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
coresys.homeassistant.core.start = AsyncMock(return_value=None)
|
|
coresys.homeassistant.core.stop = AsyncMock(return_value=None)
|
|
coresys.homeassistant.core.update = AsyncMock(return_value=None)
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
backup_instance = partial_backup_mock.return_value
|
|
assert await manager.do_restore_partial(
|
|
backup_instance,
|
|
apps=[TEST_ADDON_SLUG],
|
|
folders=[FOLDER_SHARE, FOLDER_HOMEASSISTANT],
|
|
homeassistant=True,
|
|
)
|
|
|
|
backup_instance.restore_homeassistant.assert_called_once()
|
|
backup_instance.restore_repositories.assert_called_once()
|
|
|
|
backup_instance.restore_apps.assert_called_once()
|
|
|
|
backup_instance.restore_folders.assert_called_once()
|
|
backup_instance.restore_homeassistant.assert_called_once()
|
|
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet")
|
|
async def test_fail_invalid_full_backup(
|
|
coresys: CoreSys,
|
|
full_backup_mock: MagicMock,
|
|
partial_backup_mock: MagicMock,
|
|
):
|
|
"""Test restore fails with invalid backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
with pytest.raises(BackupInvalidError):
|
|
await manager.do_restore_full(partial_backup_mock.return_value)
|
|
|
|
backup_instance = full_backup_mock.return_value
|
|
backup_instance.all_locations[None].protected = True
|
|
backup_instance.validate_backup.side_effect = BackupInvalidError()
|
|
|
|
with pytest.raises(BackupInvalidError):
|
|
await manager.do_restore_full(backup_instance)
|
|
|
|
backup_instance.all_locations[None].protected = False
|
|
backup_instance.supervisor_version = "2022.08.4"
|
|
with (
|
|
patch.object(
|
|
type(coresys.supervisor),
|
|
"version",
|
|
new=PropertyMock(return_value="2022.08.3"),
|
|
),
|
|
pytest.raises(BackupInvalidError),
|
|
):
|
|
await manager.do_restore_full(backup_instance)
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet")
|
|
async def test_fail_invalid_partial_backup(
|
|
coresys: CoreSys, partial_backup_mock: MagicMock
|
|
):
|
|
"""Test restore fails with invalid backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
manager = await BackupManager(coresys).load_config()
|
|
|
|
backup_instance = partial_backup_mock.return_value
|
|
backup_instance.all_locations[None].protected = True
|
|
backup_instance.validate_backup.side_effect = BackupInvalidError()
|
|
|
|
with pytest.raises(BackupInvalidError):
|
|
await manager.do_restore_partial(backup_instance)
|
|
|
|
backup_instance.all_locations[None].protected = False
|
|
backup_instance.homeassistant = None
|
|
|
|
with pytest.raises(BackupInvalidError):
|
|
await manager.do_restore_partial(backup_instance, homeassistant=True)
|
|
|
|
backup_instance.supervisor_version = "2022.08.4"
|
|
with (
|
|
patch.object(
|
|
type(coresys.supervisor),
|
|
"version",
|
|
new=PropertyMock(return_value="2022.08.3"),
|
|
),
|
|
pytest.raises(BackupInvalidError),
|
|
):
|
|
await manager.do_restore_partial(backup_instance)
|
|
|
|
|
|
@pytest.mark.usefixtures("install_app_ssh", "capture_exception")
|
|
async def test_backup_error_homeassistant(coresys: CoreSys, backup_mock: MagicMock):
|
|
"""Test error collected and file deleted when Home Assistant Core backup fails."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
backup_instance = backup_mock.return_value
|
|
|
|
backup_instance.store_homeassistant.side_effect = (
|
|
err := BackupError("Error while storing homeassistant")
|
|
)
|
|
|
|
job, backup_task = coresys.jobs.schedule_job(
|
|
coresys.backups.do_backup_full, JobSchedulerOptions()
|
|
)
|
|
assert await backup_task is None
|
|
|
|
assert job.errors[0].type_ is type(err)
|
|
assert job.errors[0].message == str(err)
|
|
assert job.errors[0].stage == BackupJobStage.HOME_ASSISTANT
|
|
|
|
backup_instance.tarfile.unlink.assert_called_once()
|
|
|
|
|
|
@pytest.mark.usefixtures("install_app_ssh")
|
|
async def test_backup_error_capture(
|
|
coresys: CoreSys, backup_mock: MagicMock, capture_exception: Mock
|
|
):
|
|
"""Test error captured when backup fails."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
backup_mock.return_value.store_folders.side_effect = (err := OSError())
|
|
backup = await coresys.backups.do_backup_full()
|
|
|
|
assert backup is None
|
|
|
|
capture_exception.assert_called_once_with(err)
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet")
|
|
async def test_restore_error(
|
|
coresys: CoreSys, full_backup_mock: MagicMock, capture_exception: Mock
|
|
):
|
|
"""Test restoring full Backup with errors."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
coresys.homeassistant.core.start = AsyncMock(return_value=None)
|
|
|
|
backup_instance = full_backup_mock.return_value
|
|
backup_instance.protected = False
|
|
backup_instance.restore_homeassistant.side_effect = BackupError()
|
|
with pytest.raises(BackupError):
|
|
await coresys.backups.do_restore_full(backup_instance)
|
|
capture_exception.assert_not_called()
|
|
|
|
backup_instance.restore_homeassistant.side_effect = (err := DockerError())
|
|
with pytest.raises(BackupError):
|
|
await coresys.backups.do_restore_full(backup_instance)
|
|
capture_exception.assert_called_once_with(err)
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"supervisor_internet",
|
|
"tmp_supervisor_data",
|
|
"path_extern",
|
|
"mount_propagation",
|
|
"mock_is_mount",
|
|
)
|
|
async def test_backup_media_with_mounts(
|
|
coresys: CoreSys, all_dbus_services: dict[str, DBusServiceMock]
|
|
):
|
|
"""Test backing up media folder with mounts."""
|
|
systemd_service: SystemdService = all_dbus_services["systemd"]
|
|
systemd_unit_service: SystemdUnitService = all_dbus_services["systemd_unit"]
|
|
systemd_service.response_get_unit = [
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
]
|
|
|
|
# Make some normal test files
|
|
(test_file_1 := coresys.config.path_media / "test.txt").touch()
|
|
(test_dir := coresys.config.path_media / "test").mkdir()
|
|
(test_file_2 := coresys.config.path_media / "test" / "inner.txt").touch()
|
|
|
|
# Add a media mount
|
|
await coresys.mounts.load()
|
|
await coresys.mounts.create_mount(
|
|
Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "media_test",
|
|
"usage": "media",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
)
|
|
assert (mount_dir := coresys.config.path_media / "media_test").is_dir()
|
|
|
|
# Make a partial backup
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
backup: Backup = await coresys.backups.do_backup_partial("test", folders=["media"])
|
|
|
|
# Remove the mount and wipe the media folder
|
|
systemd_unit_service.active_state = "inactive"
|
|
await coresys.mounts.remove_mount("media_test")
|
|
rmtree(coresys.config.path_media)
|
|
coresys.config.path_media.mkdir()
|
|
|
|
# Restore the backup and check that only the test files we made returned
|
|
with patch.object(DockerHomeAssistant, "is_running", return_value=True):
|
|
await coresys.backups.do_restore_partial(backup, folders=["media"])
|
|
|
|
assert test_file_1.exists()
|
|
assert test_dir.is_dir()
|
|
assert test_file_2.exists()
|
|
assert not mount_dir.exists()
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"supervisor_internet",
|
|
"tmp_supervisor_data",
|
|
"path_extern",
|
|
"mount_propagation",
|
|
"mock_is_mount",
|
|
)
|
|
async def test_backup_media_with_mounts_retains_files(
|
|
coresys: CoreSys, all_dbus_services: dict[str, DBusServiceMock]
|
|
):
|
|
"""Test backing up media folder with mounts retains mount files."""
|
|
systemd_service: SystemdService = all_dbus_services["systemd"]
|
|
systemd_unit_service: SystemdUnitService = all_dbus_services["systemd_unit"]
|
|
systemd_unit_service.active_state = "active"
|
|
systemd_service.response_get_unit = [
|
|
# create_mount: no .mount, no .automount
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
# create_mount: post-arm state refresh resolves the new .mount
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
# create_mount: no legacy data mount unit to clean up
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
# folder restore: unmount resolves the .mount after automount stop
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
# folder restore: repair_trigger finds no .automount after the unmount
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
# folder restore: repair's own unmount finds no .mount either
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
# folder restore: re-arm post-arm state refresh
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
# mount config restore: unmount of the replaced mount
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
]
|
|
|
|
# Add a media mount
|
|
await coresys.mounts.load()
|
|
await coresys.mounts.create_mount(
|
|
Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "media_test",
|
|
"usage": "media",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
)
|
|
|
|
# Make a partial backup
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
backup: Backup = await coresys.backups.do_backup_partial("test", folders=["media"])
|
|
|
|
systemd_service.StopUnit.calls.clear()
|
|
systemd_service.StartTransientUnit.calls.clear()
|
|
# Freeze background activation of the restored mount config — it is
|
|
# exercised by its own tests and would race the call assertions here
|
|
with (
|
|
patch.object(DockerHomeAssistant, "is_running", return_value=True),
|
|
patch.object(MountManager, "_activate_restored_mount"),
|
|
):
|
|
await coresys.backups.do_restore_partial(backup, folders=["media"])
|
|
|
|
# Restore unmounts the network mount nested inside `media` (stops
|
|
# both the .automount and .mount), then repairs the trigger after
|
|
# writes: repair_trigger sees the .automount is gone, tears down once
|
|
# more (only the .automount stop is dispatched — the .mount is gone
|
|
# too) and arms a fresh transient .automount + aux .mount pair.
|
|
# The backup also carries the mount configuration, so the mount is
|
|
# replaced afterwards: another unmount plus the activation mount.
|
|
assert systemd_service.StopUnit.calls == [
|
|
("mnt-data-supervisor-media-media_test.automount", "fail"),
|
|
("mnt-data-supervisor-media-media_test.mount", "fail"),
|
|
("mnt-data-supervisor-media-media_test.automount", "fail"),
|
|
("mnt-data-supervisor-media-media_test.automount", "fail"),
|
|
("mnt-data-supervisor-media-media_test.mount", "fail"),
|
|
]
|
|
assert systemd_service.StartTransientUnit.calls == [
|
|
("mnt-data-supervisor-media-media_test.automount", "fail", ANY, ANY)
|
|
]
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"supervisor_internet",
|
|
"tmp_supervisor_data",
|
|
"path_extern",
|
|
"mount_propagation",
|
|
"mock_is_mount",
|
|
)
|
|
async def test_folder_restore_repairs_trigger_on_unmount_failure(
|
|
coresys: CoreSys, all_dbus_services: dict[str, DBusServiceMock]
|
|
):
|
|
"""Test folder restore repairs the automount trigger if unmount fails."""
|
|
systemd_service: SystemdService = all_dbus_services["systemd"]
|
|
systemd_unit_service: SystemdUnitService = all_dbus_services["systemd_unit"]
|
|
systemd_unit_service.active_state = "active"
|
|
systemd_service.response_get_unit = [
|
|
# create_mount: no .mount, no .automount
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
# create_mount: post-arm state refresh resolves the new .mount
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
# create_mount: no legacy data mount unit to clean up
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
]
|
|
|
|
# Add a media mount nested inside the folder to be restored
|
|
await coresys.mounts.load()
|
|
await coresys.mounts.create_mount(
|
|
Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "media_test",
|
|
"usage": "media",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
)
|
|
|
|
# Make a partial backup
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
backup: Backup = await coresys.backups.do_backup_partial("test", folders=["media"])
|
|
|
|
# A failed unmount aborts the restore before any file is written, but
|
|
# the finally must still repair the automount trigger so the path does
|
|
# not stay a plain writable directory — and the error must surface
|
|
with (
|
|
patch.object(Mount, "unmount", side_effect=MountError("boom")),
|
|
patch.object(Mount, "repair_trigger") as repair_trigger,
|
|
pytest.raises(MountError),
|
|
):
|
|
async with backup.open(None):
|
|
# pylint: disable-next=protected-access
|
|
await backup._folder_restore("media")
|
|
|
|
repair_trigger.assert_awaited_once()
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"supervisor_internet",
|
|
"tmp_supervisor_data",
|
|
"path_extern",
|
|
"mount_propagation",
|
|
"mock_is_mount",
|
|
)
|
|
async def test_backup_share_with_mounts(
|
|
coresys: CoreSys, all_dbus_services: dict[str, DBusServiceMock]
|
|
):
|
|
"""Test backing up share folder with mounts."""
|
|
systemd_service: SystemdService = all_dbus_services["systemd"]
|
|
systemd_unit_service: SystemdUnitService = all_dbus_services["systemd_unit"]
|
|
systemd_unit_service.active_state = [
|
|
"active",
|
|
"active",
|
|
"active",
|
|
"inactive",
|
|
"active",
|
|
"inactive",
|
|
]
|
|
systemd_service.response_get_unit = [
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
]
|
|
|
|
# Make some normal test files
|
|
(test_file_1 := coresys.config.path_share / "test.txt").touch()
|
|
(test_dir := coresys.config.path_share / "test").mkdir()
|
|
(test_file_2 := coresys.config.path_share / "test" / "inner.txt").touch()
|
|
|
|
# Add a media mount
|
|
await coresys.mounts.load()
|
|
await coresys.mounts.create_mount(
|
|
Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "share_test",
|
|
"usage": "share",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
)
|
|
assert (mount_dir := coresys.config.path_share / "share_test").is_dir()
|
|
|
|
# Make a partial backup
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
backup: Backup = await coresys.backups.do_backup_partial("test", folders=["share"])
|
|
|
|
# Remove the mount and wipe the media folder
|
|
await coresys.mounts.remove_mount("share_test")
|
|
rmtree(coresys.config.path_share)
|
|
coresys.config.path_share.mkdir()
|
|
|
|
# Restore the backup and check that only the test files we made returned
|
|
with patch.object(DockerHomeAssistant, "is_running", return_value=True):
|
|
await coresys.backups.do_restore_partial(backup, folders=["share"])
|
|
|
|
assert test_file_1.exists()
|
|
assert test_dir.is_dir()
|
|
assert test_file_2.exists()
|
|
assert not mount_dir.exists()
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"supervisor_internet",
|
|
"tmp_supervisor_data",
|
|
"path_extern",
|
|
"mount_propagation",
|
|
"mock_is_mount",
|
|
)
|
|
async def test_full_backup_to_mount(coresys: CoreSys):
|
|
"""Test full backup to and restoring from a mount."""
|
|
(marker := coresys.config.path_homeassistant / "test.txt").touch()
|
|
|
|
# Add a backup mount
|
|
(mount_dir := coresys.config.path_mounts / "backup_test").mkdir()
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
assert "backup_test" in coresys.backups.backup_locations
|
|
assert coresys.backups.backup_locations["backup_test"] == mount_dir
|
|
|
|
# Make a backup and add it to mounts. Confirm it exists in the right place
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
backup: Backup = await coresys.backups.do_backup_full("test", location=mount)
|
|
assert (mount_dir / f"{backup.slug}.tar").exists()
|
|
|
|
# Reload and check that backups in mounts are listed
|
|
await coresys.backups.reload()
|
|
assert coresys.backups.get(backup.slug)
|
|
|
|
# Remove marker file and restore. Confirm it comes back
|
|
marker.unlink()
|
|
|
|
with (
|
|
patch.object(DockerHomeAssistant, "is_running", return_value=True),
|
|
patch.object(
|
|
Backup,
|
|
"restore_supervisor_config",
|
|
new_callable=AsyncMock,
|
|
return_value=(True, []),
|
|
),
|
|
):
|
|
await coresys.backups.do_restore_full(backup)
|
|
|
|
assert marker.exists()
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"supervisor_internet",
|
|
"tmp_supervisor_data",
|
|
"path_extern",
|
|
"mount_propagation",
|
|
"mock_is_mount",
|
|
)
|
|
async def test_partial_backup_to_mount(coresys: CoreSys):
|
|
"""Test partial backup to and restoring from a mount."""
|
|
(marker := coresys.config.path_homeassistant / "test.txt").touch()
|
|
|
|
# Add a backup mount
|
|
(mount_dir := coresys.config.path_mounts / "backup_test").mkdir()
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
assert "backup_test" in coresys.backups.backup_locations
|
|
assert coresys.backups.backup_locations["backup_test"] == mount_dir
|
|
|
|
# Make a backup and add it to mounts. Confirm it exists in the right place
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
with patch.object(
|
|
HomeAssistant,
|
|
"version",
|
|
new=PropertyMock(return_value=AwesomeVersion("2023.1.1")),
|
|
):
|
|
backup: Backup = await coresys.backups.do_backup_partial(
|
|
"test", homeassistant=True, location=mount
|
|
)
|
|
|
|
assert (mount_dir / f"{backup.slug}.tar").exists()
|
|
|
|
# Reload and check that backups in mounts are listed
|
|
await coresys.backups.reload()
|
|
assert coresys.backups.get(backup.slug)
|
|
|
|
# Remove marker file and restore. Confirm it comes back
|
|
marker.unlink()
|
|
|
|
with (
|
|
patch.object(DockerHomeAssistant, "is_running", return_value=True),
|
|
patch.object(
|
|
Backup,
|
|
"restore_supervisor_config",
|
|
new_callable=AsyncMock,
|
|
return_value=(True, []),
|
|
),
|
|
):
|
|
await coresys.backups.do_restore_partial(backup, homeassistant=True)
|
|
|
|
assert marker.exists()
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern", "mount_propagation")
|
|
async def test_backup_to_down_mount_error(coresys: CoreSys, mock_is_mount: MagicMock):
|
|
"""Test backup to mount when down raises error."""
|
|
# Add a backup mount
|
|
(mount_dir := coresys.config.path_mounts / "backup_test").mkdir()
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
assert "backup_test" in coresys.backups.backup_locations
|
|
assert coresys.backups.backup_locations["backup_test"] == mount_dir
|
|
|
|
# Attempt to make a backup which fails because the probe (statvfs)
|
|
# surfaces the server as unreachable.
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
with patch(
|
|
"supervisor.mounts.mount._probe_network_mount",
|
|
side_effect=OSError(errno.EHOSTDOWN, "Host is down"),
|
|
):
|
|
with pytest.raises(BackupMountDownError):
|
|
await coresys.backups.do_backup_full("test", location=mount)
|
|
with pytest.raises(BackupMountDownError):
|
|
await coresys.backups.do_backup_partial(
|
|
"test", location=mount, homeassistant=True
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"tmp_supervisor_data", "path_extern", "mount_propagation", "mock_is_mount"
|
|
)
|
|
async def test_backup_to_local_with_default(coresys: CoreSys):
|
|
"""Test making backup to local when a default mount is specified."""
|
|
# Add a default backup mount
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
coresys.mounts.default_backup_mount = mount
|
|
|
|
# Make a backup for local. Confirm it exists in the right place
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
with patch.object(
|
|
HomeAssistant,
|
|
"version",
|
|
new=PropertyMock(return_value=AwesomeVersion("2023.1.1")),
|
|
):
|
|
backup: Backup = await coresys.backups.do_backup_partial(
|
|
"test", homeassistant=True, location=None
|
|
)
|
|
|
|
assert (coresys.config.path_backup / f"{backup.slug}.tar").exists()
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"tmp_supervisor_data", "path_extern", "mount_propagation", "mock_is_mount"
|
|
)
|
|
async def test_backup_to_default(coresys: CoreSys):
|
|
"""Test making backup to default mount."""
|
|
# Add a default backup mount
|
|
(mount_dir := coresys.config.path_mounts / "backup_test").mkdir()
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
coresys.mounts.default_backup_mount = mount
|
|
|
|
# Make a backup for default. Confirm it exists in the right place
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
with patch.object(
|
|
HomeAssistant,
|
|
"version",
|
|
new=PropertyMock(return_value=AwesomeVersion("2023.1.1")),
|
|
):
|
|
backup: Backup = await coresys.backups.do_backup_partial(
|
|
"test", homeassistant=True
|
|
)
|
|
|
|
assert (mount_dir / f"{backup.slug}.tar").exists()
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern", "mount_propagation")
|
|
async def test_backup_to_default_mount_down_error(
|
|
coresys: CoreSys, mock_is_mount: MagicMock
|
|
):
|
|
"""Test making backup to default mount when it is down."""
|
|
# Add a default backup mount
|
|
(coresys.config.path_mounts / "backup_test").mkdir()
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
coresys.mounts.default_backup_mount = mount
|
|
|
|
# Attempt to make a backup which fails because the probe (statvfs)
|
|
# surfaces the server as unreachable.
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
with (
|
|
patch(
|
|
"supervisor.mounts.mount._probe_network_mount",
|
|
side_effect=OSError(errno.EHOSTDOWN, "Host is down"),
|
|
),
|
|
pytest.raises(BackupMountDownError),
|
|
):
|
|
await coresys.backups.do_backup_partial("test", homeassistant=True)
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern", "mount_propagation")
|
|
async def test_load_network_error(
|
|
coresys: CoreSys, caplog: pytest.LogCaptureFixture, mock_is_mount: MagicMock
|
|
):
|
|
"""Test load of backup manager when there is a network error."""
|
|
(coresys.config.path_mounts / "backup_test").mkdir()
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
caplog.clear()
|
|
|
|
# This should not raise, manager should just ignore backup locations with errors
|
|
mock_is_mount.return_value = False
|
|
mock_path = MagicMock()
|
|
mock_path.is_dir.side_effect = OSError("Host is down")
|
|
mock_path.as_posix.return_value = "/data/backup_test"
|
|
with patch.object(Mount, "local_where", new=PropertyMock(return_value=mock_path)):
|
|
await coresys.backups.load()
|
|
|
|
assert "Could not list backups from /data/backup_test" in caplog.text
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
async def test_backup_with_healthcheck(
|
|
coresys: CoreSys, install_app_ssh: App, container: DockerContainer
|
|
):
|
|
"""Test backup of app with healthcheck in cold mode."""
|
|
container.show.return_value["State"]["Status"] = "running"
|
|
container.show.return_value["State"]["Running"] = True
|
|
container.show.return_value["Config"] = {"Healthcheck": "exists"}
|
|
install_app_ssh.path_data.mkdir()
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
await install_app_ssh.load()
|
|
await asyncio.sleep(0)
|
|
assert install_app_ssh.state == AppState.STARTUP
|
|
|
|
state_changes: list[AppState] = []
|
|
_container_events_task: asyncio.Task | None = None
|
|
|
|
async def container_events():
|
|
nonlocal state_changes
|
|
await asyncio.sleep(0)
|
|
|
|
await install_app_ssh.container_state_changed(
|
|
DockerContainerStateEvent(
|
|
name=f"app_{TEST_ADDON_SLUG}",
|
|
state=ContainerState.STOPPED,
|
|
id="abc123",
|
|
time=1,
|
|
)
|
|
)
|
|
|
|
state_changes.append(install_app_ssh.state)
|
|
await install_app_ssh.container_state_changed(
|
|
DockerContainerStateEvent(
|
|
name=f"app_{TEST_ADDON_SLUG}",
|
|
state=ContainerState.RUNNING,
|
|
id="abc123",
|
|
time=1,
|
|
)
|
|
)
|
|
|
|
state_changes.append(install_app_ssh.state)
|
|
await install_app_ssh.container_state_changed(
|
|
DockerContainerStateEvent(
|
|
name=f"app_{TEST_ADDON_SLUG}",
|
|
state=ContainerState.HEALTHY,
|
|
id="abc123",
|
|
time=1,
|
|
)
|
|
)
|
|
|
|
async def container_events_task(*args, **kwargs):
|
|
nonlocal _container_events_task
|
|
_container_events_task = asyncio.create_task(container_events())
|
|
|
|
with (
|
|
patch.object(DockerApp, "run", new=container_events_task),
|
|
patch.object(
|
|
AppModel,
|
|
"backup_mode",
|
|
new=PropertyMock(return_value=AppBackupMode.COLD),
|
|
),
|
|
patch.object(DockerApp, "is_running", side_effect=[True, False, False]),
|
|
):
|
|
backup = await coresys.backups.do_backup_partial(
|
|
homeassistant=False, apps=["local_ssh"]
|
|
)
|
|
|
|
assert backup
|
|
assert state_changes == [AppState.STOPPED, AppState.STARTUP]
|
|
assert install_app_ssh.state == AppState.STARTED
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
await _container_events_task
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet", "tmp_supervisor_data", "path_extern")
|
|
async def test_restore_with_healthcheck(
|
|
coresys: CoreSys, install_app_ssh: App, container: DockerContainer
|
|
):
|
|
"""Test backup of app with healthcheck in cold mode."""
|
|
container.show.return_value["State"]["Status"] = "running"
|
|
container.show.return_value["State"]["Running"] = True
|
|
container.show.return_value["Config"] = {"Healthcheck": "exists"}
|
|
install_app_ssh.path_data.mkdir()
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
await install_app_ssh.load()
|
|
await asyncio.sleep(0)
|
|
assert install_app_ssh.state == AppState.STARTUP
|
|
|
|
backup = await coresys.backups.do_backup_partial(
|
|
homeassistant=False, apps=["local_ssh"]
|
|
)
|
|
state_changes: list[AppState] = []
|
|
_container_events_task: asyncio.Task | None = None
|
|
|
|
async def container_events():
|
|
nonlocal state_changes
|
|
|
|
await install_app_ssh.container_state_changed(
|
|
DockerContainerStateEvent(
|
|
name=f"app_{TEST_ADDON_SLUG}",
|
|
state=ContainerState.STOPPED,
|
|
id="abc123",
|
|
time=1,
|
|
)
|
|
)
|
|
|
|
state_changes.append(install_app_ssh.state)
|
|
await install_app_ssh.container_state_changed(
|
|
DockerContainerStateEvent(
|
|
name=f"app_{TEST_ADDON_SLUG}",
|
|
state=ContainerState.RUNNING,
|
|
id="abc123",
|
|
time=1,
|
|
)
|
|
)
|
|
|
|
state_changes.append(install_app_ssh.state)
|
|
await install_app_ssh.container_state_changed(
|
|
DockerContainerStateEvent(
|
|
name=f"app_{TEST_ADDON_SLUG}",
|
|
state=ContainerState.HEALTHY,
|
|
id="abc123",
|
|
time=1,
|
|
)
|
|
)
|
|
|
|
async def container_events_task(*args, **kwargs):
|
|
nonlocal _container_events_task
|
|
_container_events_task = asyncio.create_task(container_events())
|
|
|
|
with (
|
|
patch.object(DockerApp, "run", new=container_events_task),
|
|
patch.object(DockerApp, "is_running", return_value=False),
|
|
patch.object(AppModel, "_validate_availability"),
|
|
patch.object(App, "with_ingress", new=PropertyMock(return_value=False)),
|
|
):
|
|
await coresys.backups.do_restore_partial(backup, apps=["local_ssh"])
|
|
|
|
assert state_changes == [AppState.STOPPED, AppState.STARTUP]
|
|
assert install_app_ssh.state == AppState.STARTED
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
await _container_events_task
|
|
|
|
|
|
def _make_backup_message_for_assert(
|
|
*,
|
|
action: str = "full_backup",
|
|
reference: str,
|
|
stage: str | None,
|
|
done: bool = False,
|
|
progress: float = 0.0,
|
|
):
|
|
"""Make a backup message to use for assert test."""
|
|
return {
|
|
"type": "supervisor/event",
|
|
"data": {
|
|
"event": "job",
|
|
"data": {
|
|
"name": f"backup_manager_{action}",
|
|
"reference": reference,
|
|
"uuid": ANY,
|
|
"progress": progress,
|
|
"stage": stage,
|
|
"done": done,
|
|
"parent_id": None,
|
|
"errors": [],
|
|
"created": ANY,
|
|
"extra": None,
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
async def test_backup_progress(
|
|
coresys: CoreSys,
|
|
install_app_ssh: App,
|
|
container: DockerContainer,
|
|
ha_ws_client: AsyncMock,
|
|
):
|
|
"""Test progress is tracked during backups."""
|
|
container.show.return_value["State"]["Status"] = "running"
|
|
container.show.return_value["State"]["Running"] = True
|
|
install_app_ssh.path_data.mkdir()
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
with (
|
|
patch.object(
|
|
AppModel,
|
|
"backup_mode",
|
|
new=PropertyMock(return_value=AppBackupMode.COLD),
|
|
),
|
|
patch("supervisor.apps.app.asyncio.Event.wait"),
|
|
):
|
|
full_backup: Backup = await coresys.backups.do_backup_full()
|
|
await asyncio.sleep(0)
|
|
|
|
messages = [
|
|
call.args[0]
|
|
for call in ha_ws_client.async_send_command.call_args_list
|
|
if call.args[0]["type"] == WSType.SUPERVISOR_EVENT
|
|
and call.args[0]["data"].get("data", {}).get("name")
|
|
== "backup_manager_full_backup"
|
|
]
|
|
assert messages == [
|
|
_make_backup_message_for_assert(reference=None, stage=None),
|
|
_make_backup_message_for_assert(reference=full_backup.slug, stage=None),
|
|
_make_backup_message_for_assert(
|
|
reference=full_backup.slug, stage="addon_repositories"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
reference=full_backup.slug, stage="home_assistant"
|
|
),
|
|
_make_backup_message_for_assert(reference=full_backup.slug, stage="addons"),
|
|
_make_backup_message_for_assert(reference=full_backup.slug, stage="folders"),
|
|
_make_backup_message_for_assert(
|
|
reference=full_backup.slug, stage="supervisor_config"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
reference=full_backup.slug, stage="finishing_file"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
reference=full_backup.slug, stage="await_addon_restarts"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
reference=full_backup.slug,
|
|
stage="await_addon_restarts",
|
|
done=True,
|
|
progress=100,
|
|
),
|
|
]
|
|
|
|
ha_ws_client.async_send_command.reset_mock()
|
|
partial_backup: Backup = await coresys.backups.do_backup_partial(
|
|
apps=["local_ssh"], folders=["media", "share", "ssl"]
|
|
)
|
|
await asyncio.sleep(0)
|
|
|
|
messages = [
|
|
call.args[0]
|
|
for call in ha_ws_client.async_send_command.call_args_list
|
|
if call.args[0]["type"] == WSType.SUPERVISOR_EVENT
|
|
and call.args[0]["data"].get("data", {}).get("name")
|
|
== "backup_manager_partial_backup"
|
|
]
|
|
assert messages == [
|
|
_make_backup_message_for_assert(
|
|
action="partial_backup", reference=None, stage=None
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_backup", reference=partial_backup.slug, stage=None
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_backup",
|
|
reference=partial_backup.slug,
|
|
stage="addon_repositories",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_backup", reference=partial_backup.slug, stage="addons"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_backup", reference=partial_backup.slug, stage="folders"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_backup",
|
|
reference=partial_backup.slug,
|
|
stage="supervisor_config",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_backup",
|
|
reference=partial_backup.slug,
|
|
stage="finishing_file",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_backup",
|
|
reference=partial_backup.slug,
|
|
stage="finishing_file",
|
|
done=True,
|
|
progress=100,
|
|
),
|
|
]
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet", "tmp_supervisor_data", "path_extern")
|
|
async def test_restore_progress(
|
|
coresys: CoreSys,
|
|
install_app_ssh: App,
|
|
container: DockerContainer,
|
|
ha_ws_client: AsyncMock,
|
|
):
|
|
"""Test progress is tracked during backups."""
|
|
container.show.return_value["State"]["Status"] = "running"
|
|
container.show.return_value["State"]["Running"] = True
|
|
install_app_ssh.path_data.mkdir()
|
|
force_app_state(install_app_ssh, AppState.STARTED)
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
full_backup: Backup = await coresys.backups.do_backup_full()
|
|
await asyncio.sleep(0)
|
|
ha_ws_client.async_send_command.reset_mock()
|
|
|
|
# Install another app to be uninstalled
|
|
# Duplicate code from install_addon_example fixture
|
|
# Apparently request.getfixturevalue does not work with async fixtures: https://github.com/pytest-dev/pytest-asyncio/issues/112
|
|
store = coresys.apps.store["local_example"]
|
|
await coresys.apps.data.install(store)
|
|
# pylint: disable-next=protected-access
|
|
coresys.apps.data._data = coresys.apps.data._schema(coresys.apps.data._data)
|
|
coresys.apps.local[store.slug] = App(coresys, store.slug)
|
|
|
|
with (
|
|
patch("supervisor.apps.app.asyncio.Event.wait"),
|
|
patch.object(HomeAssistant, "restore"),
|
|
patch.object(HomeAssistantCore, "update"),
|
|
patch.object(AppModel, "_validate_availability"),
|
|
patch.object(AppModel, "with_ingress", new=PropertyMock(return_value=False)),
|
|
):
|
|
await coresys.backups.do_restore_full(full_backup)
|
|
await asyncio.sleep(0)
|
|
|
|
messages = [
|
|
call.args[0]
|
|
for call in ha_ws_client.async_send_command.call_args_list
|
|
if call.args[0]["type"] == WSType.SUPERVISOR_EVENT
|
|
and call.args[0]["data"].get("data", {}).get("name")
|
|
== "backup_manager_full_restore"
|
|
]
|
|
assert messages == [
|
|
_make_backup_message_for_assert(
|
|
action="full_restore", reference=None, stage=None
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore", reference=full_backup.slug, stage=None
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore", reference=full_backup.slug, stage="folders"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore",
|
|
reference=full_backup.slug,
|
|
stage="home_assistant",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore",
|
|
reference=full_backup.slug,
|
|
stage="remove_delta_addons",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore",
|
|
reference=full_backup.slug,
|
|
stage="addon_repositories",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore", reference=full_backup.slug, stage="addons"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore",
|
|
reference=full_backup.slug,
|
|
stage="supervisor_config",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore",
|
|
reference=full_backup.slug,
|
|
stage="await_addon_restarts",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore",
|
|
reference=full_backup.slug,
|
|
stage="await_home_assistant_restart",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="full_restore",
|
|
reference=full_backup.slug,
|
|
stage="await_home_assistant_restart",
|
|
done=True,
|
|
progress=100,
|
|
),
|
|
]
|
|
|
|
folders_backup: Backup = await coresys.backups.do_backup_partial(
|
|
folders=["media", "share", "ssl"]
|
|
)
|
|
ha_ws_client.async_send_command.reset_mock()
|
|
await coresys.backups.do_restore_partial(
|
|
folders_backup, folders=["media", "share", "ssl"]
|
|
)
|
|
await asyncio.sleep(0)
|
|
|
|
messages = [
|
|
call.args[0]
|
|
for call in ha_ws_client.async_send_command.call_args_list
|
|
if call.args[0]["type"] == WSType.SUPERVISOR_EVENT
|
|
and call.args[0]["data"].get("data", {}).get("name")
|
|
== "backup_manager_partial_restore"
|
|
]
|
|
assert messages == [
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore", reference=None, stage=None
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore",
|
|
reference=folders_backup.slug,
|
|
stage=None,
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore",
|
|
reference=folders_backup.slug,
|
|
stage="folders",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore",
|
|
reference=folders_backup.slug,
|
|
stage="supervisor_config",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore",
|
|
reference=folders_backup.slug,
|
|
stage="supervisor_config",
|
|
done=True,
|
|
progress=100,
|
|
),
|
|
]
|
|
|
|
container.show.return_value["State"]["Status"] = "stopped"
|
|
container.show.return_value["State"]["Running"] = False
|
|
force_app_state(install_app_ssh, AppState.STOPPED)
|
|
app_backup: Backup = await coresys.backups.do_backup_partial(apps=["local_ssh"])
|
|
|
|
ha_ws_client.async_send_command.reset_mock()
|
|
with (
|
|
patch.object(AppModel, "_validate_availability"),
|
|
patch.object(HomeAssistantCore, "start"),
|
|
):
|
|
await coresys.backups.do_restore_partial(app_backup, apps=["local_ssh"])
|
|
await asyncio.sleep(0)
|
|
|
|
messages = [
|
|
call.args[0]
|
|
for call in ha_ws_client.async_send_command.call_args_list
|
|
if call.args[0]["type"] == WSType.SUPERVISOR_EVENT
|
|
and call.args[0]["data"].get("data", {}).get("name")
|
|
== "backup_manager_partial_restore"
|
|
]
|
|
assert messages == [
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore", reference=None, stage=None
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore",
|
|
reference=app_backup.slug,
|
|
stage=None,
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore",
|
|
reference=app_backup.slug,
|
|
stage="addon_repositories",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore",
|
|
reference=app_backup.slug,
|
|
stage="addons",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore",
|
|
reference=app_backup.slug,
|
|
stage="supervisor_config",
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="partial_restore",
|
|
reference=app_backup.slug,
|
|
stage="supervisor_config",
|
|
done=True,
|
|
progress=100,
|
|
),
|
|
]
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
async def test_freeze_thaw(
|
|
coresys: CoreSys,
|
|
install_app_ssh: App,
|
|
container: DockerContainer,
|
|
ha_ws_client: AsyncMock,
|
|
):
|
|
"""Test manual freeze and thaw for external snapshots."""
|
|
container.show.return_value["State"]["Status"] = "running"
|
|
container.show.return_value["State"]["Running"] = True
|
|
install_app_ssh.path_data.mkdir()
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
ha_ws_client.ha_version = AwesomeVersion("2022.1.0")
|
|
|
|
with (
|
|
patch.object(
|
|
AppModel, "backup_pre", new=PropertyMock(return_value="pre_backup")
|
|
),
|
|
patch.object(
|
|
AppModel, "backup_post", new=PropertyMock(return_value="post_backup")
|
|
),
|
|
):
|
|
# Run the freeze
|
|
await coresys.backups.freeze_all()
|
|
container.exec.assert_called_once_with("pre_backup")
|
|
assert coresys.core.state == CoreState.FREEZE
|
|
|
|
await asyncio.sleep(0)
|
|
messages = [
|
|
call.args[0]
|
|
for call in ha_ws_client.async_send_command.call_args_list
|
|
if call.args[0]["type"] in ["backup/start", "backup/end"]
|
|
or call.args[0]["data"].get("data", {}).get("name")
|
|
in ["backup_manager_freeze_all", "backup_manager_thaw_all"]
|
|
]
|
|
assert messages == [
|
|
_make_backup_message_for_assert(
|
|
action="freeze_all", reference=None, stage=None
|
|
),
|
|
{"type": "backup/start"},
|
|
_make_backup_message_for_assert(
|
|
action="freeze_all", reference=None, stage="home_assistant"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="freeze_all", reference=None, stage="apps"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="thaw_all", reference=None, stage=None
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="freeze_all",
|
|
reference=None,
|
|
stage="apps",
|
|
done=True,
|
|
progress=100,
|
|
),
|
|
]
|
|
|
|
# Release the thaw task
|
|
container.exec.reset_mock()
|
|
ha_ws_client.async_send_command.reset_mock()
|
|
await coresys.backups.thaw_all()
|
|
container.exec.assert_called_once_with("post_backup")
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
|
|
await asyncio.sleep(0)
|
|
messages = [
|
|
call.args[0]
|
|
for call in ha_ws_client.async_send_command.call_args_list
|
|
if call.args[0]["type"] in ["backup/start", "backup/end"]
|
|
or call.args[0]["data"].get("data", {}).get("name")
|
|
in ["backup_manager_freeze_all", "backup_manager_thaw_all"]
|
|
]
|
|
assert messages == [
|
|
{"type": "backup/end"},
|
|
_make_backup_message_for_assert(
|
|
action="thaw_all", reference=None, stage="home_assistant"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="thaw_all", reference=None, stage="apps"
|
|
),
|
|
_make_backup_message_for_assert(
|
|
action="thaw_all",
|
|
reference=None,
|
|
stage="apps",
|
|
done=True,
|
|
progress=100,
|
|
),
|
|
]
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
async def test_freeze_thaw_timeout(
|
|
coresys: CoreSys, ha_ws_client: AsyncMock, caplog: pytest.LogCaptureFixture
|
|
):
|
|
"""Test manual freeze ends due to timeout expiration."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
ha_ws_client.ha_version = AwesomeVersion("2022.1.0")
|
|
|
|
await coresys.backups.freeze_all(timeout=0.01)
|
|
assert coresys.core.state == CoreState.FREEZE
|
|
await asyncio.sleep(0)
|
|
assert any(
|
|
call.args[0] == {"type": "backup/start"}
|
|
for call in ha_ws_client.async_send_command.call_args_list
|
|
)
|
|
|
|
ha_ws_client.async_send_command.reset_mock()
|
|
await asyncio.sleep(0.02)
|
|
assert coresys.core.state == CoreState.RUNNING
|
|
assert any(
|
|
call.args[0] == {"type": "backup/end"}
|
|
for call in ha_ws_client.async_send_command.call_args_list
|
|
)
|
|
assert "Timeout waiting for signal to thaw after manual freeze" in caplog.text
|
|
|
|
|
|
async def test_cannot_manually_thaw_normal_freeze(coresys: CoreSys):
|
|
"""Test thaw_all cannot be used unless freeze was started by freeze_all method."""
|
|
await coresys.core.set_state(CoreState.FREEZE)
|
|
with pytest.raises(BackupError):
|
|
await coresys.backups.thaw_all()
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet", "tmp_supervisor_data", "path_extern")
|
|
async def test_restore_only_reloads_ingress_on_change(
|
|
coresys: CoreSys, install_app_ssh: App
|
|
):
|
|
"""Test restore only tells core to reload ingress when something has changed."""
|
|
install_app_ssh.path_data.mkdir()
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
backup_no_ingress: Backup = await coresys.backups.do_backup_partial(
|
|
apps=["local_ssh"]
|
|
)
|
|
|
|
install_app_ssh.ingress_panel = True
|
|
await install_app_ssh.save_persist()
|
|
backup_with_ingress: Backup = await coresys.backups.do_backup_partial(
|
|
apps=["local_ssh"]
|
|
)
|
|
|
|
async def mock_is_running(*_) -> bool:
|
|
return True
|
|
|
|
with (
|
|
patch.object(HomeAssistantCore, "is_running", new=mock_is_running),
|
|
patch.object(AppModel, "_validate_availability"),
|
|
patch.object(DockerApp, "attach"),
|
|
patch.object(HomeAssistantAPI, "make_request") as make_request,
|
|
):
|
|
make_request.return_value.__aenter__.return_value.status = 200
|
|
|
|
# Has ingress before and after - not called
|
|
await coresys.backups.do_restore_partial(
|
|
backup_with_ingress, apps=["local_ssh"]
|
|
)
|
|
make_request.assert_not_called()
|
|
|
|
# Restore removes ingress - tell Home Assistant
|
|
await coresys.backups.do_restore_partial(backup_no_ingress, apps=["local_ssh"])
|
|
make_request.assert_called_once_with(
|
|
"delete", "api/hassio_push/panel/local_ssh"
|
|
)
|
|
|
|
# No ingress before or after - not called
|
|
make_request.reset_mock()
|
|
await coresys.backups.do_restore_partial(backup_no_ingress, apps=["local_ssh"])
|
|
make_request.assert_not_called()
|
|
|
|
# Restore adds ingress - tell Home Assistant
|
|
await coresys.backups.do_restore_partial(
|
|
backup_with_ingress, apps=["local_ssh"]
|
|
)
|
|
make_request.assert_called_once_with("post", "api/hassio_push/panel/local_ssh")
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet", "tmp_supervisor_data", "path_extern")
|
|
async def test_restore_new_app(coresys: CoreSys, install_app_example: App):
|
|
"""Test restore installing new app."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
assert not install_app_example.path_data.exists()
|
|
assert not install_app_example.path_config.exists()
|
|
|
|
backup: Backup = await coresys.backups.do_backup_partial(apps=["local_example"])
|
|
await coresys.apps.uninstall("local_example")
|
|
assert "local_example" not in coresys.apps.local
|
|
|
|
with (
|
|
patch.object(AppModel, "_validate_availability"),
|
|
patch.object(DockerApp, "attach"),
|
|
):
|
|
assert await coresys.backups.do_restore_partial(backup, apps=["local_example"])
|
|
|
|
assert "local_example" in coresys.apps.local
|
|
assert install_app_example.path_data.exists()
|
|
assert install_app_example.path_config.exists()
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet", "tmp_supervisor_data", "path_extern")
|
|
async def test_restore_app_image_missing(coresys: CoreSys, install_app_example: App):
|
|
"""Test restore when local image is missing installs from registry.
|
|
|
|
Exercises the ``not instance.exists()`` branch in ``App.restore()`` which
|
|
checks the extracted bundle for an ``image.tar`` to import. This path is
|
|
skipped by other restore tests because the mocked Docker image inspect
|
|
always reports the image as present.
|
|
"""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
backup: Backup = await coresys.backups.do_backup_partial(apps=["local_example"])
|
|
await coresys.apps.uninstall("local_example")
|
|
assert "local_example" not in coresys.apps.local
|
|
|
|
with (
|
|
patch.object(AppModel, "_validate_availability"),
|
|
patch.object(DockerApp, "attach"),
|
|
patch.object(DockerApp, "exists", new=AsyncMock(return_value=False)),
|
|
patch.object(DockerApp, "install", new=AsyncMock()) as install_mock,
|
|
patch.object(DockerApp, "cleanup", new=AsyncMock()),
|
|
patch.object(DockerApp, "import_image", new=AsyncMock()) as import_mock,
|
|
):
|
|
assert await coresys.backups.do_restore_partial(backup, apps=["local_example"])
|
|
|
|
import_mock.assert_called_once()
|
|
install_mock.assert_not_called()
|
|
|
|
|
|
@pytest.mark.usefixtures("supervisor_internet", "tmp_supervisor_data", "path_extern")
|
|
async def test_restore_preserves_data_config(
|
|
coresys: CoreSys, install_app_example: App
|
|
):
|
|
"""Test restore preserves data and config."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
install_app_example.path_data.mkdir()
|
|
(test_data := install_app_example.path_data / "data.txt").touch()
|
|
install_app_example.path_config.mkdir()
|
|
(test_config := install_app_example.path_config / "config.yaml").touch()
|
|
|
|
backup: Backup = await coresys.backups.do_backup_partial(apps=["local_example"])
|
|
(test_config2 := install_app_example.path_config / "config2.yaml").touch()
|
|
|
|
await coresys.apps.uninstall("local_example")
|
|
assert not install_app_example.path_data.exists()
|
|
assert install_app_example.path_config.exists()
|
|
assert test_config2.exists()
|
|
|
|
with (
|
|
patch.object(AppModel, "_validate_availability"),
|
|
patch.object(DockerApp, "attach"),
|
|
):
|
|
assert await coresys.backups.do_restore_partial(backup, apps=["local_example"])
|
|
|
|
assert test_data.exists()
|
|
assert test_config.exists()
|
|
assert not test_config2.exists()
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"tmp_supervisor_data", "path_extern", "mount_propagation", "mock_is_mount"
|
|
)
|
|
async def test_backup_to_mount_bypasses_free_space_condition(
|
|
coresys: CoreSys, all_dbus_services: dict[str, DBusServiceMock]
|
|
):
|
|
"""Test backing up to a mount bypasses the check on local free space."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda _: 0.1
|
|
|
|
# These fail due to lack of local free space
|
|
with pytest.raises(BackupJobError):
|
|
await coresys.backups.do_backup_full()
|
|
with pytest.raises(BackupJobError):
|
|
await coresys.backups.do_backup_partial(folders=["media"])
|
|
|
|
systemd_service: SystemdService = all_dbus_services["systemd"]
|
|
systemd_service.response_get_unit = [
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
DBusError("org.freedesktop.systemd1.NoSuchUnit", "error"),
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
"/org/freedesktop/systemd1/unit/tmp_2dyellow_2emount",
|
|
]
|
|
|
|
# Add a backup mount
|
|
await coresys.mounts.load()
|
|
await coresys.mounts.create_mount(
|
|
Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
)
|
|
mount = coresys.mounts.get("backup_test")
|
|
|
|
# These succeed because local free space does not matter when using a mount
|
|
await coresys.backups.do_backup_full(location=mount)
|
|
await coresys.backups.do_backup_partial(folders=["media"], location=mount)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("partial_backup", "exclude_db_setting"),
|
|
[(False, True), (True, True), (False, False), (True, False)],
|
|
)
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
async def test_skip_homeassistant_database(
|
|
coresys: CoreSys, partial_backup: bool, exclude_db_setting: bool | None
|
|
):
|
|
"""Test exclude database option skips database in backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
coresys.jobs.ignore_conditions = [
|
|
JobCondition.INTERNET_HOST,
|
|
JobCondition.INTERNET_SYSTEM,
|
|
]
|
|
coresys.homeassistant.version = AwesomeVersion("2023.09.0")
|
|
coresys.homeassistant.backups_exclude_database = exclude_db_setting
|
|
|
|
test_file = coresys.config.path_homeassistant / "configuration.yaml"
|
|
test_db = coresys.config.path_homeassistant / "home-assistant_v2.db"
|
|
test_db_wal = coresys.config.path_homeassistant / "home-assistant_v2.db-wal"
|
|
test_db_shm = coresys.config.path_homeassistant / "home-assistant_v2.db-shm"
|
|
|
|
def setup_1():
|
|
test_db.touch()
|
|
test_db_wal.touch()
|
|
test_db_shm.touch()
|
|
write_json_file(test_file, {"default_config": {}})
|
|
|
|
await coresys.run_in_executor(setup_1)
|
|
|
|
kwargs = {} if exclude_db_setting else {"homeassistant_exclude_database": True}
|
|
if partial_backup:
|
|
backup: Backup = await coresys.backups.do_backup_partial(
|
|
homeassistant=True, **kwargs
|
|
)
|
|
else:
|
|
backup: Backup = await coresys.backups.do_backup_full(**kwargs)
|
|
|
|
def setup_2():
|
|
test_file.unlink()
|
|
write_json_file(test_db, {"hello": "world"})
|
|
write_json_file(test_db_wal, {"hello": "world"})
|
|
|
|
await coresys.run_in_executor(setup_2)
|
|
|
|
with (
|
|
patch.object(HomeAssistantCore, "update"),
|
|
patch.object(HomeAssistantCore, "start"),
|
|
):
|
|
await coresys.backups.do_restore_partial(backup, homeassistant=True)
|
|
|
|
def test_assertions():
|
|
assert read_json_file(test_file) == {"default_config": {}}
|
|
assert read_json_file(test_db) == {"hello": "world"}
|
|
assert read_json_file(test_db_wal) == {"hello": "world"}
|
|
assert not test_db_shm.exists()
|
|
|
|
await coresys.run_in_executor(test_assertions)
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
@pytest.mark.parametrize(
|
|
("backup_locations", "location_name", "healthy_expected"),
|
|
[
|
|
(["test"], "test", True),
|
|
([None], None, False),
|
|
],
|
|
indirect=["backup_locations"],
|
|
)
|
|
async def test_backup_remove_error(
|
|
coresys: CoreSys,
|
|
backup_locations: list[LOCATION_TYPE],
|
|
location_name: str | None,
|
|
healthy_expected: bool,
|
|
):
|
|
"""Test removing a backup error."""
|
|
location: LOCATION_TYPE = backup_locations[0]
|
|
backup_base_path = coresys.backups._get_base_path(location) # pylint: disable=protected-access
|
|
backup_base_path.mkdir(exist_ok=True)
|
|
copy(get_fixture_path("backup_example.tar"), backup_base_path)
|
|
|
|
await coresys.backups.reload()
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
|
|
assert location_name in backup.all_locations
|
|
backup.all_locations[location_name].path = (tar_file_mock := MagicMock())
|
|
tar_file_mock.unlink.side_effect = (err := OSError())
|
|
|
|
err.errno = errno.EBUSY
|
|
with pytest.raises(BackupError, match="Cannot delete backup at"):
|
|
await coresys.backups.remove(backup)
|
|
assert coresys.core.healthy is True
|
|
|
|
err.errno = errno.EBADMSG
|
|
with pytest.raises(BackupError, match="Cannot delete backup at"):
|
|
await coresys.backups.remove(backup)
|
|
assert coresys.core.healthy is healthy_expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("error_path", "healthy_expected"),
|
|
[(Path("/data/backup"), False), (Path("/data/mounts/backup_test"), True)],
|
|
)
|
|
@pytest.mark.usefixtures("path_extern", "mount_propagation")
|
|
async def test_reload_error(
|
|
coresys: CoreSys,
|
|
caplog: pytest.LogCaptureFixture,
|
|
error_path: Path,
|
|
healthy_expected: bool,
|
|
mock_is_mount: MagicMock,
|
|
):
|
|
"""Test error during reload."""
|
|
err = OSError()
|
|
|
|
def mock_is_dir(path: Path) -> bool:
|
|
"""Mock of is_dir."""
|
|
if path == error_path:
|
|
raise err
|
|
return True
|
|
|
|
# Add a backup mount
|
|
await coresys.mounts.load()
|
|
await coresys.mounts.create_mount(
|
|
Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
)
|
|
|
|
mock_is_mount.return_value = False
|
|
with (
|
|
patch("supervisor.backups.manager.Path.is_dir", new=mock_is_dir),
|
|
patch("supervisor.backups.manager.Path.glob", return_value=[]),
|
|
):
|
|
err.errno = errno.EBUSY
|
|
await coresys.backups.reload()
|
|
|
|
assert "Could not list backups" in caplog.text
|
|
assert coresys.core.healthy is True
|
|
|
|
caplog.clear()
|
|
err.errno = errno.EBADMSG
|
|
await coresys.backups.reload()
|
|
|
|
assert "Could not list backups" in caplog.text
|
|
assert coresys.core.healthy is healthy_expected
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"pre_backup_error",
|
|
[
|
|
{
|
|
"code": "pre_backup_actions_failed",
|
|
"message": "Database migration in progress",
|
|
},
|
|
{"code": "unknown_command", "message": "Unknown command."},
|
|
],
|
|
)
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
async def test_core_pre_backup_actions_failed(
|
|
coresys: CoreSys,
|
|
ha_ws_client: AsyncMock,
|
|
caplog: pytest.LogCaptureFixture,
|
|
pre_backup_error: dict[str, str],
|
|
):
|
|
"""Test pre-backup actions failed in HA core stops backup."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
ha_ws_client.ha_version = AwesomeVersion("2024.7.0")
|
|
ha_ws_client.async_send_command.return_value = {
|
|
"error": pre_backup_error,
|
|
"id": 1,
|
|
"success": False,
|
|
"type": "result",
|
|
}
|
|
|
|
assert not await coresys.backups.do_backup_full()
|
|
assert (
|
|
f"Preparing backup of Home Assistant Core failed due to: {pre_backup_error['message']}"
|
|
in caplog.text
|
|
)
|
|
|
|
|
|
@pytest.mark.usefixtures("mount_propagation", "mock_is_mount", "path_extern")
|
|
async def test_reload_multiple_locations(coresys: CoreSys, tmp_supervisor_data: Path):
|
|
"""Test reload with a backup that exists in multiple locations."""
|
|
(mount_dir := coresys.config.path_mounts / "backup_test").mkdir()
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
|
|
assert not coresys.backups.list_backups
|
|
|
|
backup_file = get_fixture_path("backup_example.tar")
|
|
copy(backup_file, tmp_supervisor_data / "core/backup")
|
|
await coresys.backups.reload()
|
|
|
|
assert coresys.backups.list_backups
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
assert backup.location == ".cloud_backup"
|
|
assert backup.locations == [".cloud_backup"]
|
|
assert backup.all_locations.keys() == {".cloud_backup"}
|
|
|
|
copy(backup_file, tmp_supervisor_data / "backup")
|
|
await coresys.backups.reload()
|
|
|
|
assert coresys.backups.list_backups
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
assert backup.location is None
|
|
assert backup.locations == [None]
|
|
assert backup.all_locations.keys() == {".cloud_backup", None}
|
|
|
|
copy(backup_file, mount_dir)
|
|
await coresys.backups.reload()
|
|
|
|
assert coresys.backups.list_backups
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
assert backup.location in {None, "backup_test"}
|
|
assert None in backup.locations
|
|
assert "backup_test" in backup.locations
|
|
assert backup.all_locations.keys() == {".cloud_backup", None, "backup_test"}
|
|
|
|
|
|
@pytest.mark.usefixtures("mount_propagation", "mock_is_mount", "path_extern")
|
|
async def test_partial_reload_multiple_locations(
|
|
coresys: CoreSys, tmp_supervisor_data: Path
|
|
):
|
|
"""Test a partial reload with a backup that exists in multiple locations."""
|
|
(mount_dir := coresys.config.path_mounts / "backup_test").mkdir()
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "backup_test",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "test.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
|
|
assert not coresys.backups.list_backups
|
|
|
|
backup_file = get_fixture_path("backup_example.tar")
|
|
copy(backup_file, tmp_supervisor_data / "core/backup")
|
|
await coresys.backups.reload()
|
|
|
|
assert coresys.backups.list_backups
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
assert backup.location == ".cloud_backup"
|
|
assert backup.locations == [".cloud_backup"]
|
|
assert backup.all_locations.keys() == {".cloud_backup"}
|
|
|
|
copy(backup_file, tmp_supervisor_data / "backup")
|
|
await coresys.backups.reload()
|
|
|
|
assert coresys.backups.list_backups
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
assert backup.location is None
|
|
assert backup.locations == [None]
|
|
assert backup.all_locations.keys() == {".cloud_backup", None}
|
|
|
|
copy(backup_file, mount_dir)
|
|
await coresys.backups.reload()
|
|
|
|
assert coresys.backups.list_backups
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
assert backup.location is None
|
|
assert backup.locations == [None, "backup_test"]
|
|
assert backup.all_locations.keys() == {".cloud_backup", None, "backup_test"}
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data")
|
|
async def test_backup_remove_multiple_locations(coresys: CoreSys):
|
|
"""Test removing a backup that exists in multiple locations."""
|
|
backup_file = get_fixture_path("backup_example.tar")
|
|
location_1 = Path(copy(backup_file, coresys.config.path_backup))
|
|
location_2 = Path(copy(backup_file, coresys.config.path_core_backup))
|
|
|
|
await coresys.backups.reload()
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
assert backup.all_locations == {
|
|
None: BackupLocation(path=location_1, protected=False, size_bytes=10240),
|
|
".cloud_backup": BackupLocation(
|
|
path=location_2, protected=False, size_bytes=10240
|
|
),
|
|
}
|
|
|
|
await coresys.backups.remove(backup)
|
|
assert not location_1.exists()
|
|
assert not location_2.exists()
|
|
assert not coresys.backups.get("7fed74c8")
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data")
|
|
async def test_backup_remove_one_location_of_multiple(coresys: CoreSys):
|
|
"""Test removing a backup that exists in multiple locations from one location."""
|
|
backup_file = get_fixture_path("backup_example.tar")
|
|
location_1 = Path(copy(backup_file, coresys.config.path_backup))
|
|
location_2 = Path(copy(backup_file, coresys.config.path_core_backup))
|
|
|
|
await coresys.backups.reload()
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
assert backup.all_locations == {
|
|
None: BackupLocation(path=location_1, protected=False, size_bytes=10240),
|
|
".cloud_backup": BackupLocation(
|
|
path=location_2, protected=False, size_bytes=10240
|
|
),
|
|
}
|
|
|
|
await coresys.backups.remove(backup, locations=[".cloud_backup"])
|
|
assert location_1.exists()
|
|
assert not location_2.exists()
|
|
assert coresys.backups.get("7fed74c8")
|
|
assert backup.all_locations == {
|
|
None: BackupLocation(path=location_1, protected=False, size_bytes=10240),
|
|
}
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "supervisor_internet")
|
|
async def test_app_backup_excludes(coresys: CoreSys, install_app_example: App):
|
|
"""Test backup excludes option for apps."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
install_app_example.path_data.mkdir(parents=True)
|
|
(test1 := install_app_example.path_data / "test1").touch()
|
|
(test_dir := install_app_example.path_data / "test_dir").mkdir()
|
|
(test2 := test_dir / "test2").touch()
|
|
(test3 := test_dir / "test3").touch()
|
|
|
|
install_app_example.data["backup_exclude"] = ["test1", "*/test2"]
|
|
backup = await coresys.backups.do_backup_partial(apps=["local_example"])
|
|
test1.unlink()
|
|
test2.unlink()
|
|
test3.unlink()
|
|
test_dir.rmdir()
|
|
|
|
await coresys.backups.do_restore_partial(backup, apps=["local_example"])
|
|
assert not test1.exists()
|
|
assert not test2.exists()
|
|
assert test_dir.is_dir()
|
|
assert test3.exists()
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
async def test_remove_non_existing_backup_raises(coresys: CoreSys):
|
|
"""Test removing a backup error."""
|
|
location: LOCATION_TYPE = None
|
|
backup_base_path = coresys.backups._get_base_path(location) # pylint: disable=protected-access
|
|
backup_base_path.mkdir(exist_ok=True)
|
|
copy(get_fixture_path("backup_example.tar"), backup_base_path)
|
|
|
|
await coresys.backups.reload()
|
|
assert (backup := coresys.backups.get("7fed74c8"))
|
|
|
|
assert None in backup.all_locations
|
|
backup.all_locations[None].path = (tar_file_mock := MagicMock())
|
|
tar_file_mock.unlink.side_effect = (err := FileNotFoundError())
|
|
err.errno = errno.ENOENT
|
|
|
|
with pytest.raises(BackupFileNotFoundError):
|
|
await coresys.backups.remove(backup)
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
@pytest.mark.parametrize(
|
|
("error_num", "unhealthy", "default_location", "additional_location"),
|
|
[
|
|
(errno.EBUSY, False, None, ".cloud_backup"),
|
|
(errno.EBUSY, False, ".cloud_backup", None),
|
|
(errno.EBADMSG, True, None, ".cloud_backup"),
|
|
(errno.EBADMSG, True, ".cloud_backup", None),
|
|
],
|
|
)
|
|
async def test_backup_multiple_locations_oserror(
|
|
coresys: CoreSys,
|
|
error_num: int,
|
|
unhealthy: bool,
|
|
default_location: str | None,
|
|
additional_location: str | None,
|
|
):
|
|
"""Test backup to multiple locations raises oserror."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
|
|
with patch("supervisor.backups.manager.copy", side_effect=(err := OSError())):
|
|
err.errno = error_num
|
|
backup: Backup | None = await coresys.backups.do_backup_full(
|
|
name="test",
|
|
location=default_location,
|
|
additional_locations=[additional_location],
|
|
)
|
|
|
|
assert backup
|
|
assert coresys.backups.get(backup.slug) == backup
|
|
assert backup.location == default_location
|
|
assert additional_location not in backup.all_locations
|
|
assert (
|
|
UnhealthyReason.OSERROR_BAD_MESSAGE in coresys.resolution.unhealthy
|
|
) is unhealthy
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
@pytest.mark.parametrize(
|
|
("error_num", "unhealthy"),
|
|
[(errno.EBUSY, False), (errno.EBADMSG, True)],
|
|
)
|
|
async def test_import_backup_invalid_unlink_oserror(
|
|
coresys: CoreSys, error_num: int, unhealthy: bool
|
|
):
|
|
"""Test import backup where load fails and cleanup unlink raises OSError."""
|
|
tar_file = Path(
|
|
copy(get_fixture_path("backup_example.tar"), coresys.config.path_tmp)
|
|
)
|
|
|
|
err = OSError()
|
|
err.errno = error_num
|
|
|
|
with (
|
|
patch.object(Backup, "load", side_effect=[True, False]),
|
|
patch("pathlib.Path.unlink", side_effect=err),
|
|
):
|
|
backup = await coresys.backups.import_backup(tar_file)
|
|
|
|
assert backup is None
|
|
assert (
|
|
UnhealthyReason.OSERROR_BAD_MESSAGE in coresys.resolution.unhealthy
|
|
) is unhealthy
|
|
|
|
|
|
@pytest.mark.usefixtures("tmp_supervisor_data", "path_extern")
|
|
@pytest.mark.parametrize(
|
|
("error_num", "unhealthy"),
|
|
[(errno.EBUSY, False), (errno.EBADMSG, True)],
|
|
)
|
|
async def test_import_backup_consolidate_unlink_oserror(
|
|
coresys: CoreSys, error_num: int, unhealthy: bool
|
|
):
|
|
"""Test import backup where consolidate fails and cleanup unlink raises OSError."""
|
|
copy(get_fixture_path("backup_example.tar"), coresys.config.path_backup)
|
|
await coresys.backups.reload()
|
|
assert coresys.backups.get("7fed74c8")
|
|
|
|
tar_file = Path(
|
|
copy(get_fixture_path("backup_example.tar"), coresys.config.path_tmp)
|
|
)
|
|
|
|
err = OSError()
|
|
err.errno = error_num
|
|
|
|
with (
|
|
patch.object(
|
|
Backup, "consolidate", side_effect=BackupInvalidError("Test error")
|
|
),
|
|
patch("pathlib.Path.unlink", side_effect=err),
|
|
pytest.raises(BackupInvalidError),
|
|
):
|
|
await coresys.backups.import_backup(tar_file, location=".cloud_backup")
|
|
|
|
assert (
|
|
UnhealthyReason.OSERROR_BAD_MESSAGE in coresys.resolution.unhealthy
|
|
) is unhealthy
|
|
|
|
|
|
@pytest.mark.parametrize("same_mount", [True, False])
|
|
async def test_get_upload_path_for_backup_location(coresys: CoreSys, same_mount: bool):
|
|
"""Test get_upload_path_for_location with local backup location."""
|
|
manager = BackupManager(coresys)
|
|
|
|
target_path = coresys.config.path_backup
|
|
tmp_path = coresys.config.path_tmp
|
|
|
|
def make_stat_mock(target_path: Path, tmp_path: Path, same_mount: bool):
|
|
def _mock_stat(self):
|
|
if self == target_path:
|
|
return SimpleNamespace(st_dev=1)
|
|
if self == tmp_path:
|
|
return SimpleNamespace(st_dev=1 if same_mount else 2)
|
|
raise ValueError(f"Unexpected path: {self}")
|
|
|
|
return _mock_stat
|
|
|
|
with patch(
|
|
"pathlib.Path.stat", new=make_stat_mock(target_path, tmp_path, same_mount)
|
|
):
|
|
result = await manager.get_upload_path_for_location(None)
|
|
|
|
if same_mount:
|
|
assert result == tmp_path
|
|
else:
|
|
assert result == target_path
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"tmp_supervisor_data", "path_extern", "mount_propagation", "mock_is_mount"
|
|
)
|
|
async def test_get_upload_path_for_mount_location(coresys: CoreSys):
|
|
"""Test get_upload_path_for_location with a Mount location."""
|
|
manager = BackupManager(coresys)
|
|
|
|
await coresys.mounts.load()
|
|
mount = Mount.from_dict(
|
|
coresys,
|
|
{
|
|
"name": "test_mount",
|
|
"usage": "backup",
|
|
"type": "cifs",
|
|
"server": "server.local",
|
|
"share": "test",
|
|
},
|
|
)
|
|
await coresys.mounts.create_mount(mount)
|
|
|
|
result = await manager.get_upload_path_for_location(mount)
|
|
|
|
assert result == mount.local_where
|
|
|
|
|
|
@pytest.mark.usefixtures(
|
|
"supervisor_internet", "tmp_supervisor_data", "path_extern", "install_app_example"
|
|
)
|
|
async def test_backup_app_skips_uninstalled(
|
|
coresys: CoreSys, caplog: pytest.LogCaptureFixture
|
|
):
|
|
"""Test restore installing new app."""
|
|
await coresys.core.set_state(CoreState.RUNNING)
|
|
coresys.hardware.disk.get_disk_free_space = lambda x: 5000
|
|
assert "local_example" in coresys.apps.local
|
|
orig_store_apps = Backup.store_apps
|
|
|
|
async def mock_store_apps(*args, **kwargs):
|
|
# Mock an uninstall during the backup process
|
|
await coresys.apps.uninstall("local_example")
|
|
await orig_store_apps(*args, **kwargs)
|
|
|
|
with patch.object(Backup, "store_apps", new=mock_store_apps):
|
|
backup: Backup = await coresys.backups.do_backup_partial(
|
|
apps=["local_example"], folders=["ssl"]
|
|
)
|
|
|
|
assert "local_example" not in coresys.apps.local
|
|
assert not backup.apps
|
|
assert (
|
|
"Skipping backup of app local_example because it has been uninstalled"
|
|
in caplog.text
|
|
)
|