mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Don't allow creating backups if Home Assistant is not running (#139499)
* Don't allow creating backups if hass is not running
* Revert "Don't allow creating backups if hass is not running"
This reverts commit 1bf545eb25.
* Set backup manager to idle only after Home Assistant has started
* Update according to discussion, add tests
* Add more test
This commit is contained in:
@@ -47,7 +47,8 @@ from homeassistant.components.backup.manager import (
|
||||
WrittenBackup,
|
||||
)
|
||||
from homeassistant.components.backup.util import password_to_key
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STARTED
|
||||
from homeassistant.core import CoreState, HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import issue_registry as ir
|
||||
|
||||
@@ -3469,3 +3470,66 @@ async def test_restore_progress_after_restart_fail_to_remove(
|
||||
"Unexpected error deleting backup restore result file: <class 'OSError'> Boom!"
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
|
||||
async def test_manager_blocked_until_home_assistant_started(
|
||||
hass: HomeAssistant,
|
||||
hass_ws_client: WebSocketGenerator,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
"""Test backup manager's state is blocked until Home Assistant has started."""
|
||||
|
||||
hass.set_state(CoreState.not_running)
|
||||
|
||||
await setup_backup_integration(hass)
|
||||
manager = hass.data[DATA_MANAGER]
|
||||
|
||||
assert manager.state == BackupManagerState.BLOCKED
|
||||
assert manager.last_non_idle_event is None
|
||||
|
||||
# Fired when Home Assistant changes to starting state
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
assert manager.state == BackupManagerState.BLOCKED
|
||||
assert manager.last_non_idle_event is None
|
||||
|
||||
# Fired when Home Assistant changes to running state
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
|
||||
await hass.async_block_till_done()
|
||||
assert manager.state == BackupManagerState.IDLE
|
||||
assert manager.last_non_idle_event is None
|
||||
|
||||
|
||||
async def test_manager_not_blocked_after_restore(
|
||||
hass: HomeAssistant,
|
||||
hass_ws_client: WebSocketGenerator,
|
||||
) -> None:
|
||||
"""Test restore backup progress after restart."""
|
||||
restore_result = {"error": None, "error_type": None, "success": True}
|
||||
|
||||
hass.set_state(CoreState.not_running)
|
||||
with patch(
|
||||
"pathlib.Path.read_bytes", return_value=json.dumps(restore_result).encode()
|
||||
):
|
||||
await setup_backup_integration(hass)
|
||||
|
||||
ws_client = await hass_ws_client(hass)
|
||||
await ws_client.send_json_auto_id({"type": "backup/info"})
|
||||
result = await ws_client.receive_json()
|
||||
assert result["success"] is True
|
||||
assert result["result"] == {
|
||||
"agent_errors": {},
|
||||
"backups": [],
|
||||
"last_attempted_automatic_backup": None,
|
||||
"last_completed_automatic_backup": None,
|
||||
"last_non_idle_event": {
|
||||
"manager_state": "restore_backup",
|
||||
"reason": None,
|
||||
"stage": None,
|
||||
"state": "completed",
|
||||
},
|
||||
"next_automatic_backup": None,
|
||||
"next_automatic_backup_additional": False,
|
||||
"state": "idle",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user