test_backup_restore_off_event_loop.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """Backup and restore do their file work off the event loop.
  2. Unzipping a backup, copying the data directories back and writing a backup ZIP
  3. ran on the event loop. With a large archive folder that froze the whole server
  4. for over half a minute -- no page, no websocket, no printer message -- and the
  5. loop watchdog dumped every thread's stack in the middle of a restore that then
  6. succeeded.
  7. Each test makes one step slow and counts how often a ticker on the loop gets to
  8. run meanwhile. On the loop, the step starves it; in a worker thread it ticks on.
  9. """
  10. from __future__ import annotations
  11. import asyncio
  12. import io
  13. import sqlite3
  14. import time
  15. import zipfile
  16. from unittest.mock import AsyncMock, patch
  17. import pytest
  18. from backend.app.api.routes import settings as settings_routes
  19. from backend.app.core.config import settings as app_settings
  20. STEP_SECONDS = 0.5
  21. TICK_SECONDS = 0.02
  22. # Off the loop the ticker runs ~25 times during the step; on it, not at all.
  23. MIN_TICKS = 8
  24. class _Ticker:
  25. """Counts loop ticks, and how many happened while the slow step ran."""
  26. def __init__(self):
  27. self.ticks = 0
  28. self.during_step: list[int] = []
  29. def slow(self, fn):
  30. def wrapper(*args, **kwargs):
  31. before = self.ticks
  32. time.sleep(STEP_SECONDS)
  33. self.during_step.append(self.ticks - before)
  34. return fn(*args, **kwargs)
  35. return wrapper
  36. async def run_while(self, coro):
  37. done = False
  38. async def tick():
  39. while not done:
  40. await asyncio.sleep(TICK_SECONDS)
  41. self.ticks += 1
  42. task = asyncio.create_task(tick())
  43. try:
  44. return await coro
  45. finally:
  46. done = True
  47. await task
  48. def _data_dir(monkeypatch, tmp_path):
  49. db_path = tmp_path / "bambuddy.db"
  50. with sqlite3.connect(db_path) as db:
  51. db.execute("CREATE TABLE backup_marker (id INTEGER PRIMARY KEY)")
  52. (tmp_path / "archive" / "1").mkdir(parents=True)
  53. (tmp_path / "archive" / "1" / "print.3mf").write_bytes(b"x" * 1024)
  54. monkeypatch.setenv("DATA_DIR", str(tmp_path))
  55. monkeypatch.setattr(app_settings, "base_dir", tmp_path)
  56. monkeypatch.setattr(app_settings, "database_url", f"sqlite+aiosqlite:///{db_path}")
  57. @pytest.mark.asyncio
  58. @pytest.mark.integration
  59. @pytest.mark.sqlite_only # copies the app's own SQLite database file
  60. @pytest.mark.parametrize("step", ["_copy_backup_dirs", "_write_backup_zip", "shutil.copy2"])
  61. async def test_a_slow_backup_step_does_not_block_the_loop(async_client, monkeypatch, tmp_path, step):
  62. import shutil
  63. _data_dir(monkeypatch, tmp_path)
  64. ticker = _Ticker()
  65. if step == "shutil.copy2":
  66. monkeypatch.setattr(shutil, "copy2", ticker.slow(shutil.copy2))
  67. else:
  68. monkeypatch.setattr(settings_routes, step, ticker.slow(getattr(settings_routes, step)))
  69. out = tmp_path / "out"
  70. out.mkdir()
  71. zip_path, _ = await ticker.run_while(settings_routes.create_backup_zip(output_path=out))
  72. with zipfile.ZipFile(zip_path) as zf:
  73. names = zf.namelist()
  74. assert "bambuddy.db" in names
  75. assert "archive/1/print.3mf" in names
  76. assert ticker.during_step, f"{step} was not called"
  77. assert min(ticker.during_step) >= MIN_TICKS, f"{step} blocked the event loop: {ticker.during_step}"
  78. @pytest.mark.asyncio
  79. @pytest.mark.integration
  80. @pytest.mark.sqlite_only # restores into the app's own SQLite database file
  81. @pytest.mark.parametrize("step", ["_extract_backup_zip", "_restore_sqlite_db", "_restore_data_dirs"])
  82. async def test_a_slow_restore_step_does_not_block_the_loop(async_client, monkeypatch, tmp_path, step):
  83. _data_dir(monkeypatch, tmp_path)
  84. out = tmp_path / "out"
  85. out.mkdir()
  86. zip_path, _ = await settings_routes.create_backup_zip(output_path=out)
  87. payload = zip_path.read_bytes()
  88. # The restore must put the archive back, so take it away first.
  89. (tmp_path / "archive" / "1" / "print.3mf").unlink()
  90. ticker = _Ticker()
  91. monkeypatch.setattr(settings_routes, step, ticker.slow(getattr(settings_routes, step)))
  92. with (
  93. patch("backend.app.core.database.close_all_connections", new_callable=AsyncMock),
  94. patch("backend.app.core.database.reinitialize_database", new_callable=AsyncMock),
  95. patch("backend.app.core.database.init_db", new_callable=AsyncMock),
  96. patch("backend.app.core.database.invalidate_all_sessions", new_callable=AsyncMock),
  97. patch("backend.app.services.backup_paths.convert_windows_paths", new_callable=AsyncMock),
  98. patch("backend.app.services.print_scheduler.scheduler.stop"),
  99. patch("backend.app.services.smart_plug_manager.smart_plug_manager.stop_scheduler"),
  100. patch("backend.app.services.notification_service.notification_service.stop_digest_scheduler"),
  101. ):
  102. response = await ticker.run_while(
  103. async_client.post(
  104. "/api/v1/settings/restore",
  105. files={"file": ("backup.zip", payload, "application/zip")},
  106. )
  107. )
  108. assert response.status_code == 200, response.text
  109. assert (tmp_path / "archive" / "1" / "print.3mf").read_bytes() == b"x" * 1024
  110. assert ticker.during_step, f"{step} was not called"
  111. assert min(ticker.during_step) >= MIN_TICKS, f"{step} blocked the event loop: {ticker.during_step}"
  112. @pytest.mark.asyncio
  113. @pytest.mark.integration
  114. async def test_an_unsafe_path_is_still_refused(async_client):
  115. buffer = io.BytesIO()
  116. with zipfile.ZipFile(buffer, "w") as zf:
  117. zf.writestr("../evil.txt", "x")
  118. zf.writestr("bambuddy.db", "x")
  119. response = await async_client.post(
  120. "/api/v1/settings/restore",
  121. files={"file": ("backup.zip", buffer.getvalue(), "application/zip")},
  122. )
  123. assert response.status_code == 400
  124. assert "unsafe path" in response.json()["detail"]
  125. @pytest.mark.asyncio
  126. @pytest.mark.integration
  127. async def test_a_file_that_is_not_a_zip_is_still_refused(async_client):
  128. response = await async_client.post(
  129. "/api/v1/settings/restore",
  130. files={"file": ("backup.zip", b"not a zip", "application/zip")},
  131. )
  132. assert response.status_code == 400
  133. assert "not a valid ZIP" in response.json()["detail"]